Browse Source

Avoid EEPROM commits on callbacks (#1214)

ota
Xose Pérez 6 years ago
parent
commit
6fee2f1f9f
4 changed files with 46 additions and 13 deletions
  1. +3
    -3
      code/espurna/config/hardware.h
  2. +1
    -1
      code/espurna/mqtt.ino
  3. +39
    -6
      code/espurna/relay.ino
  4. +3
    -3
      code/espurna/utils.ino

+ 3
- 3
code/espurna/config/hardware.h View File

@ -70,7 +70,7 @@
// Buttons // Buttons
#define BUTTON1_PIN 0 #define BUTTON1_PIN 0
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH #define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define BUTTON1_RELAY 1
#define BUTTON1_RELAY 1
// Hidden button will enter AP mode if dblclick and reset the device when long-long-clicked // Hidden button will enter AP mode if dblclick and reset the device when long-long-clicked
#define RELAY1_PIN 12 #define RELAY1_PIN 12
@ -1943,11 +1943,11 @@
#define LED1_PIN 0 // red #define LED1_PIN 0 // red
#define LED1_PIN_INVERSE 1 #define LED1_PIN_INVERSE 1
#define LED1_MODE LED_MODE_WIFI #define LED1_MODE LED_MODE_WIFI
#define LED2_PIN 15 // blue #define LED2_PIN 15 // blue
#define LED2_PIN_INVERSE 1 #define LED2_PIN_INVERSE 1
#define LED2_MODE LED_MODE_RELAY #define LED2_MODE LED_MODE_RELAY
// HLW8012 // HLW8012
#ifndef HLW8012_SUPPORT #ifndef HLW8012_SUPPORT
#define HLW8012_SUPPORT 1 #define HLW8012_SUPPORT 1


+ 1
- 1
code/espurna/mqtt.ino View File

@ -291,7 +291,7 @@ unsigned long _mqttNextMessageId() {
EEPROMr.write(EEPROM_MESSAGE_ID + 1, (id >> 16) & 0xFF); EEPROMr.write(EEPROM_MESSAGE_ID + 1, (id >> 16) & 0xFF);
EEPROMr.write(EEPROM_MESSAGE_ID + 2, (id >> 8) & 0xFF); EEPROMr.write(EEPROM_MESSAGE_ID + 2, (id >> 8) & 0xFF);
EEPROMr.write(EEPROM_MESSAGE_ID + 3, (id >> 0) & 0xFF); EEPROMr.write(EEPROM_MESSAGE_ID + 3, (id >> 0) & 0xFF);
EEPROMr.commit();
saveSettings();
} }
id++; id++;


+ 39
- 6
code/espurna/relay.ino View File

@ -173,11 +173,19 @@ void _relayProcess(bool mode) {
#endif #endif
if (!_relayRecursive) { if (!_relayRecursive) {
relayPulse(id); relayPulse(id);
_relaySaveTicker.once_ms(RELAY_SAVE_DELAY, relaySave);
// We will trigger a commit only if
// we care about current relay status on boot
unsigned char boot_mode = getSetting("relayBoot", id, RELAY_BOOT_MODE).toInt();
bool do_commit = ((RELAY_BOOT_SAME == boot_mode) || (RELAY_BOOT_TOGGLE == boot_mode));
_relaySaveTicker.once_ms(RELAY_SAVE_DELAY, relaySave, do_commit);
#if WEB_SUPPORT #if WEB_SUPPORT
wsSend(_relayWebSocketUpdate); wsSend(_relayWebSocketUpdate);
#endif #endif
} }
#if DOMOTICZ_SUPPORT #if DOMOTICZ_SUPPORT
@ -383,16 +391,41 @@ void relaySync(unsigned char id) {
} }
void relaySave() {
void relaySave(bool do_commit) {
// Relay status is stored in a single byte
// This means that, atm,
// we are only storing the status of the first 8 relays.
unsigned char bit = 1; unsigned char bit = 1;
unsigned char mask = 0; unsigned char mask = 0;
for (unsigned int i=0; i < _relays.size(); i++) {
unsigned char count = _relays.size();
if (count > 8) count = 8;
for (unsigned int i=0; i < count; i++) {
if (relayStatus(i)) mask += bit; if (relayStatus(i)) mask += bit;
bit += bit; bit += bit;
} }
EEPROMr.write(EEPROM_RELAY_STATUS, mask); EEPROMr.write(EEPROM_RELAY_STATUS, mask);
DEBUG_MSG_P(PSTR("[RELAY] Saving mask: %d\n"), mask);
EEPROMr.commit();
DEBUG_MSG_P(PSTR("[RELAY] Setting relay mask: %d\n"), mask);
// The 'do_commit' flag controls wether we are commiting this change or not.
// It is useful to set it to 'false' if the relay change triggering the
// save involves a relay whose boot mode is independent from current mode,
// thus storing the last relay value is not absolutely necessary.
// Nevertheless, we store the value in the EEPROM buffer so it will be written
// on the next commit.
if (do_commit) {
// We are actually enqueuing the commit so it will be
// executed on the main loop, in case this is called from a callback
saveSettings();
}
}
void relaySave() {
relaySave(true);
} }
void relayToggle(unsigned char id, bool report, bool group_report) { void relayToggle(unsigned char id, bool report, bool group_report) {
@ -508,7 +541,7 @@ void _relayBoot() {
// Save if there is any relay in the RELAY_BOOT_TOGGLE mode // Save if there is any relay in the RELAY_BOOT_TOGGLE mode
if (trigger_save) { if (trigger_save) {
EEPROMr.write(EEPROM_RELAY_STATUS, mask); EEPROMr.write(EEPROM_RELAY_STATUS, mask);
EEPROMr.commit();
saveSettings();
} }
_relayRecursive = false; _relayRecursive = false;


+ 3
- 3
code/espurna/utils.ino View File

@ -468,13 +468,13 @@ void resetReason(unsigned char reason) {
EEPROMr.commit(); EEPROMr.commit();
} }
void reset(unsigned char reason) {
resetReason(reason);
void reset() {
ESP.restart(); ESP.restart();
} }
void deferredReset(unsigned long delay, unsigned char reason) { void deferredReset(unsigned long delay, unsigned char reason) {
_defer_reset.once_ms(delay, reset, reason);
resetReason(reason);
_defer_reset.once_ms(delay, reset);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------


Loading…
Cancel
Save