fixed climate card fram bug

This commit is contained in:
Siwat Sirichai 2023-12-31 02:59:25 +07:00
parent 091dc183fe
commit c635427d64
8 changed files with 112 additions and 44 deletions

View File

@ -86,10 +86,9 @@ void ClimateCard::setFRAMAutoSave(bool autoSave)
void ClimateCard::saveStateToFRAM() void ClimateCard::saveStateToFRAM()
{ {
// fram->write8(fram_address, state.ac_temperature); fram->write8(fram_address, state.ac_temperature);
// fram->write8(fram_address + 1, state.ac_mode); fram->write8(fram_address + 1, state.ac_mode);
// fram->write8(fram_address + 2, state.ac_fan_speed); fram->write8(fram_address + 2, state.ac_fan_speed);
fram->writeObject(fram_address, state);
Serial.println("Saved state to FRAM"); Serial.println("Saved state to FRAM");
Serial.write(0xFF); Serial.write(0xFF);
Serial.write(0xFF); Serial.write(0xFF);
@ -98,10 +97,24 @@ void ClimateCard::saveStateToFRAM()
void ClimateCard::loadStateFromFRAM() void ClimateCard::loadStateFromFRAM()
{ {
// state.ac_temperature = fram->read8(fram_address); Serial.print("Loading temperature from FRAM at address ");
// state.ac_mode = fram->read8(fram_address + 1); Serial.print(fram_address);
// state.ac_fan_speed = fram->read8(fram_address + 2); Serial.print(": ");
fram->readObject(fram_address, state); Serial.println(fram->read8(fram_address));
state.ac_temperature = fram->read8(fram_address);
Serial.print("Loading mode from FRAM at address ");
Serial.print(fram_address + 1);
Serial.print(": ");
Serial.println(fram->read8(fram_address + 1));
state.ac_mode = fram->read8(fram_address + 1);
Serial.print("Loading fan speed from FRAM at address ");
Serial.print(fram_address + 2);
Serial.print(": ");
Serial.println(fram->read8(fram_address + 2));
state.ac_fan_speed = fram->read8(fram_address + 2);
Serial.write(0xFF);
Serial.write(0xFF);
Serial.write(0xFF);
// if (state.ac_temperature > ac.max_temperature) // if (state.ac_temperature > ac.max_temperature)
// state.ac_temperature = ac.max_temperature; // state.ac_temperature = ac.max_temperature;
// else if (state.ac_temperature < ac.min_temperature) // else if (state.ac_temperature < ac.min_temperature)

View File

@ -84,7 +84,7 @@ class ClimateCard : public ExpansionCard {
float room_temperature; float room_temperature;
// FRAM variables // FRAM variables
FRAM *fram; FRAM *fram;
uint8_t fram_address; uint16_t fram_address;
bool fram_auto_save; bool fram_auto_save;
uint16_t* getIrIndex(uint8_t mode, uint8_t fan_speed, uint8_t temperature); uint16_t* getIrIndex(uint8_t mode, uint8_t fan_speed, uint8_t temperature);
}; };

View File

@ -314,33 +314,33 @@ void ESPMegaIoT::setNetworkConfig(NetworkConfig network_config)
void ESPMegaIoT::loadNetworkConfig() void ESPMegaIoT::loadNetworkConfig()
{ {
// Load the network config from FRAM // Load the network config from FRAM
network_config.ip = fram->read32(FRAM_ADDRESS); network_config.ip = fram->read32(IOT_FRAM_ADDRESS);
network_config.gateway = fram->read32(FRAM_ADDRESS + 4); network_config.gateway = fram->read32(IOT_FRAM_ADDRESS + 4);
network_config.subnet = fram->read32(FRAM_ADDRESS + 8); network_config.subnet = fram->read32(IOT_FRAM_ADDRESS + 8);
network_config.dns1 = fram->read32(FRAM_ADDRESS + 12); network_config.dns1 = fram->read32(IOT_FRAM_ADDRESS + 12);
network_config.dns2 = fram->read32(FRAM_ADDRESS + 16); network_config.dns2 = fram->read32(IOT_FRAM_ADDRESS + 16);
fram->read(FRAM_ADDRESS + 20, (uint8_t*)network_config.hostname, 32); fram->read(IOT_FRAM_ADDRESS + 20, (uint8_t*)network_config.hostname, 32);
network_config.useStaticIp = fram->read8(FRAM_ADDRESS + 52); network_config.useStaticIp = fram->read8(IOT_FRAM_ADDRESS + 52);
network_config.useWifi = fram->read8(FRAM_ADDRESS + 53); network_config.useWifi = fram->read8(IOT_FRAM_ADDRESS + 53);
network_config.wifiUseAuth = fram->read8(FRAM_ADDRESS + 54); network_config.wifiUseAuth = fram->read8(IOT_FRAM_ADDRESS + 54);
fram->read(FRAM_ADDRESS + 55, (uint8_t*)network_config.ssid, 32); fram->read(IOT_FRAM_ADDRESS + 55, (uint8_t*)network_config.ssid, 32);
fram->read(FRAM_ADDRESS + 87, (uint8_t*)network_config.password, 32); fram->read(IOT_FRAM_ADDRESS + 87, (uint8_t*)network_config.password, 32);
} }
void ESPMegaIoT::saveNetworkConfig() void ESPMegaIoT::saveNetworkConfig()
{ {
// Save the network config to FRAM // Save the network config to FRAM
fram->write32(FRAM_ADDRESS, network_config.ip); fram->write32(IOT_FRAM_ADDRESS, network_config.ip);
fram->write32(FRAM_ADDRESS + 4, network_config.gateway); fram->write32(IOT_FRAM_ADDRESS + 4, network_config.gateway);
fram->write32(FRAM_ADDRESS + 8, network_config.subnet); fram->write32(IOT_FRAM_ADDRESS + 8, network_config.subnet);
fram->write32(FRAM_ADDRESS + 12, network_config.dns1); fram->write32(IOT_FRAM_ADDRESS + 12, network_config.dns1);
fram->write32(FRAM_ADDRESS + 16, network_config.dns2); fram->write32(IOT_FRAM_ADDRESS + 16, network_config.dns2);
fram->write(FRAM_ADDRESS + 20, (uint8_t*)network_config.hostname, 32); fram->write(IOT_FRAM_ADDRESS + 20, (uint8_t*)network_config.hostname, 32);
fram->write8(FRAM_ADDRESS + 52, network_config.useStaticIp); fram->write8(IOT_FRAM_ADDRESS + 52, network_config.useStaticIp);
fram->write8(FRAM_ADDRESS + 53, network_config.useWifi); fram->write8(IOT_FRAM_ADDRESS + 53, network_config.useWifi);
fram->write8(FRAM_ADDRESS + 54, network_config.wifiUseAuth); fram->write8(IOT_FRAM_ADDRESS + 54, network_config.wifiUseAuth);
fram->write(FRAM_ADDRESS + 55, (uint8_t*)network_config.ssid, 32); fram->write(IOT_FRAM_ADDRESS + 55, (uint8_t*)network_config.ssid, 32);
fram->write(FRAM_ADDRESS + 87, (uint8_t*)network_config.password, 32); fram->write(IOT_FRAM_ADDRESS + 87, (uint8_t*)network_config.password, 32);
} }
@ -353,22 +353,22 @@ void ESPMegaIoT::loadMqttConfig()
{ {
// Load the mqtt config from FRAM // Load the mqtt config from FRAM
// We skip bytes 119-127 because they are reserved for the network config // We skip bytes 119-127 because they are reserved for the network config
mqtt_config.mqtt_port = fram->read16(FRAM_ADDRESS + 128); mqtt_config.mqtt_port = fram->read16(IOT_FRAM_ADDRESS + 128);
fram->read(FRAM_ADDRESS + 130, (uint8_t*)mqtt_config.mqtt_server, 32); fram->read(IOT_FRAM_ADDRESS + 130, (uint8_t*)mqtt_config.mqtt_server, 32);
fram->read(FRAM_ADDRESS + 162, (uint8_t*)mqtt_config.mqtt_user, 32); fram->read(IOT_FRAM_ADDRESS + 162, (uint8_t*)mqtt_config.mqtt_user, 32);
fram->read(FRAM_ADDRESS + 194, (uint8_t*)mqtt_config.mqtt_password, 32); fram->read(IOT_FRAM_ADDRESS + 194, (uint8_t*)mqtt_config.mqtt_password, 32);
mqtt_config.mqtt_useauth = fram->read8(FRAM_ADDRESS + 226); mqtt_config.mqtt_useauth = fram->read8(IOT_FRAM_ADDRESS + 226);
fram->read(FRAM_ADDRESS + 227, (uint8_t*)mqtt_config.base_topic, 32); fram->read(IOT_FRAM_ADDRESS + 227, (uint8_t*)mqtt_config.base_topic, 32);
} }
void ESPMegaIoT::saveMqttConfig() void ESPMegaIoT::saveMqttConfig()
{ {
fram->write16(FRAM_ADDRESS + 128, mqtt_config.mqtt_port); fram->write16(IOT_FRAM_ADDRESS + 128, mqtt_config.mqtt_port);
fram->write(FRAM_ADDRESS + 130, (uint8_t*)mqtt_config.mqtt_server, 32); fram->write(IOT_FRAM_ADDRESS + 130, (uint8_t*)mqtt_config.mqtt_server, 32);
fram->write(FRAM_ADDRESS + 162, (uint8_t*)mqtt_config.mqtt_user, 32); fram->write(IOT_FRAM_ADDRESS + 162, (uint8_t*)mqtt_config.mqtt_user, 32);
fram->write(FRAM_ADDRESS + 194, (uint8_t*)mqtt_config.mqtt_password, 32); fram->write(IOT_FRAM_ADDRESS + 194, (uint8_t*)mqtt_config.mqtt_password, 32);
fram->write8(FRAM_ADDRESS + 226, mqtt_config.mqtt_useauth); fram->write8(IOT_FRAM_ADDRESS + 226, mqtt_config.mqtt_useauth);
fram->write(FRAM_ADDRESS + 227, (uint8_t*)mqtt_config.base_topic, 32); fram->write(IOT_FRAM_ADDRESS + 227, (uint8_t*)mqtt_config.base_topic, 32);
} }
void ESPMegaIoT::connectToMqtt() void ESPMegaIoT::connectToMqtt()

View File

@ -18,7 +18,7 @@
// Starts from 34 // Starts from 34
// Ends at 300 (inclusive) // Ends at 300 (inclusive)
// Total of 267 bytes // Total of 267 bytes
#define FRAM_ADDRESS 34 #define IOT_FRAM_ADDRESS 34
struct NetworkConfig struct NetworkConfig
{ {

View File

@ -128,4 +128,19 @@ void ESPMegaPRO::enableInternalDisplay(HardwareSerial *serial) {
internalDisplayEnabled = true; internalDisplayEnabled = true;
ESP_LOGD("ESPMegaPRO", "Internal Display Enabled"); ESP_LOGD("ESPMegaPRO", "Internal Display Enabled");
}
void ESPMegaPRO::dumpFRAMtoSerial(uint16_t start, uint16_t end) {
for (int i = start; i <=end; i++) {
if (i % 16 == 0) {
Serial.printf("\n%03d: ", i);
}
Serial.printf("%03d ", this->fram.read8(i));
}
}
void ESPMegaPRO::dumpFRAMtoSerialASCII(uint16_t start, uint16_t end) {
for (int i = 0; i < 500; i++) {
Serial.printf("%d: %c\n", i,this->fram.read8(i));
}
} }

View File

@ -31,6 +31,8 @@ class ESPMegaPRO {
void enableIotModule(); void enableIotModule();
void enableInternalDisplay(HardwareSerial *serial); void enableInternalDisplay(HardwareSerial *serial);
rtctime_t getTime(); rtctime_t getTime();
void dumpFRAMtoSerial(uint16_t start, uint16_t end);
void dumpFRAMtoSerialASCII(uint16_t start, uint16_t end);
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;

View File

@ -0,0 +1,26 @@
#include <ESPMegaPRO_OOP.hpp>
ESPMegaPRO espmega = ESPMegaPRO();
void setup() {
espmega.begin();
//espmega.fram.write8(301, 25);
// Dump FRAM to a prettified table
for (int i = 0; i < 500; i++) {
if (i % 16 == 0) {
Serial.printf("\n%03d: ", i);
}
Serial.printf("%03d ", espmega.fram.read8(i));
}
// Dump FRAM again but treat it as a long string
Serial.printf("\n\n");
for (int i = 0; i < 500; i++) {
Serial.printf("%d: %c\n", i,espmega.fram.read8(i));
}
}
void loop() {
espmega.loop();
}

View File

@ -107,6 +107,18 @@ void setup() {
Serial.println(sizeof(NetworkConfig)); Serial.println(sizeof(NetworkConfig));
} }
// Every 20 seconds, dump FRAM 0-500 to serial
void loop() { void loop() {
espmega.loop(); espmega.loop();
static uint32_t last_fram_dump = 0;
if (millis() - last_fram_dump >= 20000) {
last_fram_dump = millis();
Serial.println("Dumping FRAM");
espmega.dumpFRAMtoSerial(0, 500);
Serial.println("Dumping FRAM ASCII");
espmega.dumpFRAMtoSerialASCII(0, 500);
}
} }