Browse Source

Load config on boot from embedded JSON file

v2
Xose Pérez 6 years ago
parent
commit
e9d8ad7364
14 changed files with 222 additions and 1984 deletions
  1. +84
    -113
      code/espurna/button.ino
  2. +4
    -2
      code/espurna/config/general.h
  3. +6
    -60
      code/espurna/config/hardware.h
  4. +3
    -1
      code/espurna/config/types.h
  5. +3
    -2
      code/espurna/espurna.ino
  6. +62
    -62
      code/espurna/led.ino
  7. +1
    -1
      code/espurna/mdns.ino
  8. +10
    -1669
      code/espurna/migrate.ino
  9. +22
    -29
      code/espurna/relay.ino
  10. +1
    -1
      code/espurna/ssdp.ino
  11. +23
    -3
      code/espurna/utils.ino
  12. +1
    -1
      code/espurna/web.ino
  13. +2
    -2
      code/espurna/ws.ino
  14. +0
    -38
      code/lib/readme.txt

+ 84
- 113
code/espurna/button.ino View File

@ -48,19 +48,7 @@ bool _buttonKeyCheck(const char * key) {
return (strncmp(key, "btn", 3) == 0); return (strncmp(key, "btn", 3) == 0);
} }
int buttonFromRelay(unsigned int relayID) {
for (unsigned int i=0; i < _buttons.size(); i++) {
if (_buttons[i].relayID == relayID) return i;
}
return -1;
}
bool buttonState(unsigned char id) {
if (id >= _buttons.size()) return false;
return _buttons[id].button->pressed();
}
unsigned char buttonAction(unsigned char id, unsigned char event) {
unsigned char _buttonGetAction(unsigned char id, unsigned char event) {
if (id >= _buttons.size()) return BUTTON_MODE_NONE; if (id >= _buttons.size()) return BUTTON_MODE_NONE;
unsigned long actions = _buttons[id].actions; unsigned long actions = _buttons[id].actions;
if (event == BUTTON_EVENT_PRESSED) return (actions) & 0x0F; if (event == BUTTON_EVENT_PRESSED) return (actions) & 0x0F;
@ -72,18 +60,27 @@ unsigned char buttonAction(unsigned char id, unsigned char event) {
return BUTTON_MODE_NONE; return BUTTON_MODE_NONE;
} }
unsigned long buttonStore(unsigned long pressed, unsigned long click, unsigned long dblclick, unsigned long lngclick, unsigned long lnglngclick, unsigned long tripleclick) {
unsigned int value;
value = pressed;
value += click << 4;
value += dblclick << 8;
value += lngclick << 12;
value += lnglngclick << 16;
value += tripleclick << 20;
unsigned long _buttonGetActionMask(unsigned char id) {
unsigned long pressAction = getSetting("btnPress", id, BUTTON_MODE_NONE).toInt();
unsigned long clickAction = getSetting("btnClick", id, BUTTON_MODE_TOGGLE).toInt();
unsigned long dblClickAction = getSetting("btnDblClick", id, (id == 1) ? BUTTON_MODE_AP : BUTTON_MODE_NONE).toInt();
unsigned long lngClickAction = getSetting("btnLngClick", id, (id == 1) ? BUTTON_MODE_RESET : BUTTON_MODE_NONE).toInt();
unsigned long lnglngClickAction = getSetting("btnLngLngClick", id, (id == 1) ? BUTTON_MODE_FACTORY : BUTTON_MODE_NONE).toInt();
unsigned long tripleClickAction = getSetting("btnTripleClick", id, (id == 1) ? BUTTON_MODE_NONE : BUTTON_MODE_NONE).toInt();
unsigned long value;
value = pressAction;
value += clickAction << 4;
value += dblClickAction << 8;
value += lngClickAction << 12;
value += lnglngClickAction << 16;
value += tripleClickAction << 20;
return value; return value;
} }
uint8_t mapEvent(uint8_t event, uint8_t count, uint16_t length) {
uint8_t _buttonGetEvent(uint8_t event, uint8_t count, uint16_t length) {
if (event == EVENT_PRESSED) return BUTTON_EVENT_PRESSED; if (event == EVENT_PRESSED) return BUTTON_EVENT_PRESSED;
if (event == EVENT_CHANGED) return BUTTON_EVENT_CLICK; if (event == EVENT_CHANGED) return BUTTON_EVENT_CLICK;
if (event == EVENT_RELEASED) { if (event == EVENT_RELEASED) {
@ -98,7 +95,7 @@ uint8_t mapEvent(uint8_t event, uint8_t count, uint16_t length) {
return BUTTON_EVENT_NONE; return BUTTON_EVENT_NONE;
} }
void buttonEvent(unsigned int id, unsigned char event) {
void _buttonExecuteEvent(unsigned int id, unsigned char event) {
DEBUG_MSG_P(PSTR("[BUTTON] Button #%u event %u\n"), id, event); DEBUG_MSG_P(PSTR("[BUTTON] Button #%u event %u\n"), id, event);
if (event == 0) return; if (event == 0) return;
@ -107,21 +104,21 @@ void buttonEvent(unsigned int id, unsigned char event) {
buttonMQTT(id, event); buttonMQTT(id, event);
#endif #endif
unsigned char action = buttonAction(id, event);
unsigned char action = _buttonGetAction(id, event);
if (action == BUTTON_MODE_TOGGLE) { if (action == BUTTON_MODE_TOGGLE) {
if (_buttons[id].relayID > 0) {
relayToggle(_buttons[id].relayID - 1);
if (RELAY_NONE != _buttons[id].relayID) {
relayToggle(_buttons[id].relayID);
} }
} }
if (action == BUTTON_MODE_ON) { if (action == BUTTON_MODE_ON) {
if (_buttons[id].relayID > 0) {
relayStatus(_buttons[id].relayID - 1, true);
if (RELAY_NONE != _buttons[id].relayID) {
relayStatus(_buttons[id].relayID, true);
} }
} }
if (action == BUTTON_MODE_OFF) { if (action == BUTTON_MODE_OFF) {
if (_buttons[id].relayID > 0) {
relayStatus(_buttons[id].relayID - 1, false);
if (RELAY_NONE != _buttons[id].relayID) {
relayStatus(_buttons[id].relayID, false);
} }
} }
if (action == BUTTON_MODE_AP) wifiStartAP(); if (action == BUTTON_MODE_AP) wifiStartAP();
@ -142,85 +139,7 @@ void buttonEvent(unsigned int id, unsigned char event) {
} }
void buttonSetup() {
#ifdef ITEAD_SONOFF_DUAL
unsigned int actions = buttonStore(BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE);
_buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 1});
_buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 2});
_buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, BUTTON3_RELAY});
#else
unsigned long btnDelay = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt();
#if BUTTON1_PIN != GPIO_NONE
{
unsigned int actions = buttonStore(BUTTON1_PRESS, BUTTON1_CLICK, BUTTON1_DBLCLICK, BUTTON1_LNGCLICK, BUTTON1_LNGLNGCLICK, BUTTON1_TRIPLECLICK);
_buttons.push_back({new DebounceEvent(BUTTON1_PIN, BUTTON1_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON1_RELAY});
}
#endif
#if BUTTON2_PIN != GPIO_NONE
{
unsigned int actions = buttonStore(BUTTON2_PRESS, BUTTON2_CLICK, BUTTON2_DBLCLICK, BUTTON2_LNGCLICK, BUTTON2_LNGLNGCLICK, BUTTON2_TRIPLECLICK);
_buttons.push_back({new DebounceEvent(BUTTON2_PIN, BUTTON2_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON2_RELAY});
}
#endif
#if BUTTON3_PIN != GPIO_NONE
{
unsigned int actions = buttonStore(BUTTON3_PRESS, BUTTON3_CLICK, BUTTON3_DBLCLICK, BUTTON3_LNGCLICK, BUTTON3_LNGLNGCLICK, BUTTON3_TRIPLECLICK);
_buttons.push_back({new DebounceEvent(BUTTON3_PIN, BUTTON3_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON3_RELAY});
}
#endif
#if BUTTON4_PIN != GPIO_NONE
{
unsigned int actions = buttonStore(BUTTON4_PRESS, BUTTON4_CLICK, BUTTON4_DBLCLICK, BUTTON4_LNGCLICK, BUTTON4_LNGLNGCLICK, BUTTON4_TRIPLECLICK);
_buttons.push_back({new DebounceEvent(BUTTON4_PIN, BUTTON4_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON4_RELAY});
}
#endif
#if BUTTON5_PIN != GPIO_NONE
{
unsigned int actions = buttonStore(BUTTON5_PRESS, BUTTON5_CLICK, BUTTON5_DBLCLICK, BUTTON5_LNGCLICK, BUTTON5_LNGLNGCLICK, BUTTON5_TRIPLECLICK);
_buttons.push_back({new DebounceEvent(BUTTON5_PIN, BUTTON5_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON5_RELAY});
}
#endif
#if BUTTON6_PIN != GPIO_NONE
{
unsigned int actions = buttonStore(BUTTON6_PRESS, BUTTON6_CLICK, BUTTON6_DBLCLICK, BUTTON6_LNGCLICK, BUTTON6_LNGLNGCLICK, BUTTON6_TRIPLECLICK);
_buttons.push_back({new DebounceEvent(BUTTON6_PIN, BUTTON6_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON6_RELAY});
}
#endif
#if BUTTON7_PIN != GPIO_NONE
{
unsigned int actions = buttonStore(BUTTON7_PRESS, BUTTON7_CLICK, BUTTON7_DBLCLICK, BUTTON7_LNGCLICK, BUTTON7_LNGLNGCLICK, BUTTON7_TRIPLECLICK);
_buttons.push_back({new DebounceEvent(BUTTON7_PIN, BUTTON7_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON7_RELAY});
}
#endif
#if BUTTON8_PIN != GPIO_NONE
{
unsigned int actions = buttonStore(BUTTON8_PRESS, BUTTON8_CLICK, BUTTON8_DBLCLICK, BUTTON8_LNGCLICK, BUTTON8_LNGLNGCLICK, BUTTON8_TRIPLECLICK);
_buttons.push_back({new DebounceEvent(BUTTON8_PIN, BUTTON8_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON8_RELAY});
}
#endif
#endif
DEBUG_MSG_P(PSTR("[BUTTON] Number of buttons: %u\n"), _buttons.size());
// Websocket Callbacks
#if WEB_SUPPORT
wsOnSendRegister(_buttonWebSocketOnSend);
#endif
settingsRegisterKeyCheck(_buttonKeyCheck);
// Register loop
espurnaRegisterLoop(buttonLoop);
}
void buttonLoop() {
void _buttonLoop() {
#ifdef ITEAD_SONOFF_DUAL #ifdef ITEAD_SONOFF_DUAL
@ -236,7 +155,7 @@ void buttonLoop() {
// (in the relayStatus method) it will only be present // (in the relayStatus method) it will only be present
// here if it has actually been pressed // here if it has actually been pressed
if ((value & 4) == 4) { if ((value & 4) == 4) {
buttonEvent(2, BUTTON_EVENT_CLICK);
_buttonExecuteEvent(2, BUTTON_EVENT_CLICK);
return; return;
} }
@ -251,7 +170,7 @@ void buttonLoop() {
// Check if the status for that relay has changed // Check if the status for that relay has changed
if (relayStatus(i) != status) { if (relayStatus(i) != status) {
buttonEvent(i, BUTTON_EVENT_CLICK);
_buttonExecuteEvent(i, BUTTON_EVENT_CLICK);
break; break;
} }
@ -268,8 +187,8 @@ void buttonLoop() {
if (unsigned char event = _buttons[i].button->loop()) { if (unsigned char event = _buttons[i].button->loop()) {
unsigned char count = _buttons[i].button->getEventCount(); unsigned char count = _buttons[i].button->getEventCount();
unsigned long length = _buttons[i].button->getEventLength(); unsigned long length = _buttons[i].button->getEventLength();
unsigned char mapped = mapEvent(event, count, length);
buttonEvent(i, mapped);
unsigned char mapped = _buttonGetEvent(event, count, length);
_buttonExecuteEvent(i, mapped);
} }
} }
@ -277,4 +196,56 @@ void buttonLoop() {
} }
// -----------------------------------------------------------------------------
void buttonSetup() {
#ifdef ITEAD_SONOFF_DUAL
unsigned char relayId = getSetting("btnRelay", 2, RELAY_NONE).toInt();
unsigned long actions = BUTTON_MODE_TOGGLE << 4;
_buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 1});
_buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, 2});
_buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, relayId});
#else
// TODO: maybe this setting should be changed, btnDelay => btnClickDelay?
unsigned long btnDelay = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt();
unsigned char index = 0;
while (index < MAX_COMPONENTS) {
unsigned char pin = getSetting("btnGPIO", index, GPIO_NONE).toInt();
if (GPIO_NONE == pin) break;
unsigned char relayId = getSetting("btnRelay", index, RELAY_NONE).toInt();
unsigned char mode = getSetting("btnMode", index, BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH).toInt();
unsigned long actions = _buttonGetActionMask(index);
// DebounceEvent takes 4 parameters
// * GPIO
// * Button mode
// * Debounce delay
// * Wait delay for more clicks
_buttons.push_back({new DebounceEvent(pin, mode, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, relayId});
++index;
}
#endif
DEBUG_MSG_P(PSTR("[BUTTON] Number of buttons: %u\n"), _buttons.size());
// Websocket Callbacks
#if WEB_SUPPORT
wsOnSendRegister(_buttonWebSocketOnSend);
#endif
settingsRegisterKeyCheck(_buttonKeyCheck);
// Register loop
espurnaRegisterLoop(_buttonLoop);
}
#endif // BUTTON_SUPPORT #endif // BUTTON_SUPPORT

+ 4
- 2
code/espurna/config/general.h View File

@ -7,8 +7,6 @@
// GENERAL // GENERAL
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#define DEVICE_NAME MANUFACTURER "_" DEVICE // Concatenate both to get a unique device name
#ifndef ADMIN_PASS #ifndef ADMIN_PASS
#define ADMIN_PASS "fibonacci" // Default password (WEB, OTA, WIFI) #define ADMIN_PASS "fibonacci" // Default password (WEB, OTA, WIFI)
#endif #endif
@ -21,6 +19,10 @@
#define LOOP_DELAY_TIME 10 // Delay for this millis in the main loop [0-250] #define LOOP_DELAY_TIME 10 // Delay for this millis in the main loop [0-250]
#endif #endif
#ifndef MAX_COMPONENTS
#define MAX_COMPONENTS 8 // Max number of components (buttons, leds, relays,...)
#endif
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// DEBUG // DEBUG
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------


+ 6
- 60
code/espurna/config/hardware.h View File

@ -63,48 +63,11 @@
#elif defined(NODEMCU_LOLIN) #elif defined(NODEMCU_LOLIN)
// Info
#define MANUFACTURER "NODEMCU"
#define DEVICE "LOLIN"
// Buttons
#define BUTTON1_PIN 0
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define BUTTON1_RELAY 1
// Relays
#define RELAY1_PIN 12
#define RELAY1_TYPE RELAY_TYPE_NORMAL
// LEDs
#define LED1_PIN 2
#define LED1_PIN_INVERSE 1
#include "devices/002_nodemcu_lolin.json.h"
#elif defined(WEMOS_D1_MINI_RELAYSHIELD) #elif defined(WEMOS_D1_MINI_RELAYSHIELD)
// Info
#define MANUFACTURER "WEMOS"
#define DEVICE "D1_MINI_RELAYSHIELD"
// Buttons
// No buttons on the D1 MINI alone, but defining it without adding a button doen't create problems
#define BUTTON1_PIN 0 // Connect a pushbutton between D3 and GND,
// it's the same as using a Wemos one button shield
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define BUTTON1_RELAY 1
// Relays
#define RELAY1_PIN 5
#define RELAY1_TYPE RELAY_TYPE_NORMAL
// LEDs
#define LED1_PIN 2
#define LED1_PIN_INVERSE 1
// When Wemos relay shield is connected GPIO5 (D1) is used for relay,
// so I2C must be remapped to other pins
#define I2C_SDA_PIN 12 // D6
#define I2C_SCL_PIN 14 // D5
#include "devices/003_wemos_d1_mini_relayshield.json.h"
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// ESPurna // ESPurna
@ -112,24 +75,7 @@
#elif defined(TINKERMAN_ESPURNA_H06) #elif defined(TINKERMAN_ESPURNA_H06)
// Info
#define MANUFACTURER "TINKERMAN"
#define DEVICE "ESPURNA_H06"
// Buttons
#define BUTTON1_PIN 4
#define BUTTON1_RELAY 1
// Normal pushbutton
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
// Relays
#define RELAY1_PIN 12
#define RELAY1_TYPE RELAY_TYPE_INVERSE
// LEDs
#define LED1_PIN 5
#define LED1_PIN_INVERSE 1
#include "devices/023_tinkerman_espurna_h06.json.h"
// HLW8012 // HLW8012
#ifndef HLW8012_SUPPORT #ifndef HLW8012_SUPPORT
@ -2576,6 +2522,6 @@
// Check definitions // Check definitions
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#if not defined(MANUFACTURER) || not defined(DEVICE)
#error "UNSUPPORTED HARDWARE!!"
#endif
//#if not defined(MANUFACTURER) || not defined(DEVICE)
// #error "UNSUPPORTED HARDWARE!!"
//#endif

+ 3
- 1
code/espurna/config/types.h View File

@ -77,6 +77,8 @@
#define RELAY_PROVIDER_RFBRIDGE 3 #define RELAY_PROVIDER_RFBRIDGE 3
#define RELAY_PROVIDER_STM 4 #define RELAY_PROVIDER_STM 4
#define RELAY_NONE 0xFF
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// UDP SYSLOG // UDP SYSLOG
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -305,7 +307,7 @@
// GPIO // GPIO
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
#define GPIO_INVALID 0xFF
#define GPIO_NONE 0xFF
#define GPIO_LOGIC_DIRECT 0 #define GPIO_LOGIC_DIRECT 0
#define GPIO_LOGIC_INVERSE 1 #define GPIO_LOGIC_INVERSE 1

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

@ -58,8 +58,9 @@ void setup() {
// Init persistance and terminal features // Init persistance and terminal features
settingsSetup(); settingsSetup();
// Board name initialization
setBoardName();
// Load board data
loadBoard();
migrate();
// Show welcome message and system configuration // Show welcome message and system configuration
info(); info();


+ 62
- 62
code/espurna/led.ino View File

@ -22,7 +22,7 @@ typedef struct {
} led_t; } led_t;
std::vector<led_t> _leds; std::vector<led_t> _leds;
bool _led_update = false; // For relay-based modes
bool _led_update = true; // For relay-based modes
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -128,65 +128,7 @@ void _ledConfigure() {
_led_update = true; _led_update = true;
} }
// -----------------------------------------------------------------------------
void ledUpdate(bool value) {
_led_update = value;
}
void ledSetup() {
#if LED1_PIN != GPIO_NONE
_leds.push_back((led_t) { LED1_PIN, LED1_PIN_INVERSE, LED1_MODE, LED1_RELAY });
#endif
#if LED2_PIN != GPIO_NONE
_leds.push_back((led_t) { LED2_PIN, LED2_PIN_INVERSE, LED2_MODE, LED2_RELAY });
#endif
#if LED3_PIN != GPIO_NONE
_leds.push_back((led_t) { LED3_PIN, LED3_PIN_INVERSE, LED3_MODE, LED3_RELAY });
#endif
#if LED4_PIN != GPIO_NONE
_leds.push_back((led_t) { LED4_PIN, LED4_PIN_INVERSE, LED4_MODE, LED4_RELAY });
#endif
#if LED5_PIN != GPIO_NONE
_leds.push_back((led_t) { LED5_PIN, LED5_PIN_INVERSE, LED5_MODE, LED5_RELAY });
#endif
#if LED6_PIN != GPIO_NONE
_leds.push_back((led_t) { LED6_PIN, LED6_PIN_INVERSE, LED6_MODE, LED6_RELAY });
#endif
#if LED7_PIN != GPIO_NONE
_leds.push_back((led_t) { LED7_PIN, LED7_PIN_INVERSE, LED7_MODE, LED7_RELAY });
#endif
#if LED8_PIN != GPIO_NONE
_leds.push_back((led_t) { LED8_PIN, LED8_PIN_INVERSE, LED8_MODE, LED8_RELAY });
#endif
for (unsigned int i=0; i < _leds.size(); i++) {
pinMode(_leds[i].pin, OUTPUT);
_ledStatus(i, false);
}
_ledConfigure();
#if MQTT_SUPPORT
mqttRegister(_ledMQTTCallback);
#endif
#if WEB_SUPPORT
wsOnSendRegister(_ledWebSocketOnSend);
wsOnAfterParseRegister(_ledConfigure);
#endif
DEBUG_MSG_P(PSTR("[LED] Number of leds: %d\n"), _leds.size());
// Registers
espurnaRegisterLoop(ledLoop);
settingsRegisterKeyCheck(_ledKeyCheck);
}
void ledLoop() {
void _ledLoop() {
uint8_t wifi_state = wifiState(); uint8_t wifi_state = wifiState();
@ -204,6 +146,8 @@ void ledLoop() {
_ledBlink(i, 500, 500); _ledBlink(i, 500, 500);
} }
continue;
} }
if (_ledMode(i) == LED_MODE_FINDME_WIFI) { if (_ledMode(i) == LED_MODE_FINDME_WIFI) {
@ -226,6 +170,8 @@ void ledLoop() {
_ledBlink(i, 500, 500); _ledBlink(i, 500, 500);
} }
continue;
} }
if (_ledMode(i) == LED_MODE_RELAY_WIFI) { if (_ledMode(i) == LED_MODE_RELAY_WIFI) {
@ -248,17 +194,25 @@ void ledLoop() {
_ledBlink(i, 500, 500); _ledBlink(i, 500, 500);
} }
continue;
} }
// Relay-based modes, update only if relays have been updated // Relay-based modes, update only if relays have been updated
if (!_led_update) continue; if (!_led_update) continue;
if (_ledMode(i) == LED_MODE_FOLLOW) { if (_ledMode(i) == LED_MODE_FOLLOW) {
_ledStatus(i, relayStatus(_leds[i].relay-1));
if (RELAY_NONE != _leds[i].relay) {
_ledStatus(i, relayStatus(_leds[i].relay));
}
continue;
} }
if (_ledMode(i) == LED_MODE_FOLLOW_INVERSE) { if (_ledMode(i) == LED_MODE_FOLLOW_INVERSE) {
_ledStatus(i, !relayStatus(_leds[i].relay-1));
if (RELAY_NONE != _leds[i].relay) {
_ledStatus(i, !relayStatus(_leds[i].relay));
}
continue;
} }
if (_ledMode(i) == LED_MODE_FINDME) { if (_ledMode(i) == LED_MODE_FINDME) {
@ -270,6 +224,7 @@ void ledLoop() {
} }
} }
_ledStatus(i, status); _ledStatus(i, status);
continue;
} }
if (_ledMode(i) == LED_MODE_RELAY) { if (_ledMode(i) == LED_MODE_RELAY) {
@ -281,14 +236,17 @@ void ledLoop() {
} }
} }
_ledStatus(i, status); _ledStatus(i, status);
continue;
} }
if (_ledMode(i) == LED_MODE_ON) { if (_ledMode(i) == LED_MODE_ON) {
_ledStatus(i, true); _ledStatus(i, true);
continue;
} }
if (_ledMode(i) == LED_MODE_OFF) { if (_ledMode(i) == LED_MODE_OFF) {
_ledStatus(i, false); _ledStatus(i, false);
continue;
} }
} }
@ -297,4 +255,46 @@ void ledLoop() {
} }
// -----------------------------------------------------------------------------
void ledUpdate(bool value) {
_led_update = value;
}
void ledSetup() {
unsigned char index = 0;
while (index < MAX_COMPONENTS) {
unsigned char pin = getSetting("ledGPIO", index, GPIO_NONE).toInt();
if (pin == GPIO_NONE) break;
bool inverse = getSetting("ledLogic", index, 0).toInt() == 1;
unsigned char mode = getSetting("ledMode", index, index==0 ? LED_MODE_WIFI : LED_MODE_MQTT).toInt();
unsigned char relayId = getSetting("ledRelay", index, RELAY_NONE).toInt();
_leds.push_back((led_t) { pin, inverse, mode, relayId });
pinMode(pin, OUTPUT);
_ledStatus(index, false);
++index;
}
#if MQTT_SUPPORT
mqttRegister(_ledMQTTCallback);
#endif
#if WEB_SUPPORT
wsOnSendRegister(_ledWebSocketOnSend);
wsOnAfterParseRegister(_ledConfigure);
#endif
DEBUG_MSG_P(PSTR("[LED] Number of leds: %d\n"), _leds.size());
// Registers
espurnaRegisterLoop(_ledLoop);
settingsRegisterKeyCheck(_ledKeyCheck);
}
#endif LED_SUPPORT #endif LED_SUPPORT

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

