Skip to content

Commit

Permalink
events inpection example
Browse files Browse the repository at this point in the history
  • Loading branch information
neu-rah committed Feb 17, 2020
1 parent 456743f commit 027a298
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 4 deletions.
211 changes: 211 additions & 0 deletions examples/handlers/handlers/handlers.ino
@@ -0,0 +1,211 @@
/********************
Arduino generic menu system
printing events and navigation info
Rui Azevedo - ruihfazevedo(@rrob@)gmail.com
output: Serial
input: Serial
*/

#include <menu.h>
#include <menuIO/serialOut.h>
#include <menuIO/serialIn.h>

using namespace Menu;

#define LEDPIN LED_BUILTIN

void showPath(navRoot& root) {
Serial.print("nav level:");
Serial.print(root.level);
Serial.print(" path:[");
for(int n=0;n<=root.level;n++) {
Serial.print(n?",":"");
Serial.print(root.path[n].sel);
}
Serial.println("]");
}

result showEvent(eventMask e,navNode& nav,prompt& item) {
Serial.println();
Serial.println("========");
Serial.print("Event for target: 0x");
Serial.println((long)nav.target,HEX);
showPath(*nav.root);
Serial.print(e);
switch(e) {
case noEvent://just ignore all stuff
Serial.println(" noEvent");break;
case activateEvent://this item is about to be active (system event)
Serial.println(" activateEvent");break;
case enterEvent://entering navigation level (this menu is now active)
Serial.println(" enterEvent");break;
case exitEvent://leaving navigation level
Serial.println(" exitEvent");break;
case returnEvent://TODO:entering previous level (return)
Serial.println(" returnEvent");break;
case focusEvent://element just gained focus
Serial.println(" focusEvent");break;
case blurEvent://element about to lose focus
Serial.println(" blurEvent");break;
case selFocusEvent://TODO:child just gained focus
Serial.println(" selFocusEvent");break;
case selBlurEvent://TODO:child about to lose focus
Serial.println(" selBlurEvent");break;
case updateEvent://Field value has been updated
Serial.println(" updateEvent");break;
case anyEvent:
Serial.println(" anyEvent");break;
}
return proceed;
}

float test=55;

int ledCtrl=LOW;

result myLedOn() {
ledCtrl=HIGH;
return proceed;
}
result myLedOff() {
ledCtrl=LOW;
return proceed;
}

TOGGLE(ledCtrl,setLed,"Led: ",showEvent,anyEvent,noStyle//,doExit,enterEvent,noStyle
,VALUE("On",HIGH,showEvent,anyEvent)
,VALUE("Off",LOW,showEvent,anyEvent)
);

int selTest=0;
SELECT(selTest,selMenu,"Select",showEvent,anyEvent,noStyle
,VALUE("Zero",0,showEvent,anyEvent)
,VALUE("One",1,showEvent,anyEvent)
,VALUE("Two",2,showEvent,anyEvent)
);

int chooseTest=-1;
CHOOSE(chooseTest,chooseMenu,"Choose",showEvent,anyEvent,noStyle
,VALUE("First",1,showEvent,anyEvent)
,VALUE("Second",2,showEvent,anyEvent)
,VALUE("Third",3,showEvent,anyEvent)
,VALUE("Last",-1,showEvent,anyEvent)
);

//customizing a prompt look!
//by extending the prompt class
class altPrompt:public prompt {
public:
// altPrompt(constMEM promptShadow& p):prompt(p) {}
using prompt::prompt;
Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t) override {
return out.printRaw(F("special prompt!"),len);
}
};

MENU(subMenu,"Sub-Menu",showEvent,anyEvent,noStyle
,OP("Sub1",showEvent,anyEvent)
,OP("Sub2",showEvent,anyEvent)
,OP("Sub3",showEvent,anyEvent)
,altOP(altPrompt,"",showEvent,anyEvent)
,EXIT("<Back")
);

uint16_t year=2017;
uint16_t month=10;
uint16_t day=7;

//define a pad style menu (single line menu)
//here with a set of fields to enter a date in YYYY/MM/DD format
//altMENU(menu,birthDate,"Birth",showEvent,anyEvent,noStyle,(systemStyles)(_asPad|Menu::_menuData|Menu::_canNav|_parentDraw)
PADMENU(birthDate,"Birth",showEvent,anyEvent,noStyle
,FIELD(year,"","/",1900,3000,20,1,showEvent,anyEvent,noStyle)
,FIELD(month,"","/",1,12,1,0,showEvent,anyEvent,wrapStyle)
,FIELD(day,"","",1,31,1,0,showEvent,anyEvent,wrapStyle)
);

