-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClimateCard.cpp
468 lines (435 loc) · 12.4 KB
/
ClimateCard.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
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
/**
* @file ClimateCard.cpp
* @brief Implementation file for the ClimateCard class.
*
* This file contains the implementation of the ClimateCard class, which represents a climate control card.
* The ClimateCard class provides methods for controlling an air conditioner, reading temperature and humidity
* from sensors, and saving and loading the state to/from FRAM memory.
*/
#include <ClimateCard.hpp>
/**
* @brief Construct a new ClimateCard object.
*
* @note RMT channel must be universally unique, this means that you can't use the same channel for multiple cards.
*
* @param ir_pin The GPIO pin number of the IR transmitter.
* @param ac The AirConditioner object that represents the air conditioner.
* @param sensor_type The type of the sensor connected to the card.
* @param sensor_pin The GPIO pin number of the sensor.
* @param channel The RMT channel to use for IR transmission.
*/
ClimateCard::ClimateCard(uint8_t ir_pin, AirConditioner ac, uint8_t sensor_type, uint8_t sensor_pin, rmt_channel_t channel) : ir_blaster(ir_pin, channel)
{
this->ir_pin = ir_pin;
this->ac = ac;
this->sensor_type = sensor_type;
this->sensor_pin = sensor_pin;
// Initialize Pointers
this->dht = nullptr;
this->ds18b20 = nullptr;
this->fram = nullptr;
// Initialize Variables
this->fram_address = 0;
this->fram_auto_save = false;
this->state.ac_temperature = 0;
this->state.ac_mode = 0;
this->state.ac_fan_speed = 0;
this->humidity = 0;
this->room_temperature = 0;
// Initialize state
this->state.ac_temperature = 25;
this->state.ac_mode = 0;
this->state.ac_fan_speed = 0;
}
/**
* @brief Construct a new ClimateCard object.
*
* @param ir_pin The GPIO pin number of the IR transmitter.
* @param ac The AirConditioner object that represents the air conditioner.
* @param channel The RMT channel to use for IR transmission.
*
* @note RMT channel must be universally unique, this means that you can't use the same channel for multiple cards.
*
* @note This constructor can be used when no sensor is connected to the card.
*/
ClimateCard::ClimateCard(uint8_t ir_pin, AirConditioner ac, rmt_channel_t channel) : ClimateCard(ir_pin, ac, AC_SENSOR_TYPE_NONE, 0, channel)
{
}
/**
* @brief The destructor of the ClimateCard class.
*/
ClimateCard::~ClimateCard()
{
delete dht;
delete ds18b20;
}
/**
* @brief Initialize the ClimateCard object.
*
* @return true if initialization was successful.
* @return false if initialization failed.
*/
bool ClimateCard::begin()
{
switch (sensor_type)
{
case AC_SENSOR_TYPE_DHT22:
dht = new DHTNEW(sensor_pin);
break;
case AC_SENSOR_TYPE_DS18B20:
OneWire oneWire(sensor_pin);
ds18b20 = new DS18B20(&oneWire);
break;
}
updateAirConditioner();
return true;
}
/**
* @brief Loop function of the ClimateCard class.
*
* @note When this card is installed in an ESPMega, this function is called automatically by the ESPMega class.
*/
void ClimateCard::loop()
{
static uint32_t last_sensor_update = 0;
if (millis() - last_sensor_update >= AC_SENSOR_READ_INTERVAL)
{
last_sensor_update = millis();
updateSensor();
}
}
/**
* @brief bind FRAM memory to the ClimateCard object at the specified address.
*
* @note This function must be called before calling loadStateFromFRAM() or saveStateToFRAM().
* @note This card takes up 3 bytes of FRAM memory.
*
* @param fram The FRAM object.
* @param fram_address The starting address of the card in FRAM memory.
*/
void ClimateCard::bindFRAM(FRAM *fram, uint16_t fram_address)
{
this->fram = fram;
this->fram_address = fram_address;
}
/**
* @brief Set whether the state should be automatically saved to FRAM memory.
*
* @note This function has no effect if bindFRAM() has not been called.
* @param autoSave Whether the state should be automatically saved to FRAM memory.
*/
void ClimateCard::setFRAMAutoSave(bool autoSave)
{
this->fram_auto_save = autoSave;
}
/**
* @brief Save the state to FRAM memory.
* @note This function has no effect if bindFRAM() has not been called.
*/
void ClimateCard::saveStateToFRAM()
{
if (fram == nullptr)
return;
fram->write8(fram_address, state.ac_temperature);
fram->write8(fram_address + 1, state.ac_mode);
fram->write8(fram_address + 2, state.ac_fan_speed);
}
/**
* @brief Load the state from FRAM memory.
*
* @note This function has no effect if bindFRAM() has not been called.
*/
void ClimateCard::loadStateFromFRAM()
{
if (fram == nullptr)
return;
// Retrieve state from FRAM
state.ac_temperature = fram->read8(fram_address);
state.ac_mode = fram->read8(fram_address + 1);
state.ac_fan_speed = fram->read8(fram_address + 2);
if (state.ac_temperature > ac.max_temperature)
state.ac_temperature = ac.max_temperature;
else if (state.ac_temperature < ac.min_temperature)
state.ac_temperature = ac.min_temperature;
// If mode is out of range, set to 0
if (state.ac_mode > ac.modes)
state.ac_mode = 0;
// If fan speed is out of range, set to 0
if (state.ac_fan_speed > ac.fan_speeds)
state.ac_fan_speed = 0;
updateAirConditioner();
for (const auto &callback : callbacks)
{
callback.second(this->state.ac_mode, this->state.ac_fan_speed, this->state.ac_temperature);
}
}
/**
* @brief Set the temperature of the air conditioner.
*
* @param temperature The temperature to set.
* @note If the temperature is out of range, it will be set to its respective maximum or minimum.
*/
void ClimateCard::setTemperature(uint8_t temperature)
{
// If temperature is out of range, set to its respective maximum or minimum
if (temperature > ac.max_temperature)
temperature = ac.max_temperature;
else if (temperature < ac.min_temperature)
temperature = ac.min_temperature;
this->state.ac_temperature = temperature;
updateAirConditioner();
if (fram_auto_save)
saveStateToFRAM();
}
/**
* @brief Set the mode of the air conditioner.
*
* @note If the mode is out of range, it will be set to 0.
* @param mode The mode to set.
*/
void ClimateCard::setMode(uint8_t mode)
{
if (mode > ac.modes)
mode = 0;
this->state.ac_mode = mode;
updateAirConditioner();
if (fram_auto_save)
saveStateToFRAM();
}
/**
* @brief Get the name of the current mode.
* @return The name of the current mode.
*/
char *ClimateCard::getModeName()
{
return (char *)ac.mode_names[state.ac_mode];
}
/**
* @brief Get the name of the current fan speed.
*
* @return The name of the current fan speed.
*/
char *ClimateCard::getFanSpeedName()
{
return (char *)ac.fan_speed_names[state.ac_fan_speed];
}
/**
* @brief Set the fan speed of the air conditioner.
*
* @note If the fan speed is out of range, it will be set to 0.
* @param fan_speed The fan speed to set.
*/
void ClimateCard::setFanSpeed(uint8_t fan_speed)
{
if (fan_speed > ac.fan_speeds)
fan_speed = 0;
this->state.ac_fan_speed = fan_speed;
updateAirConditioner();
if (fram_auto_save)
saveStateToFRAM();
}
/**
* @brief Set fan speed by name.
*
* @param fan_speed_name The name of the fan speed to set.
* @note If the fan speed is not found, the function will not do anything.
*/
void ClimateCard::setFanSpeedByName(const char *fan_speed_name)
{
for (uint8_t i = 0; i < ac.fan_speeds; i++)
{
if (strcmp(fan_speed_name, ac.fan_speed_names[i]) == 0)
{
setFanSpeed(i);
return;
}
}
}
/**
* @brief Set mode by name.
*
* @param mode_name The name of the mode to set.
* @note If the mode is not found, the function will not do anything.
*/
void ClimateCard::setModeByName(const char *mode_name)
{
for (uint8_t i = 0; i < ac.modes; i++)
{
if (strcmp(mode_name, ac.mode_names[i]) == 0)
{
setMode(i);
return;
}
}
}
/**
* @brief Register a callback function that will be called when the state of the air conditioner changes.
*
* @param callback The callback function to register.
*
* @return uint8_t The handler of the callback function.
*/
uint8_t ClimateCard::registerChangeCallback(std::function<void(uint8_t, uint8_t, uint8_t)> callback)
{
callbacks[callbacks_handler_count] = callback;
return callbacks_handler_count++;
}
/**
* @brief Get the type of the card.
*
* @return The handler of the callback function.
*/
uint8_t ClimateCard::getType()
{
return CARD_TYPE_CLIMATE;
}
/**
* @brief update environmental sensor data.
*
* @note This function is called automatically by the loop() function.
* @note This function has no effect if no sensor is connected to the card.
* @note This function also calls the sensor callbacks.
*/
void ClimateCard::updateSensor()
{
if (sensor_type == AC_SENSOR_TYPE_NONE)
return;
// Read sensor data and update variables
switch (sensor_type)
{
case AC_SENSOR_TYPE_DHT22:
if (millis() - dht->lastRead() < AC_SENSOR_READ_INTERVAL)
return;
dht->read();
room_temperature = dht->getTemperature();
humidity = dht->getHumidity();
break;
case AC_SENSOR_TYPE_DS18B20:
ds18b20->requestTemperatures();
uint32_t start = millis();
while (!ds18b20->isConversionComplete())
{
if (millis() - start >= AC_SENSOR_READ_TIMEOUT)
{
return;
}
}
room_temperature = ds18b20->getTempC();
break;
}
for (const auto &callback : sensor_callbacks)
{
callback.second(room_temperature, humidity);
}
}
/**
* @brief Update the air conditioner state to match the state of the card.
*
* @warning This function is not working yet.
*/
void ClimateCard::updateAirConditioner()
{
const uint16_t* ir_code_ptr = nullptr;
size_t itemCount = (*(this->ac.getInfraredCode))(this->state.ac_mode, this->state.ac_fan_speed, this->state.ac_temperature-this->ac.min_temperature, &ir_code_ptr);
if (ir_code_ptr == nullptr)
return;
ir_blaster.send(ir_code_ptr, itemCount);
// Publish state
for (const auto &callback : callbacks)
{
callback.second(this->state.ac_mode, this->state.ac_fan_speed, this->state.ac_temperature);
}
}
/**
* @brief Get the type of the sensor connected to the card.
*
* @return The type of the sensor connected to the card.
*/
uint8_t ClimateCard::getSensorType()
{
return sensor_type;
}
/**
* @brief Get the room temperature in degrees Celsius.
*
* @return The room temperature.
*/
float ClimateCard::getRoomTemperature()
{
return room_temperature;
}
/**
* @brief Get the humidity in percent.
*
* @return The humidity.
*/
float ClimateCard::getHumidity()
{
return humidity;
}
/**
* @brief Get the temperature of the air conditioner.
*
* @return The temperature of the air conditioner.
*/
uint8_t ClimateCard::getTemperature()
{
return state.ac_temperature;
}
/**
* @brief Get the mode of the air conditioner.
*
* @return The mode of the air conditioner.
*/
uint8_t ClimateCard::getMode()
{
return state.ac_mode;
}
/**
* @brief Get the fan speed of the air conditioner.
*
* @return The fan speed of the air conditioner.
*/
uint8_t ClimateCard::getFanSpeed()
{
return state.ac_fan_speed;
}
/**
* @brief Register a callback function that will be called when the sensor data changes.
*
* @param callback The callback function to register.
*
* @return The handler of the callback function
*/
uint8_t ClimateCard::registerSensorCallback(std::function<void(float, float)> callback)
{
sensor_callbacks[sensor_callbacks_handler_count] = callback;
return sensor_callbacks_handler_count++;
}
/**
* @brief Unregister a callback function.
*
* @param handler The handler of the callback function to unregister.
*/
void ClimateCard::unregisterChangeCallback(uint8_t handler)
{
callbacks.erase(handler);
}
/**
* @brief Unregister a sensor callback function.
*
* @param handler The handler of the callback function to unregister.
*/
void ClimateCard::unregisterSensorCallback(uint8_t handler)
{
sensor_callbacks.erase(handler);
}
void ClimateCard::setState(uint8_t mode, uint8_t fan_speed, uint8_t temperature)
{
this->state.ac_mode = mode;
this->state.ac_fan_speed = fan_speed;
this->state.ac_temperature = temperature;
updateAirConditioner();
if (fram_auto_save)
saveStateToFRAM();
}