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

65 lines
2.8 KiB
C++
Raw Permalink Normal View History

2023-12-27 20:09:41 +00:00
#pragma once
2023-12-28 05:46:39 +00:00
#include <IoTComponent.hpp>
2023-12-27 20:09:41 +00:00
#include <AnalogCard.hpp>
#include <map>
2023-12-28 17:39:11 +00:00
2023-12-31 18:38:25 +00:00
// MQTT Topics
2023-12-28 17:39:11 +00:00
#define DAC_SET_STATE_TOPIC "/set/state"
#define DAC_SET_VALUE_TOPIC "/set/value"
#define DAC_STATE_TOPIC "/dac/00/state"
#define DAC_VALUE_TOPIC "/dac/00/value"
#define DAC_PUBLISH_ENABLE_TOPIC "/publish_enable"
#define REQUEST_STATE_TOPIC "requeststate"
2023-12-31 18:56:49 +00:00
/**
* @brief The AnalogIoT class is a class for connecting the Analog Card to the IoT module.
*
* This function allows you to control the Analog Card using MQTT.
*
* @warning You should not instantiate this class directly, instead use ESPMegaIoT's registerCard function.
*/
2023-12-28 05:46:39 +00:00
class AnalogIoT : public IoTComponent {
2023-12-27 20:09:41 +00:00
public:
2023-12-28 06:14:18 +00:00
AnalogIoT();
~AnalogIoT();
2023-12-28 17:39:11 +00:00
bool begin(uint8_t card_id, ExpansionCard *card, PubSubClient *mqtt, char *base_topic);
2023-12-27 20:09:41 +00:00
void handleMqttMessage(char *topic, char *payload);
2023-12-28 17:39:11 +00:00
void handleDACChange(uint8_t pin, uint16_t value);
2023-12-27 20:09:41 +00:00
void publishADCs();
2023-12-28 17:39:11 +00:00
void publishADC(uint8_t pin);
void publishDACs();
void publishDAC(uint8_t pin);
void publishDACState(uint8_t pin);
void publishDACValue(uint8_t pin);
2023-12-27 20:09:41 +00:00
void setADCsPublishInterval(uint32_t interval);
void setADCsPublishEnabled(bool enabled);
2023-12-31 06:32:38 +00:00
uint8_t registerADCConversionCallback(std::function<void(uint8_t, uint16_t)> callback);
2023-12-31 06:41:48 +00:00
void unregisterADCConversionCallback(uint8_t handler);
2023-12-28 17:39:11 +00:00
void setADCConversionInterval(uint8_t pin, uint16_t interval);
void setADCConversionEnabled(uint8_t pin, bool enabled);
bool processADCSetConversionIntervalMessage(char *topic, char *payload, uint8_t topic_length);
bool processADCSetConversionEnabledMessage(char *topic, char *payload, uint8_t topic_length);
bool processDACSetStateMessage(char *topic, char *payload, uint8_t topic_length);
bool processDACSetValueMessage(char *topic, char *payload, uint8_t topic_length);
bool processRequestStateMessage(char *topic, char *payload, uint8_t topic_length);
void publishReport();
void subscribe();
void loop();
2023-12-28 06:14:18 +00:00
uint8_t getType();
2023-12-27 20:09:41 +00:00
private:
2023-12-31 18:38:25 +00:00
// The index of the next callback to be registered
uint8_t adc_conversion_callback_index = 0;
2023-12-31 18:38:25 +00:00
// We keep track of the length of the topics so we don't have to calculate it every time
2023-12-28 17:39:11 +00:00
uint8_t dac_set_state_length;
uint8_t dac_set_value_length;
uint8_t dac_state_length;
uint8_t dac_value_length;
uint8_t request_state_length;
uint8_t dac_publish_enable_length;
2023-12-27 20:09:41 +00:00
uint32_t last_adc_publish = 0;
AnalogCard *card;
2023-12-28 17:39:11 +00:00
bool adc_publish_enabled[8];
uint16_t adc_conversion_interval[8];
uint32_t last_adc_conversion[8];
std::map<uint8_t, std::function<void(uint8_t, uint16_t)>> adc_conversion_callbacks;
2023-12-27 20:09:41 +00:00
};