a
Dependencies: mbed Sht31 MAX44009
main.cpp@6:94a26631ec03, 2020-04-23 (annotated)
- Committer:
- kentwong
- Date:
- Thu Apr 23 14:33:38 2020 +0000
- Revision:
- 6:94a26631ec03
- Parent:
- 5:006e5cf5c2de
- Child:
- 7:e4984029012e
hey look
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chapman515 | 0:b1b7113a1441 | 1 | #include "mbed.h" |
chapman515 | 0:b1b7113a1441 | 2 | #include "Sht31.h" |
chapman515 | 0:b1b7113a1441 | 3 | #include "MAX44009.h" |
chapman515 | 0:b1b7113a1441 | 4 | #include "mbed_wait_api.h" |
chapman515 | 0:b1b7113a1441 | 5 | #include <list> |
chapman515 | 0:b1b7113a1441 | 6 | |
chapman515 | 0:b1b7113a1441 | 7 | Serial pc(p13,p14); |
chapman515 | 0:b1b7113a1441 | 8 | Sht31 sht31(p9, p10); |
chapman515 | 0:b1b7113a1441 | 9 | MAX44009 myMAX44009 ( p9, p10, MAX44009::MAX44009_ADDRESS_A0_GND, 400000 ); |
chapman515 | 0:b1b7113a1441 | 10 | I2C i2c(p9, p10); //pins for I2C communication (SDA, SCL) |
chapman515 | 0:b1b7113a1441 | 11 | |
chapman515 | 1:3889765eb394 | 12 | DigitalOut Batt2SYS(p8); |
chapman515 | 0:b1b7113a1441 | 13 | DigitalOut BLE_Can_receive(p11); // 0 = can recieve |
chapman515 | 0:b1b7113a1441 | 14 | |
chapman515 | 0:b1b7113a1441 | 15 | class RGB{ |
chapman515 | 0:b1b7113a1441 | 16 | public : int C; |
chapman515 | 0:b1b7113a1441 | 17 | int R; |
chapman515 | 0:b1b7113a1441 | 18 | int G; |
chapman515 | 0:b1b7113a1441 | 19 | int B; |
chapman515 | 0:b1b7113a1441 | 20 | }; |
chapman515 | 0:b1b7113a1441 | 21 | class RGBf{ |
chapman515 | 0:b1b7113a1441 | 22 | public : float C; |
chapman515 | 0:b1b7113a1441 | 23 | float R; |
chapman515 | 0:b1b7113a1441 | 24 | float G; |
chapman515 | 0:b1b7113a1441 | 25 | float B; |
chapman515 | 0:b1b7113a1441 | 26 | }; |
chapman515 | 0:b1b7113a1441 | 27 | class TandH{ |
chapman515 | 0:b1b7113a1441 | 28 | public : float t; |
chapman515 | 0:b1b7113a1441 | 29 | float h; |
chapman515 | 0:b1b7113a1441 | 30 | }; |
chapman515 | 0:b1b7113a1441 | 31 | |
chapman515 | 0:b1b7113a1441 | 32 | RGB RGBdata[10]; |
chapman515 | 0:b1b7113a1441 | 33 | TandH THdata[10]; |
chapman515 | 0:b1b7113a1441 | 34 | float luxdata[10]; |
chapman515 | 0:b1b7113a1441 | 35 | |
chapman515 | 0:b1b7113a1441 | 36 | int counter = 10; //to tackle with 0 mod 10 problem, use 10 instead; |
chapman515 | 0:b1b7113a1441 | 37 | |
chapman515 | 0:b1b7113a1441 | 38 | bool allfilled = false; |
chapman515 | 0:b1b7113a1441 | 39 | RGBf RGBavg; |
chapman515 | 0:b1b7113a1441 | 40 | TandH THavg; |
chapman515 | 0:b1b7113a1441 | 41 | float luxavg; |
chapman515 | 0:b1b7113a1441 | 42 | |
kentwong | 6:94a26631ec03 | 43 | bool wrote=false; |
chapman515 | 1:3889765eb394 | 44 | DigitalOut led(LED1); |
kentwong | 6:94a26631ec03 | 45 | char signal[1]; |
kentwong | 6:94a26631ec03 | 46 | LocalFileSystem local("local"); //Create the local filesystem under the name "local" |
chapman515 | 1:3889765eb394 | 47 | |
kentwong | 6:94a26631ec03 | 48 | |
chapman515 | 0:b1b7113a1441 | 49 | int main() |
kentwong | 6:94a26631ec03 | 50 | { |
kentwong | 6:94a26631ec03 | 51 | |
kentwong | 6:94a26631ec03 | 52 | //pc.read |
chapman515 | 0:b1b7113a1441 | 53 | pc.baud ( 19200 ); |
chapman515 | 1:3889765eb394 | 54 | Batt2SYS = 0; |
kentwong | 6:94a26631ec03 | 55 | |
kentwong | 6:94a26631ec03 | 56 | |
kentwong | 6:94a26631ec03 | 57 | |
kentwong | 6:94a26631ec03 | 58 | while(1) { |
chapman515 | 0:b1b7113a1441 | 59 | counter = counter%10; |
chapman515 | 0:b1b7113a1441 | 60 | { |
chapman515 | 0:b1b7113a1441 | 61 | int sensor_addr = 41 << 1; |
chapman515 | 0:b1b7113a1441 | 62 | char id_regval[1] = {146}; |
chapman515 | 0:b1b7113a1441 | 63 | char data[1] = {0}; |
chapman515 | 0:b1b7113a1441 | 64 | i2c.write(sensor_addr,id_regval,1, true); |
chapman515 | 0:b1b7113a1441 | 65 | i2c.read(sensor_addr,data,1,false); |
chapman515 | 0:b1b7113a1441 | 66 | |
chapman515 | 0:b1b7113a1441 | 67 | |
chapman515 | 0:b1b7113a1441 | 68 | // Initialize color sensor |
chapman515 | 0:b1b7113a1441 | 69 | char timing_register[2] = {129,192}; |
chapman515 | 0:b1b7113a1441 | 70 | //char timing_register[2] = {129,0}; |
chapman515 | 0:b1b7113a1441 | 71 | i2c.write(sensor_addr,timing_register,2,false); |
chapman515 | 0:b1b7113a1441 | 72 | char control_register[2] = {143,0}; |
chapman515 | 0:b1b7113a1441 | 73 | char temp[2]={0,0}; |
chapman515 | 0:b1b7113a1441 | 74 | //char control_register[2] = {143,3}; |
chapman515 | 0:b1b7113a1441 | 75 | i2c.write(sensor_addr,control_register,2,false); |
chapman515 | 0:b1b7113a1441 | 76 | |
chapman515 | 0:b1b7113a1441 | 77 | char enable_register[2] = {128,3}; |
chapman515 | 0:b1b7113a1441 | 78 | i2c.write(sensor_addr,enable_register,2,false); |
chapman515 | 0:b1b7113a1441 | 79 | |
chapman515 | 0:b1b7113a1441 | 80 | // Read data from color sensor (Clear/Red/Green/Blue) |
chapman515 | 0:b1b7113a1441 | 81 | |
chapman515 | 0:b1b7113a1441 | 82 | char clear_reg[1] = {148}; |
chapman515 | 0:b1b7113a1441 | 83 | char clear_data[2] = {0,0}; |
chapman515 | 0:b1b7113a1441 | 84 | i2c.write(sensor_addr,clear_reg,1, true); |
chapman515 | 0:b1b7113a1441 | 85 | i2c.read(sensor_addr,clear_data,2, false); |
chapman515 | 0:b1b7113a1441 | 86 | |
chapman515 | 0:b1b7113a1441 | 87 | int clear_value = ((int)clear_data[1] << 8) | clear_data[0]; |
chapman515 | 0:b1b7113a1441 | 88 | |
chapman515 | 0:b1b7113a1441 | 89 | char red_reg[1] = {150}; |
chapman515 | 0:b1b7113a1441 | 90 | char red_data[2] = {0,0}; |
chapman515 | 0:b1b7113a1441 | 91 | i2c.write(sensor_addr,red_reg,1, true); |
chapman515 | 0:b1b7113a1441 | 92 | i2c.read(sensor_addr,red_data,2, false); |
chapman515 | 0:b1b7113a1441 | 93 | |
chapman515 | 0:b1b7113a1441 | 94 | int red_value = ((int)red_data[1] << 8) | red_data[0]; |
chapman515 | 0:b1b7113a1441 | 95 | |
chapman515 | 0:b1b7113a1441 | 96 | char green_reg[1] = {152}; |
chapman515 | 0:b1b7113a1441 | 97 | char green_data[2] = {0,0}; |
chapman515 | 0:b1b7113a1441 | 98 | i2c.write(sensor_addr,green_reg,1, true); |
chapman515 | 0:b1b7113a1441 | 99 | i2c.read(sensor_addr,green_data,2, false); |
chapman515 | 0:b1b7113a1441 | 100 | |
chapman515 | 0:b1b7113a1441 | 101 | int green_value = ((int)green_data[1] << 8) | green_data[0]; |
chapman515 | 0:b1b7113a1441 | 102 | |
chapman515 | 0:b1b7113a1441 | 103 | char blue_reg[1] = {154}; |
chapman515 | 0:b1b7113a1441 | 104 | char blue_data[2] = {0,0}; |
chapman515 | 0:b1b7113a1441 | 105 | i2c.write(sensor_addr,blue_reg,1, true); |
chapman515 | 0:b1b7113a1441 | 106 | i2c.read(sensor_addr,blue_data,2, false); |
chapman515 | 0:b1b7113a1441 | 107 | |
chapman515 | 0:b1b7113a1441 | 108 | int blue_value = ((int)blue_data[1] << 8) | blue_data[0]; |
chapman515 | 0:b1b7113a1441 | 109 | |
chapman515 | 0:b1b7113a1441 | 110 | // print sensor readings |
chapman515 | 0:b1b7113a1441 | 111 | if (allfilled == true){ |
chapman515 | 0:b1b7113a1441 | 112 | RGBavg.C = RGBavg.C*10-RGBdata[counter].C; |
chapman515 | 0:b1b7113a1441 | 113 | RGBavg.R = RGBavg.R*10-RGBdata[counter].R; |
chapman515 | 0:b1b7113a1441 | 114 | RGBavg.G = RGBavg.G*10-RGBdata[counter].G; |
chapman515 | 0:b1b7113a1441 | 115 | RGBavg.B = RGBavg.B*10-RGBdata[counter].B; |
chapman515 | 0:b1b7113a1441 | 116 | } |
chapman515 | 0:b1b7113a1441 | 117 | RGBdata[counter].C= clear_value; |
chapman515 | 0:b1b7113a1441 | 118 | RGBdata[counter].R= red_value; |
chapman515 | 0:b1b7113a1441 | 119 | RGBdata[counter].G= green_value; |
chapman515 | 0:b1b7113a1441 | 120 | RGBdata[counter].B= blue_value; |
chapman515 | 0:b1b7113a1441 | 121 | //pc.printf("Clear (%d), Red (%d), Green (%d), Blue (%d)\n", clear_value, red_value, green_value, blue_value); |
chapman515 | 0:b1b7113a1441 | 122 | } |
chapman515 | 0:b1b7113a1441 | 123 | { |
chapman515 | 0:b1b7113a1441 | 124 | |
chapman515 | 0:b1b7113a1441 | 125 | MAX44009::MAX44009_status_t aux; |
chapman515 | 0:b1b7113a1441 | 126 | MAX44009::MAX44009_vector_data_t myMAX44009_Data; |
chapman515 | 0:b1b7113a1441 | 127 | |
chapman515 | 0:b1b7113a1441 | 128 | 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 ); |
chapman515 | 0:b1b7113a1441 | 129 | aux = myMAX44009.MAX44009_GetLux( MAX44009::RESOLUTION_EXTENDED_RESOLUTION, &myMAX44009_Data ); |
chapman515 | 0:b1b7113a1441 | 130 | aux = myMAX44009.MAX44009_GetCurrentDivisionRatio ( &myMAX44009_Data ); |
chapman515 | 0:b1b7113a1441 | 131 | aux = myMAX44009.MAX44009_GetIntegrationTime ( &myMAX44009_Data ); |
chapman515 | 0:b1b7113a1441 | 132 | |
chapman515 | 0:b1b7113a1441 | 133 | if (allfilled == true){ |
chapman515 | 0:b1b7113a1441 | 134 | luxavg = luxavg*10-luxdata[counter]; |
chapman515 | 0:b1b7113a1441 | 135 | } |
chapman515 | 0:b1b7113a1441 | 136 | |
chapman515 | 0:b1b7113a1441 | 137 | luxdata[counter] = myMAX44009_Data.lux; |
chapman515 | 0:b1b7113a1441 | 138 | |
chapman515 | 0:b1b7113a1441 | 139 | |
chapman515 | 0:b1b7113a1441 | 140 | } |
chapman515 | 0:b1b7113a1441 | 141 | |
chapman515 | 0:b1b7113a1441 | 142 | { |
chapman515 | 0:b1b7113a1441 | 143 | |
chapman515 | 0:b1b7113a1441 | 144 | //float t = sht31.readTemperature(); |
chapman515 | 0:b1b7113a1441 | 145 | //float h = sht31.readHumidity(); |
chapman515 | 0:b1b7113a1441 | 146 | |
chapman515 | 0:b1b7113a1441 | 147 | if (allfilled == true){ |
chapman515 | 0:b1b7113a1441 | 148 | THavg.t = THavg.t*10-THdata[counter].t; |
chapman515 | 0:b1b7113a1441 | 149 | THavg.h = THavg.h*10-THdata[counter].h; |
chapman515 | 0:b1b7113a1441 | 150 | } |
chapman515 | 0:b1b7113a1441 | 151 | THdata[counter].t = sht31.readTemperature(); |
chapman515 | 0:b1b7113a1441 | 152 | THdata[counter].h = sht31.readHumidity(); |
chapman515 | 0:b1b7113a1441 | 153 | |
chapman515 | 0:b1b7113a1441 | 154 | //pc.printf("[TEMP/HUM]"); |
chapman515 | 0:b1b7113a1441 | 155 | |
chapman515 | 0:b1b7113a1441 | 156 | } |
chapman515 | 0:b1b7113a1441 | 157 | //////////////////////////////////////////////////////////////////// |
chapman515 | 0:b1b7113a1441 | 158 | |
chapman515 | 0:b1b7113a1441 | 159 | /*BLE_Can_receive = 0; |
chapman515 | 0:b1b7113a1441 | 160 | wait_ms(5); |
chapman515 | 0:b1b7113a1441 | 161 | pc.printf("counter119\n = %d", counter); |
chapman515 | 0:b1b7113a1441 | 162 | for (int i =0;i<10;i++){ |
chapman515 | 0:b1b7113a1441 | 163 | pc.printf("i=%d ,", i); |
chapman515 | 0:b1b7113a1441 | 164 | 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]); |
chapman515 | 0:b1b7113a1441 | 165 | } |
chapman515 | 0:b1b7113a1441 | 166 | BLE_Can_receive = 1;*/ |
chapman515 | 0:b1b7113a1441 | 167 | |
chapman515 | 0:b1b7113a1441 | 168 | if (allfilled == true){ |
chapman515 | 0:b1b7113a1441 | 169 | RGBavg.C = ((RGBavg.C+RGBdata[counter].C)/10.0); |
chapman515 | 0:b1b7113a1441 | 170 | RGBavg.R = ((RGBavg.R+RGBdata[counter].R)/10.0); |
chapman515 | 0:b1b7113a1441 | 171 | RGBavg.G = ((RGBavg.G+RGBdata[counter].G)/10.0); |
chapman515 | 0:b1b7113a1441 | 172 | RGBavg.B = ((RGBavg.B+RGBdata[counter].B)/10.0); |
chapman515 | 0:b1b7113a1441 | 173 | |
chapman515 | 0:b1b7113a1441 | 174 | THavg.t = (THavg.t+THdata[counter].t)/10; |
chapman515 | 0:b1b7113a1441 | 175 | THavg.h = (THavg.h+THdata[counter].h)/10; |
chapman515 | 0:b1b7113a1441 | 176 | |
chapman515 | 0:b1b7113a1441 | 177 | luxavg = (luxavg+luxdata[counter])/10; |
chapman515 | 0:b1b7113a1441 | 178 | } |
chapman515 | 0:b1b7113a1441 | 179 | else{ |
chapman515 | 0:b1b7113a1441 | 180 | |
chapman515 | 0:b1b7113a1441 | 181 | RGBavg.C= (RGBavg.C*(counter) + RGBdata[counter].C) /(float)(counter+1); |
chapman515 | 0:b1b7113a1441 | 182 | RGBavg.R= (RGBavg.R*(counter) + RGBdata[counter].R) /(float)(counter+1); |
chapman515 | 0:b1b7113a1441 | 183 | RGBavg.G= (RGBavg.G*(counter) + RGBdata[counter].G) /(float)(counter+1); |
chapman515 | 0:b1b7113a1441 | 184 | RGBavg.B= (RGBavg.B*(counter) + RGBdata[counter].B) /(float)(counter+1); |
chapman515 | 0:b1b7113a1441 | 185 | |
chapman515 | 0:b1b7113a1441 | 186 | THavg.t = ((THavg.t*(counter) + THdata[counter].t))/(counter+1); |
chapman515 | 0:b1b7113a1441 | 187 | THavg.h = ((THavg.h*(counter) + THdata[counter].h))/(counter+1); |
kentwong | 6:94a26631ec03 | 188 | |
chapman515 | 0:b1b7113a1441 | 189 | luxavg = ((luxavg*(counter) + luxdata[counter]))/(counter+1); |
chapman515 | 0:b1b7113a1441 | 190 | } |
chapman515 | 0:b1b7113a1441 | 191 | |
kentwong | 6:94a26631ec03 | 192 | //pc.printf("test"); |
kentwong | 6:94a26631ec03 | 193 | // 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); |
chapman515 | 0:b1b7113a1441 | 194 | BLE_Can_receive = 0; |
kentwong | 6:94a26631ec03 | 195 | counter++; |
kentwong | 6:94a26631ec03 | 196 | if (counter == 10){ |
kentwong | 6:94a26631ec03 | 197 | FILE *fp = fopen("/local/out.txt", "a"); |
kentwong | 6:94a26631ec03 | 198 | fprintf(fp, "{%d,%d,%d,%d,%3.2f,%3.2f%,%0.001f},",(int)(RGBavg.C+0.5), |
kentwong | 6:94a26631ec03 | 199 | (int)(RGBavg.R+0.5), (int)(RGBavg.G+0.5), (int)(RGBavg.B+0.5),THavg.t, THavg.h,luxavg); |
kentwong | 6:94a26631ec03 | 200 | fclose(fp); |
kentwong | 6:94a26631ec03 | 201 | allfilled = true; |
kentwong | 6:94a26631ec03 | 202 | } |
chapman515 | 0:b1b7113a1441 | 203 | BLE_Can_receive = 1; |
kentwong | 6:94a26631ec03 | 204 | if (wrote){ |
kentwong | 6:94a26631ec03 | 205 | //wait_ms(5); |
kentwong | 6:94a26631ec03 | 206 | pc.printf("%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); |
kentwong | 6:94a26631ec03 | 207 | if (counter == 10){ //10 = all element is filled { |
kentwong | 6:94a26631ec03 | 208 | if (signal[0]=='i'){ |
kentwong | 6:94a26631ec03 | 209 | pc.printf("end"); |
kentwong | 6:94a26631ec03 | 210 | } |
kentwong | 6:94a26631ec03 | 211 | else if (signal[0]=='a') |
kentwong | 6:94a26631ec03 | 212 | { |
kentwong | 6:94a26631ec03 | 213 | pc.printf("aend"); |
kentwong | 6:94a26631ec03 | 214 | } |
kentwong | 6:94a26631ec03 | 215 | wrote=false; |
kentwong | 6:94a26631ec03 | 216 | } |
kentwong | 6:94a26631ec03 | 217 | } |
kentwong | 6:94a26631ec03 | 218 | else{ |
kentwong | 6:94a26631ec03 | 219 | if(pc.readable() ) { |
kentwong | 6:94a26631ec03 | 220 | //pc.printf("can read now"); |
kentwong | 6:94a26631ec03 | 221 | led=!led; |
kentwong | 6:94a26631ec03 | 222 | pc.scanf("%1s", signal); |
kentwong | 6:94a26631ec03 | 223 | wrote=true; |
kentwong | 6:94a26631ec03 | 224 | } |
kentwong | 6:94a26631ec03 | 225 | |
kentwong | 6:94a26631ec03 | 226 | } |
kentwong | 6:94a26631ec03 | 227 | |
kentwong | 6:94a26631ec03 | 228 | |
chapman515 | 0:b1b7113a1441 | 229 | } |
chapman515 | 0:b1b7113a1441 | 230 | } |