Skip to content

Commit 78a043e

Browse files
committed
fixing toggles over web/sockets
toggle updates SHOULD not do default action before update or we can have pulses on the output with a toggle preceeding an update. This is an exception to the event schema. Toggle update is trigering an updateEvent right after. This happens because we never "enter" a toggle but we need to skip entering default action (toggle) when updating.
1 parent bd438c0 commit 78a043e

File tree

13 files changed

+94
-56
lines changed

13 files changed

+94
-56
lines changed

examples/esp8266/WebMenu/WebMenu/WebMenu.ino

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,23 @@ menuOut& operator<<(menuOut& o,endlObj) {
6060
}
6161

6262
//this version numbers MUST be the same as data/1.2
63-
#define CUR_VERSION "1.3"
63+
#define CUR_VERSION "1.4"
6464
#define APName "WebMenu"
65+
66+
int ledCtrl=LOW;
67+
//on my esp12e led pin is 2
68+
#define LEDPIN 2
69+
//this is ok on other boards
70+
// #define LEDPIN LED_BUILTIN
71+
void updLed() {
72+
_trace(Serial<<"update led state!"<<endl);
73+
digitalWrite(LEDPIN,!ledCtrl);
74+
}
75+
6576
#define ANALOG_PIN 4
6677

67-
constexpr size_t wifiSSIDLen=64;
68-
constexpr size_t wifiPwdLen=32;
78+
// constexpr size_t wifiSSIDLen=64;
79+
// constexpr size_t wifiPwdLen=32;
6980

7081
#ifndef MENU_SSID
7182
#error "need to define WiFi SSID here"
@@ -76,8 +87,8 @@ constexpr size_t wifiPwdLen=32;
7687
#define MENU_PASS ""
7788
#endif
7889

79-
char wifiSSID[wifiSSIDLen+1];//=" ";
80-
char wifiPwd [wifiPwdLen+1];//=" ";
90+
// char wifiSSID[wifiSSIDLen+1];//=" ";
91+
// char wifiPwd [wifiPwdLen+1];//=" ";
8192

8293
const char* ssid = MENU_SSID;
8394
const char* password = MENU_PASS;
@@ -108,15 +119,13 @@ result action2(eventMask event, navNode& nav, prompt &item) {
108119
return proceed;
109120
}
110121

