-
Notifications
You must be signed in to change notification settings - Fork 0
/
deviceConfig.cpp
370 lines (319 loc) · 8.74 KB
/
deviceConfig.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
#include "deviceConfig.hpp"
deviceConfig::deviceConfig(void)
{
_DGAenable = false;
//At the constructor we get the credentials from NV memory
_refreshCredentials();
//Now we get mqtt Preferences
_refreshMqtt();
_refreshHttp();
//We refresh other general preferences
_refreshGeneralConfig();
//We refresh APN configurations
_refreshApnConfig();
}
void deviceConfig::_refreshCredentials(void)
{
_preferences.begin("credentials",false);
_user = _preferences.getString("user",""); //default value is ""
_password = _preferences.getString("password",""); //default value is ""
_preferences.end();
}
void deviceConfig::_refreshApnConfig(void)
{
_preferences.begin("simconfig",false);
_apn = _preferences.getString("apn","data.mono");
_apn_user = _preferences.getString("user","");
_apn_password = _preferences.getString("password","");
_apnAuthMethod = _preferences.getShort("auth",1);
_preferences.end();
}
const char * deviceConfig::getAPN(void)
{
return _apn.c_str();
}
const char *deviceConfig::getApnPassword(void)
{
return _apn_password.c_str();
}
const char *deviceConfig::getApnUser(void)
{
return _apn_user.c_str();
}
uint8_t deviceConfig::getApnAuth(void)
{
return _apnAuthMethod;
}
void deviceConfig::configAPN(String APN,String user, String Password, uint8_t ApnAuth)
{
ESP_LOGI("configAPN","New configuration. APN: %s",APN.c_str());
_preferences.begin("simconfig",false);
_preferences.putString("apn",APN);
_preferences.putString("user",user);
_preferences.putString("password",Password);
_preferences.putShort("auth",ApnAuth);
_preferences.end();
_refreshApnConfig();
}
void deviceConfig::configHttp(String httpUrl)
{
_preferences.begin("http",false);
_preferences.putString("url",httpUrl);
_preferences.end();
_refreshHttp();
}
void deviceConfig::configMqtt(String mqttUrl,String mqttTopic, uint16_t mqttPort)
{
_preferences.begin("mqtt",false);
_preferences.putUShort("port",mqttPort);
_preferences.putString("topic",mqttTopic);
_preferences.putString("url",mqttUrl);
_preferences.end();
_refreshMqtt();
}
const char *deviceConfig::getUrl(void)
{
if (_postMethod == POST_METHOD::HTTP_POST)
{
return _httpUrl.c_str();
}
if (_postMethod == POST_METHOD::MQTT)
{
return _mqttUrl.c_str();
}
else
{
return "";
}
}
uint16_t deviceConfig::getPort(void)
{
return _mqttPort;
}
const char * deviceConfig::getTopic(void)
{
return _mqttTopic.c_str();
}
uint16_t deviceConfig::getSamplePeriod(void)
{
return _samplePeriod;
}
void deviceConfig::_refreshMqtt(void)
{
_preferences.begin("mqtt",false);
_mqttPort = _preferences.getUShort("port",1883);
_mqttTopic = _preferences.getString("topic","");
_mqttUrl = _preferences.getString("url","");
_preferences.end();
}
void deviceConfig::_refreshHttp(void)
{
_preferences.begin("http",false);
_httpUrl = _preferences.getString("url","");
_preferences.end();
}
void deviceConfig::setPostMethod(POST_METHOD method)
{
_preferences.begin("generalConfig",false);
_preferences.putUChar("postMethod",(uint8_t)(method));
_preferences.end();
_refreshGeneralConfig();
}
POST_METHOD deviceConfig::getCurrentMethod(void)
{
return _postMethod;
}
void deviceConfig::_refreshGeneralConfig(void)
{
_preferences.begin("generalConfig",false);
_samplePeriod = _preferences.getUShort("samplePeriod",60);
_postMethod = (POST_METHOD)_preferences.getUChar("postMethod",(uint8_t)(POST_METHOD::NONE));
_customName = _preferences.getString("name","pulse board");
_preferences.end();
}
void deviceConfig::setSamplePeriod(uint16_t samplePeriod)
{
if (samplePeriod != 0)
{
_preferences.begin("generalConfig",false);
_preferences.putUShort("samplePeriod",samplePeriod);
_preferences.end();
}
_refreshGeneralConfig();
}
const char* deviceConfig::getCustomName(void)
{
return _customName.c_str();
}
void deviceConfig::setCustomName(String name)
{
if (name.length()>0)
{
_preferences.begin("generalConfig",false);
_preferences.putString("name",name);
_preferences.end(); //close preferences
_refreshGeneralConfig();
}
}
const char *deviceConfig::getUser(void)
{
return _user.c_str();
}
const char* deviceConfig::getPassword(void)
{
return _password.c_str();
}
void deviceConfig::setCredentials(String user, String password)
{
_preferences.begin("credentials",false); //open as read/write
_preferences.putString("user",user);
_preferences.putString("password",password);
_preferences.end(); //close preferences
_refreshCredentials();
}
void deviceConfig::setDGAconfig(String codigoObra, uint16_t sampleRate)
{
_dgaSampleRate = sampleRate;
_dgaCodigoObra = codigoObra;
_DGAenable = true;
}
uint16_t deviceConfig::getDGASampleRate(void)
{
return _dgaSampleRate;
}
const char *deviceConfig::getCodigoObraDGA(void)
{
return _dgaCodigoObra.c_str();
}
bool deviceConfig::getDGAenabled(void)
{
return _DGAenable;
}
/**********************************************************************/
/* */
/* Digital ports */
/* */
/**********************************************************************/
pulseIoPort::pulseIoPort(uint32_t portNumber, pulseIoType type,PCAL9535A::PCAL9535A<TwoWire> &gpio,SemaphoreHandle_t &i2cMutex )
{
_port = portNumber;
_gpio = &gpio;
_varDataType = type;
_i2cMutex = &i2cMutex;
if (_varDataType == pulseInput)
{
if (xSemaphoreTake(*_i2cMutex, (TickType_t ) 100) == pdTRUE)
{
//1 salida, 0 entrada
_gpio->pinMode(_port,0);
xSemaphoreGive(*_i2cMutex);
}
}
else
{
if (xSemaphoreTake(*_i2cMutex, (TickType_t ) 100) == pdTRUE)
{
//1 salida, 0 entrada
_gpio->pinMode(_port,1);
xSemaphoreGive(*_i2cMutex);
}
}
}
bool pulseIoPort::getPortValue(void)
{
bool readData;
if(_varDataType == pulseInput)
{
if (xSemaphoreTake(*_i2cMutex, (TickType_t ) 100) == pdTRUE)
{
readData = _gpio->digitalRead(_port);
xSemaphoreGive(*_i2cMutex);
}
}
return readData;
}
void pulseIoPort::setPortValue(bool inputData)
{
if (_varDataType == pulseOutput)
{
if (xSemaphoreTake(*_i2cMutex, (TickType_t ) 100) == pdTRUE)
{
_gpio->digitalWrite(_port,inputData);
xSemaphoreGive(*_i2cMutex);
}
}
return;
}
/**********************************************************************/
/* */
/* Analog ports */
/* */
/**********************************************************************/
pulseAiPort::pulseAiPort(const char *name, float gain, float offset,bool requirePower, uint8_t address,SemaphoreHandle_t &i2cMutex, TwoWire &port)
{
_gain = gain;
_offset = offset;
_requirePower = requirePower;
//To-Do: check for _name correct memory allocation;
if (name != NULL)
{
_name = (char*)malloc(strlen(name)*sizeof(char));
}
_address = address;
_i2cMutex = &i2cMutex;
_i2cPort = &port;
xTaskCreate(
this->startTaskImpl,
"aiTask",
2048,
this,
4,
&_samplerHandle
);
}
float pulseAiPort::getConvertedData(void)
{
return _rawData*_gain + _offset;
}
float pulseAiPort::getRawData(void)
{
return _rawData;
}
void pulseAiPort::startTaskImpl(void *_this)
{
static_cast<pulseAiPort*>(_this)->_sampleMethod();
}
float pulseAiPort::_readSensor(void)
{
uint16_t result;
if (xSemaphoreTake(*_i2cMutex, (TickType_t ) 20) == pdTRUE)
{
_i2cPort->requestFrom(_address,2U);
if (_i2cPort->available() == 2)
{
result = (_i2cPort->read() << 8) | (_i2cPort->read());
}
xSemaphoreGive(*_i2cMutex);
return (result*3300.0/4096/50/2.61);
}
else
{
return _rawData;
}
}
bool pulseAiPort::getRequirePower(void)
{
return _requirePower;
}
void pulseAiPort::_sampleMethod(void)
{
const TickType_t xFrequency = pdMS_TO_TICKS(AI_SAMPLE_PERIOD);
TickType_t xLastWakeTime;
xLastWakeTime = xTaskGetTickCount();
while(1)
{
_rawData = _readSensor();
//Periodic excecution
vTaskDelayUntil(&xLastWakeTime,xFrequency);
}
}