Kent Wong / Mbed 2 deprecated FYPhh

Dependencies:   mbed Sht31 MAX44009

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Sht31.h"
00003 #include "MAX44009.h"
00004 #include "mbed_wait_api.h"
00005 #include <list>
00006 
00007 Serial pc(p13,p14);
00008 Sht31 sht31(p9, p10);
00009 MAX44009 myMAX44009 ( p9, p10, MAX44009::MAX44009_ADDRESS_A0_GND, 400000 );
00010 I2C i2c(p9, p10); //pins for I2C communication (SDA, SCL)
00011 
00012 DigitalOut Batt2SYS(p8);
00013 DigitalOut BLE_Can_receive(p11); // 0 = can recieve
00014 
00015 class RGB{
00016     public :    int C;
00017                 int R;
00018                 int G;
00019                 int B;
00020     };
00021 class RGBf{
00022     public :    float C;
00023                 float R;
00024                 float G;
00025                 float B;
00026     };  
00027 class TandH{
00028     public : float t;
00029              float h;
00030     };
00031     
00032 RGB RGBdata[10];
00033 TandH THdata[10];
00034 float luxdata[10];
00035 
00036 int counter = 10; //to tackle with 0 mod 10 problem, use 10 instead;
00037 int sendCounter=0; // for sending in auto/ instant measure
00038 int _30Scounter=0;
00039 bool allfilled = false;
00040 RGBf RGBavg;
00041 TandH THavg;
00042 float luxavg;
00043 
00044 bool wrote=false;
00045 DigitalOut led(LED1);
00046 char    signal[1];
00047 //LocalFileSystem local("local"); //Create the local filesystem under the name "local"
00048 
00049 
00050 int main()
00051 {   
00052   //  set_time(1587699064); 
00053  
00054     //pc.read
00055     pc.baud ( 19200 );
00056     Batt2SYS = 0;
00057 
00058  
00059        
00060     while(1) { 
00061         counter = counter%10;
00062         //time_t seconds = time(NULL);
00063         {
00064             int sensor_addr = 41 << 1;
00065             char id_regval[1] = {146};
00066             char data[1] = {0};
00067             i2c.write(sensor_addr,id_regval,1, true);
00068             i2c.read(sensor_addr,data,1,false);
00069 
00070     
00071     // Initialize color sensor
00072             char timing_register[2] = {129,192};
00073             //char timing_register[2] = {129,0};
00074             i2c.write(sensor_addr,timing_register,2,false);
00075             char control_register[2] = {143,0};
00076             char temp[2]={0,0};
00077             //char control_register[2] = {143,3};
00078             i2c.write(sensor_addr,control_register,2,false);
00079     
00080             char enable_register[2] = {128,3};
00081             i2c.write(sensor_addr,enable_register,2,false);
00082     
00083     // Read data from color sensor (Clear/Red/Green/Blue)
00084     
00085            char clear_reg[1] = {148};
00086            char clear_data[2] = {0,0};
00087            i2c.write(sensor_addr,clear_reg,1, true);
00088            i2c.read(sensor_addr,clear_data,2, false);
00089         
00090            int clear_value = ((int)clear_data[1] << 8) | clear_data[0];
00091         
00092            char red_reg[1] = {150};
00093            char red_data[2] = {0,0};
00094            i2c.write(sensor_addr,red_reg,1, true);
00095            i2c.read(sensor_addr,red_data,2, false);
00096         
00097            int red_value = ((int)red_data[1] << 8) | red_data[0];
00098         
00099            char green_reg[1] = {152};
00100            char green_data[2] = {0,0};
00101            i2c.write(sensor_addr,green_reg,1, true);
00102            i2c.read(sensor_addr,green_data,2, false);
00103         
00104            int green_value = ((int)green_data[1] << 8) | green_data[0];
00105         
00106            char blue_reg[1] = {154};
00107            char blue_data[2] = {0,0};
00108            i2c.write(sensor_addr,blue_reg,1, true);
00109            i2c.read(sensor_addr,blue_data,2, false);
00110         
00111            int blue_value = ((int)blue_data[1] << 8) | blue_data[0];
00112         
00113                 // print sensor readings
00114            if (allfilled == true){
00115                 RGBavg.C = RGBavg.C*10-RGBdata[counter].C;
00116                 RGBavg.R = RGBavg.R*10-RGBdata[counter].R;
00117                 RGBavg.G = RGBavg.G*10-RGBdata[counter].G;
00118                 RGBavg.B = RGBavg.B*10-RGBdata[counter].B;
00119             }
00120             RGBdata[counter].C= clear_value;
00121             RGBdata[counter].R= red_value;
00122             RGBdata[counter].G= green_value;
00123             RGBdata[counter].B= blue_value;
00124            //pc.printf("Clear (%d), Red (%d), Green (%d), Blue (%d)\n", clear_value, red_value, green_value, blue_value);
00125         }
00126         {
00127 
00128         MAX44009::MAX44009_status_t       aux;
00129         MAX44009::MAX44009_vector_data_t  myMAX44009_Data;
00130         
00131         aux  =   myMAX44009.MAX44009_Configuration ( MAX44009::CONFIGURATION_CONT_DEFAULT_MODE, MAX44009::CONFIGURATION_MANUAL_DEFAULT_MODE, MAX44009::CONFIGURATION_CDR_CURRENT_NOT_DIVIDED, MAX44009::CONFIGURATION_TIM_800_MS );
00132         aux  =   myMAX44009.MAX44009_GetLux( MAX44009::RESOLUTION_EXTENDED_RESOLUTION, &myMAX44009_Data );
00133         aux  =   myMAX44009.MAX44009_GetCurrentDivisionRatio   ( &myMAX44009_Data );
00134         aux  =   myMAX44009.MAX44009_GetIntegrationTime        ( &myMAX44009_Data );
00135         
00136         if (allfilled == true){
00137             luxavg = luxavg*10-luxdata[counter];
00138         } 
00139         
00140         luxdata[counter] = myMAX44009_Data.lux;
00141         
00142 
00143         }
00144         
00145         {
00146 
00147         //float t = sht31.readTemperature();
00148         //float h = sht31.readHumidity();
00149 
00150         if (allfilled == true){
00151             THavg.t = THavg.t*10-THdata[counter].t;
00152             THavg.h = THavg.h*10-THdata[counter].h;
00153         } 
00154         THdata[counter].t = sht31.readTemperature();
00155         THdata[counter].h = sht31.readHumidity();
00156 
00157         //pc.printf("[TEMP/HUM]");
00158         
00159         }
00160 ////////////////////////////////////////////////////////////////////
00161             
00162             /*BLE_Can_receive = 0;
00163             wait_ms(5);
00164             pc.printf("counter119\n = %d", counter);
00165             for (int i =0;i<10;i++){
00166                 pc.printf("i=%d ,", i);
00167                 pc.printf("%d,%d,%d,%d,%3.2f,%3.2f%,%0.001f\n", RGBdata[i].C, RGBdata[i].R, RGBdata[i].G, RGBdata[i].B,THdata[i].t, THdata[i].h,luxdata[i]);
00168             }
00169             BLE_Can_receive = 1;*/
00170             
00171             if (allfilled == true){
00172             RGBavg.C = ((RGBavg.C+RGBdata[counter].C)/10.0);
00173             RGBavg.R = ((RGBavg.R+RGBdata[counter].R)/10.0);
00174             RGBavg.G = ((RGBavg.G+RGBdata[counter].G)/10.0);
00175             RGBavg.B = ((RGBavg.B+RGBdata[counter].B)/10.0);
00176                         
00177             THavg.t = (THavg.t+THdata[counter].t)/10;
00178             THavg.h = (THavg.h+THdata[counter].h)/10;
00179             
00180             luxavg = (luxavg+luxdata[counter])/10;
00181             }
00182             else{
00183 
00184                     RGBavg.C= (RGBavg.C*(counter) + RGBdata[counter].C)   /(float)(counter+1);
00185                     RGBavg.R= (RGBavg.R*(counter) + RGBdata[counter].R)   /(float)(counter+1);
00186                     RGBavg.G= (RGBavg.G*(counter) + RGBdata[counter].G)   /(float)(counter+1);
00187                     RGBavg.B= (RGBavg.B*(counter) + RGBdata[counter].B)   /(float)(counter+1);
00188                     
00189                     THavg.t = ((THavg.t*(counter) + THdata[counter].t))/(counter+1);
00190                     THavg.h = ((THavg.h*(counter) + THdata[counter].h))/(counter+1);
00191                 
00192                     luxavg = ((luxavg*(counter) + luxdata[counter]))/(counter+1);
00193             }
00194             
00195         //pc.printf("test");
00196        // pc.printf("Shine Like:%s,%d,%d,%d,%d,%3.2f,%3.2f%,%0.001f\n",signal,(int)(RGBavg.C+0.5), (int)(RGBavg.R+0.5), (int)(RGBavg.G+0.5), (int)(RGBavg.B+0.5),THavg.t, THavg.h,luxavg);
00197             BLE_Can_receive = 0;
00198             counter++;
00199            // if (counter == 10 &&_30Scounter==3){
00200                 //_30Scounter=0;
00201                // FILE *fp = fopen("/local/out.txt", "a");   
00202                //fprintf(fp, "[%d,%d,%d,%d,%d,%d,%d,%0.001f],\n",seconds,(int)(RGBavg.C+0.5),
00203                        // (int)(RGBavg.R+0.5), (int)(RGBavg.G+0.5), (int)(RGBavg.B+0.5),(int)THavg.t,(int) THavg.h,luxavg);   
00204                // fclose(fp); 
00205                // allfilled = true; 
00206            // }
00207             //else 
00208             if (counter==10){
00209                // _30Scounter++;
00210                  allfilled = true; 
00211             }
00212             BLE_Can_receive = 1;
00213         if (wrote){
00214            /* if (signal[0]=='s'){
00215                 FILE *fp = fopen("/local/out.txt", "r"); 
00216                 char str[45];   
00217                  while(!feof(fp))
00218                     if(fgets(str,40,fp)!=NULL) {
00219                         pc.printf("%s\n",str);
00220                         //wait_ms(300);
00221                     }
00222                  fclose(fp);
00223                 wrote=false;
00224             }
00225             else */
00226             {
00227                 sendCounter++;
00228                 pc.printf("%d,%d,%d,%d,%d,%3.2f,%3.2f%,%0.001f\n",sendCounter,(int)(RGBavg.C+0.5), (int)(RGBavg.R+0.5), (int)(RGBavg.G+0.5), (int)(RGBavg.B+0.5),THavg.t, THavg.h,luxavg);
00229                 if (sendCounter == 10){ //10 = all element is filled {  
00230                     if (signal[0]=='i'){
00231                         pc.printf("end");
00232                     }
00233                     else if (signal[0]=='a')
00234                     {
00235                         pc.printf("aend");
00236                     }
00237                     sendCounter=0;
00238                     wrote=false;
00239                     }
00240                 }
00241             
00242         }
00243         else{
00244             if(pc.readable() ) {
00245                 led=!led;
00246                 pc.scanf("%1s", signal);
00247                 wrote=true;
00248             }
00249     
00250         }
00251 
00252 
00253     }
00254 }