Browse Source

Map button events to actions

fastled
Xose Pérez 7 years ago
parent
commit
6d86a7108a
3 changed files with 147 additions and 24 deletions
  1. +57
    -22
      code/espurna/button.ino
  2. +18
    -0
      code/espurna/config/general.h
  3. +72
    -2
      code/espurna/config/hardware.h

+ 57
- 22
code/espurna/button.ino View File

@ -18,7 +18,6 @@ void buttonMQTT(unsigned char id) {
char buffer[strlen(MQTT_BUTTON_TOPIC) + mqttGetter.length() + 3];
sprintf(buffer, "%s/%d%s", MQTT_BUTTON_TOPIC, id, mqttGetter.c_str());
mqttSend(buffer, "1");
mqttSend(buffer, "0");
}
#endif
@ -76,34 +75,69 @@ void buttonLoop() {
typedef struct {
DebounceEvent * button;
unsigned int actions;
unsigned int relayID;
} button_t;
std::vector<button_t> _buttons;
#ifdef MQTT_BUTTON_TOPIC
void buttonMQTT(unsigned char id, const char * payload) {
void buttonMQTT(unsigned char id, uint8_t event) {
if (id >= _buttons.size()) return;
String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
char buffer[strlen(MQTT_BUTTON_TOPIC) + mqttGetter.length() + 3];
sprintf(buffer, "%s/%d%s", MQTT_BUTTON_TOPIC, id, mqttGetter.c_str());
char payload[2];
sprintf(payload, "%d", event);
mqttSend(buffer, payload);
}
#endif
unsigned char buttonAction(unsigned char id, unsigned char event) {
if (id >= _buttons.size()) return BUTTON_MODE_NONE;
unsigned int actions = _buttons[id].actions;
if (event == BUTTON_EVENT_PRESSED) return (actions >> 12) & 0x0F;
if (event == BUTTON_EVENT_CLICK) return (actions >> 8) & 0x0F;
if (event == BUTTON_EVENT_DBLCLICK) return (actions >> 4) & 0x0F;
if (event == BUTTON_EVENT_LNGCLICK) return (actions) & 0x0F;
return BUTTON_MODE_NONE;
}
unsigned int buttonStore(unsigned char pressed, unsigned char click, unsigned char dblclick, unsigned char lngclick) {
unsigned int value;
value = pressed << 12;
value += click << 8;
value += dblclick << 4;
value += lngclick;
return value;
}
void buttonSetup() {
#ifdef BUTTON1_PIN
_buttons.push_back({new DebounceEvent(BUTTON1_PIN, BUTTON1_MODE), BUTTON1_RELAY});
{
unsigned int actions = buttonStore(BUTTON1_PRESS, BUTTON1_CLICK, BUTTON1_DBLCLICK, BUTTON1_LNGCLICK);
_buttons.push_back({new DebounceEvent(BUTTON1_PIN, BUTTON1_MODE), actions, BUTTON1_RELAY});
}
#endif
#ifdef BUTTON2_PIN
_buttons.push_back({new DebounceEvent(BUTTON2_PIN, BUTTON2_MODE), BUTTON2_RELAY});
{
unsigned int actions = buttonStore(BUTTON2_PRESS, BUTTON2_CLICK, BUTTON2_DBLCLICK, BUTTON2_LNGCLICK);
_buttons.push_back({new DebounceEvent(BUTTON2_PIN, BUTTON2_MODE), actions, BUTTON2_RELAY});
}
#endif
#ifdef BUTTON3_PIN
_buttons.push_back({new DebounceEvent(BUTTON3_PIN, BUTTON3_MODE), BUTTON3_RELAY});
{
unsigned int actions = buttonStore(BUTTON3_PRESS, BUTTON3_CLICK, BUTTON3_DBLCLICK, BUTTON3_LNGCLICK);
_buttons.push_back({new DebounceEvent(BUTTON3_PIN, BUTTON3_MODE), actions, BUTTON3_RELAY});
}
#endif
#ifdef BUTTON4_PIN
_buttons.push_back({new DebounceEvent(BUTTON4_PIN, BUTTON4_MODE), BUTTON4_RELAY});
{
unsigned int actions = buttonStore(BUTTON4_PRESS, BUTTON4_CLICK, BUTTON4_DBLCLICK, BUTTON4_LNGCLICK);
_buttons.push_back({new DebounceEvent(BUTTON4_PIN, BUTTON4_MODE), actions, BUTTON4_RELAY});
}
#endif
#ifdef LED_PULSE
@ -116,37 +150,38 @@ void buttonSetup() {
}
uint8_t mapEvent(uint8_t event) {
if (event == EVENT_PRESSED) return BUTTON_EVENT_PRESSED;
if (event == EVENT_CHANGED) return BUTTON_EVENT_CLICK;
if (event == EVENT_SINGLE_CLICK) return BUTTON_EVENT_CLICK;
if (event == EVENT_DOUBLE_CLICK) return BUTTON_EVENT_DBLCLICK;
if (event == EVENT_LONG_CLICK) return BUTTON_EVENT_LNGCLICK;
return BUTTON_EVENT_NONE;
}
void buttonLoop() {
for (unsigned int i=0; i < _buttons.size(); i++) {
if (_buttons[i].button->loop()) {
uint8_t event = _buttons[i].button->getEvent();
uint8_t event = mapEvent(_buttons[i].button->getEvent());
DEBUG_MSG("[BUTTON] Pressed #%d, event: %d\n", i, event);
if (event == 0) continue;
#ifdef MQTT_BUTTON_TOPIC
buttonMQTT(i, (event == EVENT_CHANGED || event == EVENT_PRESSED) ? "1" : "0");
buttonMQTT(i, event);
#endif
if (i == 0) {
if (event == EVENT_DOUBLE_CLICK) createAP();
if (event == EVENT_LONG_CLICK) ESP.reset();
}
#ifdef ITEAD_1CH_INCHING
if (i == 1) {
relayPulseToggle();
continue;
}
#endif
unsigned char action = buttonAction(i, event);
// Here we can have EVENT_CHANGED only when using BUTTON_SWITCH
// and EVENT_SINGLE_CLICK only when using BUTTON_PUSHBUTTON
if (event == EVENT_SINGLE_CLICK || event == EVENT_CHANGED) {
if (action == BUTTON_MODE_TOGGLE) {
if (_buttons[i].relayID > 0) {
relayToggle(_buttons[i].relayID - 1);
}
}
if (action == BUTTON_MODE_AP) createAP();
if (action == BUTTON_MODE_RESET) ESP.reset();
if (action == BUTTON_MODE_PULSE) relayPulseToggle();
}
}


+ 18
- 0
code/espurna/config/general.h View File

@ -14,6 +14,24 @@
#define EEPROM_RELAY_STATUS 0
#define EEPROM_ENERGY_COUNT 1
//--------------------------------------------------------------------------------
// BUTTON
//--------------------------------------------------------------------------------
#define BUTTON_EVENT_NONE 0
#define BUTTON_EVENT_PRESSED 1
#define BUTTON_EVENT_CLICK 2
#define BUTTON_EVENT_DBLCLICK 3
#define BUTTON_EVENT_LNGCLICK 4
#define BUTTON_MODE_NONE 0
#define BUTTON_MODE_TOGGLE 1
#define BUTTON_MODE_AP 2
#define BUTTON_MODE_RESET 3
#define BUTTON_MODE_PULSE 4
#define BUTTON_DEFAULT_MODE BUTTON_MODE_TOGGLE
//--------------------------------------------------------------------------------
// RELAY
//--------------------------------------------------------------------------------


+ 72
- 2
code/espurna/config/hardware.h View File

@ -26,7 +26,8 @@
#define DEVICE "LOLIN"
#define BUTTON1_PIN 0
#define BUTTON1_RELAY 1
#define BUTTON1_MODE BUTTON_SWITCH | BUTTON_DEFAULT_HIGH
#define BUTTON1_LNGCLICK BUTTON_MODE_PULSE
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0
#define LED1_PIN 2
@ -179,7 +180,7 @@
#define BUTTON1_RELAY 1
#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define BUTTON2_PIN 15
#define BUTTON2_RELAY 0
#define BUTTON2_CLICK BUTTON_MODE_PULSE
#define BUTTON2_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define RELAY1_PIN 12
#define RELAY1_PIN_INVERSE 0
@ -298,3 +299,72 @@
#else
#error "UNSUPPORTED HARDWARE!"
#endif
// -----------------------------------------------------------------------------
// Default values
// -----------------------------------------------------------------------------
#ifndef BUTTON1_PRESS
#define BUTTON1_PRESS BUTTON_MODE_NONE
#endif
#ifndef BUTTON2_PRESS
#define BUTTON2_PRESS BUTTON_MODE_NONE
#endif
#ifndef BUTTON3_PRESS
#define BUTTON3_PRESS BUTTON_MODE_NONE
#endif
#ifndef BUTTON4_PRESS
#define BUTTON4_PRESS BUTTON_MODE_NONE
#endif
#ifndef BUTTON1_CLICK
#define BUTTON1_CLICK BUTTON_MODE_TOGGLE
#endif
#ifndef BUTTON2_CLICK
#define BUTTON2_CLICK BUTTON_MODE_TOGGLE
#endif
#ifndef BUTTON3_CLICK
#define BUTTON3_CLICK BUTTON_MODE_TOGGLE
#endif
#ifndef BUTTON4_CLICK
#define BUTTON4_CLICK BUTTON_MODE_TOGGLE
#endif
#ifndef BUTTON1_DBLCLICK
#define BUTTON1_DBLCLICK BUTTON_MODE_AP
#endif
#ifndef BUTTON2_DBLCLICK
#define BUTTON2_DBLCLICK BUTTON_MODE_AP
#endif
#ifndef BUTTON3_DBLCLICK
#define BUTTON3_DBLCLICK BUTTON_MODE_AP
#endif
#ifndef BUTTON4_DBLCLICK
#define BUTTON4_DBLCLICK BUTTON_MODE_AP
#endif
#ifndef BUTTON1_LNGCLICK
#define BUTTON1_LNGCLICK BUTTON_MODE_RESET
#endif
#ifndef BUTTON2_LNGCLICK
#define BUTTON2_LNGCLICK BUTTON_MODE_RESET
#endif
#ifndef BUTTON3_LNGCLICK
#define BUTTON3_LNGCLICK BUTTON_MODE_RESET
#endif
#ifndef BUTTON4_LNGCLICK
#define BUTTON4_LNGCLICK BUTTON_MODE_RESET
#endif
#ifndef BUTTON1_RELAY
#define BUTTON1_RELAY 0
#endif
#ifndef BUTTON2_RELAY
#define BUTTON2_RELAY 0
#endif
#ifndef BUTTON3_RELAY
#define BUTTON3_RELAY 0
#endif
#ifndef BUTTON4_RELAY
#define BUTTON4_RELAY 0
#endif

Loading…
Cancel
Save