a

Dependencies:   mbed Sht31 MAX44009

Committer:
kentwong
Date:
Mon Apr 13 09:48:01 2020 +0000
Revision:
5:006e5cf5c2de
Parent:
4:2ca382ec8a09
Child:
6:94a26631ec03
123

Who changed what in which revision?

UserRevisionLine numberNew 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 2:b366d73a213c 13 //132132134
kentwong 3:cd64f8ea4b8e 14 //fuckyou
chapman515 4:2ca382ec8a09 15 //fuckme
kentwong 5:006e5cf5c2de 16 //dont fuck me
chapman515 1:3889765eb394 17 InterruptIn BLE_Can_temp(p12);
chapman515 0:b1b7113a1441 18
chapman515 0:b1b7113a1441 19
chapman515 0:b1b7113a1441 20 DigitalOut BLE_Can_receive(p11); // 0 = can recieve
chapman515 0:b1b7113a1441 21
chapman515 0:b1b7113a1441 22 class RGB{
chapman515 0:b1b7113a1441 23 public : int C;
chapman515 0:b1b7113a1441 24 int R;
chapman515 0:b1b7113a1441 25 int G;
chapman515 0:b1b7113a1441 26 int B;
chapman515 0:b1b7113a1441 27 };
chapman515 0:b1b7113a1441 28 class RGBf{
chapman515 0:b1b7113a1441 29 public : float C;
chapman515 0:b1b7113a1441 30 float R;
chapman515 0:b1b7113a1441 31 float G;
chapman515 0:b1b7113a1441 32 float B;
chapman515 0:b1b7113a1441 33 };
chapman515 0:b1b7113a1441 34 class TandH{
chapman515 0:b1b7113a1441 35 public : float t;
chapman515 0:b1b7113a1441 36 float h;
chapman515 0:b1b7113a1441 37 };
chapman515 0:b1b7113a1441 38
chapman515 0:b1b7113a1441 39 RGB RGBdata[10];
chapman515 0:b1b7113a1441 40 TandH THdata[10];
chapman515 0:b1b7113a1441 41 float luxdata[10];
chapman515 0:b1b7113a1441 42
chapman515 0:b1b7113a1441 43 int counter = 10; //to tackle with 0 mod 10 problem, use 10 instead;
chapman515 0:b1b7113a1441 44
chapman515 0:b1b7113a1441 45 bool allfilled = false;
chapman515 0:b1b7113a1441 46 RGBf RGBavg;
chapman515 0:b1b7113a1441 47 TandH THavg;
chapman515 0:b1b7113a1441 48 float luxavg;
chapman515 0:b1b7113a1441 49
chapman515 1:3889765eb394 50 DigitalOut led(LED1);
chapman515 1:3889765eb394 51
chapman515 1:3889765eb394 52 void flip() {
chapman515 1:3889765eb394 53 led = !led;
chapman515 1:3889765eb394 54 }
chapman515 0:b1b7113a1441 55
chapman515 0:b1b7113a1441 56 int main()
chapman515 0:b1b7113a1441 57 {
chapman515 0:b1b7113a1441 58 pc.baud ( 19200 );
chapman515 1:3889765eb394 59 Batt2SYS = 0;
chapman515 0:b1b7113a1441 60
chapman515 1:3889765eb394 61 BLE_Can_temp.fall(&flip);
chapman515 0:b1b7113a1441 62 while(1) {
chapman515 0:b1b7113a1441 63 counter = counter%10;
chapman515 0:b1b7113a1441 64 //BLE_Can_receive = 0;
chapman515 0:b1b7113a1441 65 //wait_ms(5);
chapman515 0:b1b7113a1441 66 //pc.printf("counter31 = %d\n", counter);
chapman515 0:b1b7113a1441 67 //BLE_Can_receive = 1;
chapman515 0:b1b7113a1441 68 {
chapman515 0:b1b7113a1441 69 int sensor_addr = 41 << 1;
chapman515 0:b1b7113a1441 70 char id_regval[1] = {146};
chapman515 0:b1b7113a1441 71 char data[1] = {0};
chapman515 0:b1b7113a1441 72 i2c.write(sensor_addr,id_regval,1, true);
chapman515 0:b1b7113a1441 73 i2c.read(sensor_addr,data,1,false);
chapman515 0:b1b7113a1441 74
chapman515 0:b1b7113a1441 75
chapman515 0:b1b7113a1441 76 // Initialize color sensor
chapman515 0:b1b7113a1441 77
chapman515 0:b1b7113a1441 78 char timing_register[2] = {129,192};
chapman515 0:b1b7113a1441 79 //char timing_register[2] = {129,0};
chapman515 0:b1b7113a1441 80 i2c.write(sensor_addr,timing_register,2,false);
chapman515 0:b1b7113a1441 81
chapman515 0:b1b7113a1441 82 char control_register[2] = {143,0};
chapman515 0:b1b7113a1441 83 char temp[2]={0,0};
chapman515 0:b1b7113a1441 84 //char control_register[2] = {143,3};
chapman515 0:b1b7113a1441 85 i2c.write(sensor_addr,control_register,2,false);
chapman515 0:b1b7113a1441 86
chapman515 0:b1b7113a1441 87 char enable_register[2] = {128,3};
chapman515 0:b1b7113a1441 88 i2c.write(sensor_addr,enable_register,2,false);
chapman515 0:b1b7113a1441 89
chapman515 0:b1b7113a1441 90 // Read data from color sensor (Clear/Red/Green/Blue)
chapman515 0:b1b7113a1441 91
chapman515 0:b1b7113a1441 92 char clear_reg[1] = {148};
chapman515 0:b1b7113a1441 93 char clear_data[2] = {0,0};
chapman515 0:b1b7113a1441 94 i2c.write(sensor_addr,clear_reg,1, true);
chapman515 0:b1b7113a1441 95 i2c.read(sensor_addr,clear_data,2, false);
chapman515 0:b1b7113a1441 96
chapman515 0:b1b7113a1441 97 int clear_value = ((int)clear_data[1] << 8) | clear_data[0];
chapman515 0:b1b7113a1441 98
chapman515 0:b1b7113a1441 99 char red_reg[1] = {150};
chapman515 0:b1b7113a1441 100 char red_data[2] = {0,0};
chapman515 0:b1b7113a1441 101 i2c.write(sensor_addr,red_reg,1, true);
chapman515 0:b1b7113a1441 102 i2c.read(sensor_addr,red_data,2, false);
chapman515 0:b1b7113a1441 103
chapman515 0:b1b7113a1441 104 int red_value = ((int)red_data[1] << 8) | red_data[0];
chapman515 0:b1b7113a1441 105
chapman515 0:b1b7113a1441 106 char green_reg[1] = {152};
chapman515 0:b1b7113a1441 107 char green_data[2] = {0,0};
chapman515 0:b1b7113a1441 108 i2c.write(sensor_addr,green_reg,1, true);
chapman515 0:b1b7113a1441 109 i2c.read(sensor_addr,green_data,2, false);
chapman515 0:b1b7113a1441 110
chapman515 0:b1b7113a1441 111 int green_value = ((int)green_data[1] << 8) | green_data[0];
chapman515 0:b1b7113a1441 112
chapman515 0:b1b7113a1441 113 char blue_reg[1] = {154};
chapman515 0:b1b7113a1441 114 char blue_data[2] = {0,0};
chapman515 0:b1b7113a1441 115 i2c.write(sensor_addr,blue_reg,1, true);
chapman515 0:b1b7113a1441 116 i2c.read(sensor_addr,blue_data,2, false);
chapman515 0:b1b7113a1441 117
chapman515 0:b1b7113a1441 118 int blue_value = ((int)blue_data[1] << 8) | blue_data[0];
chapman515 0:b1b7113a1441 119
chapman515 0:b1b7113a1441 120 // print sensor readings
chapman515 0:b1b7113a1441 121 if (allfilled == true){
chapman515 0:b1b7113a1441 122 RGBavg.C = RGBavg.C*10-RGBdata[counter].C;
chapman515 0:b1b7113a1441 123 RGBavg.R = RGBavg.R*10-RGBdata[counter].R;
chapman515 0:b1b7113a1441 124 RGBavg.G = RGBavg.G*10-RGBdata[counter].G;
chapman515 0:b1b7113a1441 125 RGBavg.B = RGBavg.B*10-RGBdata[counter].B;
chapman515 0:b1b7113a1441 126 }
chapman515 0:b1b7113a1441 127 RGBdata[counter].C= clear_value;
chapman515 0:b1b7113a1441 128 RGBdata[counter].R= red_value;
chapman515 0:b1b7113a1441 129 RGBdata[counter].G= green_value;
chapman515 0:b1b7113a1441 130 RGBdata[counter].B= blue_value;
chapman515 0:b1b7113a1441 131 //pc.printf("Clear (%d), Red (%d), Green (%d), Blue (%d)\n", clear_value, red_value, green_value, blue_value);
chapman515 0:b1b7113a1441 132 }
chapman515 0:b1b7113a1441 133 {
chapman515 0:b1b7113a1441 134
chapman515 0:b1b7113a1441 135 MAX44009::MAX44009_status_t aux;
chapman515 0:b1b7113a1441 136 MAX44009::MAX44009_vector_data_t myMAX44009_Data;
chapman515 0:b1b7113a1441 137
chapman515 0:b1b7113a1441 138 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 139 aux = myMAX44009.MAX44009_GetLux( MAX44009::RESOLUTION_EXTENDED_RESOLUTION, &myMAX44009_Data );
chapman515 0:b1b7113a1441 140 aux = myMAX44009.MAX44009_GetCurrentDivisionRatio ( &myMAX44009_Data );
chapman515 0:b1b7113a1441 141 aux = myMAX44009.MAX44009_GetIntegrationTime ( &myMAX44009_Data );
chapman515 0:b1b7113a1441 142
chapman515 0:b1b7113a1441 143 if (allfilled == true){
chapman515 0:b1b7113a1441 144 luxavg = luxavg*10-luxdata[counter];
chapman515 0:b1b7113a1441 145 }
chapman515 0:b1b7113a1441 146
chapman515 0:b1b7113a1441 147 luxdata[counter] = myMAX44009_Data.lux;
chapman515 0:b1b7113a1441 148
chapman515 0:b1b7113a1441 149
chapman515 0:b1b7113a1441 150 }
chapman515 0:b1b7113a1441 151
chapman515 0:b1b7113a1441 152 {
chapman515 0:b1b7113a1441 153
chapman515 0:b1b7113a1441 154 //float t = sht31.readTemperature();
chapman515 0:b1b7113a1441 155 //float h = sht31.readHumidity();
chapman515 0:b1b7113a1441 156
chapman515 0:b1b7113a1441 157 if (allfilled == true){
chapman515 0:b1b7113a1441 158 THavg.t = THavg.t*10-THdata[counter].t;
chapman515 0:b1b7113a1441 159 THavg.h = THavg.h*10-THdata[counter].h;
chapman515 0:b1b7113a1441 160 }
chapman515 0:b1b7113a1441 161 THdata[counter].t = sht31.readTemperature();
chapman515 0:b1b7113a1441 162 THdata[counter].h = sht31.readHumidity();
chapman515 0:b1b7113a1441 163
chapman515 0:b1b7113a1441 164 //pc.printf("[TEMP/HUM]");
chapman515 0:b1b7113a1441 165
chapman515 0:b1b7113a1441 166 }
chapman515 0:b1b7113a1441 167 ////////////////////////////////////////////////////////////////////
chapman515 0:b1b7113a1441 168
chapman515 0:b1b7113a1441 169 /*BLE_Can_receive = 0;
chapman515 0:b1b7113a1441 170 wait_ms(5);
chapman515 0:b1b7113a1441 171 pc.printf("counter119\n = %d", counter);
chapman515 0:b1b7113a1441 172 for (int i =0;i<10;i++){
chapman515 0:b1b7113a1441 173 pc.printf("i=%d ,", i);
chapman515 0:b1b7113a1441 174 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 175 }
chapman515 0:b1b7113a1441 176 BLE_Can_receive = 1;*/
chapman515 0:b1b7113a1441 177
chapman515 0:b1b7113a1441 178 if (allfilled == true){
chapman515 0:b1b7113a1441 179 RGBavg.C = ((RGBavg.C+RGBdata[counter].C)/10.0);
chapman515 0:b1b7113a1441 180 RGBavg.R = ((RGBavg.R+RGBdata[counter].R)/10.0);
chapman515 0:b1b7113a1441 181 RGBavg.G = ((RGBavg.G+RGBdata[counter].G)/10.0);
chapman515 0:b1b7113a1441 182 RGBavg.B = ((RGBavg.B+RGBdata[counter].B)/10.0);
chapman515 0:b1b7113a1441 183
chapman515 0:b1b7113a1441 184 THavg.t = (THavg.t+THdata[counter].t)/10;
chapman515 0:b1b7113a1441 185 THavg.h = (THavg.h+THdata[counter].h)/10;
chapman515 0:b1b7113a1441 186
chapman515 0:b1b7113a1441 187 luxavg = (luxavg+luxdata[counter])/10;
chapman515 0:b1b7113a1441 188 }
chapman515 0:b1b7113a1441 189 else{
chapman515 0:b1b7113a1441 190
chapman515 0:b1b7113a1441 191 RGBavg.C= (RGBavg.C*(counter) + RGBdata[counter].C) /(float)(counter+1);
chapman515 0:b1b7113a1441 192 RGBavg.R= (RGBavg.R*(counter) + RGBdata[counter].R) /(float)(counter+1);
chapman515 0:b1b7113a1441 193 RGBavg.G= (RGBavg.G*(counter) + RGBdata[counter].G) /(float)(counter+1);
chapman515 0:b1b7113a1441 194 RGBavg.B= (RGBavg.B*(counter) + RGBdata[counter].B) /(float)(counter+1);
chapman515 0:b1b7113a1441 195
chapman515 0:b1b7113a1441 196 THavg.t = ((THavg.t*(counter) + THdata[counter].t))/(counter+1);
chapman515 0:b1b7113a1441 197 THavg.h = ((THavg.h*(counter) + THdata[counter].h))/(counter+1);
chapman515 0:b1b7113a1441 198
chapman515 0:b1b7113a1441 199 luxavg = ((luxavg*(counter) + luxdata[counter]))/(counter+1);
chapman515 0:b1b7113a1441 200 }
chapman515 0:b1b7113a1441 201
chapman515 0:b1b7113a1441 202 BLE_Can_receive = 0;
chapman515 0:b1b7113a1441 203 wait_ms(5);
chapman515 0:b1b7113a1441 204 pc.printf("%d,%d,%d,%d,%3.2f,%3.2f%,%0.001f\n",(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 205 counter = counter++;
chapman515 0:b1b7113a1441 206
chapman515 0:b1b7113a1441 207 if (counter == 10) //10 = all element is filled
chapman515 0:b1b7113a1441 208 allfilled = true;
chapman515 0:b1b7113a1441 209
chapman515 0:b1b7113a1441 210 wait(1);
chapman515 0:b1b7113a1441 211 BLE_Can_receive = 1;
chapman515 0:b1b7113a1441 212 }
chapman515 0:b1b7113a1441 213 }