ac card implementation
This commit is contained in:
parent
f17b220a38
commit
1ee4209e3c
|
@ -49,7 +49,9 @@
|
|||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"cinttypes": "cpp",
|
||||
"typeinfo": "cpp"
|
||||
"typeinfo": "cpp",
|
||||
"bitset": "cpp",
|
||||
"regex": "cpp"
|
||||
},
|
||||
"cmake.configureOnOpen": true,
|
||||
"cmake.sourceDirectory": "D:/Git/ESPMegaPRO-v3-SDK/Template Project/.pio/libdeps/wt32-eth01/Adafruit BusIO",
|
||||
|
|
|
@ -0,0 +1,174 @@
|
|||
#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;
|
||||
}
|
|
@ -2,6 +2,9 @@
|
|||
#include <ExpansionCard.hpp>
|
||||
#include <IRremote.hpp>
|
||||
#include <FRAM.h>
|
||||
#include <OneWire.h>
|
||||
#include <DS18B20.h>
|
||||
#include <dhtnew.h>
|
||||
#include <vector>
|
||||
|
||||
#define CARD_TYPE_CLIMATE 0x03
|
||||
|
@ -10,41 +13,63 @@
|
|||
#define AC_SENSOR_TYPE_DHT22 0x01
|
||||
#define AC_SENSOR_TYPE_DS18B20 0x02
|
||||
|
||||
#define AC_SENSOR_READ_INTERVAL 5000
|
||||
#define AC_SENSOR_READ_TIMEOUT 250
|
||||
|
||||
struct ClimateCardData {
|
||||
uint8_t ac_temperature;
|
||||
uint8_t ac_mode;
|
||||
uint8_t ac_fan_speed;
|
||||
};
|
||||
|
||||
struct AirConditioner {
|
||||
uint8_t max_temperature;
|
||||
uint8_t min_temperature;
|
||||
uint8_t modes;
|
||||
char **mode_names;
|
||||
uint8_t fan_speeds;
|
||||
char **fan_speed_names;
|
||||
uint16_t ****infraredCodes;
|
||||
};
|
||||
// This requires 3 bytes of FRAM
|
||||
|
||||
class ClimateCard : public ExpansionCard {
|
||||
public:
|
||||
bool begin(uint16_t *infraredCodes, uint8_t sensor_type, uint8_t sensor_pin);
|
||||
bool begin(uint16_t *infraredCodes);
|
||||
ClimateCard(uint8_t ir_pin);
|
||||
~ClimateCard();
|
||||
bool begin(AirConditioner ac, uint8_t sensor_type, uint8_t sensor_pin);
|
||||
bool begin(AirConditioner ac);
|
||||
void loop();
|
||||
void bindFRAM(FRAM *fram, uint16_t fram_address);
|
||||
void setFRAMAutoSave(bool autoSave);
|
||||
void saveStateToFRAM();
|
||||
void loadStateFromFRAM();
|
||||
void setTemperature(uint8_t temperature);
|
||||
uint8_t getTemperature();
|
||||
void setMode(uint8_t mode);
|
||||
uint8_t getMode();
|
||||
void setFanSpeed(uint8_t fan_speed);
|
||||
uint8_t getFanSpeed();
|
||||
float getRoomTemperature();
|
||||
float getHumidity();
|
||||
void registerCallback(std::function<void(uint8_t, uint8_t, uint8_t)> callback);
|
||||
uint8_t getType();
|
||||
private:
|
||||
// Sensor objects
|
||||
// We use pointers here because we don't know which sensor will be used
|
||||
DHTNEW *dht;
|
||||
DS18B20 *ds18b20;
|
||||
// Callbacks
|
||||
std::vector<std::function<void(uint8_t, uint8_t, uint8_t)>> callbacks;
|
||||
// Update functions
|
||||
void updateSensor();
|
||||
void updateAirConditioner();
|
||||
void updateAirConditioner(uint8_t mode, uint8_t fan_speed, uint8_t temperature);
|
||||
// IR variables
|
||||
IRsend irsender;
|
||||
uint16_t *infraredCodes;
|
||||
uint8_t ir_pin;
|
||||
// Air conditioner variables
|
||||
int ac_temperature;
|
||||
int ac_mode;
|
||||
int ac_fan_speed;
|
||||
AirConditioner ac;
|
||||
ClimateCardData state;
|
||||
// Sensor variables
|
||||
uint8_t sensor_type;
|
||||
uint8_t sensor_pin;
|
||||
|
@ -53,4 +78,5 @@ class ClimateCard : public ExpansionCard {
|
|||
// FRAM variables
|
||||
FRAM *fram;
|
||||
uint8_t fram_address;
|
||||
bool fram_auto_save;
|
||||
};
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#include <ESPMegaPRO_OOP.hpp>
|
||||
|
||||
#define DISPLAY_FETCH_TIMEOUT 100 // ms
|
||||
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
#include <DS1307RTC.h>
|
||||
#include <time.h>
|
||||
#include <TimeStructure.hpp>
|
||||
#include <ESPMegaDisplay.hpp>
|
||||
#include <InternalDisplay.hpp>
|
||||
|
||||
#define FRAM_ADDRESS 0x56
|
||||
#define INPUT_BANK_A_ADDRESS 0x21
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#pragma once
|
||||
#include <ESPMegaDisplay.hpp>
|
||||
#include <TimeStructure.hpp>
|
||||
#include <ESPMegaIoT.hpp>
|
||||
|
|
|
@ -26,6 +26,9 @@ lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1
|
|||
knolleary/pubsubclient@^2.8.0
|
||||
seithan/Easy Nextion Library@^1.0.6
|
||||
z3t0/IRremote@^4.2.0
|
||||
esphome/ESPAsyncWebServer-esphome@^3.1.0
|
||||
bblanchon/ArduinoJson@^6.21.4
|
||||
robtillaart/DS18B20@^0.2.1
|
||||
robtillaart/DHTNEW@^0.4.18
|
||||
#esphome/ESPAsyncWebServer-esphome@^3.1.0
|
||||
monitor_speed = 115200
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
#include <ETH.h>
|
||||
|
||||
ESPMegaPRO espmega = ESPMegaPRO();
|
||||
InternalDisplay display = InternalDisplay(&Serial);
|
||||
|
||||
void input_change_callback(uint8_t pin, uint8_t value) {
|
||||
Serial.print("Input change callback: ");
|
||||
|
|
Loading…
Reference in New Issue