Skip to content

Commit 7275b69

Browse files
committed
enahnced i2c example
1 parent 5c5c402 commit 7275b69

File tree

1 file changed

+142
-11
lines changed

1 file changed

+142
-11
lines changed

examples/I2C_menu/I2C_menu.ino

Lines changed: 142 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,160 @@ https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home
1616
http://playground.arduino.cc/Code/LCD3wires
1717
*/
1818
#include <Wire.h>
19-
#include <LiquidCrystal_I2C.h>
19+
#include <LiquidCrystal_I2C.h>//F. Malpartida LCD's driver
2020
#include <menu.h>//menu macros and objects
21-
#include <menuLCDs.h>//F. Malpartida LCD's
21+
#include <menuLCDs.h>
22+
#include <menuFields.h>
23+
#include <quadEncoder.h>//quadrature encoder driver and fake stream
24+
#include <keyStream.h>//keyboard driver and fake stream (for the encoder button)
25+
#include <chainStream.h>// concatenate multiple input streams (this allows adding a button to the encoder)
2226

23-
LiquidCrystal_I2C lcd(0x27);//, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
24-
menuLCD menu_lcd(lcd,16,2);//menu output device
27+
// rotary encoder pins
28+
#define encA 2
29+
#define encB 3
30+
#define encBtn 4
31+
32+
#define LEDPIN 13
33+
34+
//LiquidCrystal_I2C lcd(0x27);//, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
35+
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address and pinout
36+
37+
//aux vars
38+
int ledCtrl=0;
39+
bool runMenu=false;
40+
bool scrSaverEnter=true;
41+
int percent;//just testing changing this var
42+
double fps=0;
43+
unsigned long lastFpsChk=0;
44+
int counter=0;
45+
46+
///////////////////////////////////////////////////////////////////////////
47+
//functions to wire as menu actions
48+
bool pauseMenu() {
49+
runMenu=false;
50+
scrSaverEnter=true;
51+
}
52+
bool ledOn() {
53+
Serial.println("set led on!");
54+
digitalWrite(LEDPIN,ledCtrl=1);
55+
return false;
56+
}
57+
58+
bool ledOff() {
59+
Serial.println("set led off!");
60+
digitalWrite(LEDPIN,ledCtrl=0);
61+
return false;
62+
}
63+
64+
bool quit() {
65+
Serial.println("Quiting after action call");
66+
return true;
67+
}
68+
69+
/////////////////////////////////////////////////////////////////////////
70+
// MENU DEFINITION
71+
// here we define the menu structure and wire actions functions to it
72+
// empty options are just for scroll testing
73+
74+
/*bool setLed() {
75+
digitalWrite(LEDPIN,ledCtrl);
76+
return false;
77+
}*/
78+
TOGGLE(ledCtrl,setLed,"Led:",
79+
VALUE("On",HIGH,ledOn),
80+
VALUE("Off",LOW,ledOff)
81+
);
2582

26-
MENU(mainMenu,"Main",
27-
OP("Frequency",menu::nothing),
28-
OP("Dutty",menu::nothing),
29-
OP("Setup",menu::nothing)
83+
int selTest=0;
84+
SELECT(selTest,selMenu,"Select",
85+
VALUE("Zero",0),
86+
VALUE("One",1),
87+
VALUE("Two",2)
3088
);
3189

90+
int chooseTest=-1;
91+
CHOOSE(chooseTest,chooseMenu,"Choose ",
92+
VALUE("First",1),
93+
VALUE("Second",2),
94+
VALUE("Third",3),
95+
VALUE("Last",-1)
96+
);
97+
98+
MENU(subMenu,"SubMenu"
99+
,OP("A",quit)
100+
,OP("B",quit)
101+
,OP("C",quit)
102+
,OP("D",quit)
103+
,OP("E",quit)
104+
,OP("F",quit)
105+
,OP("G",quit)
106+
,OP("H",quit)
107+
);
108+
109+
MENU(mainMenu,"Main menu",
110+
SUBMENU(setLed),
111+
OP("LED On",ledOn),
112+
OP("LED Off",ledOff),
113+
SUBMENU(selMenu),
114+
SUBMENU(chooseMenu),
115+
SUBMENU(subMenu),
116+
FIELD(percent,"Percent","%",0,100,10,1),
117+
FIELD(fps,"fps [","]",0,0,0,0),
118+
FIELD(counter,"counter [","]",0,0,0,0),
119+
OP("Exit",pauseMenu)
120+
);
121+
122+
void scrSaver() {
123+
if (scrSaverEnter) {
124+
lcd.clear();
125+
lcd.print("|www.r-site.net|");
126+
lcd.setCursor(0,1);
127+
lcd.print("|click to enter|");
128+
scrSaverEnter=false;
129+
}
130+
}
131+
132+
//the quadEncoder
133+
#define ENC_SENSIVITY 4
134+
quadEncoder quadEncoder(encA,encB);//simple quad encoder driver
135+
quadEncoderStream enc(quadEncoder,ENC_SENSIVITY);// simple quad encoder fake Stream
136+
137+
//a keyboard with only one key :D, this is the encoder button
138+
keyMap encBtn_map[]={{-encBtn,menu::enterCode}};//negative pin numbers means we have a pull-up, this is on when low
139+
keyLook<1> encButton(encBtn_map);
140+
141+
//alternative to previous but now we can input from Serial too...
142+
Stream* in3[]={&enc,&encButton,&Serial};
143+
chainStream<3> allIn(in3);
144+
145+
//describing a menu output, alternatives so far are Serial or LiquidCrystal LCD
146+
menuLCD menu_lcd(lcd,16,2);//menu output device
147+
32148
void setup()
33149
{
150+
pinMode(LEDPIN,OUTPUT);
151+
pinMode(encBtn,INPUT_PULLUP);
34152
Serial.begin(115200);
153+
while(!Serial);
35154
Wire.begin();
155+
quadEncoder.begin();
36156
lcd.begin(16,2);
37-
lcd.print("Ok");
157+
lcd.home();
158+
lcd.print("Menu 2.x test");
38159
}
39160

40161
void loop()
41162
{
42-
//serial can use numbers or +/- and Enter=* Esc=/ to navigate (nav keys can be redefined)
43-
mainMenu.poll(menu_lcd,Serial);
163+
if (runMenu) mainMenu.poll(menu_lcd,allIn);
164+
else if (allIn.read()==menu::enterCode) runMenu=true;
165+
else scrSaver();
166+
//simulate the delay of your program... if this number rises too much the menu will have bad navigation experience
167+
//if so, then the menu can be wired into a timmer... leaving the shorter end to your code while it is running
168+
counter=millis()/1000%60;
169+
int d=micros()-lastFpsChk;
170+
if (d>0) {
171+
fps=1000000.0/d;
172+
lastFpsChk+=d;
173+
}
174+
delay(50);
44175
}

0 commit comments

Comments
 (0)