|
| 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 | +//#define DEBUG |
| 15 | +#define NUM_KEYS 5 |
| 16 | +#define Key_Pin 0 // LCD key pad analog pin |
| 17 | +int adc_key_val[5] ={50,200,400,600,800}; |
| 18 | +int adc_key_in; |
| 19 | +int key=-1; |
| 20 | +int oldkey=-1; |
| 21 | + |
| 22 | +// Menu variables |
| 23 | +MenuSystem ms; |
| 24 | +Menu mm("ROOT Menu Title"); |
| 25 | +MenuItem mm_mi1("Level1- Item1 (Item)"); |
| 26 | +MenuItem mm_mi2("Level1- Item2 (Item)"); |
| 27 | +Menu mu1("Level1- Item3 (Menu)"); |
| 28 | +MenuItem mu1_mi1("Level2- Item1 (Item)"); |
| 29 | + |
| 30 | +/* |
| 31 | +The LCD circuit: |
| 32 | + * LCD RS pin to digital pin 8 |
| 33 | + * LCD Enable pin to digital pin 9 |
| 34 | + * LCD D4 pin to digital pin 4 |
| 35 | + * LCD D5 pin to digital pin 5 |
| 36 | + * LCD D6 pin to digital pin 6 |
| 37 | + * LCD D7 pin to digital pin 7 |
| 38 | + * LCD R/W pin to ground |
| 39 | + */ |
| 40 | +// initialize the library with the numbers of the interface pins |
| 41 | +LiquidCrystal lcd(26, 24, 22, 23, 25, 27, 29); |
| 42 | +// Menu callback function |
| 43 | +// In this example all menu items use the same callback. |
| 44 | + |
| 45 | +void on_item1_selected(MenuItem* p_menu_item) |
| 46 | +{ |
| 47 | + lcd.setCursor(0,2); |
| 48 | + lcd.print("Item1 Selected "); |
| 49 | + delay(1500); // so we can look the result on the LCD |
| 50 | +} |
| 51 | + |
| 52 | +void on_item2_selected(MenuItem* p_menu_item) |
| 53 | +{ |
| 54 | + lcd.setCursor(0,2); |
| 55 | + lcd.print("Item2 Selected "); |
| 56 | + delay(1500); // so we can look the result on the LCD |
| 57 | +} |
| 58 | + |
| 59 | +void on_item3_selected(MenuItem* p_menu_item) |
| 60 | +{ |
| 61 | + lcd.setCursor(0,2); |
| 62 | + lcd.print("Item3 Selected "); |
| 63 | + delay(1500); // so we can look the result on the LCD |
| 64 | +} |
| 65 | + |
| 66 | +// Standard arduino functions |
| 67 | + |
| 68 | +void setup() |
| 69 | +{ |
| 70 | + Serial.begin(9600); |
| 71 | + lcd.begin(20, 4); |
| 72 | + |
| 73 | + serialPrintHelp(); |
| 74 | + Serial.println("Setting up the menu."); |
| 75 | + // Menu setup |
| 76 | + /* |
| 77 | + Menu Structure: |
| 78 | + -Item1 |
| 79 | + -Item2 |
| 80 | + -Item3 |
| 81 | + --Item1 |
| 82 | + |
| 83 | + */ |
| 84 | + mm.add_item(&mm_mi1, &on_item1_selected); |
| 85 | + mm.add_item(&mm_mi2, &on_item2_selected); |
| 86 | + mm.add_menu(&mu1); |
| 87 | + mu1.add_item(&mu1_mi1, &on_item3_selected); |
| 88 | + ms.set_root_menu(&mm); |
| 89 | + Serial.println("Menu setted."); |
| 90 | + displayMenu(); |
| 91 | +} |
| 92 | + |
| 93 | +void loop() |
| 94 | +{ |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + // Handle serial commands |
| 99 | + serialHandler(); |
| 100 | + |
| 101 | + // Wait for two seconds so the output is viewable |
| 102 | +// delay(2000); |
| 103 | +} |
| 104 | + |
| 105 | +void displayMenu() { |
| 106 | + lcd.clear(); |
| 107 | + lcd.setCursor(0,0); |
| 108 | + // Display the menu |
| 109 | + Menu const* cp_menu = ms.get_current_menu(); |
| 110 | + |
| 111 | + //lcd.print("Current menu name: "); |
| 112 | + lcd.print(cp_menu->get_name()); |
| 113 | + |
| 114 | + lcd.setCursor(0,1); |
| 115 | + |
| 116 | + lcd.print(cp_menu->get_selected()->get_name()); |
| 117 | +} |
| 118 | + |
| 119 | +void serialHandler() { |
| 120 | + //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 121 | + adc_key_in = analogRead(Key_Pin); // Read the value of the pulsation |
| 122 | + key = get_key(adc_key_in); // We get the button pressed |
| 123 | + |
| 124 | + if (key != oldkey) // if keypress is detected |
| 125 | + { |
| 126 | + delay(50); // Expected to avoid bouncing pulsations |
| 127 | + adc_key_in = analogRead(Key_Pin); // Read the value of the pulsation |
| 128 | + key = get_key(adc_key_in); // We get the button pressed |
| 129 | +// if (key != oldkey) |
| 130 | + oldkey = key; |
| 131 | + //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 132 | + |
| 133 | + |
| 134 | +if (key == 1){ |
| 135 | + ms.prev(); |
| 136 | + displayMenu(); |
| 137 | + #ifdef DEBUG |
| 138 | + Serial.print("key- ");Serial.println(key); |
| 139 | + #endif |
| 140 | +} |
| 141 | +if (key == 2) { |
| 142 | + ms.next(); |
| 143 | + displayMenu(); |
| 144 | +#ifdef DEBUG |
| 145 | + Serial.print("key- ");Serial.println(key); |
| 146 | + #endif |
| 147 | +} |
| 148 | +if (key == 3){ |
| 149 | + ms.back(); |
| 150 | + displayMenu(); |
| 151 | +#ifdef DEBUG |
| 152 | + Serial.print("key- ");Serial.println(key); |
| 153 | + #endif |
| 154 | +} |
| 155 | +if (key == 4) |
| 156 | + ms.select(); |
| 157 | + displayMenu(); |
| 158 | +#ifdef DEBUG |
| 159 | + Serial.print("key- ");Serial.println(key); |
| 160 | + #endif |
| 161 | +} |
| 162 | +if (key == 0) |
| 163 | + serialPrintHelp(); |
| 164 | +#ifdef DEBUG |
| 165 | + Serial.print("key- ");Serial.println(key); |
| 166 | + #endif |
| 167 | +} |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | +//} |
| 173 | +void serialPrintHelp() { |
| 174 | + Serial.println("***************"); |
| 175 | + Serial.println("w: go to previus item (up)"); |
| 176 | + Serial.println("s: go to next item (down)"); |
| 177 | + Serial.println("a: go back (right)"); |
| 178 | + Serial.println("d: select \"selected\" item"); |
| 179 | + Serial.println("?: print this help"); |
| 180 | + Serial.println("h: print this help"); |
| 181 | + Serial.println("***************"); |
| 182 | + |
| 183 | +} |
| 184 | +// Convert the analog value read in a number of button pressed |
| 185 | +int get_key(unsigned int input) { |
| 186 | + int k; |
| 187 | + for (k = 0; k < NUM_KEYS; k++) { |
| 188 | + if (input < adc_key_val[k]) { |
| 189 | + return k; |
| 190 | + } |
| 191 | + } |
| 192 | + if (k >= NUM_KEYS) k = -1; // Error in reading. |
| 193 | + return k; |
| 194 | +} |
0 commit comments