111-
int ledCtrl=LOW;
112-
#define LEDPIN LED_BUILTIN
113-
void updLed() {
114-
digitalWrite(LEDPIN,!ledCtrl);
122+
void debugLedUpd() {
123+
_trace(Serial<<"debug led update! "<<ledCtrl<<endl);
115124
}
116125

117-
TOGGLE(ledCtrl,setLed,"Led: ",updLed,enterEvent,noStyle//,doExit,enterEvent,noStyle
118-
,VALUE("On",HIGH,doNothing,noEvent)
126+
TOGGLE(ledCtrl,setLed,"Led: ",updLed,(Menu::eventMask)(updateEvent|enterEvent),noStyle
119127
,VALUE("Off",LOW,doNothing,noEvent)
128+
,VALUE("On",HIGH,doNothing,noEvent)
120129
);
121130

122131
int selTest=0;
@@ -138,6 +147,7 @@ int duty=50;//%
138147
int timeOn=50;//ms
139148
void updAnalog() {
140149
// analogWrite(ANALOG_PIN,map(timeOn,0,100,0,255/*PWMRANGE*/));
150+
analogWrite(ANALOG_PIN,map(duty,0,100,0,255/*PWMRANGE*/));
141151
}
142152

143153
char* constMEM alphaNum MEMMODE=" 0123456789.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,\\|!\"#$%&/()=?~*^+-{}[]€";
@@ -182,7 +192,7 @@ MENU(mainMenu,"Main menu",doNothing,noEvent,wrapStyle
182192
,OP("Action A",action1,enterEvent)
183193
,OP("Action B",action2,enterEvent)
184194
,FIELD(duty,"Duty","%",0,100,10,1, updAnalog, anyEvent, noStyle)
185-
,FIELD(timeOn,"On","ms",0,100,10,1, updAnalog, anyEvent, noStyle)
195+
// ,FIELD(timeOn,"On","ms",0,100,10,1, updAnalog, anyEvent, noStyle)
186196
,EDIT("Name",name,alphaNumMask,doNothing,noEvent,noStyle)
187197
,SUBMENU(birthDate)
188198
,SUBMENU(selMenu)
@@ -353,8 +363,8 @@ void setup(){
353363
pinMode(LEDPIN,OUTPUT);
354364
updLed();
355365
// analogWriteRange(1023);
356-
// pinMode(ANALOG_PIN,OUTPUT);
357-
// updAnalog();
366+
pinMode(ANALOG_PIN,OUTPUT);
367+
updAnalog();
358368
//options=&myOptions;//menu options
359369
Serial.begin(115200);
360370
while(!Serial)

examples/esp8266/WebMenu/WebMenu/data/1.3/r-site.js renamed to examples/esp8266/WebMenu/WebMenu/data/1.4/r-site.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function connectWS() {
1818
console.log("NO JSON!",e.data);
1919
}
2020
eval("lastResult="+e.data);
21-
updatePage();
21+
//updatePage();
2222
};
2323
return connection.readyState;
2424
}
@@ -103,21 +103,21 @@ function mkPath(m) {
103103
}
104104

105105
function menu(o,cnt) {
106-
if(connection.readyState!=1) {//not connected (yet)?
107-
console.log("not connected yet...")
108-
if (connection.readyState==3) {//disconnected?
109-
console.log("connecting websocket");
110-
connectWS();
111-
}
112-
setTimeout(function() {//check latter...
113-
console.log("checking websocket connetion");
114-
if (connection.readyState!=1&&cnt>5) {//give up?
115-
alert("websocket disconnected, please refresh page");//TODO: put mutilang here
116-
return;
117-
} else menu(o,cnt?cnt+1:0);
118-
},500);
119-
return;
120-
}
106+
// if(connection.readyState!=1) {//not connected (yet)?
107+
// console.log("not connected yet...")
108+
// if (connection.readyState==3) {//disconnected?
109+
// console.log("connecting websocket");
110+
// connectWS();
111+
// }
112+
// setTimeout(function() {//check latter...
113+
// console.log("checking websocket connetion");
114+
// if (connection.readyState!=1&&cnt>5) {//give up?
115+
// alert("websocket disconnected, please refresh page");//TODO: put mutilang here
116+
// return;
117+
// } else menu(o,cnt?cnt+1:0);
118+
// },500);
119+
// return;
120+
// }
121121
console.log("send ws",o);
122122
return connection.send(o);
123123
}
@@ -139,11 +139,11 @@ function updateLink_(e) {
139139
var n = $(e),
140140
o = n.attr("data-grp"),
141141
t = n.attr("data-src");
142-
console.log("removing selections");
142+
// console.log("removing selections");
143143
$("a[data-grp='" + o + "']").removeClass("btn-primary btn-success");
144-
console.log("adding selection to", e);
144+
// console.log("adding selection to", e);
145145
n.addClass("btn-primary");
146-
console.log("sending: " + t);
146+
// console.log("sending: " + t);
147147
menu(t);
148148
// connection.send(t)
149149
}
@@ -154,7 +154,7 @@ function updateSelect(e) {
154154

155155
function updateSelect_(e) {
156156
var n = $(e).attr("data-src") + "/" + e.selectedIndex;
157-
console.log("sending: " + n);
157+
// console.log("sending: " + n);
158158
menu(n);
159159
// connection.send(n);
160160
}

examples/esp8266/WebMenu/platformio.ini

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
; http://docs.platformio.org/en/stable/projectconf.html
99

1010
[platformio]
11-
env_default = wemos
11+
env_default = esp12e
1212
lib_dir= lib
1313
; lib_dir =~/Arduino/Libraries
1414
;lib_dir =~/Sketchbook/libraries
@@ -21,20 +21,11 @@ data_dir=WebMenu/data
2121
; framework = arduino
2222
; build_flags = -Wno-comment -Wno-strict-aliasing -Wno-builtin-macro-redefined -DNODEBUG
2323

24-
; [env:esp12e]
25-
; platform = espressif8266
26-
; board = esp12e
27-
; framework = arduino
28-
; upload_speed=921600
29-
; build_flags = -DDEBUG -DMENU_ASYNC -DMENU_FMT_WRAPS
30-
31-
[env:wemos]
24+
[env:esp12e]
3225
platform = espressif8266
33-
board = d1_mini
26+
board = esp12e
3427
framework = arduino
35-
; upload_speed=1500000
3628
upload_speed=921600
37-
upload_port=/dev/ttyUSB*
3829
build_flags =
3930
-DMENU_ASYNC
4031
-DMENU_FMT_WRAPS
@@ -43,3 +34,19 @@ build_flags =
4334
-DNO_WEB_DEBUG
4435
-DMENU_SSID="\"r-site.net\"" -DMENU_PASS="\"rsite.2011\""
4536
src_build_flags = !echo "-Wno-write-strings -Wno-reorder -Wno-strict-aliasing -Wno-sign-compare -DLOC="$PLATFORMIO_LOC
37+
38+
; [env:wemos]
39+
; platform = espressif8266
40+
; board = d1_mini
41+
; framework = arduino
42+
; ; upload_speed=1500000
43+
; upload_speed=921600
44+
; upload_port=/dev/ttyUSB*
45+
; build_flags =
46+
; -DMENU_ASYNC
47+
; -DMENU_FMT_WRAPS
48+
; -DMENU_IDLE_BKGND
49+
; -DNO_MENU_DEBUG
50+
; -DNO_WEB_DEBUG
51+
; -DMENU_SSID="\"r-site.net\"" -DMENU_PASS="\"rsite.2011\""
52+
; src_build_flags = !echo "-Wno-write-strings -Wno-reorder -Wno-strict-aliasing -Wno-sign-compare -DLOC="$PLATFORMIO_LOC

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ArduinoMenu library
2-
version=4.15.5
2+
version=4.15.6
33
author=Rui Azevedo, [email protected]
44
maintainer=neu-rah, [email protected]
55
sentence=Generic menu/interactivity system

src/items.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Used prompt::printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,id
5555
idx_t prompt::selected() const {return 0;}
5656
const char* prompt::typeName() const {return "prompt";}
5757
bool prompt::async(const char*uri,navRoot& root,idx_t lvl) {
58-
trace(MENU_DEBUG_OUT<<"prompt::async ["<<uri<<"]"<<endl;);
58+
_trace(MENU_DEBUG_OUT<<"prompt::async ["<<uri<<"]"<<endl;);
5959
return true;
6060
}
6161
idx_t menuNode::parseUriNode(const char*&uri) {
@@ -92,10 +92,22 @@ Used prompt::printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,id
9292
assert(strchr(numericChars,uri[0]));
9393
int n=parseUriNode(uri);
9494
trace(MENU_DEBUG_OUT<<"n:"<<n<<" sel:"<<root.path[lvl].sel<<endl);
95-
if (!(root.path[lvl].sel==n&&root.path[lvl+1].target==&operator[](n)&&root.level>lvl)) {
95+
_trace(Serial<<"root.level>lvl:"<<(root.level>lvl)<<endl);
96+
_trace(Serial<<"root.path[lvl].sel==n:"<<(root.path[lvl].sel==n)<<endl);
97+
_trace(Serial<<"root.path[lvl+1].target==&operator[](n))):"<<(root.path[lvl+1].target==&operator[](n))<<endl);
98+
_trace(Serial<<"operator[](n).type()==toggleClass:"<<(operator[](n).type()==toggleClass)<<endl);
99+
if (!(
100+
//if updating a toggle to not toggle the value, was deffault action
101+
//but as we can't enter toggles this action would be preceeding every update otherwise.
102+
(operator[](n).type()==toggleClass&&strchr(uri,'/'))
103+
||(root.level>lvl&&root.path[lvl].sel==n&&root.path[lvl+1].target==&operator[](n))
104+
)) {
105+
_trace(Serial<<"menuNode, escTo "<<lvl<<endl);
96106
root.escTo(lvl/*+(lvl&&root.path[lvl].sel==n?-1:0)*/);
107+
_trace(Serial<<"menuNode, doNav "<<n<<endl);
97108
root.doNav(navCmd(idxCmd,n));
98109
}
110+
_trace(Serial<<"menuNode, proceed async"<<endl);
99111
return operator[](n).async(uri,root,lvl+1);
100112
}
101113
const char* navTarget::typeName() const {return "navTarget";}

0 commit comments

Comments
 (0)