Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Maple_console.cpp@3:eec13a411e94, 2011-10-30 (annotated)
- Committer:
- uehara00
- Date:
- Sun Oct 30 21:20:23 2011 +0000
- Revision:
- 3:eec13a411e94
OLED(MARY-OB) drivers and demonstrations are added.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| uehara00 | 3:eec13a411e94 | 1 | //copyright 2011 Uehara Yoshiyuki |
| uehara00 | 3:eec13a411e94 | 2 | //==================================================================== |
| uehara00 | 3:eec13a411e94 | 3 | //The author provide the programs without any guarantees or warranty. |
| uehara00 | 3:eec13a411e94 | 4 | //The author is not responsible for any damage or losses of any kind |
| uehara00 | 3:eec13a411e94 | 5 | //caused by using or misusing of the programs. |
| uehara00 | 3:eec13a411e94 | 6 | //The author is under no obligation to provide support, service, |
| uehara00 | 3:eec13a411e94 | 7 | //corrections, or upgrades to the programs. |
| uehara00 | 3:eec13a411e94 | 8 | //==================================================================== |
| uehara00 | 3:eec13a411e94 | 9 | // MAPLE board[MARM01-BASE] |
| uehara00 | 3:eec13a411e94 | 10 | // operation console utilizing text-LCD and buttons |
| uehara00 | 3:eec13a411e94 | 11 | #include "Maple_console.h" |
| uehara00 | 3:eec13a411e94 | 12 | #include "Maple_alarm_clock.h" |
| uehara00 | 3:eec13a411e94 | 13 | #include "Maple_test.h" |
| uehara00 | 3:eec13a411e94 | 14 | #include "Maple_RTC.h" |
| uehara00 | 3:eec13a411e94 | 15 | #include "Maple_I2C.h" |
| uehara00 | 3:eec13a411e94 | 16 | #include "Maple_LCD.h" |
| uehara00 | 3:eec13a411e94 | 17 | #include "Maple.h" |
| uehara00 | 3:eec13a411e94 | 18 | #include "mbed.h" |
| uehara00 | 3:eec13a411e94 | 19 | |
| uehara00 | 3:eec13a411e94 | 20 | // global variables |
| uehara00 | 3:eec13a411e94 | 21 | int console_mode; // console function selected |
| uehara00 | 3:eec13a411e94 | 22 | int console_cursor; // cursor position |
| uehara00 | 3:eec13a411e94 | 23 | int console_display; // console display on/off |
| uehara00 | 3:eec13a411e94 | 24 | int button_count[BUTTON_SIZE]; |
| uehara00 | 3:eec13a411e94 | 25 | int button_status[BUTTON_SIZE]; |
| uehara00 | 3:eec13a411e94 | 26 | int test_mode; // used within test |
| uehara00 | 3:eec13a411e94 | 27 | |
| uehara00 | 3:eec13a411e94 | 28 | // timer interrupt to refresh console |
| uehara00 | 3:eec13a411e94 | 29 | Ticker console_refresh_tick; |
| uehara00 | 3:eec13a411e94 | 30 | |
| uehara00 | 3:eec13a411e94 | 31 | // initialize console |
| uehara00 | 3:eec13a411e94 | 32 | void console_initialize() { |
| uehara00 | 3:eec13a411e94 | 33 | enter_mode_clock(); |
| uehara00 | 3:eec13a411e94 | 34 | console_display = DISPLAY_ON; |
| uehara00 | 3:eec13a411e94 | 35 | for (int i = 0; i < BUTTON_SIZE; ++i) { |
| uehara00 | 3:eec13a411e94 | 36 | button_count[i] = 0; |
| uehara00 | 3:eec13a411e94 | 37 | button_status[i] = BUTTON_IDLE; |
| uehara00 | 3:eec13a411e94 | 38 | } |
| uehara00 | 3:eec13a411e94 | 39 | console_refresh_tick.attach(&console_refresh, CONSOLE_REFRESH_RATE); |
| uehara00 | 3:eec13a411e94 | 40 | } |
| uehara00 | 3:eec13a411e94 | 41 | |
| uehara00 | 3:eec13a411e94 | 42 | // refresh console called by ticker interrupt |
| uehara00 | 3:eec13a411e94 | 43 | void console_refresh() { |
| uehara00 | 3:eec13a411e94 | 44 | refresh_display(); |
| uehara00 | 3:eec13a411e94 | 45 | button_action(); |
| uehara00 | 3:eec13a411e94 | 46 | } |
| uehara00 | 3:eec13a411e94 | 47 | |
| uehara00 | 3:eec13a411e94 | 48 | // read RTC and print to LCD |
| uehara00 | 3:eec13a411e94 | 49 | static void refresh_display() { |
| uehara00 | 3:eec13a411e94 | 50 | char row0[17], row1[17]; |
| uehara00 | 3:eec13a411e94 | 51 | int cursor_r, cursor_c; |
| uehara00 | 3:eec13a411e94 | 52 | |
| uehara00 | 3:eec13a411e94 | 53 | switch(console_mode) { |
| uehara00 | 3:eec13a411e94 | 54 | case MODE_ADJUST: display_adjust(row0, row1, cursor_r, cursor_c); break; |
| uehara00 | 3:eec13a411e94 | 55 | case MODE_ALARM: display_alarm( row0, row1, cursor_r, cursor_c); break; |
| uehara00 | 3:eec13a411e94 | 56 | case MODE_TIMER: display_timer( row0, row1, cursor_r, cursor_c); break; |
| uehara00 | 3:eec13a411e94 | 57 | case MODE_TEST: display_test( row0, row1, cursor_r, cursor_c); break; |
| uehara00 | 3:eec13a411e94 | 58 | default: display_clock( row0, row1, cursor_r, cursor_c); |
| uehara00 | 3:eec13a411e94 | 59 | } |
| uehara00 | 3:eec13a411e94 | 60 | if(console_display == DISPLAY_ON) { |
| uehara00 | 3:eec13a411e94 | 61 | LCD_cursor_off(); |
| uehara00 | 3:eec13a411e94 | 62 | LCD_locate(0, 0); LCD_print_string(row0); |
| uehara00 | 3:eec13a411e94 | 63 | LCD_locate(1, 0); LCD_print_string(row1); |
| uehara00 | 3:eec13a411e94 | 64 | LCD_locate(cursor_r, cursor_c); |
| uehara00 | 3:eec13a411e94 | 65 | LCD_cursor_on(); |
| uehara00 | 3:eec13a411e94 | 66 | } |
| uehara00 | 3:eec13a411e94 | 67 | } |
| uehara00 | 3:eec13a411e94 | 68 | |
| uehara00 | 3:eec13a411e94 | 69 | // read button and launch action |
| uehara00 | 3:eec13a411e94 | 70 | // called by timer tick interrupt |
| uehara00 | 3:eec13a411e94 | 71 | static void button_action() { |
| uehara00 | 3:eec13a411e94 | 72 | char r = i2c_BTN_read(); |
| uehara00 | 3:eec13a411e94 | 73 | button_check(r, BUTTON_A ); |
| uehara00 | 3:eec13a411e94 | 74 | button_check(r, BUTTON_B ); |
| uehara00 | 3:eec13a411e94 | 75 | button_check(r, BUTTON_LEFT ); |
| uehara00 | 3:eec13a411e94 | 76 | button_check(r, BUTTON_DOWN ); |
| uehara00 | 3:eec13a411e94 | 77 | button_check(r, BUTTON_RIGHT); |
| uehara00 | 3:eec13a411e94 | 78 | button_check(r, BUTTON_UP ); |
| uehara00 | 3:eec13a411e94 | 79 | if(button_trigger(BUTTON_A, 0, 0)) { button_exit(); } |
| uehara00 | 3:eec13a411e94 | 80 | if(button_trigger(BUTTON_B, 1, 0)) { button_function(); } |
| uehara00 | 3:eec13a411e94 | 81 | if(button_trigger(BUTTON_LEFT, 1, 0)) { button_cursor_move(0); } |
| uehara00 | 3:eec13a411e94 | 82 | if(button_trigger(BUTTON_RIGHT, 1, 0)) { button_cursor_move(1); } |
| uehara00 | 3:eec13a411e94 | 83 | if(button_trigger(BUTTON_DOWN, 1, 1)) { button_xxcrement(0); } |
| uehara00 | 3:eec13a411e94 | 84 | if(button_trigger(BUTTON_UP, 1, 1)) { button_xxcrement(1); } |
| uehara00 | 3:eec13a411e94 | 85 | } |
| uehara00 | 3:eec13a411e94 | 86 | |
| uehara00 | 3:eec13a411e94 | 87 | // button check and count up |
| uehara00 | 3:eec13a411e94 | 88 | static void button_check(int r, int button_number) { |
| uehara00 | 3:eec13a411e94 | 89 | if((r & (0x01 << button_number)) == BUTTON_ON) { |
| uehara00 | 3:eec13a411e94 | 90 | ++button_count[button_number]; |
| uehara00 | 3:eec13a411e94 | 91 | } |
| uehara00 | 3:eec13a411e94 | 92 | else { |
| uehara00 | 3:eec13a411e94 | 93 | button_count[button_number] = 0; |
| uehara00 | 3:eec13a411e94 | 94 | button_status[button_number] = BUTTON_IDLE; |
| uehara00 | 3:eec13a411e94 | 95 | } |
| uehara00 | 3:eec13a411e94 | 96 | } |
| uehara00 | 3:eec13a411e94 | 97 | |
| uehara00 | 3:eec13a411e94 | 98 | // button action trigger |
| uehara00 | 3:eec13a411e94 | 99 | // b: button number |
| uehara00 | 3:eec13a411e94 | 100 | // r: repeat enable |
| uehara00 | 3:eec13a411e94 | 101 | // f: fast-repeat enable |
| uehara00 | 3:eec13a411e94 | 102 | static bool button_trigger(int b, int r, int f) { |
| uehara00 | 3:eec13a411e94 | 103 | if((button_status[b] == BUTTON_IDLE) && (button_count[b] > BUTTON_THRESHOLD)) { |
| uehara00 | 3:eec13a411e94 | 104 | button_status[b] = BUTTON_BUSY; |
| uehara00 | 3:eec13a411e94 | 105 | return true; // one shot |
| uehara00 | 3:eec13a411e94 | 106 | } |
| uehara00 | 3:eec13a411e94 | 107 | else if((f != 0) && (button_count[b] > BUTTON_FAST) && (button_count[b] % BUTTON_PERIOD_FAST == 0)) { |
| uehara00 | 3:eec13a411e94 | 108 | return true; // repeat fast |
| uehara00 | 3:eec13a411e94 | 109 | } |
| uehara00 | 3:eec13a411e94 | 110 | else if((r != 0) && (button_count[b] > BUTTON_REPEAT) && (button_count[b] % BUTTON_PERIOD_REPEAT == 0)) { |
| uehara00 | 3:eec13a411e94 | 111 | return true; // repeat |
| uehara00 | 3:eec13a411e94 | 112 | } |
| uehara00 | 3:eec13a411e94 | 113 | else { |
| uehara00 | 3:eec13a411e94 | 114 | return false; |
| uehara00 | 3:eec13a411e94 | 115 | } |
| uehara00 | 3:eec13a411e94 | 116 | } |
| uehara00 | 3:eec13a411e94 | 117 | |
| uehara00 | 3:eec13a411e94 | 118 | // exit to normal mode |
| uehara00 | 3:eec13a411e94 | 119 | void button_exit() { |
| uehara00 | 3:eec13a411e94 | 120 | LCD_clear_display(); |
| uehara00 | 3:eec13a411e94 | 121 | console_display = DISPLAY_ON; |
| uehara00 | 3:eec13a411e94 | 122 | enter_mode_clock(); |
| uehara00 | 3:eec13a411e94 | 123 | } |
| uehara00 | 3:eec13a411e94 | 124 | |
| uehara00 | 3:eec13a411e94 | 125 | // select a function |
| uehara00 | 3:eec13a411e94 | 126 | static void button_function() { |
| uehara00 | 3:eec13a411e94 | 127 | switch(console_mode) { |
| uehara00 | 3:eec13a411e94 | 128 | case MODE_CLOCK: enter_mode_adjust(); break; |
| uehara00 | 3:eec13a411e94 | 129 | case MODE_ADJUST: enter_mode_alarm(); break; |
| uehara00 | 3:eec13a411e94 | 130 | case MODE_ALARM: enter_mode_timer(); break; |
| uehara00 | 3:eec13a411e94 | 131 | case MODE_TIMER: enter_mode_test(); break; |
| uehara00 | 3:eec13a411e94 | 132 | case MODE_TEST: test_function(); break; |
| uehara00 | 3:eec13a411e94 | 133 | default: button_exit(); |
| uehara00 | 3:eec13a411e94 | 134 | } |
| uehara00 | 3:eec13a411e94 | 135 | } |
| uehara00 | 3:eec13a411e94 | 136 | |
| uehara00 | 3:eec13a411e94 | 137 | // move cursor |
| uehara00 | 3:eec13a411e94 | 138 | // flag 0:left, 1:right |
| uehara00 | 3:eec13a411e94 | 139 | static void button_cursor_move(int flag) { |
| uehara00 | 3:eec13a411e94 | 140 | switch(console_mode) { |
| uehara00 | 3:eec13a411e94 | 141 | case MODE_ADJUST: cursor_move_adjust(flag); break; |
| uehara00 | 3:eec13a411e94 | 142 | case MODE_ALARM: cursor_move_alarm( flag); break; |
| uehara00 | 3:eec13a411e94 | 143 | case MODE_TIMER: cursor_move_timer( flag); break; |
| uehara00 | 3:eec13a411e94 | 144 | case MODE_TEST: cursor_move_test( flag); break; |
| uehara00 | 3:eec13a411e94 | 145 | default: cursor_move_clock( flag); |
| uehara00 | 3:eec13a411e94 | 146 | } |
| uehara00 | 3:eec13a411e94 | 147 | } |
| uehara00 | 3:eec13a411e94 | 148 | |
| uehara00 | 3:eec13a411e94 | 149 | // move cursor common |
| uehara00 | 3:eec13a411e94 | 150 | // flag 0:left, 1:right |
| uehara00 | 3:eec13a411e94 | 151 | void cursor_move(int flag, int cursor_size) { |
| uehara00 | 3:eec13a411e94 | 152 | switch(flag) { |
| uehara00 | 3:eec13a411e94 | 153 | case 0: // move left .. decrement |
| uehara00 | 3:eec13a411e94 | 154 | if(console_cursor > 0) { |
| uehara00 | 3:eec13a411e94 | 155 | --console_cursor; |
| uehara00 | 3:eec13a411e94 | 156 | } |
| uehara00 | 3:eec13a411e94 | 157 | else { |
| uehara00 | 3:eec13a411e94 | 158 | console_cursor = cursor_size - 1; |
| uehara00 | 3:eec13a411e94 | 159 | } |
| uehara00 | 3:eec13a411e94 | 160 | break; |
| uehara00 | 3:eec13a411e94 | 161 | default: // move right .. increment |
| uehara00 | 3:eec13a411e94 | 162 | ++console_cursor; |
| uehara00 | 3:eec13a411e94 | 163 | if(console_cursor >= cursor_size) { |
| uehara00 | 3:eec13a411e94 | 164 | console_cursor = 0; |
| uehara00 | 3:eec13a411e94 | 165 | } |
| uehara00 | 3:eec13a411e94 | 166 | } |
| uehara00 | 3:eec13a411e94 | 167 | } |
| uehara00 | 3:eec13a411e94 | 168 | |
| uehara00 | 3:eec13a411e94 | 169 | // increment/decrement |
| uehara00 | 3:eec13a411e94 | 170 | // flag 0:decrement, 1:increment |
| uehara00 | 3:eec13a411e94 | 171 | static void button_xxcrement(int flag) { |
| uehara00 | 3:eec13a411e94 | 172 | switch(console_mode) { |
| uehara00 | 3:eec13a411e94 | 173 | case MODE_ADJUST: |
| uehara00 | 3:eec13a411e94 | 174 | button_xxcrement_adjust(flag); |
| uehara00 | 3:eec13a411e94 | 175 | break; |
| uehara00 | 3:eec13a411e94 | 176 | case MODE_ALARM: |
| uehara00 | 3:eec13a411e94 | 177 | button_xxcrement_alarm(flag); |
| uehara00 | 3:eec13a411e94 | 178 | break; |
| uehara00 | 3:eec13a411e94 | 179 | case MODE_TIMER: |
| uehara00 | 3:eec13a411e94 | 180 | button_xxcrement_timer(flag); |
| uehara00 | 3:eec13a411e94 | 181 | break; |
| uehara00 | 3:eec13a411e94 | 182 | case MODE_TEST: |
| uehara00 | 3:eec13a411e94 | 183 | button_xxcrement_test(flag); |
| uehara00 | 3:eec13a411e94 | 184 | break; |
| uehara00 | 3:eec13a411e94 | 185 | default: |
| uehara00 | 3:eec13a411e94 | 186 | button_xxcrement_clock(flag); |
| uehara00 | 3:eec13a411e94 | 187 | } |
| uehara00 | 3:eec13a411e94 | 188 | } |