rename folders
This commit is contained in:
parent
1ec8effe90
commit
1815597374
63 changed files with 1 additions and 0 deletions
68
ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputIoT.cpp
Normal file
68
ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputIoT.cpp
Normal file
|
@ -0,0 +1,68 @@
|
|||
#include <DigitalInputIoT.hpp>
|
||||
|
||||
|
||||
bool DigitalInputIoT::begin(uint8_t card_id, ExpansionCard *card, PubSubClient *mqtt, char *base_topic) {
|
||||
this->card = (DigitalInputCard *)card;
|
||||
this->card_id = card_id;
|
||||
this->mqtt = mqtt;
|
||||
this->base_topic = base_topic;
|
||||
this->setDigitalInputsPublishEnabled(true);
|
||||
this->card->registerCallback(std::bind(&DigitalInputIoT::handleValueChange, this, std::placeholders::_1, std::placeholders::_2));
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void DigitalInputIoT::subscribe() {
|
||||
char topic[64];
|
||||
sprintf(topic, "%s/%d/%s", this->base_topic, this->card_id, PUBLISH_ENABLE_TOPIC);
|
||||
this->subscribeRelative(topic);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
void DigitalInputIoT::publishReport() {
|
||||
this->publishDigitalInputs();
|
||||
}
|
||||
uint8_t DigitalInputIoT::getType() {
|
||||
return CARD_TYPE_DIGITAL_INPUT;
|
||||
}
|
||||
|
||||
|
||||
void DigitalInputIoT::publishDigitalInput(uint8_t pin) {
|
||||
char topic[20] = {0};
|
||||
char payload[20] = {0};
|
||||
topic[0] = pin-pin%10 + '0';
|
||||
topic[1] = pin%10 + '0';
|
||||
topic[2] = '\0';
|
||||
payload[0] = this->card->digitalRead(pin, false) + '0';
|
||||
payload[1] = '\0';
|
||||
this->publishRelative(topic, payload);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue