Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Dynamic settings (part 1) #1680

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 54 additions & 108 deletions code/espurna/button.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
typedef struct {
DebounceEvent * button;
unsigned long actions;
unsigned int relayID;
unsigned char relayID;
} button_t;

std::vector<button_t> _buttons;
Expand All @@ -42,7 +42,8 @@ bool _buttonWebSocketOnReceive(const char * key, JsonVariant& value) {

#endif

int buttonFromRelay(unsigned int relayID) {
int buttonFromRelay(unsigned char relayID) {
if (relayID == RELAY_NONE) return -1;
for (unsigned int i=0; i < _buttons.size(); i++) {
if (_buttons[i].relayID == relayID) return i;
}
Expand All @@ -66,17 +67,6 @@ unsigned char buttonAction(unsigned char id, unsigned char event) {
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;
return value;
}

uint8_t mapEvent(uint8_t event, uint8_t count, uint16_t length) {
if (event == EVENT_PRESSED) return BUTTON_EVENT_PRESSED;
if (event == EVENT_CHANGED) return BUTTON_EVENT_CLICK;
Expand Down Expand Up @@ -106,20 +96,20 @@ void buttonEvent(unsigned int id, unsigned char event) {
#endif

if (BUTTON_MODE_TOGGLE == action) {
if (_buttons[id].relayID > 0) {
relayToggle(_buttons[id].relayID - 1);
if (_buttons[id].relayID != RELAY_NONE) {
relayToggle(_buttons[id].relayID);
}
}

if (BUTTON_MODE_ON == action) {
if (_buttons[id].relayID > 0) {
relayStatus(_buttons[id].relayID - 1, true);
if (_buttons[id].relayID != RELAY_NONE) {
relayStatus(_buttons[id].relayID, true);
}
}

if (BUTTON_MODE_OFF == action) {
if (_buttons[id].relayID > 0) {
relayStatus(_buttons[id].relayID - 1, false);
if (_buttons[id].relayID != RELAY_NONE) {
relayStatus(_buttons[id].relayID, false);
}
}

Expand Down Expand Up @@ -162,95 +152,6 @@ void buttonEvent(unsigned int id, unsigned char event) {

}

void buttonSetup() {

#if defined(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});

#elif defined(FOXEL_LIGHTFOX_DUAL)

unsigned int actions = buttonStore(BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE);
unsigned int btn1Relay = getSetting("btnRelay", 0, BUTTON1_RELAY - 1).toInt() + 1;
_buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, btn1Relay});
unsigned int btn2Relay = getSetting("btnRelay", 1, BUTTON2_RELAY - 1).toInt() + 1;
_buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, btn2Relay});
unsigned int btn3Relay = getSetting("btnRelay", 2, BUTTON3_RELAY - 1).toInt() + 1;
_buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, btn3Relay});
unsigned int btn4Relay = getSetting("btnRelay", 3, BUTTON4_RELAY - 1).toInt() + 1;
_buttons.push_back({new DebounceEvent(0, BUTTON_PUSHBUTTON), actions, btn4Relay});

#else

unsigned long btnDelay = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt();
UNUSED(btnDelay);

#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
wsOnReceiveRegister(_buttonWebSocketOnReceive);
#endif

// Register loop
espurnaRegisterLoop(buttonLoop);

}

void buttonLoop() {

#if defined(ITEAD_SONOFF_DUAL)
Expand Down Expand Up @@ -331,4 +232,49 @@ void buttonLoop() {

}

// -----------------------------------------------------------------------------

void buttonSetup() {

// TODO v2: maybe this setting should be changed, btnDelay => btnClickDelay?
unsigned long btnDelay = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY).toInt();
UNUSED(btnDelay);

unsigned long btnDefaultActions = buttonStore(
BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE);
UNUSED(btnDefaultActions);

// TODO: max buttons / max_components from v2 ?
unsigned char index = 0;
while (index < 8) {

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 = getSetting("btnActions", index, btnDefaultActions).toInt();

// 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;

}

DEBUG_MSG_P(PSTR("[BUTTON] Number of buttons: %u\n"), _buttons.size());

// Websocket Callbacks
#if WEB_SUPPORT
wsOnReceiveRegister(_buttonWebSocketOnReceive);
#endif

// Register loop
espurnaRegisterLoop(buttonLoop);

}

#endif // BUTTON_SUPPORT
40 changes: 40 additions & 0 deletions code/espurna/config/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -574,3 +574,43 @@
#ifndef LIGHT_PROVIDER
#define LIGHT_PROVIDER LIGHT_PROVIDER_NONE
#endif

#ifndef LIGHT_CHANNELS
#define LIGHT_CHANNELS 0
#endif

#ifndef LIGHT_ENABLE_PIN
#define LIGHT_ENABLE_PIN GPIO_NONE
#endif

#ifndef LIGHT_CH1_PIN
#define LIGHT_CH1_PIN GPIO_NONE
#endif
#ifndef LIGHT_CH2_PIN
#define LIGHT_CH2_PIN GPIO_NONE
#endif
#ifndef LIGHT_CH3_PIN
#define LIGHT_CH3_PIN GPIO_NONE
#endif
#ifndef LIGHT_CH4_PIN
#define LIGHT_CH4_PIN GPIO_NONE
#endif
#ifndef LIGHT_CH5_PIN
#define LIGHT_CH5_PIN GPIO_NONE
#endif

#ifndef LIGHT_CH1_INVERSE
#define LIGHT_CH1_INVERSE 0
#endif
#ifndef LIGHT_CH2_INVERSE
#define LIGHT_CH2_INVERSE 0
#endif
#ifndef LIGHT_CH3_INVERSE
#define LIGHT_CH3_INVERSE 0
#endif
#ifndef LIGHT_CH4_INVERSE
#define LIGHT_CH4_INVERSE 0
#endif
#ifndef LIGHT_CH5_INVERSE
#define LIGHT_CH5_INVERSE 0
#endif
Loading