Added button double press macro option

Added toggle (relay) pin
This commit is contained in:
cschwinne 2019-03-13 11:13:03 +01:00
parent 46e4350013
commit d4bf1cb23d
10 changed files with 104 additions and 46 deletions

View File

@ -6,7 +6,11 @@
#define LEDPIN 2 //strip pin. Any for ESP32, gpio2 or 3 is recommended for ESP8266 (gpio2/3 are labeled D4/RX on NodeMCU and Wemos) #define LEDPIN 2 //strip pin. Any for ESP32, gpio2 or 3 is recommended for ESP8266 (gpio2/3 are labeled D4/RX on NodeMCU and Wemos)
#define BTNPIN 0 //button pin. Needs to have pullup (gpio0 recommended) #define BTNPIN 0 //button pin. Needs to have pullup (gpio0 recommended)
#define IR_PIN 4 //infrared pin (-1 to disable) #define IR_PIN 4 //infrared pin (-1 to disable)
#define AUXPIN -1 //unused auxiliary output pin (-1 to disable) #define RLYPIN 12 //pin for relay, will be set HIGH if LEDs are on (-1 to disable). Also usable for standby leds, triggers,...
#define AUXPIN -1 //debug auxiliary output pin (-1 to disable)
#define RLYMDE 1 //mode for relay, 0: LOW if LEDs are on 1: HIGH if LEDs are on
//automatically uses the right driver method for each platform //automatically uses the right driver method for each platform
#ifdef ARDUINO_ARCH_ESP32 #ifdef ARDUINO_ARCH_ESP32

View File

@ -248,7 +248,7 @@
.whs input[type=range]::-moz-range-track{ .whs input[type=range]::-moz-range-track{
background: linear-gradient(45deg,black,white); background: linear-gradient(45deg,black,white);
} }
#cv { .ol {
position:fixed; position:fixed;
height:100%; height:100%;
width:100%; width:100%;
@ -268,7 +268,8 @@
</head> </head>
<body> <body>
<main style="max-width:280px; margin-left:auto; margin-right:auto; text-align:center;"> <main style="max-width:280px; margin-left:auto; margin-right:auto; text-align:center;">
<div id="cv">Loading WLED UI...</div> <div id="cv" class="ol">Loading WLED UI...</div>
<noscript><div class="ol" style="opacity:1;">Sorry, the WLED UI needs JavaScript to work!</div></noscript>
<div style="width:100%; max-width:260px; margin-left:auto; margin-right:auto; font-size:12px; margin-bottom:20px;"> <div style="width:100%; max-width:260px; margin-left:auto; margin-right:auto; font-size:12px; margin-bottom:20px;">
<div class="btns" style="width:70%; float:left; text-align:left;"> <div class="btns" style="width:70%; float:left; text-align:left;">
<p class="desc">Loading...</p> <p class="desc">Loading...</p>

View File

@ -350,12 +350,13 @@ Define API macros here:<br>
15: <input name="M15" maxlength="64"><br> 15: <input name="M15" maxlength="64"><br>
16: <input name="M16" maxlength="64"><br><br> 16: <input name="M16" maxlength="64"><br><br>
<i>Use 0 for the default action instead of a macro</i><br> <i>Use 0 for the default action instead of a macro</i><br>
Boot Macro: <input name="MB" type="number" min="0" max="16" required><br> Boot macro: <input name="MB" type="number" min="0" max="16" required><br>
Alexa On/Off Macros: <input name="A0" type="number" min="0" max="16" required> <input name="A1" type="number" min="0" max="16" required><br> Alexa On/Off macros: <input name="A0" type="number" min="0" max="16" required> <input name="A1" type="number" min="0" max="16" required><br>
Button Macro: <input name="MP" type="number" min="0" max="16" required> Long Press: <input name="ML" type="number" min="0" max="16" required><br> Button short press macro: <input name="MP" type="number" min="0" max="16" required><br>
Countdown-Over Macro: <input name="MC" type="number" min="0" max="16" required><br> Long press: <input name="ML" type="number" min="0" max="16" required> Double press: <input name="MD" type="number" min="0" max="16" required><br>
Timed-Light-Over Macro: <input name="MN" type="number" min="0" max="16" required><br> Countdown-Over macro: <input name="MC" type="number" min="0" max="16" required><br>
Time-Controlled Macros:<br> Timed-Light-Over macro: <input name="MN" type="number" min="0" max="16" required><br>
Time-Controlled macros:<br>
<div style="display: inline-block"> <div style="display: inline-block">
<table id="TMT"> <table id="TMT">
</table></div><hr> </table></div><hr>

