LCD implementation of our project.

Dependencies:   mbed mbed-rtos MLX90614

Committer:
ovidiup13
Date:
Fri Mar 20 23:22:41 2015 +0000
Revision:
0:1e597b0f8b3b
Child:
2:fcde41900fa5
initial menu mock-up st7565

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ovidiup13 0:1e597b0f8b3b 1 #include "Menu.h"
ovidiup13 0:1e597b0f8b3b 2
ovidiup13 0:1e597b0f8b3b 3 void Menu::display(void){
ovidiup13 0:1e597b0f8b3b 4 ui->setCurrent(this);
ovidiup13 0:1e597b0f8b3b 5 //if it is a menu item, go to referenced screen
ovidiup13 0:1e597b0f8b3b 6 if(isScreen){
ovidiup13 0:1e597b0f8b3b 7 items[0]->display();
ovidiup13 0:1e597b0f8b3b 8 return;
ovidiup13 0:1e597b0f8b3b 9 }
ovidiup13 0:1e597b0f8b3b 10 //otherwise it's a menu, display items
ovidiup13 0:1e597b0f8b3b 11 current_line = FIRST_ITEM_LINE; //set first item on screen as selected
ovidiup13 0:1e597b0f8b3b 12 selected = 0;
ovidiup13 0:1e597b0f8b3b 13 display_items(0); //display items starting from index 0
ovidiup13 0:1e597b0f8b3b 14 }
ovidiup13 0:1e597b0f8b3b 15
ovidiup13 0:1e597b0f8b3b 16 void Menu::update(char c){
ovidiup13 0:1e597b0f8b3b 17 //case down
ovidiup13 0:1e597b0f8b3b 18 if(c == DOWN){
ovidiup13 0:1e597b0f8b3b 19 //if last item already selected, do nothing
ovidiup13 0:1e597b0f8b3b 20 if(selected + 1 == size)
ovidiup13 0:1e597b0f8b3b 21 return; //do nothing
ovidiup13 0:1e597b0f8b3b 22
ovidiup13 0:1e597b0f8b3b 23 //if we are at the bottom of the screen
ovidiup13 0:1e597b0f8b3b 24 if(current_line % LAST_ITEM_LINE == 0){
ovidiup13 0:1e597b0f8b3b 25 // more items left => clear screen and add items
ovidiup13 0:1e597b0f8b3b 26 selected++;
ovidiup13 0:1e597b0f8b3b 27 //display items from the next selected item
ovidiup13 0:1e597b0f8b3b 28 current_line = FIRST_ITEM_LINE; //current selected line will be top of screen
ovidiup13 0:1e597b0f8b3b 29 printf("got down, selected is: %d; current line is: %d\n", selected, current_line);
ovidiup13 0:1e597b0f8b3b 30 }
ovidiup13 0:1e597b0f8b3b 31 //it is not last on the screen
ovidiup13 0:1e597b0f8b3b 32 else{
ovidiup13 0:1e597b0f8b3b 33 //clear the screen
ovidiup13 0:1e597b0f8b3b 34 current_line++;
ovidiup13 0:1e597b0f8b3b 35 selected++;
ovidiup13 0:1e597b0f8b3b 36 printf("selected is: %d; current line is: %d\n", selected, current_line);
ovidiup13 0:1e597b0f8b3b 37 }
ovidiup13 0:1e597b0f8b3b 38 st7565->clear();
ovidiup13 0:1e597b0f8b3b 39 display_items(selected/6 * 6);
ovidiup13 0:1e597b0f8b3b 40 return;
ovidiup13 0:1e597b0f8b3b 41 }
ovidiup13 0:1e597b0f8b3b 42 //case up
ovidiup13 0:1e597b0f8b3b 43 else if(c == UP){
ovidiup13 0:1e597b0f8b3b 44 //we are at top of items
ovidiup13 0:1e597b0f8b3b 45 if(selected == 0)
ovidiup13 0:1e597b0f8b3b 46 return; //do nothing
ovidiup13 0:1e597b0f8b3b 47
ovidiup13 0:1e597b0f8b3b 48 //if we are at the top of the screen => there are more items to show
ovidiup13 0:1e597b0f8b3b 49 if(current_line == FIRST_ITEM_LINE){
ovidiup13 0:1e597b0f8b3b 50 selected--;
ovidiup13 0:1e597b0f8b3b 51 //display items from the next selected item
ovidiup13 0:1e597b0f8b3b 52 current_line = LAST_ITEM_LINE; //current selected line will be top of screen
ovidiup13 0:1e597b0f8b3b 53 printf("got up, selected is: %d; current line is: %d\n", selected, current_line);
ovidiup13 0:1e597b0f8b3b 54 }
ovidiup13 0:1e597b0f8b3b 55 //it is not first on the screen
ovidiup13 0:1e597b0f8b3b 56 else{
ovidiup13 0:1e597b0f8b3b 57 current_line--;
ovidiup13 0:1e597b0f8b3b 58 selected--;
ovidiup13 0:1e597b0f8b3b 59 printf("selected is: %d; current line is: %d\n", selected, current_line);
ovidiup13 0:1e597b0f8b3b 60 }
ovidiup13 0:1e597b0f8b3b 61 st7565->clear();
ovidiup13 0:1e597b0f8b3b 62 display_items(selected/6 * 6);
ovidiup13 0:1e597b0f8b3b 63 return;
ovidiup13 0:1e597b0f8b3b 64 }
ovidiup13 0:1e597b0f8b3b 65 else if(c == NL && items[selected]->isSelectable){
ovidiup13 0:1e597b0f8b3b 66 //switch to other screen
ovidiup13 0:1e597b0f8b3b 67 ui->setCurrent(items[selected]);
ovidiup13 0:1e597b0f8b3b 68 ui->display();
ovidiup13 0:1e597b0f8b3b 69 }
ovidiup13 0:1e597b0f8b3b 70 }
ovidiup13 0:1e597b0f8b3b 71
ovidiup13 0:1e597b0f8b3b 72 void Menu::addItem(Item * i){
ovidiup13 0:1e597b0f8b3b 73 //if there are many items, ignore
ovidiup13 0:1e597b0f8b3b 74 if(size == MAX_ITEMS)
ovidiup13 0:1e597b0f8b3b 75 return;
ovidiup13 0:1e597b0f8b3b 76 //add pointer to item
ovidiup13 0:1e597b0f8b3b 77 items[size] = i;
ovidiup13 0:1e597b0f8b3b 78 size++;
ovidiup13 0:1e597b0f8b3b 79 isSelectable = true;
ovidiup13 0:1e597b0f8b3b 80 }
ovidiup13 0:1e597b0f8b3b 81
ovidiup13 0:1e597b0f8b3b 82 /*
ovidiup13 0:1e597b0f8b3b 83 Function that displays items on a new screen starting from an index.
ovidiup13 0:1e597b0f8b3b 84 */
ovidiup13 0:1e597b0f8b3b 85 void Menu::display_items(int index){
ovidiup13 0:1e597b0f8b3b 86 //draw title of menu
ovidiup13 0:1e597b0f8b3b 87 st7565->drawstring(LEFT_MARGIN, TITLE_LINE, title);
ovidiup13 0:1e597b0f8b3b 88 //display 1 or more items on the screen
ovidiup13 0:1e597b0f8b3b 89 int i = FIRST_ITEM_LINE, j;
ovidiup13 0:1e597b0f8b3b 90 //draw the first items
ovidiup13 0:1e597b0f8b3b 91 for(j = index; j < size && i <= LAST_ITEM_LINE; j++)
ovidiup13 0:1e597b0f8b3b 92 st7565->drawstring(LEFT_MARGIN * 4, i++, items[j]->getTitle());
ovidiup13 0:1e597b0f8b3b 93
ovidiup13 0:1e597b0f8b3b 94 //up arrow
ovidiup13 0:1e597b0f8b3b 95 if(index != 0){
ovidiup13 0:1e597b0f8b3b 96 st7565->drawline(119, 15, 125, 15,DEFAULT_COLOR);
ovidiup13 0:1e597b0f8b3b 97 st7565->drawline(119, 15, 122, 12,DEFAULT_COLOR);
ovidiup13 0:1e597b0f8b3b 98 st7565->drawline(125, 15, 122, 12,DEFAULT_COLOR);
ovidiup13 0:1e597b0f8b3b 99 }
ovidiup13 0:1e597b0f8b3b 100 //down arrow
ovidiup13 0:1e597b0f8b3b 101 if(j < size){
ovidiup13 0:1e597b0f8b3b 102 st7565->drawline(119, 60, 125, 60,DEFAULT_COLOR);
ovidiup13 0:1e597b0f8b3b 103 st7565->drawline(119, 60, 122, 63,DEFAULT_COLOR);
ovidiup13 0:1e597b0f8b3b 104 st7565->drawline(125, 60, 122, 63,DEFAULT_COLOR);
ovidiup13 0:1e597b0f8b3b 105 }
ovidiup13 0:1e597b0f8b3b 106 //set selected as first
ovidiup13 0:1e597b0f8b3b 107 draw_selected();
ovidiup13 0:1e597b0f8b3b 108 st7565->display();
ovidiup13 0:1e597b0f8b3b 109 }
ovidiup13 0:1e597b0f8b3b 110
ovidiup13 0:1e597b0f8b3b 111 /*
ovidiup13 0:1e597b0f8b3b 112 Function that draws a circle near the selected item.
ovidiup13 0:1e597b0f8b3b 113 */
ovidiup13 0:1e597b0f8b3b 114 void Menu::draw_selected(void){
ovidiup13 0:1e597b0f8b3b 115 st7565->drawcircle(LEFT_MARGIN*2, (current_line * 8) + 3, 2, 20);
ovidiup13 0:1e597b0f8b3b 116 }
ovidiup13 0:1e597b0f8b3b 117
ovidiup13 0:1e597b0f8b3b 118 /*
ovidiup13 0:1e597b0f8b3b 119 Function that draws the arrows indicating whether there are more items
ovidiup13 0:1e597b0f8b3b 120 in the menu if the user scrolls up or down.
ovidiup13 0:1e597b0f8b3b 121 */
ovidiup13 0:1e597b0f8b3b 122 void Menu::draw_arrows(int line, char c){
ovidiup13 0:1e597b0f8b3b 123
ovidiup13 0:1e597b0f8b3b 124 //to be implemented
ovidiup13 0:1e597b0f8b3b 125 }