refactor for integration of InternalDisplay

This commit is contained in:
Siwat Sirichai 2023-12-30 15:56:05 +07:00
parent d0e4825c2d
commit c224aba193
5 changed files with 33 additions and 43 deletions

View File

@ -50,11 +50,12 @@ bool ClimateCard::begin(AirConditioner ac, uint8_t sensor_type, uint8_t sensor_p
break; break;
} }
updateAirConditioner(); updateAirConditioner();
return true;
} }
bool ClimateCard::begin(AirConditioner ac) bool ClimateCard::begin(AirConditioner ac)
{ {
this->begin(ac, AC_SENSOR_TYPE_NONE, 0); return this->begin(ac, AC_SENSOR_TYPE_NONE, 0);
} }
void ClimateCard::loop() void ClimateCard::loop()

View File

@ -26,7 +26,7 @@ void ESPMegaPRO::loop() {
cards[i]->loop(); cards[i]->loop();
} }
} }
iot.loop(); iot->loop();
} }
bool ESPMegaPRO::installCard(uint8_t slot, ExpansionCard* card) { bool ESPMegaPRO::installCard(uint8_t slot, ExpansionCard* card) {
if (slot > 255) return false; if (slot > 255) return false;
@ -88,11 +88,27 @@ void ESPMegaPRO::setTime(int hours, int minutes, int seconds, int day, int month
} }
void ESPMegaPRO::enableIotModule() { void ESPMegaPRO::enableIotModule() {
iot.intr_begin(cards); if (iotEnabled) return;
this->iot = new ESPMegaIoT();
this->iot->intr_begin(cards);
iotEnabled = true;
} }
ExpansionCard* ESPMegaPRO::getCard(uint8_t slot) { ExpansionCard* ESPMegaPRO::getCard(uint8_t slot) {
if (slot > 255) return nullptr; if (slot > 255) return nullptr;
if (!cardInstalled[slot]) return nullptr; if (!cardInstalled[slot]) return nullptr;
return cards[slot]; return cards[slot];
}
void ESPMegaPRO::enableInternalDisplay(HardwareSerial *serial) {
if (internalDisplayEnabled) return;
if (!iotEnabled) {
Serial.println("Cannot Enable Internal Display without IoT Module being enabled!");
return;
}
display = new InternalDisplay(serial);
auto bindedGetTime = std::bind(&ESPMegaPRO::getTime, this);
display->begin(this->iot,bindedGetTime);
internalDisplayEnabled = true;
} }

View File

@ -29,14 +29,18 @@ class ESPMegaPRO {
bool installCard(uint8_t slot, ExpansionCard* card); bool installCard(uint8_t slot, ExpansionCard* card);
bool updateTimeFromNTP(); bool updateTimeFromNTP();
void enableIotModule(); void enableIotModule();
void enableInternalDisplay(HardwareSerial *serial);
rtctime_t getTime(); rtctime_t getTime();
void setTime(int hours, int minutes, int seconds, int day, int month, int year); void setTime(int hours, int minutes, int seconds, int day, int month, int year);
ExpansionCard* getCard(uint8_t slot); ExpansionCard* getCard(uint8_t slot);
FRAM fram; FRAM fram;
DigitalInputCard inputs = DigitalInputCard(INPUT_BANK_A_ADDRESS, INPUT_BANK_B_ADDRESS); DigitalInputCard inputs = DigitalInputCard(INPUT_BANK_A_ADDRESS, INPUT_BANK_B_ADDRESS);
DigitalOutputCard outputs = DigitalOutputCard(PWM_BANK_ADDRESS); DigitalOutputCard outputs = DigitalOutputCard(PWM_BANK_ADDRESS);
ESPMegaIoT iot = ESPMegaIoT(); InternalDisplay *display;
ESPMegaIoT *iot;
private: private:
bool iotEnabled = false;
bool internalDisplayEnabled = false;
ExpansionCard* cards[255]; ExpansionCard* cards[255];
bool cardInstalled[255]; bool cardInstalled[255];
uint8_t cardCount = 0; uint8_t cardCount = 0;

View File

@ -1,32 +0,0 @@
#include "driver/rmt.h"
#define RMT_TX_CHANNEL RMT_CHANNEL_0
#define RMT_TX_GPIO_NUM GPIO_NUM_4
void app_main() {
// Configure RMT transmitter
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(RMT_TX_GPIO_NUM, RMT_TX_CHANNEL);
config.clk_div = 80; // Set clock divider for desired frequency (80MHz / 80 = 1MHz)
rmt_config(&config);
rmt_driver_install(config.channel, 0, 0);
// Define NEC IR protocol parameters
uint16_t irTimingArray[] = { /* Your IR timing array here */ };
size_t itemCount = sizeof(irTimingArray) / sizeof(irTimingArray[0]);
rmt_item32_t items[itemCount];
// Convert IR timing array to RMT items
for (size_t i = 0; i < itemCount; i++) {
items[i].level0 = 1;
items[i].duration0 = irTimingArray[i];
items[i].level1 = 0;
items[i].duration1 = irTimingArray[i];
}
// Send IR signal
rmt_write_items(config.channel, items, itemCount, true);
rmt_wait_tx_done(config.channel, portMAX_DELAY);
// Cleanup RMT driver
rmt_driver_uninstall(config.channel);
}

View File

@ -16,7 +16,7 @@ void setup() {
espmega.begin(); espmega.begin();
espmega.enableIotModule(); espmega.enableIotModule();
ETH.begin(); ETH.begin();
espmega.iot.bindEthernetInterface(&ETH); espmega.iot->bindEthernetInterface(&ETH);
NetworkConfig config = { NetworkConfig config = {
.ip = {192, 168, 0, 11}, .ip = {192, 168, 0, 11},
.gateway = {192, 168, 0, 1}, .gateway = {192, 168, 0, 1},
@ -31,9 +31,9 @@ void setup() {
strcpy(config.password, "password"); strcpy(config.password, "password");
strcpy(config.hostname, "espmega"); strcpy(config.hostname, "espmega");
Serial.println("Setting network config"); Serial.println("Setting network config");
espmega.iot.setNetworkConfig(config); espmega.iot->setNetworkConfig(config);
Serial.println("Connecting to network"); Serial.println("Connecting to network");
espmega.iot.connectNetwork(); espmega.iot->connectNetwork();
Serial.println("Begin MQTT Modules"); Serial.println("Begin MQTT Modules");
MqttConfig mqtt_config = { MqttConfig mqtt_config = {
.mqtt_port = 1883, .mqtt_port = 1883,
@ -43,14 +43,15 @@ void setup() {
strcpy(mqtt_config.mqtt_server, "192.168.0.26"); strcpy(mqtt_config.mqtt_server, "192.168.0.26");
strcpy(mqtt_config.base_topic, "/espmegaoop"); strcpy(mqtt_config.base_topic, "/espmegaoop");
Serial.println("Loading MQTT Config Struct to IoT Module"); Serial.println("Loading MQTT Config Struct to IoT Module");
espmega.iot.setMqttConfig(mqtt_config); espmega.iot->setMqttConfig(mqtt_config);
Serial.println("Connecting to MQTT"); Serial.println("Connecting to MQTT");
espmega.iot.connectToMqtt(); espmega.iot->connectToMqtt();
Serial.println("Registering cards"); Serial.println("Registering cards");
espmega.iot.registerCard(0); espmega.iot->registerCard(0);
espmega.iot.registerCard(1); espmega.iot->registerCard(1);
Serial.println("Initialization Routine Complete"); Serial.println("Initialization Routine Complete");
espmega.inputs.registerCallback(input_change_callback); espmega.inputs.registerCallback(input_change_callback);
espmega.enableInternalDisplay(&Serial);
} }
void loop() { void loop() {