ESPMegaPRO-v3-SDK/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.hpp

91 lines
2.8 KiB
C++

#pragma once
#include <ExpansionCard.hpp>
#include <driver/rmt.h>
#include <FRAM.h>
#include <OneWire.h>
#include <DS18B20.h>
#include <dhtnew.h>
#include <vector>
#define RMT_TX_CHANNEL RMT_CHANNEL_0
#define CARD_TYPE_CLIMATE 0x03
#define AC_SENSOR_TYPE_NONE 0x00
#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;
const char **mode_names;
uint8_t fan_speeds;
const char **fan_speed_names;
// function to get IR code
// takes 3 arguments: mode, fan speed, temperature, all uint8_t
// return size of IR code array
size_t (*getInfraredCode)(uint8_t, uint8_t, uint8_t, const uint16_t**);
};
// This requires 3 bytes of FRAM
class ClimateCard : public ExpansionCard {
public:
ClimateCard(uint8_t ir_pin, AirConditioner ac, uint8_t sensor_type, uint8_t sensor_pin);
ClimateCard(uint8_t ir_pin, AirConditioner ac);
~ClimateCard();
bool begin();
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();
uint8_t getSensorType();
void registerChangeCallback(std::function<void(uint8_t, uint8_t, uint8_t)> callback);
void registerSensorCallback(std::function<void(float, float)> 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;
std::vector<std::function<void(float, float)>> sensor_callbacks;
// Update functions
void updateSensor();
void updateAirConditioner();
// IR variables
uint8_t ir_pin;
// Air conditioner variables
AirConditioner ac;
ClimateCardData state;
// Sensor variables
uint8_t sensor_type;
uint8_t sensor_pin;
float humidity;
float room_temperature;
// FRAM variables
FRAM *fram;
uint16_t fram_address;
bool fram_auto_save;
uint16_t* getIrIndex(uint8_t mode, uint8_t fan_speed, uint8_t temperature);
};