Skip to content

Commit

Permalink
Function display() that displays menu added. Example updated accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Kaczmarek authored and adacho committed Dec 21, 2012
1 parent 177e33f commit 33b03f1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
13 changes: 13 additions & 0 deletions LCDMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,17 @@ LiquidCrystal* LCDMenu::getLCD()
void LCDMenu::selectOption()
{
menuCurrent->executeAction();
}

void LCDMenu::display()
{
//LCD->clear();
LCD->setCursor(0,0);
LCD->print(">");
LCD->print(menuCurrent->getName());
if (menuCurrent->getNext() != menuCurrent)
{
LCD->setCursor(1,1);
LCD->print(menuCurrent->getNext()->getName());
}
}
1 change: 1 addition & 0 deletions LCDMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class LCDMenu{
void setLCD(LiquidCrystal*);
LiquidCrystal* getLCD();
void selectOption();
void display();
};

#endif
8 changes: 5 additions & 3 deletions LCDMenuItem.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "LCDMenuItem.h"
#include <stdio.h>
//#include <stdio.h>

LCDMenuItem::LCDMenuItem(String newName)
{
Expand All @@ -8,11 +8,13 @@ LCDMenuItem::LCDMenuItem(String newName)

void LCDMenuItem::setName(String newName)
{
char buffer[16];
/* char buffer[16];
newName.toCharArray(buffer,16);
snprintf(buffer, 15, "%s ", buffer);
Name=String(buffer);
//Name=newName;
*/
Name = newName + " ";
Name = Name.substring(0,16);
}

String LCDMenuItem::getName()
Expand Down
22 changes: 17 additions & 5 deletions examples/SimpleMenu/SimpleMenu.ino
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,58 @@ int lastButton = 0; // used in loop() to debounce reading from LCD shield's key
void settingsAction() //this function will be executed if you choose "Settings option".
{
menu->getLCD()->clear();
menu->getLCD()->setCursor(0,1);
menu->getLCD()->setCursor(2,2);
menu->getLCD()->print("Some settings...");

delay (2000);
}

void distanceAction() //this function will be executed if you choose "Settings option".
{
menu->getLCD()->clear();
menu->getLCD()->setCursor(0,0);
menu->getLCD()->print("distance metering...");
delay (2000);
}

void setup()
{
LCD = new LiquidCrystal(8, 9, 4, 5, 6, 7);
LCD->begin(16,2);
LCD->clear();
menu = new LCDMenu("Main Menu", LCD); //menu gets created
LCDMenuItem *newItem;
newItem = new LCDMenuItem("Settings"); //lets define some menu entries
newItem->setAction(&settingsAction); //here function gets assigned to menu entry - it will be executed if you select this option (Settings)
menu->addMenuItem(newItem);
newItem = new LCDMenuItem("Distance");
newItem->setAction(&distanceAction);
menu->addMenuItem(newItem);
newItem = new LCDMenuItem("Humidity");
menu->addMenuItem(newItem);
newItem = new LCDMenuItem("Temperature");
menu->addMenuItem(newItem);
menu->display();
}

void loop()
{
int buttonState = read_LCD_buttons();
if (buttonState != lastButton)
{
menu->getLCD()->setCursor(0,1);
menu->getLCD()->setCursor(0,0);
switch (buttonState)
{
case btnUP :
case btnDOWN :
menu->getLCD()->print(menu->next()->getName());
break;
case btnDOWN :
case btnUP :
menu->getLCD()->print(menu->prev()->getName());
break;
case btnRIGHT :
menu->selectOption();
break;
}
menu->display();
lastButton = buttonState;
}
}
Expand Down

0 comments on commit 33b03f1

Please sign in to comment.