-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
253 lines (217 loc) · 8.86 KB
/
main.cpp
File metadata and controls
253 lines (217 loc) · 8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "mbed.h"
#include "rtos.h"
#include "project_config.h"
Serial pc(USBTX,USBRX,19200);
InterruptIn userButton (PB_2);
BusOut monitoringTypeLed(LED1, LED2, LED3);
DigitalOut red(PH_0);
DigitalOut green(PH_1);
DigitalOut blue(PA_5);
const char *MODES [] = {TEST_MODE_LABEL, NORMAL_MODE_LABEL, LOWPOWER_MODE_LABEL, ADVANCED_MODE_LABEL};
const int SAMPLING [] = {TEST_SAMPLING,NORMAL_SAMPLING, 0, 0};
const char *COLORS [] = {NA, RED, GREEN, BLUE};
short int analogTicks;
short int i2cTicks;
extern Thread threadANALOG;
extern Thread threadGPS;
extern Thread threadI2C;
extern void ANALOG_thread();
extern void GPS_thread();
extern void I2C_thread();
extern float smValue;
extern float lightValue;
extern float tempValue;
extern float humidityValue;
extern gps_data gps_info;
extern rgb_data rgb_info;
extern acc_data acc_info;
extern sensor_sumary_data sm_summary_info;
extern sensor_sumary_data light_summary_info;
extern sensor_sumary_data temp_summary_info;
extern sensor_sumary_data humidity_summary_info;
extern acc_sumary_data acc_summary_info;
sensor_alert_data sensor_alert_info;
EventFlags gps_event;
bool modeChanged = true;
int mode = TEST_MODE;
//Timer for printing statistics
Timer st_timer;
//Ticker for defining sampling instant
Ticker app_ticker;
bool ticker_fired = false;
void sample() {
ticker_fired = true;
}
//Handle test change
void userButtonHandler(void) {
mode+=1;
if (mode>=MAX_MODES) {
mode = TEST_MODE;
}
if (mode==NORMAL_MODE) {
st_timer.start();
} else if (mode==TEST_MODE) {
st_timer.stop();
st_timer.reset();
}
//$ TICKER ON
//app_ticker.detach();
//app_ticker.attach(sample,SAMPLING[mode]);
modeChanged = true;
}
int getLedStatus(int mode) {
int result = 0;
switch(mode) {
case TEST_MODE: result = 1;
break;
case NORMAL_MODE: result = 2;
break;
case LOWPOWER_MODE: result = 4;
break;
case ADVANCED_MODE: result = 8;
break;
}
return result;
}
void lightLed(bool sm, bool light, bool temp, bool humidity) {
if (sm) {
red = 0;
green = 0;
blue = 1;
} else if (light) {
red = 0;
green = 0;
blue = 0;
} else if (temp) {
red = 0;
green = 1;
blue = 0;
} else if (humidity) {
red = 1;
green = 0;
blue = 0;
}
}
void handle_data() {
pc.printf("*******************************************\n\r");
pc.printf("Mode:%s\n\r",MODES[mode]);
pc.printf("Soil Moisture-> %6.3f%%\r\n", smValue);
pc.printf("Light -> %6.3f%%\r\n", lightValue);
pc.printf("Accelerometer-> xAxis:%f, yAxis:%f, zAxis:%f\n\r", acc_info.xPos, acc_info.yPos, acc_info.zPos);
pc.printf("Color-> Clear:%d, Red:%d,Green:%d,Blue:%d -- Predominant:%s\n\r", rgb_info.clear, rgb_info.red, rgb_info.green, rgb_info.blue, rgb_info.color);
pc.printf("Temp/Hummidity-> Temperature:%0.3fºC, Relative Humidity:%0.3f%\n\r", tempValue, humidityValue );
pc.printf("GPS-> #Sats:%d, Lat(UTC):%f, Long(UTC):%f, Altitude:%f, GPS Time: %d%d:%d%d:%d%d\r\n", gps_info.satellites,gps_info.latitude,gps_info.longitude, gps_info.altitude,
(int) gps_info.time / 100000 % 10, (int) gps_info.time / 10000 % 10,
(int) gps_info.time / 1000 % 10, (int) gps_info.time / 1000 % 10,
(int) gps_info.time / 10 % 10, (int) gps_info.time % 10);
}
void light_led_color(char *color) {
if (strncmp(color,RED,2) == 0){
red = 0;
green = 1;
blue = 1;
}else if (strncmp(color,BLUE,2) == 0){
red = 1;
green = 1;
blue = 0;
}else {
red = 1;
green = 0;
blue = 1;
}
}
void handle_statistical_led() {
//If errors
if (sm_summary_info.bounds_error || light_summary_info.bounds_error || temp_summary_info.bounds_error || humidity_summary_info.bounds_error) {
if (sensor_alert_info.sm>sensor_alert_info.light && sensor_alert_info.sm>sensor_alert_info.temperature && sensor_alert_info.sm>sensor_alert_info.humidity) {
//lightLed(sm_summary_info.bounds_error,light_summary_info.bounds_error,temp_summary_info.bounds_error,humidity_summary_info.bounds_error);
lightLed(true,false,false,false);
} else if (sensor_alert_info.light>sensor_alert_info.sm && sensor_alert_info.light>sensor_alert_info.temperature && sensor_alert_info.light>sensor_alert_info.humidity) {
//lightLed(sm_summary_info.bounds_error,light_summary_info.bounds_error,temp_summary_info.bounds_error,humidity_summary_info.bounds_error);
lightLed(false,true,false,false);
} else if (sensor_alert_info.temperature>sensor_alert_info.sm && sensor_alert_info.temperature>sensor_alert_info.light && sensor_alert_info.temperature>sensor_alert_info.humidity) {
//lightLed(sm_summary_info.bounds_error,light_summary_info.bounds_error,temp_summary_info.bounds_error,humidity_summary_info.bounds_error);
lightLed(false,false,true,false);
} else {
lightLed(false,false,false,true);
}
}
//Premodimant color in period
else {
if (sensor_alert_info.blue>sensor_alert_info.red && sensor_alert_info.blue>sensor_alert_info.green) {
light_led_color(BLUE);
} else if (sensor_alert_info.red>sensor_alert_info.blue && sensor_alert_info.red>sensor_alert_info.green) {
light_led_color(RED);
} else {
light_led_color(GREEN);
}
}
}
void handle_statistical_data() {
pc.printf("****************STATISTICS*****************\n\r");
pc.printf("Light-> Max:%0.3fºC, Mean:%0.3fºC, Min:%0.3fºC\n\r", light_summary_info.max, light_summary_info.mean, light_summary_info.min );
pc.printf("Temperature-> Max:%0.3fºC, Mean:%0.3fºC, Min:%0.3fºC\n\r", temp_summary_info.max, temp_summary_info.mean, temp_summary_info.min );
pc.printf("Humidity-> Max:%6.3f%%, Mean:%6.3f%%, Min:%6.3f%%\n\r", humidity_summary_info.max, humidity_summary_info.mean, humidity_summary_info.min );
pc.printf("Soil Moisture-> Max:%6.3f%%, Mean:%6.3f%%, Min:%6.3f%%\n\r", sm_summary_info.max, sm_summary_info.mean, sm_summary_info.min );
pc.printf("Accelerometer-> Max(x,y,z):(%6.3f,%6.3f,%6.3f) Min(x,y,z):%6.3f,%6.3f,%6.3f \n\r", acc_summary_info.xmax, acc_summary_info.ymax, acc_summary_info.zmax,
acc_summary_info.xmin, acc_summary_info.ymin, acc_summary_info.zmin );
pc.printf("Alerts-> sm:%d, light:%d, temp:%d, humidity:%d\n\r", sensor_alert_info.sm, sensor_alert_info.light, sensor_alert_info.temperature , sensor_alert_info.humidity);
handle_statistical_led();
}
//Init data
void init_statistical_data() {
light_summary_info.bounds_error = false;
humidity_summary_info.bounds_error = false;
temp_summary_info.bounds_error = false;
temp_summary_info.bounds_error = false;
light_summary_info.max=0, light_summary_info.mean=0, light_summary_info.min=0;
temp_summary_info.max=0, temp_summary_info.mean=0, temp_summary_info.min=0;
humidity_summary_info.max=0, humidity_summary_info.mean=0, humidity_summary_info.min=0;
sm_summary_info.max = 0, sm_summary_info.mean = 0, sm_summary_info.min = 0;
acc_summary_info.xmax=0, acc_summary_info.ymax=0, acc_summary_info.zmax=0,
acc_summary_info.xmin=0, acc_summary_info.ymin=0, acc_summary_info.zmin=0;
sensor_alert_info.green =0, sensor_alert_info.blue =0, sensor_alert_info.red =0;
sensor_alert_info.sm =0, sensor_alert_info.humidity =0, sensor_alert_info.light =0, sensor_alert_info.temperature =0;
i2cTicks = 1;
analogTicks = 1;
}
// main() runs in its own thread in the OS
int main() {
//Registers user button pullup for changing mode
userButton.mode(PullUp);
userButton.rise(userButtonHandler);
//Start threads
threadANALOG.start(ANALOG_thread);
threadGPS.start(GPS_thread);
threadI2C.start(I2C_thread);
init_statistical_data();
// $TICKER_ON
app_ticker.attach(sample,SAMPLING[mode]);
pc.printf("mbed-os-rev: %d.%d.%d\r\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
while (true) {
// $TICKER_ON
// if (ticker_fired) {
// gps_event.set(0x01);
// handle_data();
// ticker_fired = !ticker_fired;
// }
// $TICKER OFF
gps_event.set(0x01);
handle_data();
if (modeChanged) {
monitoringTypeLed.write(mode);
}
if (mode==NORMAL_MODE) {
if (st_timer.read_ms()>=STATISTIC_TIME) {
handle_statistical_data();
init_statistical_data();
st_timer.reset();
}
} else if (mode==TEST_MODE) {
light_led_color(rgb_info.color);
}
// Thread::wait(1);
// $TICKER OFF
Thread::wait(SAMPLING[mode]*1000);
}
}