LCD implementation of our project.

Dependencies:   mbed mbed-rtos MLX90614

Revision:
0:1e597b0f8b3b
Child:
2:fcde41900fa5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Menu.cpp	Fri Mar 20 23:22:41 2015 +0000
@@ -0,0 +1,125 @@
+#include "Menu.h"
+
+void Menu::display(void){
+    ui->setCurrent(this);
+    //if it is a menu item, go to referenced screen
+    if(isScreen){
+        items[0]->display();
+        return;
+    }
+    //otherwise it's a menu, display items
+    current_line = FIRST_ITEM_LINE; //set first item on screen as selected
+    selected = 0;
+    display_items(0); //display items starting from index 0
+}
+
+void Menu::update(char c){
+    //case down
+    if(c == DOWN){
+        //if last item already selected, do nothing
+        if(selected + 1 == size)
+            return; //do nothing
+            
+        //if we are at the bottom of the screen
+        if(current_line % LAST_ITEM_LINE == 0){
+            // more items left => clear screen and add items
+            selected++;
+            //display items from the next selected item
+            current_line = FIRST_ITEM_LINE; //current selected line will be top of screen
+            printf("got down, selected is: %d; current line is: %d\n", selected, current_line);
+        }
+        //it is not last on the screen
+        else{
+            //clear the screen
+            current_line++;
+            selected++;
+            printf("selected is: %d; current line is: %d\n", selected, current_line);
+        }
+        st7565->clear();
+        display_items(selected/6 * 6);
+        return;
+    }
+    //case up
+    else if(c == UP){
+        //we are at top of items
+        if(selected == 0)
+            return; //do nothing
+        
+        //if we are at the top of the screen => there are more items to show
+        if(current_line == FIRST_ITEM_LINE){
+            selected--;
+            //display items from the next selected item
+            current_line = LAST_ITEM_LINE; //current selected line will be top of screen
+            printf("got up, selected is: %d; current line is: %d\n", selected, current_line);
+        }
+        //it is not first on the screen
+        else{
+            current_line--;
+            selected--;
+            printf("selected is: %d; current line is: %d\n", selected, current_line);
+        }
+        st7565->clear();
+        display_items(selected/6 * 6);
+        return;
+    }
+    else if(c == NL && items[selected]->isSelectable){
+        //switch to other screen
+        ui->setCurrent(items[selected]);
+        ui->display();
+    }
+}
+
+void Menu::addItem(Item * i){
+    //if there are many items, ignore
+    if(size == MAX_ITEMS)
+        return;
+    //add pointer to item
+    items[size] = i;
+    size++;
+    isSelectable = true;
+}
+
+/*
+    Function that displays items on a new screen starting from an index.
+*/
+void Menu::display_items(int index){
+    //draw title of menu
+    st7565->drawstring(LEFT_MARGIN, TITLE_LINE, title);
+    //display 1 or more items on the screen
+    int i = FIRST_ITEM_LINE, j;
+    //draw the first items
+    for(j = index; j < size && i <= LAST_ITEM_LINE; j++)
+        st7565->drawstring(LEFT_MARGIN * 4, i++, items[j]->getTitle());
+    
+    //up arrow
+    if(index != 0){
+        st7565->drawline(119, 15, 125, 15,DEFAULT_COLOR);
+        st7565->drawline(119, 15, 122, 12,DEFAULT_COLOR);
+        st7565->drawline(125, 15, 122, 12,DEFAULT_COLOR);
+    }
+    //down arrow
+    if(j < size){
+        st7565->drawline(119, 60, 125, 60,DEFAULT_COLOR);
+        st7565->drawline(119, 60, 122, 63,DEFAULT_COLOR);
+        st7565->drawline(125, 60, 122, 63,DEFAULT_COLOR);
+    }
+    //set selected as first
+    draw_selected();
+    st7565->display();
+}
+
+/*
+    Function that draws a circle near the selected item.
+*/
+void Menu::draw_selected(void){
+    st7565->drawcircle(LEFT_MARGIN*2, (current_line * 8) + 3, 2, 20);
+}
+
+/*
+    Function that draws the arrows indicating whether there are more items
+    in the menu if the user scrolls up or down.
+*/
+void Menu::draw_arrows(int line, char c){
+    
+    //to be implemented
+}
\ No newline at end of file