Inverted button support.

Config save/load fix.
This commit is contained in:
Blaž Kristan 2021-05-20 06:45:02 +02:00
parent c925b3d218
commit a0fd02e0c0
7 changed files with 58 additions and 32 deletions

View File

@ -20,11 +20,26 @@ void shortPressAction(uint8_t b)
bool isButtonPressed(uint8_t i)
{
if (btnPin[i]<0) return false;
//TODO: this may need switch statement (for inverted buttons)
#ifdef ARDUINO_ARCH_ESP32
if (buttonType[i]==BTN_TYPE_TOUCH && touchRead(btnPin[i]) <= touchThreshold) return true; else
#endif
if (digitalRead(btnPin[i]) == LOW) return true;
switch (buttonType[i]) {
case BTN_TYPE_NONE:
case BTN_TYPE_RESERVED:
break;
case BTN_TYPE_PUSH:
case BTN_TYPE_SWITCH:
if (digitalRead(btnPin[i]) == LOW) return true;
break;
case BTN_TYPE_PUSH_ACT_HIGH:
case BTN_TYPE_SWITCH_ACT_HIGH:
if (digitalRead(btnPin[i]) == HIGH) return true;
break;
case BTN_TYPE_TOUCH:
#ifdef ARDUINO_ARCH_ESP32
if (touchRead(btnPin[i]) <= touchThreshold) return true;
DEBUG_PRINT(F("Touch value: "));
DEBUG_PRINTLN(touchRead(btnPin[i]));
#endif
break;
}
return false;
}

View File

@ -161,9 +161,18 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
macroDoublePress[s] = 0;
}
} else {
//TODO: fix JSON API call (and new install)
// new install/missing configuration (button 0 has defaults)
if (fromFS)
for (uint8_t s=1; s<WLED_MAX_BUTTONS; s++) {
btnPin[s] = -1;
buttonType[s] = BTN_TYPE_NONE;
macroButton[s] = 0;
macroLongPress[s] = 0;
macroDoublePress[s] = 0;
}
}
CJSON(touchThreshold,hw[F("btn")][F("tt")]);
DEBUG_PRINTLN(F(" Done buttons."));
/*
JsonObject hw_btn_ins_0 = hw[F("btn")][F("ins")][0];
@ -546,8 +555,10 @@ void serializeConfig() {
// button(s)
JsonObject hw_btn = hw.createNestedObject("btn");
hw_btn["max"] = WLED_MAX_BUTTONS; // just information about max number of buttons (not actually used)
JsonArray hw_btn_ins = hw_btn.createNestedArray("ins");
// there is always at least one button
JsonObject hw_btn_ins_0 = hw_btn_ins.createNestedObject();
hw_btn_ins_0["type"] = buttonType[0];
JsonArray hw_btn_ins_0_pin = hw_btn_ins_0.createNestedArray("pin");
@ -559,13 +570,12 @@ void serializeConfig() {
// additional buttons
for (uint8_t i=1; i<WLED_MAX_BUTTONS; i++) {
if (btnPin[i]<0) continue;
JsonObject hw_btn_ins_0 = hw_btn_ins.createNestedObject();
//if (btnPin[i]<0) continue;
hw_btn_ins_0 = hw_btn_ins.createNestedObject();
hw_btn_ins_0["type"] = buttonType[i];
JsonArray hw_btn_ins_0_pin = hw_btn_ins_0.createNestedArray("pin");
hw_btn_ins_0_pin = hw_btn_ins_0.createNestedArray("pin");
hw_btn_ins_0_pin.add(btnPin[i]);
JsonArray hw_btn_ins_0_macros = hw_btn_ins_0.createNestedArray("macros");
hw_btn_ins_0_macros = hw_btn_ins_0.createNestedArray("macros");
hw_btn_ins_0_macros.add(macroButton[i]);
hw_btn_ins_0_macros.add(macroLongPress[i]);
hw_btn_ins_0_macros.add(macroDoublePress[i]);

View File

@ -291,7 +291,9 @@ Reverse (rotated 180°): <input type="checkbox" name="CV${i}">
c += `<select name="${be}">`
c += `<option value="0" ${t==0?"selected":""}>Disabled</option>`;
c += `<option value="2" ${t==2?"selected":""}>Pushbutton</option>`;
c += `<option value="2" ${t==3?"selected":""}>Push inverted</option>`;
c += `<option value="4" ${t==4?"selected":""}>Switch</option>`;
c += `<option value="5" ${t==4?"selected":""}>Switch inverted</option>`;
c += `<option value="6" ${t==6?"selected":""}>Touch</option>`;
c += `</select>`;
c += `<span style="cursor: pointer;" onclick="off('${bt}')">&nbsp;&#215;</span><br>`;

View File

@ -172,10 +172,10 @@
<table style="margin: 0 auto;" id="macros">
<thead>
<tr>
<td></td>
<td>push<br>switch</td>
<td>short<br>on-&gt;off</td>
<td>long<br>off-&gt;on</td>
<td>double</td>
<td>double<br>N/A</td>
</tr>
</thead>
<tbody>

File diff suppressed because one or more lines are too long

View File

@ -151,17 +151,15 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
for (uint8_t i=0; i<WLED_MAX_BUTTONS; i++) {
char bt[4] = "BT"; bt[2] = 48+i; bt[3] = 0; // button pin
char be[4] = "BE"; be[2] = 48+i; be[3] = 0; // button type
//if (request->hasArg(bt)) {
int hw_btn_pin = request->arg(bt).toInt();
if (pinManager.allocatePin(hw_btn_pin,false)) {
btnPin[i] = hw_btn_pin;
pinMode(btnPin[i], INPUT_PULLUP);
buttonType[i] = request->arg(be).toInt();
} else {
btnPin[i] = -1;
buttonType[i] = BTN_TYPE_NONE;
}
//}
int hw_btn_pin = request->arg(bt).toInt();
if (pinManager.allocatePin(hw_btn_pin,false)) {
btnPin[i] = hw_btn_pin;
pinMode(btnPin[i], INPUT_PULLUP);
buttonType[i] = request->arg(be).toInt();
} else {
btnPin[i] = -1;
buttonType[i] = BTN_TYPE_NONE;
}
}
touchThreshold = request->arg(F("TT")).toInt();
@ -342,7 +340,8 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
char mp[4] = "MP"; mp[2] = 48+i; mp[3] = 0; // short
char ml[4] = "ML"; ml[2] = 48+i; ml[3] = 0; // long
char md[4] = "MD"; md[2] = 48+i; md[3] = 0; // double
macroButton[i] = request->arg(mp).toInt();
//if (!request->hasArg(mp)) break;
macroButton[i] = request->arg(mp).toInt(); // these will default to 0 if not present
macroLongPress[i] = request->arg(ml).toInt();
macroDoublePress[i] = request->arg(md).toInt();
}

View File

@ -8,7 +8,7 @@
*/
// version code in format yymmddb (b = daily build)
#define VERSION 2105191
#define VERSION 2105201
//uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG