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

Single button dimmer control #1331

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
54 changes: 42 additions & 12 deletions code/espurna/button.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ typedef struct {
DebounceEvent * button;
unsigned long actions;
unsigned int relayID;
unsigned long lastEvent;
bool longPressSent;
} button_t;

std::vector<button_t> _buttons;
Expand Down Expand Up @@ -63,17 +65,19 @@ unsigned char buttonAction(unsigned char id, unsigned char event) {
if (event == BUTTON_EVENT_LNGCLICK) return (actions >> 12) & 0x0F;
if (event == BUTTON_EVENT_LNGLNGCLICK) return (actions >> 16) & 0x0F;
if (event == BUTTON_EVENT_TRIPLECLICK) return (actions >> 20) & 0x0F;
if (event == BUTTON_EVENT_LNGPRESS) return (actions >> 24) & 0x0F;
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 long buttonStore(unsigned long pressed, unsigned long click, unsigned long dblclick, unsigned long lngclick, unsigned long lnglngclick, unsigned long tripleclick, unsigned long lngpress) {
unsigned int value;
value = pressed;
value += click << 4;
value += dblclick << 8;
value += lngclick << 12;
value += lnglngclick << 16;
value += tripleclick << 20;
value += lngpress << 24;
return value;
}

Expand Down Expand Up @@ -120,6 +124,21 @@ void buttonEvent(unsigned int id, unsigned char event) {
relayStatus(_buttons[id].relayID - 1, false);
}
}
if (action == BUTTON_MODE_DIMMER_TOGGLE) {
if (_buttons[id].relayID > 0) {
relayToggleDimming(_buttons[id].relayID - 1);
}
}
if (action == BUTTON_MODE_DIMMER_START) {
if (_buttons[id].relayID > 0) {
relayStartDimming(_buttons[id].relayID - 1);
}
}
if (action == BUTTON_MODE_DIMMER_STOP) {
if (_buttons[id].relayID > 0) {
relayStopDimming(_buttons[id].relayID - 1);
}
}
if (action == BUTTON_MODE_AP) wifiStartAP();
#if defined(JUSTWIFI_ENABLE_WPS)
if (action == BUTTON_MODE_WPS) wifiStartWPS();
Expand All @@ -142,7 +161,7 @@ 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);
unsigned int actions = buttonStore(BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE, 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});
Expand All @@ -153,49 +172,49 @@ void buttonSetup() {

#if BUTTON1_PIN != GPIO_NONE
{
unsigned int actions = buttonStore(BUTTON1_PRESS, BUTTON1_CLICK, BUTTON1_DBLCLICK, BUTTON1_LNGCLICK, BUTTON1_LNGLNGCLICK, BUTTON1_TRIPLECLICK);
unsigned int actions = buttonStore(BUTTON1_PRESS, BUTTON1_CLICK, BUTTON1_DBLCLICK, BUTTON1_LNGCLICK, BUTTON1_LNGLNGCLICK, BUTTON1_TRIPLECLICK, BUTTON1_LNGPRESS);
_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);
unsigned int actions = buttonStore(BUTTON2_PRESS, BUTTON2_CLICK, BUTTON2_DBLCLICK, BUTTON2_LNGCLICK, BUTTON2_LNGLNGCLICK, BUTTON2_TRIPLECLICK, BUTTON2_LNGPRESS);
_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);
unsigned int actions = buttonStore(BUTTON3_PRESS, BUTTON3_CLICK, BUTTON3_DBLCLICK, BUTTON3_LNGCLICK, BUTTON3_LNGLNGCLICK, BUTTON3_TRIPLECLICK, BUTTON3_LNGPRESS);
_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);
unsigned int actions = buttonStore(BUTTON4_PRESS, BUTTON4_CLICK, BUTTON4_DBLCLICK, BUTTON4_LNGCLICK, BUTTON4_LNGLNGCLICK, BUTTON4_TRIPLECLICK, BUTTON4_LNGPRESS);
_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);
unsigned int actions = buttonStore(BUTTON5_PRESS, BUTTON5_CLICK, BUTTON5_DBLCLICK, BUTTON5_LNGCLICK, BUTTON5_LNGLNGCLICK, BUTTON5_TRIPLECLICK, BUTTON5_LNGPRESS);
_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);
unsigned int actions = buttonStore(BUTTON6_PRESS, BUTTON6_CLICK, BUTTON6_DBLCLICK, BUTTON6_LNGCLICK, BUTTON6_LNGLNGCLICK, BUTTON6_TRIPLECLICK, BUTTON6_LNGPRESS);
_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);
unsigned int actions = buttonStore(BUTTON7_PRESS, BUTTON7_CLICK, BUTTON7_DBLCLICK, BUTTON7_LNGCLICK, BUTTON7_LNGLNGCLICK, BUTTON7_TRIPLECLICK, BUTTON7_LNGPRESS);
_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);
unsigned int actions = buttonStore(BUTTON8_PRESS, BUTTON8_CLICK, BUTTON8_DBLCLICK, BUTTON8_LNGCLICK, BUTTON8_LNGLNGCLICK, BUTTON8_TRIPLECLICK, BUTTON8_LNGPRESS);
_buttons.push_back({new DebounceEvent(BUTTON8_PIN, BUTTON8_MODE, BUTTON_DEBOUNCE_DELAY, btnDelay), actions, BUTTON8_RELAY});
}
#endif
Expand Down Expand Up @@ -259,13 +278,24 @@ void buttonLoop() {
#else

for (unsigned int i=0; i < _buttons.size(); i++) {
if (unsigned char event = _buttons[i].button->loop()) {
unsigned char event = _buttons[i].button->loop();

if (event == EVENT_NONE) {
unsigned long length = millis() - _buttons[i].lastEvent;
bool state = _buttons[i].button->pressed();
if (!_buttons[i].longPressSent && state && length > BUTTON_LNGPRESS_DELAY) {
_buttons[i].longPressSent = true;
buttonEvent(i, BUTTON_EVENT_LNGPRESS);
}
} else {
_buttons[i].lastEvent = millis();
_buttons[i].longPressSent = false;
unsigned char count = _buttons[i].button->getEventCount();
unsigned long length = _buttons[i].button->getEventLength();
unsigned char mapped = mapEvent(event, count, length);
buttonEvent(i, mapped);
}
}
}

#endif

Expand Down
25 changes: 25 additions & 0 deletions code/espurna/config/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,31 @@
#define BUTTON8_LNGLNGCLICK BUTTON_MODE_NONE
#endif

#ifndef BUTTON1_LNGPRESS
#define BUTTON1_LNGPRESS BUTTON_MODE_NONE
#endif
#ifndef BUTTON2_LNGPRESS
#define BUTTON2_LNGPRESS BUTTON_MODE_NONE
#endif
#ifndef BUTTON3_LNGPRESS
#define BUTTON3_LNGPRESS BUTTON_MODE_NONE
#endif
#ifndef BUTTON4_LNGPRESS
#define BUTTON4_LNGPRESS BUTTON_MODE_NONE
#endif
#ifndef BUTTON5_LNGPRESS
#define BUTTON5_LNGPRESS BUTTON_MODE_NONE
#endif
#ifndef BUTTON6_LNGPRESS
#define BUTTON6_LNGPRESS BUTTON_MODE_NONE
#endif
#ifndef BUTTON7_LNGPRESS
#define BUTTON7_LNGPRESS BUTTON_MODE_NONE
#endif
#ifndef BUTTON8_LNGPRESS
#define BUTTON8_LNGPRESS BUTTON_MODE_NONE
#endif

#ifndef BUTTON1_RELAY
#define BUTTON1_RELAY 0
#endif
Expand Down
34 changes: 29 additions & 5 deletions code/espurna/config/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
//------------------------------------------------------------------------------

// UDP debug log
// To receive the message son the destination computer use nc:
// To receive the messages on the destination computer use nc:
// nc -ul 8113

#ifndef DEBUG_UDP_SUPPORT
Expand Down Expand Up @@ -237,9 +237,14 @@

#ifndef BUTTON_LNGLNGCLICK_DELAY
#define BUTTON_LNGLNGCLICK_DELAY 10000 // Time in ms holding the button down to get a long-long click
#endif

#ifndef BUTTON_LNGPRESS_DELAY
#define BUTTON_LNGPRESS_DELAY 1000 // Time in ms holding the button down to get a long click
#endif

#define BUTTON_MQTT_SEND_ALL_EVENTS 0 // 0 - to send only events the are bound to actions
// 1 - to send all button events to MQTT
#endif

//------------------------------------------------------------------------------
// ENCODER
Expand Down Expand Up @@ -835,7 +840,7 @@
#endif

#if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER
#define LIGHT_MAX_PWM 10000 // 10000 * 200ns => 2 kHz
#define LIGHT_MAX_PWM 25000 // 25000 * 200ns => 200 Hz
#endif

#endif // LIGHT_MAX_PWM
Expand All @@ -852,6 +857,10 @@
#define LIGHT_MAX_BRIGHTNESS 255 // Maximun brightness value
#endif

#ifndef LIGHT_MIN_DIMMING_BRIGHTNESS
#define LIGHT_MIN_DIMMING_BRIGHTNESS 5 // Maximun brightness value during dimming
#endif

#define LIGHT_MIN_MIREDS 153 // Default to the Philips Hue value that HA also use.
#define LIGHT_MAX_MIREDS 500 // https://developers.meethue.com/documentation/core-concepts

Expand All @@ -877,7 +886,11 @@
#define LIGHT_WARMWHITE_MIRED 500 // Warmwhite Strip, Value must be __ABOVE__ W1!! (Default: 2000 Kelvin/500 MiRed)

#ifndef LIGHT_USE_GAMMA
#define LIGHT_USE_GAMMA 0 // Use gamma correction for color channels
#define LIGHT_USE_GAMMA 0 // Use gamma correction (for single channel or for color channels in case of RGBxx)
#endif

#ifndef LIGHT_GAMMA
#define LIGHT_GAMMA 2.2 // Gamma correction to be used
#endif

#ifndef LIGHT_USE_CSS
Expand All @@ -899,13 +912,24 @@
#endif

#ifndef LIGHT_TRANSITION_STEP
#define LIGHT_TRANSITION_STEP 10 // Time in millis between each transtion step
#define LIGHT_TRANSITION_STEP 25 // Time in millis between each transtion step
#endif

#ifndef LIGHT_TRANSITION_TIME
#define LIGHT_TRANSITION_TIME 500 // Time in millis from color to color
#endif

#ifndef LIGHT_DIMMING_TIME
#define LIGHT_DIMMING_TIME 5000 // Time in millis for full scale dimming
#endif

#ifndef LIGHT_DIMMING_DIRECTION_TOGGLE
#define LIGHT_DIMMING_DIRECTION_TOGGLE 1 // dimming direction is 1: toggled, 0: up if < 50%, down of >= 50%
#endif

#ifndef LIGHT_DIMMING_CYCLE
#define LIGHT_DIMMING_CYCLE 1 // dimming cycles
#endif

// -----------------------------------------------------------------------------
// DOMOTICZ
Expand Down
4 changes: 4 additions & 0 deletions code/espurna/config/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define BUTTON_EVENT_LNGCLICK 4
#define BUTTON_EVENT_LNGLNGCLICK 5
#define BUTTON_EVENT_TRIPLECLICK 6
#define BUTTON_EVENT_LNGPRESS 7

#define BUTTON_MODE_NONE 0
#define BUTTON_MODE_TOGGLE 1
Expand All @@ -39,6 +40,9 @@
#define BUTTON_MODE_FACTORY 7
#define BUTTON_MODE_WPS 8
#define BUTTON_MODE_SMART_CONFIG 9
#define BUTTON_MODE_DIMMER_TOGGLE 10
#define BUTTON_MODE_DIMMER_START 11
#define BUTTON_MODE_DIMMER_STOP 12

// Needed for ESP8285 boards under Windows using PlatformIO (?)
#ifndef BUTTON_PUSHBUTTON
Expand Down
Binary file modified code/espurna/data/index.all.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.light.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.rfbridge.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.rfm69.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.sensor.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.small.html.gz
Binary file not shown.
Loading