View File

@ -89,7 +89,7 @@
//version code in format yymmddb (b = daily build) //version code in format yymmddb (b = daily build)
#define VERSION 1903112 #define VERSION 1903131
char versionString[] = "0.8.4-dev"; char versionString[] = "0.8.4-dev";
@ -236,7 +236,7 @@ byte macroBoot = 0; //macro loaded after startup
byte macroNl = 0; //after nightlight delay over byte macroNl = 0; //after nightlight delay over
byte macroCountdown = 0; byte macroCountdown = 0;
byte macroAlexaOn = 0, macroAlexaOff = 0; byte macroAlexaOn = 0, macroAlexaOff = 0;
byte macroButton = 0, macroLongPress = 0; byte macroButton = 0, macroLongPress = 0, macroDoublePress = 0;
//Security CONFIG //Security CONFIG
@ -278,6 +278,7 @@ byte briNlT = 0; //current nightlight brightness
//brightness //brightness
unsigned long lastOnTime = 0; unsigned long lastOnTime = 0;
bool offMode = !turnOnAtBoot;
byte bri = briS; byte bri = briS;
byte briOld = 0; byte briOld = 0;
byte briT = 0; byte briT = 0;
@ -287,7 +288,7 @@ byte briLast = 127; //brightness before turned off. Us
//button //button
bool buttonPressedBefore = false; bool buttonPressedBefore = false;
unsigned long buttonPressedTime = 0; unsigned long buttonPressedTime = 0;
unsigned long buttonReleasedTime = 0; unsigned long buttonWaitTime = 0;
//notifications //notifications
bool notifyDirectDefault = notifyDirect; bool notifyDirectDefault = notifyDirect;
@ -536,8 +537,7 @@ void loop() {
handleBlynk(); handleBlynk();
} }
yield(); yield();
if (briT) lastOnTime = millis(); if (!offMode) strip.service();
if (millis() - lastOnTime < 600) strip.service();
} }
//DEBUG serial logging //DEBUG serial logging

View File

@ -3,7 +3,7 @@
* EEPROM Map: https://github.com/Aircoookie/WLED/wiki/EEPROM-Map * EEPROM Map: https://github.com/Aircoookie/WLED/wiki/EEPROM-Map
*/ */
#define EEPSIZE 3072 #define EEPSIZE 2560
//eeprom Version code, enables default settings instead of 0 init on update //eeprom Version code, enables default settings instead of 0 init on update
#define EEPVER 10 #define EEPVER 10
@ -214,6 +214,7 @@ void saveSettingsToEEPROM()
EEPROM.write(2179, macroLongPress); EEPROM.write(2179, macroLongPress);
EEPROM.write(2180, macroCountdown); EEPROM.write(2180, macroCountdown);
EEPROM.write(2181, macroNl); EEPROM.write(2181, macroNl);
EEPROM.write(2182, macroDoublePress);
EEPROM.write(2190, e131Universe & 0xFF); EEPROM.write(2190, e131Universe & 0xFF);
EEPROM.write(2191, (e131Universe >> 8) & 0xFF); EEPROM.write(2191, (e131Universe >> 8) & 0xFF);
@ -423,6 +424,8 @@ void loadSettingsFromEEPROM(bool first)
macroLongPress = EEPROM.read(2179); macroLongPress = EEPROM.read(2179);
macroCountdown = EEPROM.read(2180); macroCountdown = EEPROM.read(2180);
macroNl = EEPROM.read(2181); macroNl = EEPROM.read(2181);
macroDoublePress = EEPROM.read(2182);
if (macroDoublePress > 16) macroDoublePress = 0;
} }
if (lastEEPROMversion > 6) if (lastEEPROMversion > 6)

View File

@ -359,6 +359,7 @@ char* getSettingsJS(byte subPage)
sappend('v',"ML",macroLongPress); sappend('v',"ML",macroLongPress);
sappend('v',"MC",macroCountdown); sappend('v',"MC",macroCountdown);
sappend('v',"MN",macroNl); sappend('v',"MN",macroNl);
sappend('v',"MD",macroDoublePress);
k[2] = 0; //Time macros k[2] = 0; //Time macros
for (int i = 0; i<8; i++) for (int i = 0; i<8; i++)

View File

@ -250,6 +250,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
macroLongPress = request->arg("ML").toInt(); macroLongPress = request->arg("ML").toInt();
macroCountdown = request->arg("MC").toInt(); macroCountdown = request->arg("MC").toInt();
macroNl = request->arg("MN").toInt(); macroNl = request->arg("MN").toInt();
macroDoublePress = request->arg("MD").toInt();
char k[3]; k[2] = 0; char k[3]; k[2] = 0;
for (int i = 0; i<8; i++) for (int i = 0; i<8; i++)