@ -51,7 +51,7 @@ void mdnsServerSetup() {
MDNS.addServiceTxt("arduino", "tcp", "app_name", APP_NAME); MDNS.addServiceTxt("arduino", "tcp", "app_name", APP_NAME);
MDNS.addServiceTxt("arduino", "tcp", "app_version", APP_VERSION); MDNS.addServiceTxt("arduino", "tcp", "app_version", APP_VERSION);
MDNS.addServiceTxt("arduino", "tcp", "mac", WiFi.macAddress()); MDNS.addServiceTxt("arduino", "tcp", "mac", WiFi.macAddress());
MDNS.addServiceTxt("arduino", "tcp", "target_board", getBoardName());
MDNS.addServiceTxt("arduino", "tcp", "target_board", getDevice());
{ {
char buffer[6] = {0}; char buffer[6] = {0};
itoa(ESP.getFlashChipRealSize() / 1024, buffer, 10); itoa(ESP.getFlashChipRealSize() / 1024, buffer, 10);


+ 10
- 1669
code/espurna/migrate.ino
File diff suppressed because it is too large
View File


+ 22
- 29
code/espurna/relay.ino View File

@ -907,39 +907,32 @@ void relaySetup() {
// Dummy relays for AI Light, Magic Home LED Controller, H801, // Dummy relays for AI Light, Magic Home LED Controller, H801,
// Sonoff Dual and Sonoff RF Bridge // Sonoff Dual and Sonoff RF Bridge
#if DUMMY_RELAY_COUNT > 0
unsigned int _delay_on[8] = {RELAY1_DELAY_ON, RELAY2_DELAY_ON, RELAY3_DELAY_ON, RELAY4_DELAY_ON, RELAY5_DELAY_ON, RELAY6_DELAY_ON, RELAY7_DELAY_ON, RELAY8_DELAY_ON};
unsigned int _delay_off[8] = {RELAY1_DELAY_OFF, RELAY2_DELAY_OFF, RELAY3_DELAY_OFF, RELAY4_DELAY_OFF, RELAY5_DELAY_OFF, RELAY6_DELAY_OFF, RELAY7_DELAY_OFF, RELAY8_DELAY_OFF};
for (unsigned char i=0; i < DUMMY_RELAY_COUNT; i++) {
_relays.push_back((relay_t) {0, RELAY_TYPE_NORMAL,0,_delay_on[i], _delay_off[i]});
#if DUMMY_RELAY_COUNT > 0 // TODO: this is yet hardcoded
for (unsigned char index=0; index < DUMMY_RELAY_COUNT; index++) {
unsigned long delay_on = getSetting("rlyDelayOn", index, 0).toInt();
unsigned long delay_off = getSetting("rlyDelayOff", index, 0).toInt();
_relays.push_back((relay_t) {0, RELAY_TYPE_NORMAL, 0, delay_on, delay_off});
} }
#else #else
#if RELAY1_PIN != GPIO_NONE
_relays.push_back((relay_t) { RELAY1_PIN, RELAY1_TYPE, RELAY1_RESET_PIN, RELAY1_DELAY_ON, RELAY1_DELAY_OFF });
#endif
#if RELAY2_PIN != GPIO_NONE
_relays.push_back((relay_t) { RELAY2_PIN, RELAY2_TYPE, RELAY2_RESET_PIN, RELAY2_DELAY_ON, RELAY2_DELAY_OFF });
#endif
#if RELAY3_PIN != GPIO_NONE
_relays.push_back((relay_t) { RELAY3_PIN, RELAY3_TYPE, RELAY3_RESET_PIN, RELAY3_DELAY_ON, RELAY3_DELAY_OFF });
#endif
#if RELAY4_PIN != GPIO_NONE
_relays.push_back((relay_t) { RELAY4_PIN, RELAY4_TYPE, RELAY4_RESET_PIN, RELAY4_DELAY_ON, RELAY4_DELAY_OFF });
#endif
#if RELAY5_PIN != GPIO_NONE
_relays.push_back((relay_t) { RELAY5_PIN, RELAY5_TYPE, RELAY5_RESET_PIN, RELAY5_DELAY_ON, RELAY5_DELAY_OFF });
#endif
#if RELAY6_PIN != GPIO_NONE
_relays.push_back((relay_t) { RELAY6_PIN, RELAY6_TYPE, RELAY6_RESET_PIN, RELAY6_DELAY_ON, RELAY6_DELAY_OFF });
#endif
#if RELAY7_PIN != GPIO_NONE
_relays.push_back((relay_t) { RELAY7_PIN, RELAY7_TYPE, RELAY7_RESET_PIN, RELAY7_DELAY_ON, RELAY7_DELAY_OFF });
#endif
#if RELAY8_PIN != GPIO_NONE
_relays.push_back((relay_t) { RELAY8_PIN, RELAY8_TYPE, RELAY8_RESET_PIN, RELAY8_DELAY_ON, RELAY8_DELAY_OFF });
#endif
unsigned char index = 0;
while (index < MAX_COMPONENTS) {
unsigned char pin = getSetting("rlyGPIO", index, GPIO_NONE).toInt();
if (GPIO_NONE == pin) break;
unsigned char type = getSetting("rlyType", index, 0).toInt();
unsigned char reset = getSetting("rlyResetGPIO", index, GPIO_NONE).toInt();
if (((type & RELAY_TYPE_LATCHED) == RELAY_TYPE_LATCHED) && (GPIO_NONE == reset)) break;
unsigned long delay_on = getSetting("rlyDelayOn", index, 0).toInt();
unsigned long delay_off = getSetting("rlyDelayOff", index, 0).toInt();
_relays.push_back((relay_t) { pin, type, reset, delay_on, delay_off });
++index;
}
#endif #endif


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

@ -56,7 +56,7 @@ void ssdpSetup() {
APP_NAME, // modelName APP_NAME, // modelName
APP_VERSION, // modelNumber APP_VERSION, // modelNumber
APP_WEBSITE, // modelURL APP_WEBSITE, // modelURL
DEVICE_NAME, // manufacturer
getDevice(), // manufacturer
"", // manufacturerURL "", // manufacturerURL
chipId // UUID chipId // UUID
); );


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

@ -34,14 +34,34 @@ String getPassword() {
return getSetting("adminPass", ADMIN_PASS); return getSetting("adminPass", ADMIN_PASS);
} }
void setBoardName() {
void loadBoard() {
#ifndef ESPURNA_CORE #ifndef ESPURNA_CORE
setSetting("boardName", DEVICE_NAME);
char buffer[device_config_len+1];
strncpy_P(buffer, (const char *) device_config, device_config_len);
buffer[device_config_len] = 0;
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buffer);
json["app"] = APP_NAME;
settingsRestoreJson(json);
#endif #endif
} }
String getManufacturer() {
String device = getDevice();
uint8_t pos = device.indexOf("_");
if (pos > 0) return device.substring(0, pos);
return String();
}
String getBoardName() { String getBoardName() {
return getSetting("boardName", DEVICE_NAME);
String device = getDevice();
uint8_t pos = device.indexOf("_");
if (pos > 0) return device.substring(pos+1);
return device;
}
String getDevice() {
return getSetting("device", "GENERIC_CUSTOM");
} }
String getCoreVersion() { String getCoreVersion() {


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

@ -67,7 +67,7 @@ void _onDiscover(AsyncWebServerRequest *request) {
root["app"] = APP_NAME; root["app"] = APP_NAME;
root["version"] = APP_VERSION; root["version"] = APP_VERSION;
root["hostname"] = getHostname(); root["hostname"] = getHostname();
root["device"] = getBoardName();
root["device"] = getDevice();
root.printTo(*response); root.printTo(*response);
request->send(response); request->send(response);


+ 2
- 2
code/espurna/ws.ino View File

@ -311,12 +311,12 @@ void _wsOnStart(JsonObject& root) {
root["app_version"] = APP_VERSION; root["app_version"] = APP_VERSION;
root["app_build"] = buildTime(); root["app_build"] = buildTime();
root["app_revision"] = APP_REVISION; root["app_revision"] = APP_REVISION;
root["manufacturer"] = MANUFACTURER;
root["manufacturer"] = getManufacturer();
root["chipid"] = String(chipid); root["chipid"] = String(chipid);
root["mac"] = WiFi.macAddress(); root["mac"] = WiFi.macAddress();
root["bssid"] = String(bssid_str); root["bssid"] = String(bssid_str);
root["channel"] = WiFi.channel(); root["channel"] = WiFi.channel();
root["device"] = DEVICE;
root["device"] = getBoardName();
root["hostname"] = getHostname(); root["hostname"] = getHostname();
root["network"] = getNetwork(); root["network"] = getNetwork();
root["deviceip"] = getIP(); root["deviceip"] = getIP();


+ 0
- 38
code/lib/readme.txt View File

@ -1,38 +0,0 @@
This directory is intended for the project specific (private) libraries.
PlatformIO will compile them to static libraries and link to executable file.
The source code of each library should be placed in separate directory, like
"lib/private_lib/[here are source files]".
For example, see how can be organized `Foo` and `Bar` libraries:
|--lib
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |- readme.txt --> THIS FILE
|- platformio.ini
|--src
|- main.c
Then in `src/main.c` you should use:
#include <Foo.h>
#include <Bar.h>
// rest H/C/CPP code
PlatformIO will find your libraries automatically, configure preprocessor's
include paths and build them.
See additional options for PlatformIO Library Dependency Finder `lib_*`:
http://docs.platformio.org/en/latest/projectconf.html#lib-install

Loading…
Cancel
Save