-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEggstia.ino
262 lines (201 loc) · 5.95 KB
/
Eggstia.ino
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
252
253
254
255
256
257
258
259
260
261
262
#include "Arduino.h"
#include "Thread.h"
#include "ThreadController.h"
#include "StaticThreadController.h"
#include "buildConf.h"
#include "RGB_LED.h"
#include "Shinyei_Model_PPD42NS_Particle_Sensor.h"
#include "HTU21D_Humidity_Temperature.h"
#include "capacitiveTouch.h"
// Values threshold
#define THRESHOLD_TEMPERATURE_MIN 15.0
#define THRESHOLD_TEMPERATURE_MAX 25.0
#define THRESHOLD_HUMIDITY_MIN 30
#define THRESHOLD_HUMIDITY_MAX 70
#define THRESHOLD_NOTE_MIN 0
#define THRESHOLD_NOTE_MAX 10
// LED mode change times
#define RGB_LED_SHOW_MODE_TIME 1000
#define RGB_LED_SHOW_BLACK_TIME 100
// THREADS
#define THREAD_DEFAULT_INTERVAL 10
Thread myThread = Thread();
Thread thread_readingSensors = Thread();
Thread thread_readingUserInteract = Thread();
Thread thread_settingLED = Thread();
Thread thread_dataSend = Thread();
ThreadController threadController = ThreadController();
enum LED_STATUS_t {
LED_STATUS_TEMPERATURE_e,
LED_STATUS_HUMIDITY_e,
LED_STATUS_AIRNOTE_e,
LED_STATUS_GLOBAL_NOTE_e
};
LED_STATUS_t led_status = LED_STATUS_TEMPERATURE_e;
LED_STATUS_t led_status_old = led_status;
void loop_readingSensors(){
#ifdef DEBUG
Serial.println("loop_readingSensors");
#endif
loop_PPD42NS();
loop_HTU21D();
}
void loop_readingUserInteract(){
#ifdef DEBUG
Serial.println("loop_readingUserInteract");
#endif
loop_capacitiveSensor();
}
void loop_RGB_LED(){
#ifdef DEBUG
Serial.println("loop_RGB_LED");
#endif
if(led_status != led_status_old)
{
led_status_old = led_status;
timeout_led = RGB_LED_SHOW_MODE_TIME + 3 * RGB_LED_SHOW_BLACK_TIME;
}
if(timeout_led == 0)
{
switch ( switch_status ) {
case SWITCH_STATUS_ON_e:
switch ( led_status ) {
case LED_STATUS_TEMPERATURE_e:
RGB_LED_display_value_RGB(value_temperature, (float) THRESHOLD_TEMPERATURE_MIN, (float) THRESHOLD_TEMPERATURE_MAX);
break;
case LED_STATUS_HUMIDITY_e:
RGB_LED_display_value_RGB(value_humidity, (float) THRESHOLD_HUMIDITY_MIN, (float) THRESHOLD_HUMIDITY_MAX);
break;
case LED_STATUS_AIRNOTE_e:
RGB_LED_display_value(value_airNote, (float) THRESHOLD_NOTE_MIN, (float) THRESHOLD_NOTE_MAX, LED_COLOR_R_e, LED_COLOR_G_e);
break;
case LED_STATUS_GLOBAL_NOTE_e:
RGB_LED_display_value(tools_GlobalNoteCalculator(), (float) THRESHOLD_NOTE_MIN, (float) THRESHOLD_NOTE_MAX, LED_COLOR_R_e,LED_COLOR_G_e);
break;
default:
// Code
break;
}
break;
case SWITCH_STATUS_OFF_e:
RGB_LED_set_black();
break;
default:
// Code
break;
}
}
else if ((timeout_led < RGB_LED_SHOW_BLACK_TIME)
|| (timeout_led < 3 * RGB_LED_SHOW_BLACK_TIME && timeout_led > 2 * RGB_LED_SHOW_BLACK_TIME )
|| (timeout_led > RGB_LED_SHOW_MODE_TIME + 2 * RGB_LED_SHOW_BLACK_TIME))
{
RGB_LED_set_black();
}
else
{
switch ( led_status ) {
case LED_STATUS_TEMPERATURE_e:
RGB_LED_set_red();
break;
case LED_STATUS_HUMIDITY_e:
RGB_LED_set_blue();
break;
case LED_STATUS_AIRNOTE_e:
RGB_LED_set_green();
break;
case LED_STATUS_GLOBAL_NOTE_e:
RGB_LED_set_white();
break;
default:
// Code
break;
}
}
}
void loop_dataSend(){
#ifdef DEBUG
Serial.println("loop_dataSend");
#endif
}
int tools_GlobalNoteCalculator(){
//TODO : Note calcul of hum and temp : MED is the best
int VALUE_TEMPERATURE_255 = ( (int) value_temperature - THRESHOLD_TEMPERATURE_MIN ) * 255 / THRESHOLD_TEMPERATURE_MAX;
int VALUE_HUMIDITY_255 = ( (int) value_humidity - THRESHOLD_HUMIDITY_MIN ) * 255 / THRESHOLD_HUMIDITY_MAX;
int VALUE_AIR_NOTE_255 = ( (int) value_airNote - THRESHOLD_NOTE_MIN ) * 255 / THRESHOLD_NOTE_MAX;
int VALUE_GLOBAL_NOTE_255 = (VALUE_TEMPERATURE_255 + VALUE_HUMIDITY_255 + VALUE_AIR_NOTE_255)/3;
int VALUE_GLOBAL_NOTE = VALUE_GLOBAL_NOTE_255 * 10 / 255;
#ifdef DEBUG
Serial.print("Global Note : ");
Serial.print(VALUE_GLOBAL_NOTE_255);
Serial.print("(");
Serial.print(VALUE_GLOBAL_NOTE);
Serial.print("/");
Serial.print(THRESHOLD_NOTE_MAX);
Serial.println(")");
#endif
return VALUE_GLOBAL_NOTE;
}
//The setup function is called once at startup of the sketch
void setup()
{
// Add your initialization code here
#ifdef DEBUG
Serial.begin(9600);
Serial.println("Project : Eggstia");
Serial.print("Build : ");
Serial.print(__TIME__);
Serial.print(" ");
Serial.println(__DATE__);
Serial.println("Setting up...");
#endif
setup_RGB_LED();
RGB_LED_set_blue();
delay(1000);
setup_timer();
setup_HTU21D();
setup_capacitiveSensor();
loop_readingSensors();
thread_readingSensors.enabled = true;
thread_readingUserInteract.enabled = true;
thread_settingLED.enabled = true;
thread_dataSend.enabled = true;
thread_readingSensors.setInterval(10000);
thread_readingUserInteract.setInterval(1);
thread_settingLED.setInterval(1);
thread_dataSend.setInterval(1000);
thread_readingSensors.onRun(loop_readingSensors);
thread_readingUserInteract.onRun(loop_readingUserInteract);
thread_settingLED.onRun(loop_RGB_LED);
thread_dataSend.onRun(loop_dataSend);
threadController.add(&thread_readingSensors);
threadController.add(&thread_readingUserInteract);
threadController.add(&thread_settingLED);
threadController.add(&thread_dataSend);
RGB_LED_set_black();
delay(RGB_LED_SHOW_BLACK_TIME);
RGB_LED_set_green();
delay(RGB_LED_SHOW_BLACK_TIME);
RGB_LED_set_black();
delay(RGB_LED_SHOW_BLACK_TIME);
RGB_LED_set_green();
delay(RGB_LED_SHOW_BLACK_TIME);
RGB_LED_set_black();
#ifdef DEBUG
Serial.println("Setted up !");
#endif
}
// The loop function is called in an endless loop
void loop()
{
//Add your repeated code here
#ifdef DEBUG
Serial.println("loop_main");
delay(100);
#endif
#ifdef TEST
RGB_LED_display_value_RGB(19.9, (float) THRESHOLD_TEMPERATURE_MIN, (float) THRESHOLD_TEMPERATURE_MAX);
delay(990);
#else
threadController.run();
#endif
}