View File

@ -156,6 +156,16 @@ void beginStrip()
if (bootPreset>0) applyPreset(bootPreset, turnOnAtBoot, true, true); if (bootPreset>0) applyPreset(bootPreset, turnOnAtBoot, true, true);
colorUpdated(0); colorUpdated(0);
//init relay pin
#if RLYPIN >= 0
pinMode(RLYPIN, OUTPUT);
#if RLYMDE
digitalWrite(RLYPIN, bri);
#else
digitalWrite(RLYPIN, !bri);
#endif
#endif
//disable button if it is "pressed" unintentionally //disable button if it is "pressed" unintentionally
if(digitalRead(BTNPIN) == LOW) buttonEnabled = false; if(digitalRead(BTNPIN) == LOW) buttonEnabled = false;
} }

View File

@ -1,7 +1,8 @@
/* /*
* This file allows you to add own functionality to WLED more easily * This file allows you to add own functionality to WLED more easily
* See: https://github.com/Aircoookie/WLED/wiki/Add-own-functionality * See: https://github.com/Aircoookie/WLED/wiki/Add-own-functionality
* EEPROM bytes 2944 to 3071 are reserved for your custom use case. * EEPROM bytes 2750+ are reserved for your custom use case. (if you extend #define EEPSIZE in wled01_eeprom.h)
* bytes 2400+ are currently ununsed, but might be used for future wled features
*/ */
//Use userVar0 and userVar1 (API calls &U0=,&U1=, uint16_t) //Use userVar0 and userVar1 (API calls &U0=,&U1=, uint16_t)

View File

@ -2,30 +2,9 @@
* Physical IO * Physical IO
*/ */
void handleButton() void shortPressAction()
{ {
if (buttonEnabled && millis() - buttonReleasedTime > 20) //debounce if (!macroButton)
{
if (digitalRead(BTNPIN) == LOW && !buttonPressedBefore) //pressed
{
buttonPressedTime = millis();
buttonPressedBefore = true;
}
else if (digitalRead(BTNPIN) == HIGH && buttonPressedBefore) //released
{
if (buttonReleasedTime == 0) {
buttonReleasedTime = millis();
} else {
if (digitalRead(BTNPIN) == HIGH)
{
if (buttonReleasedTime - buttonPressedTime > 7000) {initAP();}
else if (buttonReleasedTime - buttonPressedTime > 700)
{
if (macroLongPress != 0) {applyMacro(macroLongPress);}
else _setRandomColor(false,true);
}
else {
if (macroButton == 0)
{ {
toggleOnOff(); toggleOnOff();
colorUpdated(2); colorUpdated(2);
@ -33,11 +12,68 @@ void handleButton()
applyMacro(macroButton); applyMacro(macroButton);
} }
} }
void handleButton()
{
if (!buttonEnabled) return;
if (digitalRead(BTNPIN) == LOW && !buttonPressedBefore) //pressed
{
buttonPressedTime = millis();
buttonPressedBefore = true;
}
else if (digitalRead(BTNPIN) == HIGH && buttonPressedBefore) //released
{
long dur = millis() - buttonPressedTime;
if (dur < 50) {buttonPressedBefore = false; return;} //too short "press", debounce
bool doublePress = buttonWaitTime;
buttonWaitTime = 0;
if (dur > 6000) {initAP();}
else if (dur > 600) //long press
{
if (macroLongPress) {applyMacro(macroLongPress);}
else _setRandomColor(false,true);
}
else { //short press
if (macroDoublePress)
{
if (doublePress) applyMacro(macroDoublePress);
else buttonWaitTime = millis();
} else shortPressAction();
}
buttonPressedBefore = false; buttonPressedBefore = false;
} }
buttonReleasedTime = 0;
if (buttonWaitTime && millis() - buttonWaitTime > 450 && !buttonPressedBefore)
{
buttonWaitTime = 0;
shortPressAction();
} }
} }
void handleIO()
{
handleButton();
//set relay when LEDs turn on
if (strip.getBrightness())
{
lastOnTime = millis();
if (offMode)
{
#if RLYPIN >= 0
digitalWrite(RLYPIN, RLYMDE);
#endif
offMode = false;
}
} else if (millis() - lastOnTime > 600)
{
#if RLYPIN >= 0
if (!offMode) digitalWrite(RLYPIN, !RLYMDE);
#endif
offMode = true;
} }
#if AUXPIN >= 0 #if AUXPIN >= 0