-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAtlasStamp.cpp
402 lines (357 loc) · 10.7 KB
/
AtlasStamp.cpp
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
#include "AtlasStamp.h"
//TODO: Parametro unit_len deprecated? se peude cambiar en el malloc por strlen()?
AtlasStamp::AtlasStamp(uint8_t address, char* unit, uint8_t unit_len, float min_value, float max_value, uint8_t max_num_fields_in_response) :
_address(address),
_max_response_field_count(max_num_fields_in_response),
_response_field_count(max_num_fields_in_response),
_last_result{ (float*)malloc(sizeof(float) * _response_field_count) },
_is_init(false),
_is_busy(false),
_async_comand_ready_by(0),
_min_value(min_value),
_max_value(max_value),
_unit{ (char*)malloc(sizeof(char) * (unit_len+1)) },
stamp_version{ '0','.','0','\0' },
is_awake(true)
{
//Inicialize the sensor unit
strcpy(_unit, unit);
_clean_buffer();
}
//uint8_t AtlasStamp::raw_command(char* cmd, unsigned long timeout)
//{
// return _raw_command(cmd, timeout);
//}
void AtlasStamp::_resize_response_count(uint8_t count)
{
if (count == 0) { count = 1; }
else if (count > _max_response_field_count) { count = _max_response_field_count; }
if (count == _response_field_count)
{
#ifdef ATLAS_DEBUG
Serial.printf("AtlasStamp::_resize_response_count() cant reallocate, already have [%d] fields\n", _response_field_count);
#endif
return;
}
#ifdef ATLAS_DEBUG
Serial.printf("AtlasStamp::_resize_response_count() reallocated space for sensor readings from[%d] to[%d] floats\n", _response_field_count, count);
#endif
_response_field_count = count;
_last_result = (float*)realloc(_last_result, sizeof(float) * _response_field_count);
}
bool AtlasStamp::_command_async(char* cmd, unsigned long t)
{
if (!_is_init)
{
return false;
}
else if (_is_busy)
{
return false;
}
Wire.beginTransmission(_address);
Wire.write(cmd);
byte wireres = Wire.endTransmission(true);
if (wireres == I2C_RESPONSE_OK)
{
//The command was sent ok, so no more comunication can be done now
_is_busy = true;
//At this point we where supposed to have an answer from the sensor
//used to calculate if the result should be available
_async_comand_ready_by = millis() + t;
//Reset the bufer and read counter
_clean_buffer();
#ifdef ATLAS_DEBUG
Serial.printf("AtlasStamp::_command_async() [END] T[%d] BUSY[%d] READY_BY[%d] SUCCSED[%d] COMMAND[%s]\n", millis(), _is_busy, _async_comand_ready_by, wireres, cmd);
#endif
//Si hemos procesado correctamente un comando y el cacharro estaba dormido se habra despertado,
//así que fijamos el flag, ojo, cuando lleguemos aqui despues de enviar el comando Sleep el flag
//se pondra a true, estando realmente dormido el modulo, pero no es problema, ya que al salir y
//volver a la funcion sleep() se pone a false
is_awake = true;
return true;
}
else
{
return false;
}
}
//Obtiene el resultado de un comando asincrono
uint8_t AtlasStamp::_command_result()
{
uint8_t tmp_char = 0;
uint8_t _i2c_response_code = 254;
if (!_is_init)
{
return ATLAS_ERROR_RESPONSE;
}
else if (!_is_busy)
{
return ATLAS_NODATA_RESPONSE;
}
else if (!available())
{
return ATLAS_BUSY_RESPONSE;
}
/*_clean_buffer();*/ //Limpiamos el buffer
//Nos aseguramos de que esta listo antes de seguir :)
while (_i2c_response_code == ATLAS_BUSY_RESPONSE) {
Wire.requestFrom(_address, (uint8_t)MAX_DATA_TO_READ);
_i2c_response_code = Wire.read();
if (_i2c_response_code == ATLAS_BUSY_RESPONSE)
{
//Esto es necesario si no queremos que se cuelgue el i2c
//Tenemos que limpiar los datos del buffer antes de volver a usar el bus
_clean_wire();
}
delay(CONNECTION_DELAY_MS);
}
//OK, ha procesado el comando y la respuesta es correcta :)
//vamos a recuperar los datos
if (_i2c_response_code == ATLAS_SUCCESS_RESPONSE)
{
//Mientras tengamos datos cargamos el buffer
while (Wire.available())
{
//Obtenemos el primer byte
tmp_char = Wire.read();
//Si el caracter es NULL es el final de la transmision
if (tmp_char == NULL_CHARACTER)
{
//Añadimos el final de carro al buffer
_response_buffer[_i2c_bytes_received] = '\0';
//_i2c_bytes_received++;
//Terminamos
break;
}
else
{
//Guardamos ese jugoso caracter en nuestro array
_response_buffer[_i2c_bytes_received] = tmp_char; //load this byte into our array.
_i2c_bytes_received++;
//TODO: Controlar aquí el buffer overflow!
}
}
}
_clean_wire();
//Volvemos a poner los flags en su sitio
_is_busy = false;
_async_comand_ready_by = 0;
#ifdef ATLAS_DEBUG
Serial.printf("AtlasStamp::_command_result() [END] T[%d] BUSY[%d] CODE[%d] RESPONSE[%s] TIMEOUT[%d]\n", millis(), _is_busy, _i2c_response_code, _response_buffer, _async_comand_ready_by);
#endif
//Devolvemos el codigo de respuesta :)
//Si es 1 tendremos _response_buffer cargado con la respuesta al comando
return _i2c_response_code;
}
uint8_t AtlasStamp::_command(char* cmd, unsigned long t)
{
#ifdef ATLAS_DEBUG
Serial.printf("AtlasStamp::_command() [START] CMD[%s] T:[%d]\n", cmd, millis());
#endif
if (_command_async(cmd, t))
{
uint8_t r = 0;
delay(static_cast<unsigned long>(t/2.0f));
while (ATLAS_BUSY_RESPONSE == (r = _command_result())) { delay(50); }
if (ATLAS_SUCCESS_RESPONSE == r)
{
return true;
}
}
return false;
}
uint8_t AtlasStamp::read_ascii(char* buffer)
{
if (ATLAS_SUCCESS_RESPONSE == _command(ATLAS_READ_COMAND, 1000))
{
strcpy(buffer, _response_buffer);
return _i2c_bytes_received;
}
return 0;
}
uint8_t AtlasStamp::result_ascii_async(char* buffer)
{
if (ATLAS_SUCCESS_RESPONSE == _command_result())
{
strcpy(buffer, _response_buffer);
return _i2c_bytes_received;
}
return 0;
}
float* const AtlasStamp::read()
{
if (ATLAS_SUCCESS_RESPONSE == _command(ATLAS_READ_COMAND, 1000))
{
return _parse_sensor_read();
}
return nullptr;
}
bool const AtlasStamp::read_async()
{
return _command_async(ATLAS_READ_COMAND, 1000);
}
float* const AtlasStamp::result_async()
{
if (ATLAS_SUCCESS_RESPONSE == _command_result())
{
return _parse_sensor_read();
}
return nullptr;
}
float* const AtlasStamp::_parse_sensor_read(void)
{
//Cuando llamamos a esta fucnion deberiamos tener en el _response_buffer una cadena
//representando la medida del sensor, esta dependera, pudiendo ser, un float (58.7)
// o una lista de floats separada por comas (12.5,22.5,1.0,00.2)
#ifdef ATLAS_DEBUG
Serial.printf("AtlasStamp::_parse_sensor_read() T[%lu] sensor fields [%d] response buffer [%s]\n", millis(), _response_field_count, _response_buffer);
#endif
if (_response_field_count > 1)
{
char *current_token;
current_token = strtok(_response_buffer,",");
for (int i = 0; i < _response_field_count; i++)
{
if (current_token != NULL)
{
*(_last_result+i) = atof(current_token);
//Get next value if previous was not null
current_token = strtok(NULL, ",");
}
else
{
//The sensor is suposed to have multiple values, but
//thats not true so set the value to default error
*(_last_result + i) = -2048.0f;
}
#ifdef ATLAS_DEBUG
Serial.printf("Field[%d] value[%4.2f] current_token[%s]\n", i+1, *(_last_result + i), current_token);
#endif
}
}
else
{
if (strcmp("No output", _response_buffer) == 0)
{
*_last_result = -2048.0f;
}
else
{
//Is a simple sensor only a float string in the buffer, just set it.
*_last_result = atof(_response_buffer);
}
#ifdef ATLAS_DEBUG
Serial.printf("Field[%d] value[%4.2f]\n", 1, *_last_result);
#endif
}
return _last_result;
}
bool AtlasStamp::_stamp_connected()
{
bool r = false;
//If we are already inicialized return true
if (_is_init) { return true; }
//Temporary set the flag to allow the initial contact
//otherways the _command() function wont return ATLAS_SUCCESS_RESPONSE
_is_init = true;
for (int i = 0; i < MAX_CONNECTION_TRIES; i++)
{
//Try to get the status of the device
if (ATLAS_SUCCESS_RESPONSE == _command(ATLAS_INFO_COMAND,300))
{
//Is an EZO module
if (_response_buffer[0] == '?' && _response_buffer[1] == 'I')
{
//comeback with good news
r = true;
break;
}
}
delay(CONNECTION_DELAY_MS);
}
//Clear the flag, is the child class the one that should set it
//when more info about the module is parsed
_is_init = false;
//Return r, it contains true if we could stablish communication
//with an EZO module in the given address
return r;
}
void AtlasStamp::purge()
{
//TODO: deberia esperar a que termine lo que este haciendo si
//hay trabajo a medio?
_is_busy = false;
_async_comand_ready_by = 0;
}
float AtlasStamp::get_vcc(void)
{
//float returnVal = -2048.0f;
if (ATLAS_SUCCESS_RESPONSE == _command("Status", 200))
{
//?|S|T|A|T|U|S|,|P|,|5|.|0|6|4|NULL
//TODO: Deberiamos proteger estas lecturas con punteros con algo del tipo? if (_bytes_in_buffer() >= 10) {...}
//En este caso el atof mirara los bytes que considere y si el resultado no es correcto devolvera 0.0f asi que por ahí no parece
//haber problemas, ademas no deberia pasar ya que si command() devuelve exito, deberia estar cargado el buffer...
if (_bytes_in_buffer() >= 13)
{
char* res_buff = (char*)(_get_response_buffer() + 10);
return atof(res_buff);
}
}
return -2048.0f;
}
void AtlasStamp::info(Stream& output)
{
output.printf("ADDRESS:[0x%02x] VERSION:[%s] READY:[%d] BUSY:[%d] MIN:[%4.3f] MAX:[%4.3f] UNIT:[%s] VCC:[%4.4f]", _address, stamp_version, _is_init, _is_busy, _min_value, _max_value, _unit, get_vcc());
}
bool AtlasStamp::led()
{
if (ATLAS_SUCCESS_RESPONSE == _command("L,?", 150))
{
//Una vez qui tenemos en el bufer algo asi (las | separan, no cuentan como caracter en el buffer)
// ? | L | , | 1 | null
// ? | L | , | 0 | null
//Comporbamos que la posicion 3 del buffer sea un 0 o un 1
if (_read_buffer(3) == '1')
{
return true;
}
}
return false;
}
bool AtlasStamp::led(bool state)
{
//El comando de fijar el led es:
// L,1 para activar
// ó
// L,0 para desactivar
sprintf(_command_buffer,"L,%d", state);
//Send the command and wait for confirmation
if (ATLAS_SUCCESS_RESPONSE == _command(_command_buffer, 300))
{
return true;
}
return false;
}
bool const AtlasStamp::sleep(void)
{
//Send the command and wait for confirmation
if (ATLAS_SUCCESS_RESPONSE == _command("Sleep", 300))
{
is_awake = false;
return true;
}
return false;
}
//Not necesary because if the module is sleeping will wake up with any command,
//but I use it to keep the API clean (sleep()/sleeping()/wakeup())
bool const AtlasStamp::wakeup(void)
{
//If already awake return true
if (is_awake)
{
return true;
}
return led();
}