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

124 lines
4.4 KiB
C++
Raw Permalink Normal View History

2023-12-29 18:08:35 +00:00
#pragma once
#include <ExpansionCard.hpp>
2024-01-10 12:43:07 +00:00
#include <IRBlaster.hpp>
2023-12-29 18:08:35 +00:00
#include <FRAM.h>
2023-12-30 06:49:37 +00:00
#include <OneWire.h>
#include <DS18B20.h>
#include <dhtnew.h>
#include <map>
2023-12-29 18:08:35 +00:00
#define CARD_TYPE_CLIMATE 0x03
#define AC_SENSOR_TYPE_NONE 0x00
#define AC_SENSOR_TYPE_DHT22 0x01
#define AC_SENSOR_TYPE_DS18B20 0x02
2023-12-30 06:49:37 +00:00
#define AC_SENSOR_READ_INTERVAL 5000
#define AC_SENSOR_READ_TIMEOUT 250
2023-12-31 18:38:25 +00:00
/**
* @brief The struct is used to store the state of the air conditioner
*
* @note This struct is stored in FRAM if it is used
* @note This struct is 3 bytes long
*/
2023-12-29 18:08:35 +00:00
struct ClimateCardData {
2023-12-31 18:38:25 +00:00
uint8_t ac_temperature; ///< Temperature of the air conditioner
uint8_t ac_mode; ///< Mode of the air conditioner
uint8_t ac_fan_speed;///< Fan speed of the air conditioner
2023-12-29 18:08:35 +00:00
};
2023-12-30 06:49:37 +00:00
2023-12-31 18:38:25 +00:00
/**
* @brief This struct is used to store information about an air conditioner
*/
2023-12-30 06:49:37 +00:00
struct AirConditioner {
2023-12-31 18:38:25 +00:00
uint8_t max_temperature; ///< Maximum temperature
uint8_t min_temperature; ///< Minimum temperature
uint8_t modes; ///< Number of modes
const char **mode_names; ///< Names of modes in the form of an array of strings
uint8_t fan_speeds; ///< Number of fan speeds
const char **fan_speed_names; ///< Names of fan speeds in the form of an array of strings
/**
* @brief Function to get IR code
*
* @param mode Mode of the air conditioner
* @param fan_speed Fan speed of the air conditioner
* @param temperature Temperature of the air conditioner
* @param code Pointer to the IR code array
*
* @return Size of the IR code array
*/
2023-12-30 17:25:07 +00:00
size_t (*getInfraredCode)(uint8_t, uint8_t, uint8_t, const uint16_t**);
2023-12-30 06:49:37 +00:00
};
2023-12-29 18:08:35 +00:00
// This requires 3 bytes of FRAM
2023-12-31 18:56:49 +00:00
/**
* @brief The ClimateCard class is a class for controlling an air conditioner
*
* This class allows you to control an air conditioner using an IR LED.
* It is meant to be used with the ESPMega Climate Card.
*
* @note You can also use a DHT22 or DS18B20 temperature sensor to get the room temperature (and humidity if using a DHT22). Although, this is optional.
*
*/
2023-12-29 18:08:35 +00:00
class ClimateCard : public ExpansionCard {
public:
2024-01-10 12:43:07 +00:00
ClimateCard(uint8_t ir_pin, AirConditioner ac, uint8_t sensor_type, uint8_t sensor_pin, rmt_channel_t channel);
ClimateCard(uint8_t ir_pin, AirConditioner ac, rmt_channel_t channel);
2023-12-30 06:49:37 +00:00
~ClimateCard();
2023-12-30 17:25:07 +00:00
bool begin();
2023-12-29 18:08:35 +00:00
void loop();
void bindFRAM(FRAM *fram, uint16_t fram_address);
void setFRAMAutoSave(bool autoSave);
void saveStateToFRAM();
void loadStateFromFRAM();
void setTemperature(uint8_t temperature);
2023-12-30 06:49:37 +00:00
uint8_t getTemperature();
2023-12-29 18:08:35 +00:00
void setMode(uint8_t mode);
void setModeByName(const char* mode_name);
2023-12-30 06:49:37 +00:00
uint8_t getMode();
char* getModeName();
2023-12-29 18:08:35 +00:00
void setFanSpeed(uint8_t fan_speed);
void setFanSpeedByName(const char* fan_speed_name);
2023-12-30 06:49:37 +00:00
uint8_t getFanSpeed();
2024-02-16 16:55:43 +00:00
void setState(uint8_t mode, uint8_t fan_speed, uint8_t temperature);
char* getFanSpeedName();
2023-12-30 06:49:37 +00:00
float getRoomTemperature();
float getHumidity();
2023-12-30 07:32:40 +00:00
uint8_t getSensorType();
2023-12-31 06:32:38 +00:00
uint8_t registerChangeCallback(std::function<void(uint8_t, uint8_t, uint8_t)> callback);
uint8_t registerSensorCallback(std::function<void(float, float)> callback);
void unregisterChangeCallback(uint8_t handler);
void unregisterSensorCallback(uint8_t handler);
2023-12-29 18:08:35 +00:00
uint8_t getType();
private:
2023-12-30 06:49:37 +00:00
// Sensor objects
// We use pointers here because we don't know which sensor will be used
DHTNEW *dht;
DS18B20 *ds18b20;
2023-12-29 18:08:35 +00:00
// Callbacks
uint8_t callbacks_handler_count = 0;
uint8_t sensor_callbacks_handler_count = 0;
std::map<uint8_t,std::function<void(uint8_t, uint8_t, uint8_t)>> callbacks;
std::map<uint8_t,std::function<void(float, float)>> sensor_callbacks;
2023-12-29 18:08:35 +00:00
// Update functions
void updateSensor();
void updateAirConditioner();
// IR variables
2023-12-30 06:49:37 +00:00
uint8_t ir_pin;
2024-01-10 12:43:07 +00:00
IRBlaster ir_blaster;
2023-12-29 18:08:35 +00:00
// Air conditioner variables
2023-12-30 06:49:37 +00:00
AirConditioner ac;
ClimateCardData state;
2023-12-29 18:08:35 +00:00
// Sensor variables
uint8_t sensor_type;
uint8_t sensor_pin;
float humidity;
float room_temperature;
// FRAM variables
FRAM *fram;
2023-12-30 19:59:25 +00:00
uint16_t fram_address;
2023-12-30 06:49:37 +00:00
bool fram_auto_save;
2023-12-30 17:25:07 +00:00
uint16_t* getIrIndex(uint8_t mode, uint8_t fan_speed, uint8_t temperature);
2023-12-29 18:08:35 +00:00
};