char* constMEM hexDigit MEMMODE="0123456789ABCDEF";
char* constMEM hexNr[] MEMMODE={"0","x",hexDigit,hexDigit};
char buf1[]="0x11";

char* constMEM alphaNum MEMMODE=" 0123456789.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,\\|!\"#$%&/()=?~*^+-{}[]€";
char* constMEM alphaNumMask[] MEMMODE={alphaNum};
char name[]=" ";

MENU(mainMenu,"Main menu",showEvent,anyEvent,wrapStyle
,OP("Op1",showEvent,anyEvent)
,OP("Op2",showEvent,anyEvent)
,FIELD(test,"Test","%",0,100,10,1,showEvent,anyEvent,wrapStyle)
,SUBMENU(subMenu)
,SUBMENU(setLed)
,OP("LED On",myLedOn,enterEvent)
,OP("LED Off",myLedOff,enterEvent)
,SUBMENU(selMenu)
,SUBMENU(chooseMenu)
,OP("Alert test",doAlert,enterEvent)
,EDIT("Name",name,alphaNumMask,showEvent,anyEvent,noStyle)
,EDIT("Hex",buf1,hexNr,showEvent,anyEvent,noStyle)
,SUBMENU(birthDate)
,EXIT("<Back")
);

#define MAX_DEPTH 2

MENU_OUTPUTS(out,MAX_DEPTH
,SERIAL_OUT(Serial)
,NONE//must have 2 items at least
);

serialIn serial(Serial);
NAVROOT(nav,mainMenu,MAX_DEPTH,serial,out);

result alert(menuOut& o,idleEvent e) {
if (e==idling) {
o.setCursor(0,0);
o.print("alert test");
o.setCursor(0,1);
o.print("press [select]");
o.setCursor(0,2);
o.print("to continue...");
}
return proceed;
}

result doAlert(eventMask e, prompt &item) {
nav.idleOn(alert);
return proceed;
}

result idle(menuOut &o, idleEvent e) {
// o.clear();
switch(e) {
case idleStart:o.println("suspending menu!");break;
case idling:o.println("suspended...");break;
case idleEnd:o.println("resuming menu.");
nav.reset();
break;
}
return proceed;
}

void setup() {
pinMode(LEDPIN,OUTPUT);
digitalWrite(LEDPIN,ledCtrl);
delay(500);
Serial.begin(115200);
while(!Serial);
Serial.println("menu 4.x test");Serial.flush();
nav.timeOut=120;
nav.idleTask=idle;//point a function to be used when menu is suspended
// nav.idleOn();//this menu will start on idle state, press select to enter menu
//nav.doInput("323");
// nav.useAccel=false;
}

void loop() {
nav.poll();
digitalWrite(LEDPIN, ledCtrl);
delay(100);//simulate a delay when other tasks are done
}
39 changes: 39 additions & 0 deletions examples/handlers/include/README
@@ -0,0 +1,39 @@

This directory is intended for project header files.

A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.

```src/main.c

#include "header.h"

int main (void)
{
...
}
```

Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.

In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.

Read more about using header files in official GCC documentation:

* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes

https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
File renamed without changes.
2 changes: 1 addition & 1 deletion library.properties
@@ -1,5 +1,5 @@
name=ArduinoMenu library
version=4.18.2
version=4.18.3
author=Rui Azevedo, ruihfazevedo@gmail.com
maintainer=neu-rah, ruihfazevedo@gmail.com
sentence=Generic menu/interactivity system
Expand Down
2 changes: 1 addition & 1 deletion src/menuIO/OzOledAsciiOut.h
Expand Up @@ -17,7 +17,7 @@
OzOLED* device;
inline OzOledAsciiOut(OzOLED* o,idx_t *t,panelsList &p,menuOut::styles s=menuOut::minimalRedraw)
:menuOut(t,p,s),device(o) {}
size_t write(uint8_t ch) override { device->printChar(ch); return 1;}
size_t write(uint8_t ch) override { device->printChar(ch); return 1;}
void clearLine(idx_t ln,idx_t panelNr=0,colorDefs color=bgColor,bool selected=false,status stat=enabledStatus,bool edit=false) override {
setCursor(0,ln,panelNr);
for(int n=0;n<maxX();n++) print(' ');
Expand Down
3 changes: 1 addition & 2 deletions src/menuIo.h
Expand Up @@ -50,8 +50,7 @@
public:
idx_t* tops;
panelsList& panels;
idx_t lastSel=-1;
//TODO: turn this bool's into bitfield flags
// idx_t lastSel=-1;
enum styles {
none=0<<0, // default serialOut
redraw=1<<0,// changing one part implies printing all visible (deprecated)
Expand Down

0 comments on commit 027a298

Please sign in to comment.