Skip to content

Commit 7c6d475

Browse files
committed
Added example that use a LCD (16x2) display, this example is controled over serial
1 parent 216f981 commit 7c6d475

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

examples/LCDNav/LCDNav.ino

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
LCDNav.ino - Example code using the menu system library
3+
and a lcd 16x2 display (controled over serial).
4+
5+
Created by Niesteszeck, Dec 1th 2013.
6+
Released into the public domain.
7+
8+
License: GPL 3
9+
*/
10+
11+
#include <MenuSystem.h>
12+
#include <LiquidCrystal.h>
13+
14+
// Menu variables
15+
MenuSystem ms;
16+
Menu mm("ROOT Menu Title");
17+
MenuItem mm_mi1("Level 1 - Item 1 (Item)");
18+
MenuItem mm_mi2("Level 1 - Item 2 (Item)");
19+
Menu mu1("Level 1 - Item 3 (Menu)");
20+
MenuItem mu1_mi1("Level 2 - Item 1 (Item)");
21+
22+
/*
23+
The LCD circuit:
24+
* LCD RS pin to digital pin 8
25+
* LCD Enable pin to digital pin 9
26+
* LCD D4 pin to digital pin 4
27+
* LCD D5 pin to digital pin 5
28+
* LCD D6 pin to digital pin 6
29+
* LCD D7 pin to digital pin 7
30+
* LCD R/W pin to ground
31+
*/
32+
// initialize the library with the numbers of the interface pins
33+
LiquidCrystal lcd(8 , 9 , 4 , 5 , 6 , 7 );
34+
35+
// Menu callback function
36+
// In this example all menu items use the same callback.
37+
38+
void on_item1_selected(MenuItem* p_menu_item)
39+
{
40+
lcd.setCursor(0,1);
41+
lcd.print("Item1 Selected ");
42+
delay(1500); // so we can look the result on the LCD
43+
}
44+
45+
void on_item2_selected(MenuItem* p_menu_item)
46+
{
47+
lcd.setCursor(0,1);
48+
lcd.print("Item2 Selected ");
49+
delay(1500); // so we can look the result on the LCD
50+
}
51+
52+
void on_item3_selected(MenuItem* p_menu_item)
53+
{
54+
lcd.setCursor(0,1);
55+
lcd.print("Item3 Selected ");
56+
delay(1500); // so we can look the result on the LCD
57+
}
58+
59+
// Standard arduino functions
60+
61+
void setup()
62+
{
63+
Serial.begin(9600);
64+
lcd.begin(16, 2);
65+
66+
serialPrintHelp();
67+
Serial.println("Setting up the menu.");
68+
// Menu setup
69+
/*
70+
Menu Structure:
71+
-Item1
72+
-Item2
73+
-Item3
74+
-Item1
75+
76+
*/
77+
mm.add_item(&mm_mi1, &on_item1_selected);
78+
mm.add_item(&mm_mi2, &on_item2_selected);
79+
mm.add_menu(&mu1);
80+
mu1.add_item(&mu1_mi1, &on_item3_selected);
81+
ms.set_root_menu(&mm);
82+
Serial.println("Menu setted.");
83+
displayMenu();
84+
}
85+
86+
void loop()
87+
{
88+
// Handle serial commands
89+
serialHandler();
90+
91+
// Wait for two seconds so the output is viewable
92+
//delay(2000);
93+
}
94+
95+
void displayMenu() {
96+
lcd.clear();
97+
lcd.setCursor(0,0);
98+
// Display the menu
99+
Menu const* cp_menu = ms.get_current_menu();
100+
101+
//lcd.print("Current menu name: ");
102+
lcd.print(cp_menu->get_name());
103+
104+
lcd.setCursor(0,1);
105+
106+
lcd.print(cp_menu->get_selected()->get_name());
107+
}
108+
109+
void serialHandler() {
110+
char inChar;
111+
if((inChar = Serial.read())>0) {
112+
switch (inChar) {
113+
case 'w': // Previus item
114+
ms.prev();
115+
displayMenu();
116+
break;
117+
case 's': // Next item
118+
ms.next();
119+
displayMenu();
120+
break;
121+
case 'a': // Back presed
122+
ms.back();
123+
displayMenu();
124+
break;
125+
case 'd': // Select presed
126+
ms.select();
127+
displayMenu();
128+
break;
129+
case '?':
130+
case 'h': // Display help
131+
serialPrintHelp();
132+
break;
133+
default:
134+
break;
135+
}
136+
}
137+
}
138+
139+
void serialPrintHelp() {
140+
Serial.println("***************");
141+
Serial.println("w: go to previus itme (up)");
142+
Serial.println("s: got to next item (down)");
143+
Serial.println("a: go back (right)");
144+
Serial.println("d: select \"selected\" item");
145+
Serial.println("?: print this help");
146+
Serial.println("h: print this help");
147+
Serial.println("***************");
148+
149+
}
150+

0 commit comments

Comments
 (0)