it is a program reading sensor data and sending back to host

Dependencies:   MAG3110 MMA8451Q TSI mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Author: Edoardo De Marchi 
00002  * Name: Test Code for MMA7660FC
00003  */
00004 #include "mbed.h"
00005 #include "MMA8451Q.h"
00006 #include "MAG3110.h"
00007 #include "TSISensor.h"
00008 #include <ctype.h>
00009 #define MMA8451_I2C_ADDRESS (0x1d<<1)                  // I2C SLAVE ADDR MMA7660FC
00010 #define buffer_size 255
00011 Ticker lightsensor_tick;
00012 Ticker Mag_x_tick;
00013 Ticker Mag_y_tick;
00014 Ticker Mag_z_tick;
00015 Ticker Acc_x_tick;
00016 Ticker Acc_y_tick;
00017 Ticker Acc_z_tick;
00018 Ticker Touch_tick;
00019 float rate=1.0;
00020 int buff = 0;
00021 char buffer[32];
00022 int sample_light = 1;
00023 MAG3110 mag(PTE25,PTE24);
00024 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);      //sda, scl, Addr
00025 TSISensor touch;
00026 Serial pc(USBTX, USBRX);
00027 AnalogIn lightsensor(PTE22);
00028 volatile int tx_in=0;
00029 volatile int tx_out=0;
00030 char tx_line[80];
00031 char tx_buffer[buffer_size];
00032 unsigned char c = ' ';
00033 float x=0, y=0, z=0, l=0, t=0;
00034 int mx, my, mz;
00035 void Tx_interrupt();
00036 void send_line();
00037 void sendInt(int x)
00038 {
00039     char *p = (char *)&x;
00040     pc.putc(*p);
00041     pc.putc(*(p+1));
00042     pc.putc(*(p+2));
00043     pc.putc(*(p+3));
00044 }
00045 void sendFloat(float x)
00046 {
00047     char *p = (char *)&x;
00048     pc.putc(*p);
00049     pc.putc(*(p+1));
00050     pc.putc(*(p+2));
00051     pc.putc(*(p+3));
00052 }
00053 void send_line()
00054 {
00055     int i;
00056     char temp_char;
00057     bool empty;
00058     i = 0;
00059 // Start Critical Section - don't interrupt while changing global buffer variables
00060     NVIC_DisableIRQ(UART1_IRQn);
00061     //_disable_irq(pc.RxIrq);
00062     empty = (tx_in == tx_out);
00063     while ((i==0) || (tx_line[i-1] != '\n')) 
00064     {
00065 // Wait if buffer full
00066         if (((tx_in + 1) % buffer_size) == tx_out) 
00067         {
00068 // End Critical Section - need to let interrupt routine empty buffer by sending
00069             NVIC_EnableIRQ(UART1_IRQn);
00070             while (((tx_in + 1) % buffer_size) == tx_out) 
00071             {
00072             }
00073 // Start Critical Section - don't interrupt while changing global buffer variables
00074             NVIC_DisableIRQ(UART1_IRQn);
00075         }
00076         tx_buffer[tx_in] = tx_line[i];
00077         i++;
00078         tx_in = (tx_in + 1) % buffer_size;
00079     }
00080     if (pc.writeable() && (empty)) {
00081         temp_char = tx_buffer[tx_out];
00082         tx_out = (tx_out + 1) % buffer_size;
00083 // Send first character to start tx interrupts, if stopped
00084         pc.putc(temp_char);
00085     }
00086 // End Critical Section
00087     NVIC_EnableIRQ(UART1_IRQn);
00088     return;
00089 }
00090 void light_tick()
00091 {
00092     if(c == '1' || c=='9')
00093     {
00094         l = lightsensor.read();
00095         pc.putc('1');
00096         sendFloat(l);
00097     }
00098         
00099     return;
00100 }
00101 void mx_tick()
00102 {
00103     if(c=='2' || c=='9')
00104     {
00105         mag.getValues(&mx, &my, &mz);
00106         pc.putc('2');
00107         sendInt(mx);
00108     }
00109     return;
00110 }
00111 void my_tick()
00112 {
00113      if(c=='3' || c=='9')
00114     {
00115         mag.getValues(&mx, &my, &mz);
00116         pc.putc('3');
00117         sendInt(mz);
00118     }
00119     return;
00120 }
00121 void mz_tick()
00122 {
00123      if(c=='4' || c=='9')
00124     {
00125         mag.getValues(&mx, &my, &mz);
00126         pc.putc('4');
00127         sendInt(my);
00128     }    
00129     return;
00130 }
00131 void ax_tick()
00132 {
00133     if(c=='6' || c=='9')
00134     {
00135         x = acc.getAccX();
00136         pc.putc('6');
00137         sendFloat(x);
00138     }
00139     return;
00140 }
00141 void ay_tick()
00142 {
00143     if(c=='7' || c=='9')
00144     {
00145         y = acc.getAccY();
00146         pc.putc('7');
00147         sendFloat(y);
00148     }
00149     return;
00150 }
00151 void az_tick()
00152 {   
00153     if(c=='8' || c=='9')
00154     {
00155         z = acc.getAccZ();
00156         pc.putc('8');
00157         sendFloat(z);
00158     }
00159     return;
00160 }
00161 void touch_tick()
00162 {
00163     if(c=='5'||c=='9')
00164     {
00165         t = touch.readPercentage();
00166         pc.putc('5');
00167         sendFloat(t);
00168     }    
00169     return;
00170 }
00171 void receive_handler()
00172 {
00173     memset(buffer, 0 , sizeof(buffer));
00174     buff=0;
00175     while (1)
00176     {
00177         buffer[buff] = pc.getc();
00178         if (buffer[buff] == '\n' || buffer[buff] == '\r')
00179         {
00180         buff++;
00181        
00182         break;
00183         }
00184         buff++;
00185     }
00186      c = buffer[0];
00187      int i = 1;
00188      rate = 0;
00189      while ((buffer[i]>='0') && (buffer[i]<='9'))
00190      {
00191          rate = rate*10 + buffer[i]-'0';
00192          i++;
00193          if (buffer[i] == '\n' || buffer[i] == '\r')
00194          break;
00195      }
00196     //pc.putc(buffer[1]);
00197     //lightsensor_tick.detach();
00198     switch (c)
00199     {
00200       case '0':
00201                  lightsensor_tick.detach();
00202                  Mag_x_tick.detach();
00203                  Mag_y_tick.detach();
00204                  Mag_z_tick.detach();
00205                  Touch_tick.detach();
00206                  Acc_x_tick.detach();
00207                  Acc_y_tick.detach();
00208                  Acc_z_tick.detach();
00209                  break;
00210       case '1':  lightsensor_tick.attach(&light_tick,1/rate);
00211                 break;
00212       case '2':  Mag_x_tick.attach(&mx_tick,1/rate);
00213                 break;
00214       case '3':  
00215                  Mag_y_tick.attach(&my_tick,1/rate);
00216                 break;
00217       case '4':
00218                  Mag_z_tick.attach(&mz_tick,1/rate);
00219                  break;
00220       case '5':  Touch_tick.attach(&touch_tick,1/rate);
00221                  break;
00222       case '6':
00223                  Acc_x_tick.attach(&ax_tick,1/rate);
00224                  break;
00225       case '7':
00226                  Acc_y_tick.attach(&ay_tick,1/rate);
00227                  break;
00228       case '8': 
00229                  Acc_z_tick.attach(&az_tick,1/rate);
00230                  break;
00231       case '9':
00232                  lightsensor_tick.attach(&light_tick,1/rate);
00233                  Mag_x_tick.attach(&mx_tick,rate);
00234                  Mag_y_tick.attach(&my_tick,1/rate);
00235                  Mag_z_tick.attach(&mz_tick,1/rate);
00236                  Acc_x_tick.attach(&ax_tick,1/rate);
00237                  Acc_y_tick.attach(&ay_tick,1/rate);
00238                  Acc_z_tick.attach(&az_tick,1/rate);
00239                  Touch_tick.attach(&touch_tick,1/rate);
00240                  break;
00241     }
00242     return;
00243 }
00244 int main() 
00245 {
00246     pc.baud(9600);                                                // Initialization
00247 //    pc.attach(&keyInterrupt, pc.RxIrq);                           // Key In Interrupt 
00248     pc.attach(&receive_handler,pc.RxIrq);
00249         //lightsensor_tick.attach(&light_tick,3);
00250         
00251         
00252         
00253     while(1)
00254     {   
00255     }
00256     
00257 }
00258 void Tx_interrupt() {
00259 // Loop to fill more than one character in UART's transmit FIFO buffer
00260 // Stop if buffer empty
00261     while ((pc.writeable()) && (tx_in != tx_out)) {
00262         pc.putc(tx_buffer[tx_out]);
00263         tx_out = (tx_out + 1) % buffer_size;
00264     }
00265     return;
00266 }