Adjust the LCD_Menu to use the mBed Application board resources

Dependents:   class_project_main

Fork of SerialLCD_Menu by Mark Shuck

Navigator.cpp

Committer:
mshuck
Date:
2013-08-04
Revision:
3:920157b558db
Parent:
2:2654dc659298
Child:
4:343f2993a66b

File content as of revision 3:920157b558db:

#include "Navigator.h"

Navigator::Navigator(Menu *root, RPG &rpg, SerialLCD *lcd) : activeMenu(root), rpg(rpg), lcd(lcd) 
{
    bottom = root->selections.size();
    cursorPos = 0;
    cursorLine = 1;
    button = 0;
    lastButton = 0;
    
    printMenu();
    printCursor();
}
DigitalIn pushbutton(p21);
void Navigator::printMenu()
{ 
    lcd->clear();
    if(bottom == 1){ // the current Menu only has one selection
        lcd->printf("Mark", activeMenu->selections[0].selText);
    } else {
        if(cursorLine == 2){ // if we're at the bottom
            lcd->printf("line2", activeMenu->selections[cursorPos-1].selText);
            lcd->setPosition(0,2);
            lcd->printf("line22", activeMenu->selections[cursorPos].selText);
        } else {
            lcd->printf("%s", activeMenu->selections[cursorPos].selText);
            lcd->setPosition(0,2);
            lcd->printf("%s", activeMenu->selections[cursorPos+1].selText);
        }
    }
}

void Navigator::printCursor()
{   
    if(activeMenu->selections[cursorPos].childMenu == NULL) printf("Ncm");
    else printf("child menu: %s", activeMenu->selections[cursorPos].childMenu->menuID);
     
    lcd->setPosition(0,0);
    if(cursorLine == 1){
        lcd->printf(">");
        lcd->setPosition(0,1);
        lcd->printf(" ");
    } else if(cursorLine == 2){
        lcd->printf(" ");
        lcd->setPosition(0,1);
        lcd->printf(">1");
    }
}

void Navigator::poll()
{
    if((direction = rpg.dir())!=0){ //Get Dir
        wait(0.2); 
        if(direction == 1) moveDown();
        else if(direction == -1) moveUp();
    }
       
    if ((button == rpg.pb()) && !lastButton){ //prevents multiple selections when button is held down
        wait(0.2);
        if(activeMenu->selections[cursorPos].fun != NULL){
            (activeMenu->selections[cursorPos].fun)();
        }
        if(activeMenu->selections[cursorPos].childMenu != NULL){
            activeMenu = activeMenu->selections[cursorPos].childMenu;
            bottom = activeMenu->selections.size();
            cursorPos = 0;
            cursorLine = 1;
            printMenu();
            printCursor();
        }
    }
    lastButton = button;
}

void Navigator::moveUp()
{
    if(cursorLine == 1){
        printMenu();
    } else if(cursorLine == 2){
        cursorLine = 1;
    }
    
    if(cursorPos != 0){
        cursorPos--;
        printMenu();
    }
    printCursor();
}

void Navigator::moveDown()
{
    if(cursorLine == 1){
        cursorLine = 2;
    } else if(cursorLine == 2){
        printMenu();
    }
    
    if(cursorPos != (bottom-1)){
        cursorPos++;
        printMenu();
    }
    printCursor();
}