174 lines
3.8 KiB
C++
174 lines
3.8 KiB
C++
|
#include <ClimateCard.hpp>
|
||
|
|
||
|
ClimateCard::ClimateCard(uint8_t ir_pin) : irsender(ir_pin)
|
||
|
{
|
||
|
this->ir_pin = ir_pin;
|
||
|
irsender.begin(ir_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;
|
||
|
}
|
||
|
|
||
|
ClimateCard::~ClimateCard()
|
||
|
{
|
||
|
delete dht;
|
||
|
delete ds18b20;
|
||
|
}
|
||
|
|
||
|
bool ClimateCard::begin(AirConditioner ac, uint8_t sensor_type, uint8_t sensor_pin)
|
||
|
{
|
||
|
this->ac = ac;
|
||
|
this->sensor_type = sensor_type;
|
||
|
this->sensor_pin = sensor_pin;
|
||
|
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();
|
||
|
}
|
||
|
|
||
|
bool ClimateCard::begin(AirConditioner ac)
|
||
|
{
|
||
|
this->begin(ac, AC_SENSOR_TYPE_NONE, 0);
|
||
|
}
|
||
|
|
||
|
void ClimateCard::loop()
|
||
|
{
|
||
|
static uint32_t last_sensor_update = 0;
|
||
|
if (millis() - last_sensor_update >= AC_SENSOR_READ_INTERVAL)
|
||
|
{
|
||
|
last_sensor_update = millis();
|
||
|
updateSensor();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void ClimateCard::bindFRAM(FRAM *fram, uint16_t fram_address)
|
||
|
{
|
||
|
this->fram = fram;
|
||
|
this->fram_address = fram_address;
|
||
|
}
|
||
|
|
||
|
void ClimateCard::setFRAMAutoSave(bool autoSave)
|
||
|
{
|
||
|
this->fram_auto_save = autoSave;
|
||
|
}
|
||
|
|
||
|
void ClimateCard::saveStateToFRAM()
|
||
|
{
|
||
|
fram->writeObject(fram_address, this->state);
|
||
|
}
|
||
|
|
||
|
void ClimateCard::loadStateFromFRAM()
|
||
|
{
|
||
|
fram->readObject(fram_address, this->state);
|
||
|
}
|
||
|
|
||
|
void ClimateCard::setTemperature(uint8_t temperature)
|
||
|
{
|
||
|
this->state.ac_temperature = temperature;
|
||
|
updateAirConditioner();
|
||
|
}
|
||
|
|
||
|
void ClimateCard::setMode(uint8_t mode)
|
||
|
{
|
||
|
this->state.ac_mode = mode;
|
||
|
updateAirConditioner();
|
||
|
}
|
||
|
|
||
|
void ClimateCard::setFanSpeed(uint8_t fan_speed)
|
||
|
{
|
||
|
this->state.ac_fan_speed = fan_speed;
|
||
|
updateAirConditioner();
|
||
|
}
|
||
|
|
||
|
void ClimateCard::registerCallback(std::function<void(uint8_t, uint8_t, uint8_t)> callback)
|
||
|
{
|
||
|
callbacks.push_back(callback);
|
||
|
}
|
||
|
|
||
|
uint8_t ClimateCard::getType()
|
||
|
{
|
||
|
return CARD_TYPE_CLIMATE;
|
||
|
}
|
||
|
|
||
|
void ClimateCard::updateSensor()
|
||
|
{
|
||
|
// 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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void ClimateCard::updateAirConditioner()
|
||
|
{
|
||
|
irsender.sendRaw(ac.infraredCodes[this->state.ac_mode][this->state.ac_fan_speed][this->state.ac_temperature],
|
||
|
sizeof(ac.infraredCodes[this->state.ac_mode][this->state.ac_fan_speed][this->state.ac_temperature]) / sizeof(uint16_t),
|
||
|
NEC_KHZ);
|
||
|
for (uint8_t i = 0; i < callbacks.size(); i++)
|
||
|
{
|
||
|
callbacks[i](this->state.ac_temperature, this->state.ac_mode, this->state.ac_fan_speed);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
float ClimateCard::getRoomTemperature()
|
||
|
{
|
||
|
return room_temperature;
|
||
|
}
|
||
|
|
||
|
float ClimateCard::getHumidity()
|
||
|
{
|
||
|
return humidity;
|
||
|
}
|
||
|
|
||
|
uint8_t ClimateCard::getTemperature()
|
||
|
{
|
||
|
return state.ac_temperature;
|
||
|
}
|
||
|
|
||
|
uint8_t ClimateCard::getMode()
|
||
|
{
|
||
|
return state.ac_mode;
|
||
|
}
|
||
|
|
||
|
uint8_t ClimateCard::getFanSpeed()
|
||
|
{
|
||
|
return state.ac_fan_speed;
|
||
|
}
|