diff --git a/Template Project/.vscode/settings.json b/Template Project/.vscode/settings.json index eade1b3..29a63dd 100644 --- a/Template Project/.vscode/settings.json +++ b/Template Project/.vscode/settings.json @@ -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", diff --git a/Template Project/lib/ESPMegaPRO/ClimateCard.cpp b/Template Project/lib/ESPMegaPRO/ClimateCard.cpp new file mode 100644 index 0000000..9e2cada --- /dev/null +++ b/Template Project/lib/ESPMegaPRO/ClimateCard.cpp @@ -0,0 +1,174 @@ +#include + +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 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; +} \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ClimateCard.hpp b/Template Project/lib/ESPMegaPRO/ClimateCard.hpp index 8703d3e..5d2e286 100644 --- a/Template Project/lib/ESPMegaPRO/ClimateCard.hpp +++ b/Template Project/lib/ESPMegaPRO/ClimateCard.hpp @@ -2,6 +2,9 @@ #include #include #include +#include +#include +#include #include #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 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> 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; }; diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaDisplay.hpp b/Template Project/lib/ESPMegaPRO/ESPMegaDisplay.hpp index 2fad560..9c754ec 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaDisplay.hpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaDisplay.hpp @@ -1,6 +1,5 @@ #pragma once #include -#include #define DISPLAY_FETCH_TIMEOUT 100 // ms diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp index 584c4e2..99b0c7d 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp @@ -10,6 +10,8 @@ #include #include #include +#include +#include #define FRAM_ADDRESS 0x56 #define INPUT_BANK_A_ADDRESS 0x21 diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaWebServer.hpp b/Template Project/lib/ESPMegaPRO/ESPMegaWebServer.hpp.disabled similarity index 100% rename from Template Project/lib/ESPMegaPRO/ESPMegaWebServer.hpp rename to Template Project/lib/ESPMegaPRO/ESPMegaWebServer.hpp.disabled diff --git a/Template Project/lib/ESPMegaPRO/InternalDisplay.hpp b/Template Project/lib/ESPMegaPRO/InternalDisplay.hpp index 503c5bb..0a331b7 100644 --- a/Template Project/lib/ESPMegaPRO/InternalDisplay.hpp +++ b/Template Project/lib/ESPMegaPRO/InternalDisplay.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include #include diff --git a/Template Project/platformio.ini b/Template Project/platformio.ini index a0f53c1..5b848e1 100644 --- a/Template Project/platformio.ini +++ b/Template Project/platformio.ini @@ -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 diff --git a/Template Project/src/iot_framdemo.cpp b/Template Project/src/iot_framdemo.cpp index b85da59..c08813b 100644 --- a/Template Project/src/iot_framdemo.cpp +++ b/Template Project/src/iot_framdemo.cpp @@ -3,7 +3,6 @@ #include ESPMegaPRO espmega = ESPMegaPRO(); -InternalDisplay display = InternalDisplay(&Serial); void input_change_callback(uint8_t pin, uint8_t value) { Serial.print("Input change callback: ");