![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
application for developing a menu
menu.cpp@0:842039476c99, 2014-03-21 (annotated)
- Committer:
- Mircea3M
- Date:
- Fri Mar 21 15:22:35 2014 +0000
- Revision:
- 0:842039476c99
Menu example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mircea3M | 0:842039476c99 | 1 | #include "menu.h" |
Mircea3M | 0:842039476c99 | 2 | |
Mircea3M | 0:842039476c99 | 3 | C12832_LCD lcd; |
Mircea3M | 0:842039476c99 | 4 | BusIn joystick_sel(p12, p14, p15); |
Mircea3M | 0:842039476c99 | 5 | /* |
Mircea3M | 0:842039476c99 | 6 | DigitalIn sw_right(p16); |
Mircea3M | 0:842039476c99 | 7 | DigitalIn sw_up(p15); |
Mircea3M | 0:842039476c99 | 8 | DigitalIn sw_enter(p14); |
Mircea3M | 0:842039476c99 | 9 | DigitalIn sw_left(p13); |
Mircea3M | 0:842039476c99 | 10 | DigitalIn sw_down(p12); |
Mircea3M | 0:842039476c99 | 11 | */ |
Mircea3M | 0:842039476c99 | 12 | DigitalOut myled_2(LED2); |
Mircea3M | 0:842039476c99 | 13 | |
Mircea3M | 0:842039476c99 | 14 | unsigned char menu_selection = 1; |
Mircea3M | 0:842039476c99 | 15 | |
Mircea3M | 0:842039476c99 | 16 | |
Mircea3M | 0:842039476c99 | 17 | const char menu_000[] = " -Main Screen- "; |
Mircea3M | 0:842039476c99 | 18 | const char menu_001[] = " Option 1 "; |
Mircea3M | 0:842039476c99 | 19 | const char menu_002[] = " Option 2 "; |
Mircea3M | 0:842039476c99 | 20 | const char menu_003[] = " Option 3 "; |
Mircea3M | 0:842039476c99 | 21 | const char menu_004[] = " Option 4 "; |
Mircea3M | 0:842039476c99 | 22 | const char menu_005[] = " Option 5 "; |
Mircea3M | 0:842039476c99 | 23 | const char menu_006[] = " Option 6 "; |
Mircea3M | 0:842039476c99 | 24 | const char menu_007[] = " Option 7 "; |
Mircea3M | 0:842039476c99 | 25 | const char menu_008[] = " Option 8 "; |
Mircea3M | 0:842039476c99 | 26 | const char menu_009[] = " START "; |
Mircea3M | 0:842039476c99 | 27 | |
Mircea3M | 0:842039476c99 | 28 | |
Mircea3M | 0:842039476c99 | 29 | const char menu_100[] = " [Sub Menu] "; |
Mircea3M | 0:842039476c99 | 30 | const char menu_101[] = " SubOption 1 "; |
Mircea3M | 0:842039476c99 | 31 | const char menu_102[] = " SubOption 2 "; |
Mircea3M | 0:842039476c99 | 32 | const char menu_103[] = " SubOption 3 "; |
Mircea3M | 0:842039476c99 | 33 | const char menu_104[] = " SubOption 4 "; |
Mircea3M | 0:842039476c99 | 34 | const char menu_105[] = " SubOption 5 "; |
Mircea3M | 0:842039476c99 | 35 | const char menu_106[] = " SubOption 6 "; |
Mircea3M | 0:842039476c99 | 36 | |
Mircea3M | 0:842039476c99 | 37 | menuEntry menu[] = { |
Mircea3M | 0:842039476c99 | 38 | {menu_000, 10, 0, 0, 0, 0}, |
Mircea3M | 0:842039476c99 | 39 | {menu_001, 10, 1, 2, 11, 0}, |
Mircea3M | 0:842039476c99 | 40 | {menu_002, 10, 1, 3, 2, 0}, |
Mircea3M | 0:842039476c99 | 41 | {menu_003, 10, 2, 4, 3, 0}, |
Mircea3M | 0:842039476c99 | 42 | {menu_004, 10, 3, 5, 4, 0}, |
Mircea3M | 0:842039476c99 | 43 | {menu_005, 10, 4, 6, 5, 0}, |
Mircea3M | 0:842039476c99 | 44 | {menu_006, 10, 5, 7, 6, 0}, |
Mircea3M | 0:842039476c99 | 45 | {menu_007, 10, 6, 8, 7, 0}, |
Mircea3M | 0:842039476c99 | 46 | {menu_008, 10, 7, 9, 8, 0}, |
Mircea3M | 0:842039476c99 | 47 | {menu_009, 10, 8, 9, 9, start}, |
Mircea3M | 0:842039476c99 | 48 | |
Mircea3M | 0:842039476c99 | 49 | {menu_100, 7, 0, 0, 0, 0}, |
Mircea3M | 0:842039476c99 | 50 | {menu_101, 7, 11, 12, 11, 0}, |
Mircea3M | 0:842039476c99 | 51 | {menu_102, 7, 11, 13, 12, 0}, |
Mircea3M | 0:842039476c99 | 52 | {menu_103, 7, 12, 14, 13, 0}, |
Mircea3M | 0:842039476c99 | 53 | {menu_104, 7, 13, 15, 14, 0}, |
Mircea3M | 0:842039476c99 | 54 | {menu_105, 7, 14, 16, 15, 0}, |
Mircea3M | 0:842039476c99 | 55 | {menu_106, 7, 15, 16, 1, 0} |
Mircea3M | 0:842039476c99 | 56 | }; |
Mircea3M | 0:842039476c99 | 57 | |
Mircea3M | 0:842039476c99 | 58 | void show_menu(void) |
Mircea3M | 0:842039476c99 | 59 | { |
Mircea3M | 0:842039476c99 | 60 | unsigned int line_cnt = 0; |
Mircea3M | 0:842039476c99 | 61 | unsigned char from = 0; |
Mircea3M | 0:842039476c99 | 62 | unsigned char till = 0; |
Mircea3M | 0:842039476c99 | 63 | unsigned char temp = 0; |
Mircea3M | 0:842039476c99 | 64 | |
Mircea3M | 0:842039476c99 | 65 | while( till <= menu_selection ) |
Mircea3M | 0:842039476c99 | 66 | { |
Mircea3M | 0:842039476c99 | 67 | till += menu[till].menupoints; |
Mircea3M | 0:842039476c99 | 68 | } |
Mircea3M | 0:842039476c99 | 69 | |
Mircea3M | 0:842039476c99 | 70 | from = till - menu[menu_selection].menupoints; |
Mircea3M | 0:842039476c99 | 71 | till--; |
Mircea3M | 0:842039476c99 | 72 | |
Mircea3M | 0:842039476c99 | 73 | temp = from; |
Mircea3M | 0:842039476c99 | 74 | |
Mircea3M | 0:842039476c99 | 75 | if( (menu_selection >= (from+2)) && (menu_selection <= (till-1))) |
Mircea3M | 0:842039476c99 | 76 | { |
Mircea3M | 0:842039476c99 | 77 | from = menu_selection - 2; |
Mircea3M | 0:842039476c99 | 78 | till = from + 3; |
Mircea3M | 0:842039476c99 | 79 | |
Mircea3M | 0:842039476c99 | 80 | for(from = from; from <= till; from++) |
Mircea3M | 0:842039476c99 | 81 | { |
Mircea3M | 0:842039476c99 | 82 | lcd.locate(7, 8*line_cnt); |
Mircea3M | 0:842039476c99 | 83 | lcd.printf("%s", menu[from].text); |
Mircea3M | 0:842039476c99 | 84 | line_cnt++; |
Mircea3M | 0:842039476c99 | 85 | } |
Mircea3M | 0:842039476c99 | 86 | lcd.locate(0, 8*2); |
Mircea3M | 0:842039476c99 | 87 | lcd.printf(">"); |
Mircea3M | 0:842039476c99 | 88 | }else{ |
Mircea3M | 0:842039476c99 | 89 | // selection is within the top - 2 last 2 spaces |
Mircea3M | 0:842039476c99 | 90 | if(menu_selection <= (from+2)) |
Mircea3M | 0:842039476c99 | 91 | { |
Mircea3M | 0:842039476c99 | 92 | till = from + 3; |
Mircea3M | 0:842039476c99 | 93 | for(from = from; from <= till; from++) |
Mircea3M | 0:842039476c99 | 94 | { |
Mircea3M | 0:842039476c99 | 95 | lcd.locate(7, 8*line_cnt); |
Mircea3M | 0:842039476c99 | 96 | lcd.printf("%s", menu[from].text); |
Mircea3M | 0:842039476c99 | 97 | line_cnt++; |
Mircea3M | 0:842039476c99 | 98 | } |
Mircea3M | 0:842039476c99 | 99 | lcd.locate(0, 8*(menu_selection-temp)); |
Mircea3M | 0:842039476c99 | 100 | lcd.printf(">"); |
Mircea3M | 0:842039476c99 | 101 | } |
Mircea3M | 0:842039476c99 | 102 | if(menu_selection == till) |
Mircea3M | 0:842039476c99 | 103 | { |
Mircea3M | 0:842039476c99 | 104 | from = till - 3; |
Mircea3M | 0:842039476c99 | 105 | |
Mircea3M | 0:842039476c99 | 106 | for(from = from; from <= till; from++) |
Mircea3M | 0:842039476c99 | 107 | { |
Mircea3M | 0:842039476c99 | 108 | lcd.locate(7, 8*line_cnt); |
Mircea3M | 0:842039476c99 | 109 | lcd.printf("%s", menu[from].text); |
Mircea3M | 0:842039476c99 | 110 | line_cnt++; |
Mircea3M | 0:842039476c99 | 111 | } |
Mircea3M | 0:842039476c99 | 112 | lcd.locate(0, 8*3); |
Mircea3M | 0:842039476c99 | 113 | lcd.printf(">"); |
Mircea3M | 0:842039476c99 | 114 | } |
Mircea3M | 0:842039476c99 | 115 | } |
Mircea3M | 0:842039476c99 | 116 | lcd.locate(0,0); |
Mircea3M | 0:842039476c99 | 117 | lcd.printf("%d %d", menu_selection, line_cnt); |
Mircea3M | 0:842039476c99 | 118 | } |
Mircea3M | 0:842039476c99 | 119 | |
Mircea3M | 0:842039476c99 | 120 | void browse_menu(void) |
Mircea3M | 0:842039476c99 | 121 | { |
Mircea3M | 0:842039476c99 | 122 | do{ |
Mircea3M | 0:842039476c99 | 123 | |
Mircea3M | 0:842039476c99 | 124 | show_menu(); |
Mircea3M | 0:842039476c99 | 125 | switch(joystick_sel){ |
Mircea3M | 0:842039476c99 | 126 | case 0x01: |
Mircea3M | 0:842039476c99 | 127 | menu_selection = menu[menu_selection].down; |
Mircea3M | 0:842039476c99 | 128 | break; |
Mircea3M | 0:842039476c99 | 129 | case 0x02: |
Mircea3M | 0:842039476c99 | 130 | menu_selection = menu[menu_selection].enter; |
Mircea3M | 0:842039476c99 | 131 | if(menu[menu_selection].fp != 0) |
Mircea3M | 0:842039476c99 | 132 | { |
Mircea3M | 0:842039476c99 | 133 | menu[menu_selection].fp(); |
Mircea3M | 0:842039476c99 | 134 | |
Mircea3M | 0:842039476c99 | 135 | menu_selection = menu[menu_selection].enter; |
Mircea3M | 0:842039476c99 | 136 | } |
Mircea3M | 0:842039476c99 | 137 | break; |
Mircea3M | 0:842039476c99 | 138 | case 0x04: |
Mircea3M | 0:842039476c99 | 139 | menu_selection = menu[menu_selection].up; |
Mircea3M | 0:842039476c99 | 140 | break; |
Mircea3M | 0:842039476c99 | 141 | } |
Mircea3M | 0:842039476c99 | 142 | |
Mircea3M | 0:842039476c99 | 143 | // while(1) |
Mircea3M | 0:842039476c99 | 144 | // { |
Mircea3M | 0:842039476c99 | 145 | myled_2 = 1; |
Mircea3M | 0:842039476c99 | 146 | |
Mircea3M | 0:842039476c99 | 147 | wait(0.25); |
Mircea3M | 0:842039476c99 | 148 | myled_2 = 0; |
Mircea3M | 0:842039476c99 | 149 | |
Mircea3M | 0:842039476c99 | 150 | wait(0.25); |
Mircea3M | 0:842039476c99 | 151 | // } |
Mircea3M | 0:842039476c99 | 152 | /* if(sw_up){ |
Mircea3M | 0:842039476c99 | 153 | menu_selection = menu[menu_selection].up; |
Mircea3M | 0:842039476c99 | 154 | } |
Mircea3M | 0:842039476c99 | 155 | if(sw_down){ |
Mircea3M | 0:842039476c99 | 156 | menu_selection = menu[menu_selection].down; |
Mircea3M | 0:842039476c99 | 157 | } |
Mircea3M | 0:842039476c99 | 158 | if(sw_left){ |
Mircea3M | 0:842039476c99 | 159 | menu_selection = menu[menu_selection].up; |
Mircea3M | 0:842039476c99 | 160 | } |
Mircea3M | 0:842039476c99 | 161 | if(sw_enter){ |
Mircea3M | 0:842039476c99 | 162 | menu_selection = menu[menu_selection].enter; |
Mircea3M | 0:842039476c99 | 163 | if(menu[menu_selection].fp != 0) |
Mircea3M | 0:842039476c99 | 164 | { |
Mircea3M | 0:842039476c99 | 165 | menu[menu_selection].fp(); |
Mircea3M | 0:842039476c99 | 166 | |
Mircea3M | 0:842039476c99 | 167 | menu_selection = menu[menu_selection].enter; |
Mircea3M | 0:842039476c99 | 168 | } |
Mircea3M | 0:842039476c99 | 169 | wait(0.05); |
Mircea3M | 0:842039476c99 | 170 | } |
Mircea3M | 0:842039476c99 | 171 | */ |
Mircea3M | 0:842039476c99 | 172 | }while(1); |
Mircea3M | 0:842039476c99 | 173 | } |
Mircea3M | 0:842039476c99 | 174 | |
Mircea3M | 0:842039476c99 | 175 | |
Mircea3M | 0:842039476c99 | 176 | void start(void) |
Mircea3M | 0:842039476c99 | 177 | { |
Mircea3M | 0:842039476c99 | 178 | lcd.cls(); |
Mircea3M | 0:842039476c99 | 179 | lcd.locate(0, 0); lcd.printf("Start option selected"); |
Mircea3M | 0:842039476c99 | 180 | |
Mircea3M | 0:842039476c99 | 181 | while(1) |
Mircea3M | 0:842039476c99 | 182 | { |
Mircea3M | 0:842039476c99 | 183 | } |
Mircea3M | 0:842039476c99 | 184 | } |