sensors.c
12.4 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#include "sensors.h"
sensor light_sensor = {LIGHT_ADDR, "Light sensor", "illumination", "lux",NULL};
sensor ultrasound_sensor = {DISTANCE_ADDR, "Ultrasound sensor", "distance", "cm",NULL};
sensor pressure_sensor = {PRESSURE_ADDR, "Pressure sensor", "pressure", "hPa",NULL};
sensor humidity_temp_sensor = {HUMIDITY_TEMP_ADDR, "Humidity and Temperature","ºC,RH",NULL};
sensor sound_sensor = {SOUND_ADDR, "Sound sensor","mV",NULL};
sensor battery = {BATTERY_ADDR, "Battery Level","mV",NULL};
sensor* sensors[TOTAL_SENSORS+1] = {&light_sensor,&ultrasound_sensor,&pressure_sensor,&humidity_temp_sensor,&sound_sensor,&battery};
void wakeup_sensors(uint8_t logic_address)
{
/* Aquí habría que implementar un control
* inteligente de la alimentación de los
* sensores, levantándolos y durmiéndolos
* cuando tocara
*/
// X X X PA7 PA6 PA5 PA4 PA3
// b7 b6 b5 b4 b3 b2 b1 b0
printf("GRAB A BRUSH AND PUT A LITTLE MAKE-UP\r\n");
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ADCsound_GPIO_PORT, &GPIO_InitStructure);
if(logic_address & 0x1)
{
printf("Woken Pin 3\r\n");
GPIO_SetBits(GPIOA,GPIO_Pin_3);
}
else
{
GPIO_ResetBits(GPIOA,GPIO_Pin_3);
}
if(logic_address & 0x2)
{
printf("Woken Pin 4\r\n");
GPIO_SetBits(GPIOA,GPIO_Pin_4);
}
else
{
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
}
if(logic_address & 0x4)
{
printf("Woken Pin 5\r\n");
GPIO_SetBits(GPIOA,GPIO_Pin_5);
}
else
{
GPIO_ResetBits(GPIOA,GPIO_Pin_5);
}
if(logic_address & 0x8)
{
printf("Woken Pin 6\r\n");
GPIO_SetBits(GPIOA,GPIO_Pin_6);
}
else
{
GPIO_ResetBits(GPIOA,GPIO_Pin_6);
}
if(logic_address & 0x10)
{
printf("Woken Pin 7\r\n");
GPIO_SetBits(GPIOA,GPIO_Pin_7);
}
else
{
GPIO_ResetBits(GPIOA,GPIO_Pin_7);
}
}
uint32_t get_light_data(void)
{
uint32_t data = 0;
data = get_light_ch1();
printf("LIGHT: Got ch1\r\n");
data = data << 16;
data = data | get_light_ch0();
printf("LIGHT: Got ch0\r\n");
return data;
}
uint16_t get_light_ch0(void)
{
uint16_t data = 0;
I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x0C);
I2C_stop(I2C1);
printf("LIGHT: Sent fetch command for low byte for CH1\r\n");
I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);
data = I2C_read_ack(I2C1);
printf("LIGHT: Got low byte for CH1\r\n");
data = data | (I2C_read_ack(I2C1) << 8);
printf("LIGHT: Got high byte for CH1\r\n");
I2C_stop(I2C1);
return data;
}
uint16_t get_light_ch1(void)
{
uint16_t data = 0;
I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x0E);
I2C_stop(I2C1);
printf("LIGHT: Sent fetch command for low byte for CH1\r\n");
I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);
data = I2C_read_ack(I2C1);
printf("LIGHT: Got low byte for CH1\r\n");
data = data | (I2C_read_ack(I2C1) << 8);
printf("LIGHT: Got high byte for CH1\r\n");
I2C_stop(I2C1);
return data;
}
uint16_t get_distance_data(void)
{
init_ultrasound();
printf("DISTANCE: Initialized\r\n");
uint16_t data = get_distance_high();
printf("Got distance high byte\r\n");
data = data << 8;
data = data | get_distance_low();
printf("Got distance low byte\r\n");
return data;
}
void init_ultrasound(void)
{
I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x00);
I2C_stop(I2C1);
I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x50);
I2C_stop(I2C1);
chThdSleepMilliseconds(70);
}
uint8_t get_distance_low(void)
{
uint8_t data;
I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x03);
I2C_stop(I2C1);
I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver);
data = I2C_read_ack(I2C1);
I2C_stop(I2C1);
return data;
}
uint8_t get_distance_high(void)
{
uint8_t data;
I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x02);
I2C_stop(I2C1);
I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver);
data = I2C_read_ack(I2C1);
I2C_stop(I2C1);
return data;
}
uint16_t get_pressure_data(void)
{
uint16_t data;
init_pressure();
printf("PRESSURE: Initialized\r\n");
I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xF6);
I2C_stop(I2C1);
I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver);
data = I2C_read_ack(I2C1);
printf("PRESSURE: Got high byte\r\n");
data = data << 8;
data = data | I2C_read_nack(I2C1);
printf("PRESSURE: Got low byte\r\n");
return data;
}
uint16_t get_pressure_temperature_data(void)
{
uint16_t data;
init_pressure_temperature();
printf("PRESSURE: Initialized temperature\r\n");
I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xF6);
I2C_stop(I2C1);
I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver);
data = I2C_read_ack(I2C1);
printf("PRESSURE: Got temperature high byte\r\n");
data = data << 8;
data = data | I2C_read_nack(I2C1);
printf("PRESSURE: Got temperature low byte\r\n");
return data;
}
bmp085_callibration get_pressure_callibration_data(void)
{
printf("PRESSURE: Fetching callibration data\r\n");
bmp085_callibration calib_data;
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xAA);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.AC1 = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xAC);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.AC2 = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xAE);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.AC3 = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xB0);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.AC4 = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xB2);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.AC5 = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xB4);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.AC6 = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xB6);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.B1 = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xB8);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.B2 = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xBA);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.MB = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xBC);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.MC = I2C_read_ack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xBE);
I2C_stop(I2C1);
I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data.MD = I2C_read_ack(I2C1);
I2C_stop(I2C1);
printf("PRESSURE: Got callibration data\r\n");
return calib_data;
}
char* callibration_pressure_data_csv(bmp085_callibration parameters)
{
char *str = chHeapAlloc(NULL,sizeof(char)*(11*11-1));
memset(str,0x00,sizeof(char)*(11*11-1));
sprintf(str + strlen(str),"%d,",parameters.AC1);
sprintf(str + strlen(str),"%d,",parameters.AC2);
sprintf(str + strlen(str),"%d,",parameters.AC3);
sprintf(str + strlen(str),"%d,",parameters.AC4);
sprintf(str + strlen(str),"%d,",parameters.AC5);
sprintf(str + strlen(str),"%d,",parameters.AC6);
sprintf(str + strlen(str),"%d,",parameters.B1);
sprintf(str + strlen(str),"%d,",parameters.B2);
sprintf(str + strlen(str),"%d,",parameters.MB);
sprintf(str + strlen(str),"%d,",parameters.MC);
sprintf(str + strlen(str),"%d",parameters.MD);
return str;
}
void init_pressure(void)
{
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xF4);
I2C_write(I2C1, 0x34);
I2C_stop(I2C1);
chThdSleepMilliseconds(5);
}
void init_pressure_temperature(void)
{
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xF4);
I2C_write(I2C1, 0x2E);
I2C_stop(I2C1);
chThdSleepMilliseconds(5);
}
void init_humidity_temp(void)
{
I2C_start(I2C1,HUMIDITY_TEMP_ADDR << 1, I2C_Direction_Transmitter);
I2C_stop(I2C1);
}
uint16_t get_humidity_data(void)
{
init_humidity_temp();
printf("HUMIDITY_TEMP: Initialized humidity\r\n");
uint16_t data = 0;
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Receiver);
data = I2C_read_ack(I2C1) & 0x3F;
printf("HUMIDITY_TEMP: Got humidity high byte\r\n");
data = data << 8;
data |= I2C_read_nack(I2C1);
printf("HUMIDITY_TEMP: Got humidity low byte\r\n");
I2C_stop(I2C1);
return data;
}
uint16_t get_temperature_data(void)
{
init_humidity_temp();
printf("HUMIDITY_TEMP: Initialized temperature\r\n");
uint16_t data = 0;
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Receiver);
I2C_read_ack(I2C1);
printf("HUMIDITY_TEMP: Discarded first humidity byte\r\n");
I2C_read_ack(I2C1);
printf("HUMIDITY_TEMP: Discarded second humidity byte\r\n");
data = I2C_read_ack(I2C1);
printf("HUMIDITY_TEMP: Got temperature high byte\r\n");
data = data << 8;
data |= I2C_read_nack(I2C1) & 0xFC ;
printf("HUMIDITY_TEMP: Got temperature low byte\r\n");
data = data >> 2;
I2C_stop(I2C1);
return data;
}
char* light_value(uint32_t light)
{
char *value = chHeapAlloc(NULL,10 + 1);
sprintf(value,"%d",(unsigned int)light);
return value;
}
char* distance_value(uint16_t distance)
{
char *value = chHeapAlloc(NULL,5 + 1);
sprintf(value,"%d",distance);
return value;
}
char* pressure_value(uint16_t pressure, uint16_t temperature)
{
char *value = chHeapAlloc(NULL,11 + 1);
sprintf(value,"%d,%d",pressure,temperature);
return value;
}
char* temp_humidity_value(uint16_t temp, uint16_t humidity)
{
char *value = chHeapAlloc(NULL,11 + 1);
sprintf(value,"%d,%d",temp,humidity);
return value;
}
void init_sound(void)
{
//DARLE CHICHA AL INTERRUPTOR I2C
adc_sound_init();
}
uint32_t get_sound_data(void)
{
init_sound();
return adc_sound_process();
}
char* sound_value(uint32_t sound)
{
char *value = chHeapAlloc(NULL,11 + 1);
sprintf(value,"%4u",(unsigned int)sound);
return value;
}
void collectData(char* valueSensors[], uint8_t* sensors){
uint8_t i;
for(i=0;i<strlen((char*)sensors);i++)
{
if(valueSensors[i]!=NULL)
{
printf("Freeing memory from previous entry\r\n");
chHeapFree(valueSensors[i]);
}
if(sensors[i]==LIGHT_ADDR)
{
printf("Fetching data from light sensor\r\n");
valueSensors[i]=light_value(get_light_data());
continue;
}
else if(sensors[i]==DISTANCE_ADDR)
{
printf("Fetching data from distance sensor\r\n");
valueSensors[i]=distance_value(get_distance_data());
continue;
}
else if(sensors[i]==SOUND_ADDR)
{
printf("Fetching data from sound sensor\r\n");
valueSensors[i]=sound_value(get_sound_data());
continue;
}
else if(sensors[i]==PRESSURE_ADDR)
{
printf("Fetching data from pressure sensor\r\n");
valueSensors[i]=pressure_value(get_pressure_data(), get_pressure_temperature_data());
continue;
}
else if(sensors[i]==HUMIDITY_TEMP_ADDR)
{
printf("Fetching data from humidity and temperature sensor\r\n");
valueSensors[i]=temp_humidity_value(get_temperature_data(),get_humidity_data());
continue;
}
}
}
void init_battery(void)
{
adc_batt_init();
}
uint32_t get_battery_data(void)
{
init_battery();
return adc_batt_process();
}
char* battery_value(uint32_t battery)
{
char *value = chHeapAlloc(NULL,11 + 1);
sprintf(value,"%4u",(unsigned int)battery);
return value;
}
//light_value(get_light_data());
//temp_humidity_value(get_temperature_data(),get_humidity_data());
//pressure_value(get_pressure_data(), get_pressure_temperature_data());