diff --git a/Template Project/lib/ESPMegaPRO/AnalogCard.cpp b/Template Project/lib/ESPMegaPRO/AnalogCard.cpp index 286a08c..1569592 100644 --- a/Template Project/lib/ESPMegaPRO/AnalogCard.cpp +++ b/Template Project/lib/ESPMegaPRO/AnalogCard.cpp @@ -37,6 +37,7 @@ uint16_t AnalogCard::analogRead(uint8_t pin) { return this->analogInputBankB.readADC_SingleEnded(pin - 4); } + return 65535; } bool AnalogCard::begin() { diff --git a/Template Project/lib/ESPMegaPRO/DigitalInputIoT.cpp b/Template Project/lib/ESPMegaPRO/DigitalInputIoT.cpp new file mode 100644 index 0000000..ce0ed20 --- /dev/null +++ b/Template Project/lib/ESPMegaPRO/DigitalInputIoT.cpp @@ -0,0 +1,55 @@ +#include + + +bool DigitalInputIoT::begin(uint8_t card_id, DigitalInputCard *card, PubSubClient *mqtt, char *base_topic) { + this->card = card; + this->card_id = card_id; + this->mqtt = mqtt; + this->base_topic = base_topic; + this->card->registerCallback(std::bind(&DigitalInputIoT::handleValueChange, this, std::placeholders::_1, std::placeholders::_2)); + return true; + +} +void DigitalInputIoT::handleMqttMessage(char *topic, char *payload) { + // payload is char '0' or '1' + if (!strcmp(topic, PUBLISH_ENABLE_TOPIC)) { + if (payload[0] == '1') { + this->setDigitalInputsPublishEnabled(true); + } else { + this->setDigitalInputsPublishEnabled(false); + } + } +} +void DigitalInputIoT::publishDigitalInputs() { + if (!this->digital_inputs_publish_enabled) { + return; + } + for (int i = 0; i < 16; i++) { + this->publishDigitalInput(i); + } +} +void DigitalInputIoT::setDigitalInputsPublishEnabled(bool enabled) { + this->digital_inputs_publish_enabled = enabled; + if (enabled) { + this->publishDigitalInputs(); + } +} +void DigitalInputIoT::handleValueChange(uint8_t pin, uint8_t value) { + if (this->digital_inputs_publish_enabled) { + this->publishDigitalInput(pin); + } + if (this->change_callback != NULL) { + this->change_callback(pin, value); + } + +} +void DigitalInputIoT::registerChangeCallback(std::function callback) { + this->change_callback = callback; + +} +void DigitalInputIoT::publishReport() { + this->publishDigitalInputs(); +} +uint8_t DigitalInputIoT::getType() { + return CARD_TYPE_DIGITAL_INPUT; +} \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/DigitalInputIoT.hpp b/Template Project/lib/ESPMegaPRO/DigitalInputIoT.hpp index d84b099..51d9a62 100644 --- a/Template Project/lib/ESPMegaPRO/DigitalInputIoT.hpp +++ b/Template Project/lib/ESPMegaPRO/DigitalInputIoT.hpp @@ -3,17 +3,24 @@ #include #include +#define PUBLISH_ENABLE_TOPIC "publish_enable" + class DigitalInputIoT : public IoTComponent { public: bool begin(uint8_t card_id, DigitalInputCard *card, PubSubClient *mqtt, char *base_topic); void handleMqttMessage(char *topic, char *payload); void publishDigitalInputs(); + void publishDigitalInput(uint8_t pin); void setDigitalInputsPublishEnabled(bool enabled); void handleValueChange(uint8_t pin, uint8_t value); - void registerValueChangeCallback(void (*callback)(uint8_t, uint8_t)); + void registerChangeCallback(std::function callback); void publishReport(); uint8_t getType(); private: + uint8_t card_id; + PubSubClient *mqtt; + char *base_topic; + std::function change_callback; bool digital_inputs_publish_enabled = false; DigitalInputCard *card; }; \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp index 16a7709..e14903c 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp @@ -89,10 +89,10 @@ void ESPMegaIoT::registerCard(uint8_t card_id) // components[card_id] = new AnalogIoT(); // components[card_id]->begin(card_id, cards[card_id], &mqtt, base_topic); // break; - // case CARD_TYPE_DIGITAL_INPUT: - // components[card_id] = new DigitalInputIoT(); - // components[card_id]->begin(card_id, cards[card_id], &mqtt, base_topic); - // break; + case CARD_TYPE_DIGITAL_INPUT: + components[card_id] = new DigitalInputIoT(); + components[card_id]->begin(card_id, cards[card_id], &mqtt, base_topic); + break; case CARD_TYPE_DIGITAL_OUTPUT: components[card_id] = new DigitalOutputIoT(); components[card_id]->begin(card_id, cards[card_id], &mqtt, base_topic);