Yoshiyuki Uehara / Mbed 2 deprecated Maple

Dependencies:   mbed

Revision:
3:eec13a411e94
Parent:
2:299a1c9a5795
diff -r 299a1c9a5795 -r eec13a411e94 Maple_alarm_clock.cpp
--- a/Maple_alarm_clock.cpp	Sun Oct 16 01:27:35 2011 +0000
+++ b/Maple_alarm_clock.cpp	Sun Oct 30 21:20:23 2011 +0000
@@ -8,293 +8,203 @@
 //====================================================================
 // MAPLE board[MARM01-BASE]
 // alarm clock
+#include "Maple_console.h"
 #include "Maple_alarm_clock.h"
 #include "Maple_RTC.h"
 #include "Maple_I2C.h"
-#include "Maple_LCD.h"
 #include "Maple.h"
 #include "mbed.h"
 
-// timer interrupt to refresh display
-Ticker alarm_clock_refresh_tick;
+// global variables defined in console.cpp
+extern int console_mode;
+extern int console_cursor;
 
-// global variables
-int alarm_clock_mode;
-int alarm_clock_cursor;
-int button_count[6];
-int button_status[6];
+// make display data of RTC clock for console
+void display_clock(char row0[], char row1[], int &cursor_r, int &cursor_c) {
+    const int position_r[CURSOR_CLOCK_SIZE] = {  1,  1};
+    const int position_c[CURSOR_CLOCK_SIZE] = { 12, 15};
+    char d[17], s[4];
 
-// alarm_clock_initialize
-void alarm_clock_initialize() {
-    alarm_clock_refresh_tick.attach(&alarm_clock_refresh, ALARM_CLOCK_REFRESH_RATE);
-    alarm_clock_mode = MODE_NORMAL;
-    alarm_clock_cursor = CURSOR_NORMAL_ALARM_FLAG;
-    for (int i = 0; i < 6; ++i) {
-        button_count[i] = 0;
-        button_status[i] = BUTTON_IDLE;
+    i2c_RTC_read(RTC_REG_CONTROL1, d, 16);
+    copy_string(row0, 0, 17, "XXXX.XX.XX XXX  ");
+    copy_string(row1, 0, 17, "XX:XX:XX XXXXXXX");
+    if((d[RTC_REG_SECOND] & RTC_VOLTAGE_LOW) == 0) {    // when RTC registers are valid: 
+        copy_string(row0, 0, 2, "20");
+        if((d[RTC_REG_MONTH_CENTURY   ] & RTC_CENTURY               ) != 0) { row0[ 1] = '1'; }
+        copy_string(row0,  2, 2, int_to_hex2(d[RTC_REG_YEAR         ]       , s));
+        copy_string(row0,  5, 2, int_to_hex2(d[RTC_REG_MONTH_CENTURY] & 0x1f, s));
+        copy_string(row0,  8, 2, int_to_hex2(d[RTC_REG_DAY          ] & 0x3f, s));
+        copy_string(row0, 11, 3, weekday_to_string(d[RTC_REG_WEEKDAY] & 0x07, s));
+        copy_string(row1,  0, 2, int_to_hex2(d[RTC_REG_HOUR         ] & 0x3f, s));
+        copy_string(row1,  3, 2, int_to_hex2(d[RTC_REG_MINUTE       ] & 0x7f, s));
+        copy_string(row1,  6, 2, int_to_hex2(d[RTC_REG_SECOND       ] & 0x7f, s));
+        copy_string(row1,  9, 7, "-------");
+        if((d[RTC_REG_CONTROL2        ] & RTC_TIMER_PERIODIC        ) != 0) { row1[ 9] = 'p'; }
+        if((d[RTC_REG_TIMER_CONTROL   ] & RTC_TIMER_ENABLE          ) != 0) { row1[10] = 't'; }
+        if((d[RTC_REG_CONTROL2        ] & RTC_TIMER_INTERRUPT_ENABLE) != 0) { row1[11] = 'i'; }
+        if((d[RTC_REG_CONTROL2        ] & RTC_TIMER_FLAG            ) != 0) { row1[12] = 'T'; }
+        if((d[RTC_REG_ALARM_MINUTE    ] & RTC_ALARM_DISABLE         ) == 0) { row1[13] = 'a'; }
+        if((d[RTC_REG_ALARM_HOUR      ] & RTC_ALARM_DISABLE         ) == 0) { row1[13] = 'a'; }
+        if((d[RTC_REG_ALARM_DAY       ] & RTC_ALARM_DISABLE         ) == 0) { row1[13] = 'a'; }
+        if((d[RTC_REG_ALARM_WEEKDAY   ] & RTC_ALARM_DISABLE         ) == 0) { row1[13] = 'a'; }
+        if((d[RTC_REG_CONTROL2        ] & RTC_ALARM_INTERRUPT_ENABLE) != 0) { row1[14] = 'i'; }
+        if((d[RTC_REG_CONTROL2        ] & RTC_ALARM_FLAG            ) != 0) { row1[15] = 'A'; }
     }
+    cursor_r = position_r[console_cursor];
+    cursor_c = position_c[console_cursor];
 }
 
-// refresh display, called by a ticker
-void alarm_clock_refresh() {
-    alarm_clock_refresh_display();
-    button_check();
+// make display data of RTC adjust for console
+void display_adjust(char row0[], char row1[], int &cursor_r, int &cursor_c) {
+    const int position_r[CURSOR_ADJUST_SIZE] = {  0,  0,  0,  0,  1,  1,  1};
+    const int position_c[CURSOR_ADJUST_SIZE] = {  1,  3,  6,  9,  1,  4,  7};
+    char d[17], s[4];
+
+    i2c_RTC_read(RTC_REG_CONTROL1, d, 16);
+    copy_string(row0, 0, 17, "XXXX.XX.XX XXX  ");
+    copy_string(row1, 0, 17, "XX:XX:XX  adjust");
+    if((d[RTC_REG_SECOND] & RTC_VOLTAGE_LOW) == 0) {    // when RTC registers are valid: 
+        copy_string(row0, 0, 2, "20");
+        if((d[RTC_REG_MONTH_CENTURY   ] & RTC_CENTURY               ) != 0) { row0[ 1] = '1'; }
+        copy_string(row0,  2, 2, int_to_hex2(d[RTC_REG_YEAR         ]       , s));
+        copy_string(row0,  5, 2, int_to_hex2(d[RTC_REG_MONTH_CENTURY] & 0x1f, s));
+        copy_string(row0,  8, 2, int_to_hex2(d[RTC_REG_DAY          ] & 0x3f, s));
+        copy_string(row0, 11, 3, weekday_to_string(d[RTC_REG_WEEKDAY] & 0x07, s));
+        copy_string(row1,  0, 2, int_to_hex2(d[RTC_REG_HOUR         ] & 0x3f, s));
+        copy_string(row1,  3, 2, int_to_hex2(d[RTC_REG_MINUTE       ] & 0x7f, s));
+        copy_string(row1,  6, 2, int_to_hex2(d[RTC_REG_SECOND       ] & 0x7f, s));
+    }
+    cursor_r = position_r[console_cursor];
+    cursor_c = position_c[console_cursor];
 }
 
-// read RTC and print to LCD
-static void alarm_clock_refresh_display() {
-    const int cursor_row[4][10] = {
-        { 1,  1, 16, 16, 16, 16, 16, 16, 16, 16},   // MODE_NORMAL
-        { 0,  0,  0,  0,  1,  1,  1, 16, 16, 16},   // MODE_ADJUST
-        { 0,  0,  0,  0,  0,  0,  1,  1,  1,  1},   // MODE_ALARM
-        { 0,  0,  0,  0,  0,  0,  0,  1, 16, 16},   // MODE_TIMER
-    };
-    const int cursor_column[4][10] = {
-        {12, 15, 16, 16, 16, 16, 16, 16, 16, 16},   // NODE_NORMAL
-        { 1,  3,  6,  9,  1,  4,  7, 16, 16, 16},   // MODE_ADJUST
-        { 4,  6,  8, 11, 14, 15,  0,  2,  4,  6},   // MODE_ALARM
-        { 3,  8, 11, 12, 13, 14, 15,  6, 16, 16},   // MODE_TIMER
-    };
-    const char frequency_select[4][6] = { "32768", " 1024", "   32", "    1" };
-    const char timer_select[4][5] = { "4096", "  64", "   1", "1/60" };
-    char row0[17], row1[17], d[17], s[4];
+// make display data of RTC alarm for console
+void display_alarm(char row0[], char row1[], int &cursor_r, int &cursor_c) {
+    const int position_r[CURSOR_ALARM_SIZE] = {  0,  0,  0,  0,  0,  0,  1,  1,  1,  1};
+    const int position_c[CURSOR_ALARM_SIZE] = {  4,  6,  8, 11, 14, 15,  0,  2,  4,  6};
+    char d[17], s[4];
 
     i2c_RTC_read(RTC_REG_CONTROL1, d, 16);
-
-    switch(alarm_clock_mode) {
-    case MODE_NORMAL:
-        copy_string(row0, 0, 17, "XXXX.XX.XX XXX  ");
-        copy_string(row1, 0, 17, "XX:XX:XX XXXXXXX");
-        if((d[RTC_REG_SECOND] & RTC_VOLTAGE_LOW) == 0) {    // when RTC registers are valid: 
-            copy_string(row0, 0, 2, "20");
-            if((d[RTC_REG_MONTH_CENTURY   ] & RTC_CENTURY               ) != 0) { row0[ 1] = '1'; }
-            copy_string(row0,  2, 2, int_to_hex2(d[RTC_REG_YEAR         ]       , s));
-            copy_string(row0,  5, 2, int_to_hex2(d[RTC_REG_MONTH_CENTURY] & 0x1f, s));
-            copy_string(row0,  8, 2, int_to_hex2(d[RTC_REG_DAY          ] & 0x3f, s));
-            copy_string(row0, 11, 3, weekday_to_string(d[RTC_REG_WEEKDAY] & 0x07, s));
-            copy_string(row1,  0, 2, int_to_hex2(d[RTC_REG_HOUR         ] & 0x3f, s));
-            copy_string(row1,  3, 2, int_to_hex2(d[RTC_REG_MINUTE       ] & 0x7f, s));
-            copy_string(row1,  6, 2, int_to_hex2(d[RTC_REG_SECOND       ] & 0x7f, s));
-            copy_string(row1,  9, 7, "-------");
-            if((d[RTC_REG_CONTROL2        ] & RTC_TIMER_PERIODIC        ) != 0) { row1[ 9] = 'p'; }
-            if((d[RTC_REG_TIMER_CONTROL   ] & RTC_TIMER_ENABLE          ) != 0) { row1[10] = 't'; }
-            if((d[RTC_REG_CONTROL2        ] & RTC_TIMER_INTERRUPT_ENABLE) != 0) { row1[11] = 'i'; }
-            if((d[RTC_REG_CONTROL2        ] & RTC_TIMER_FLAG            ) != 0) { row1[12] = 'T'; }
-            if((d[RTC_REG_ALARM_MINUTE    ] & RTC_ALARM_DISABLE         ) == 0) { row1[13] = 'a'; }
-            if((d[RTC_REG_ALARM_HOUR      ] & RTC_ALARM_DISABLE         ) == 0) { row1[13] = 'a'; }
-            if((d[RTC_REG_ALARM_DAY       ] & RTC_ALARM_DISABLE         ) == 0) { row1[13] = 'a'; }
-            if((d[RTC_REG_ALARM_WEEKDAY   ] & RTC_ALARM_DISABLE         ) == 0) { row1[13] = 'a'; }
-            if((d[RTC_REG_CONTROL2        ] & RTC_ALARM_INTERRUPT_ENABLE) != 0) { row1[14] = 'i'; }
-            if((d[RTC_REG_CONTROL2        ] & RTC_ALARM_FLAG            ) != 0) { row1[15] = 'A'; }
-        }
-        break;
-
-    case MODE_ADJUST:
-        copy_string(row0, 0, 17, "XXXX.XX.XX XXX  ");
-        copy_string(row1, 0, 17, "XX:XX:XX  adjust");
-        if((d[RTC_REG_SECOND] & RTC_VOLTAGE_LOW) == 0) {    // when RTC registers are valid: 
-            copy_string(row0, 0, 2, "20");
-            if((d[RTC_REG_MONTH_CENTURY   ] & RTC_CENTURY               ) != 0) { row0[ 1] = '1'; }
-            copy_string(row0,  2, 2, int_to_hex2(d[RTC_REG_YEAR         ]       , s));
-            copy_string(row0,  5, 2, int_to_hex2(d[RTC_REG_MONTH_CENTURY] & 0x1f, s));
-            copy_string(row0,  8, 2, int_to_hex2(d[RTC_REG_DAY          ] & 0x3f, s));
-            copy_string(row0, 11, 3, weekday_to_string(d[RTC_REG_WEEKDAY] & 0x07, s));
-            copy_string(row1,  0, 2, int_to_hex2(d[RTC_REG_HOUR         ] & 0x3f, s));
-            copy_string(row1,  3, 2, int_to_hex2(d[RTC_REG_MINUTE       ] & 0x7f, s));
-            copy_string(row1,  6, 2, int_to_hex2(d[RTC_REG_SECOND       ] & 0x7f, s));
-        }
-        break;
+    copy_string(row0, 0, 17, "day:XXX XXXX XXX");
+    copy_string(row1, 0, 17, "XXX:XXX    alarm");
+    if((d[RTC_REG_SECOND] & RTC_VOLTAGE_LOW) == 0) {    // when RTC registers are valid: 
+        row0[ 4] = '-';
+        row0[ 8] = '-';
+        copy_string(row0, 13, 3, "---");
+        row1[ 0] = '-';
+        row1[ 4] = '-';
+        copy_string(row0, 5, 2, int_to_hex2(d[RTC_REG_ALARM_DAY          ] & 0x3f, s));
+        copy_string(row0, 9, 3, weekday_to_string(d[RTC_REG_ALARM_WEEKDAY] & 0x07, s));
+        copy_string(row1, 1, 2, int_to_hex2(d[RTC_REG_ALARM_HOUR         ] & 0x3f, s));
+        copy_string(row1, 5, 2, int_to_hex2(d[RTC_REG_ALARM_MINUTE       ] & 0x7f, s));
+        if((d[RTC_REG_ALARM_DAY    ] & RTC_ALARM_DISABLE         ) == 0) { row0[13] = 'a';  row0[ 4] = '*'; }
+        if((d[RTC_REG_ALARM_WEEKDAY] & RTC_ALARM_DISABLE         ) == 0) { row0[13] = 'a';  row0[ 8] = '*'; }
+        if((d[RTC_REG_ALARM_HOUR   ] & RTC_ALARM_DISABLE         ) == 0) { row0[13] = 'a';  row1[ 0] = '*'; }
+        if((d[RTC_REG_ALARM_MINUTE ] & RTC_ALARM_DISABLE         ) == 0) { row0[13] = 'a';  row1[ 4] = '*'; }
+        if((d[RTC_REG_CONTROL2     ] & RTC_ALARM_INTERRUPT_ENABLE) != 0) { row0[14] = 'i'; }
+        if((d[RTC_REG_CONTROL2     ] & RTC_ALARM_FLAG            ) != 0) { row0[15] = 'A'; }
+    }
+    cursor_r = position_r[console_cursor];
+    cursor_c = position_c[console_cursor];
+}
 
-    case MODE_ALARM:
-        copy_string(row0, 0, 17, "day:XXX XXXX XXX");
-        copy_string(row1, 0, 17, "XXX:XXX    alarm");
-        if((d[RTC_REG_SECOND] & RTC_VOLTAGE_LOW) == 0) {    // when RTC registers are valid: 
-            row0[ 4] = '-';
-            row0[ 8] = '-';
-            copy_string(row0, 13, 3, "---");
-            row1[ 0] = '-';
-            row1[ 4] = '-';
-            copy_string(row0, 5, 2, int_to_hex2(d[RTC_REG_ALARM_DAY          ] & 0x3f, s));
-            copy_string(row0, 9, 3, weekday_to_string(d[RTC_REG_ALARM_WEEKDAY] & 0x07, s));
-            copy_string(row1, 1, 2, int_to_hex2(d[RTC_REG_ALARM_HOUR         ] & 0x3f, s));
-            copy_string(row1, 5, 2, int_to_hex2(d[RTC_REG_ALARM_MINUTE       ] & 0x7f, s));
-            if((d[RTC_REG_ALARM_DAY    ] & RTC_ALARM_DISABLE         ) == 0) { row0[13] = 'a';  row0[ 4] = '*'; }
-            if((d[RTC_REG_ALARM_WEEKDAY] & RTC_ALARM_DISABLE         ) == 0) { row0[13] = 'a';  row0[ 8] = '*'; }
-            if((d[RTC_REG_ALARM_HOUR   ] & RTC_ALARM_DISABLE         ) == 0) { row0[13] = 'a';  row1[ 0] = '*'; }
-            if((d[RTC_REG_ALARM_MINUTE ] & RTC_ALARM_DISABLE         ) == 0) { row0[13] = 'a';  row1[ 4] = '*'; }
-            if((d[RTC_REG_CONTROL2     ] & RTC_ALARM_INTERRUPT_ENABLE) != 0) { row0[14] = 'i'; }
-            if((d[RTC_REG_CONTROL2     ] & RTC_ALARM_FLAG            ) != 0) { row0[15] = 'A'; }
-        }
-        break;
+// make display data of RTC timer for console
+void display_timer(char row0[], char row1[], int &cursor_r, int &cursor_c) {
+    const int position_r[CURSOR_TIMER_SIZE] = {  0,  0,  0,  0,  0,  0,  0,  1};
+    const int position_c[CURSOR_TIMER_SIZE] = {  3,  8, 11, 12, 13, 14, 15,  6};
+    const char select_f[4][6] = { "32768", " 1024", "   32", "    1" };
+    const char select_t[4][5] = { "4096", "  64", "   1", "1/60" };
+    char d[17], s[3];
 
-    case MODE_TIMER:
-        copy_string(row0, 0, 17, "XXXXHz XX  XXXXX");
-        copy_string(row1, 0, 17, "f=XXXXXHz  timer");
-        if((d[RTC_REG_SECOND] & RTC_VOLTAGE_LOW) == 0) {    // when RTC registers are valid: 
-            copy_string(row0,  0, 4, timer_select[d[RTC_REG_TIMER_CONTROL] & 0x03]);
-            copy_string(row0,  7, 2, int_to_hex2(d[RTC_REG_TIMER], s));
-            copy_string(row1,  2, 5, frequency_select[d[RTC_REG_CLKOUT_FREQUENCY] & 0x03]);
-            copy_string(row0, 11, 5, "-----");
-            if((d[RTC_REG_CLKOUT_FREQUENCY] & RTC_CLKOUT_ENABLE         ) != 0) { row0[11] = 'f'; }
-            if((d[RTC_REG_CONTROL2        ] & RTC_TIMER_PERIODIC        ) != 0) { row0[12] = 'p'; }
-            if((d[RTC_REG_TIMER_CONTROL   ] & RTC_TIMER_ENABLE          ) != 0) { row0[13] = 't'; }
-            if((d[RTC_REG_CONTROL2        ] & RTC_TIMER_INTERRUPT_ENABLE) != 0) { row0[14] = 'i'; }
-            if((d[RTC_REG_CONTROL2        ] & RTC_TIMER_FLAG            ) != 0) { row0[15] = 'T'; }
-        }
-        break;
-    }    
-
-    LCD_cursor(LCD_CURSOR_OFF);
-    LCD_locate(0, 0);
-    LCD_print_string(row0);
-    LCD_locate(1, 0);
-    LCD_print_string(row1);
-    LCD_locate(cursor_row[alarm_clock_mode][alarm_clock_cursor], cursor_column[alarm_clock_mode][alarm_clock_cursor]);
-    LCD_cursor(LCD_CURSOR_ON);
+    i2c_RTC_read(RTC_REG_CONTROL1, d, 16);
+    copy_string(row0, 0, 17, "XXXXHz XX  XXXXX");
+    copy_string(row1, 0, 17, "f=XXXXXHz  timer");
+    if((d[RTC_REG_SECOND] & RTC_VOLTAGE_LOW) == 0) {    // when RTC registers are valid: 
+        copy_string(row0,  0, 4, select_t[d[RTC_REG_TIMER_CONTROL] & 0x03]);
+        copy_string(row0,  7, 2, int_to_hex2(d[RTC_REG_TIMER], s));
+        copy_string(row1,  2, 5, select_f[d[RTC_REG_CLKOUT_FREQUENCY] & 0x03]);
+        copy_string(row0, 11, 5, "-----");
+        if((d[RTC_REG_CLKOUT_FREQUENCY] & RTC_CLKOUT_ENABLE         ) != 0) { row0[11] = 'f'; }
+        if((d[RTC_REG_CONTROL2        ] & RTC_TIMER_PERIODIC        ) != 0) { row0[12] = 'p'; }
+        if((d[RTC_REG_TIMER_CONTROL   ] & RTC_TIMER_ENABLE          ) != 0) { row0[13] = 't'; }
+        if((d[RTC_REG_CONTROL2        ] & RTC_TIMER_INTERRUPT_ENABLE) != 0) { row0[14] = 'i'; }
+        if((d[RTC_REG_CONTROL2        ] & RTC_TIMER_FLAG            ) != 0) { row0[15] = 'T'; }
+    }
+    cursor_r = position_r[console_cursor];
+    cursor_c = position_c[console_cursor];
 }
 
-// read button and update button_on_count[i]
-//   called by timer tick interrupt
-static void button_check() {
-    char r = i2c_BTN_read();
-    button_on_check(r, BUTTON_A    );
-    button_on_check(r, BUTTON_B    );
-    button_on_check(r, BUTTON_LEFT );
-    button_on_check(r, BUTTON_DOWN );
-    button_on_check(r, BUTTON_RIGHT);
-    button_on_check(r, BUTTON_UP   );
-    if(button_trigger(BUTTON_A,     0, 0)) { button_exit();         }
-    if(button_trigger(BUTTON_B,     0, 0)) { button_function();     }
-    if(button_trigger(BUTTON_LEFT,  1, 0)) { button_cursor_move(0); }
-    if(button_trigger(BUTTON_RIGHT, 1, 0)) { button_cursor_move(1); }
-    if(button_trigger(BUTTON_DOWN,  1, 1)) { button_xxcrement(0);   }
-    if(button_trigger(BUTTON_UP,    1, 1)) { button_xxcrement(1);   }
+// enter mode clock
+void enter_mode_clock() {
+    console_mode = MODE_CLOCK;
+    console_cursor = CURSOR_CLOCK_INIT;
 }
 
-// button on check
-static void button_on_check(int read_data, int button_number) {
-    if((read_data & (0x01 << button_number)) == BUTTON_ON) {
-        ++button_count[button_number];
-    }
-    else {
-        button_count[button_number] = 0;
-        button_status[button_number] = BUTTON_IDLE;
-    }
+// enter mode adjust
+void enter_mode_adjust() {
+    console_mode = MODE_ADJUST;
+    console_cursor = CURSOR_ADJUST_INIT;
 }
 
-// button action trigger check
-//   b: button number
-//   r: repeat enable
-//   f: fast-repeat enable
-static bool button_trigger(int b, int r, int f) {
-    if((button_status[b] == BUTTON_IDLE) && (button_count[b] > BUTTON_THRESHOLD)) {
-        button_status[b] = BUTTON_BUSY;
-        return true;    // one shot
-    }
-    else if((f != 0) && (button_count[b] > BUTTON_FAST) && (button_count[b] % BUTTON_PERIOD_FAST == 0)) {
-        return true;    // repeat fast
-    }
-    else if((r != 0) && (button_count[b] > BUTTON_REPEAT) && (button_count[b] % BUTTON_PERIOD_REPEAT == 0)) {
-        return true;    // repeat
-    }
-    else {
-        return false;
-    }
+// enter mode alarm
+void enter_mode_alarm() {
+    console_mode = MODE_ALARM;
+    console_cursor = CURSOR_ALARM_INIT;
 }
 
-// exit to normal mode
-static void button_exit() {
-    alarm_clock_mode = MODE_NORMAL;
-    alarm_clock_cursor = CURSOR_NORMAL_ALARM_FLAG;
+// enter mode timer
+void enter_mode_timer() {
+    console_mode = MODE_TIMER;
+    console_cursor = CURSOR_TIMER_INIT;
 }
 
-// select a function
-static void button_function() {
-    switch(alarm_clock_mode) {
-    case MODE_NORMAL:
-        alarm_clock_mode = MODE_ADJUST;
-        alarm_clock_cursor = CURSOR_ADJUST_SECOND;
-        break;
-    case MODE_ADJUST:
-        alarm_clock_mode = MODE_ALARM;
-        alarm_clock_cursor = CURSOR_ALARM_FLAG;
-        break;
-    case MODE_ALARM:
-        alarm_clock_mode = MODE_TIMER;
-        alarm_clock_cursor = CURSOR_TIMER_FLAG;
-        break;
-    case MODE_TIMER:
-        alarm_clock_mode = MODE_NORMAL;
-        alarm_clock_cursor = CURSOR_NORMAL_ALARM_FLAG;
-        break;
-    }
+// move cursor of clock
+//   flag  0:left, 1:right
+void cursor_move_clock(int flag) {
+    cursor_move(flag, CURSOR_CLOCK_SIZE);
+}
+
+// move cursor of adjust
+//   flag  0:left, 1:right
+void cursor_move_adjust(int flag) {
+    cursor_move(flag, CURSOR_ADJUST_SIZE);
 }
 
-// move cursor
+// move cursor of alarm
 //   flag  0:left, 1:right
-static void button_cursor_move(int flag) {
-    const int max[4] = { CURSOR_NORMAL_MAX, CURSOR_ADJUST_MAX, CURSOR_ALARM_MAX, CURSOR_TIMER_MAX };
-
-    switch(flag) {
-    case 0:  // move left .. decrement
-        if(alarm_clock_cursor > 0) {
-            --alarm_clock_cursor;
-        }
-        else {
-            alarm_clock_cursor = max[alarm_clock_mode];
-        }
-        break;
-    case 1:  // move right .. increment
-        ++alarm_clock_cursor;
-        if(alarm_clock_cursor > max[alarm_clock_mode]) {
-            alarm_clock_cursor = 0;
-        }
-        break;
-    }
+void cursor_move_alarm(int flag) {
+    cursor_move(flag, CURSOR_ALARM_SIZE);
 }
 
-// increment/decrement
-//   flag 0:decrement, 1:increment
-static void button_xxcrement(int flag) {
-    switch(alarm_clock_mode) {
-    case MODE_NORMAL:
-        button_xxcrement_normal(flag);
-        break;
-    case MODE_ADJUST:
-        button_xxcrement_adjust(flag);
-        break;
-    case MODE_ALARM:
-        button_xxcrement_alarm(flag);
-        break;
-    case MODE_TIMER:
-        button_xxcrement_timer(flag);
-        break;
-    }
+// move cursor of timer
+//   flag  0:left, 1:right
+void cursor_move_timer(int flag) {
+    cursor_move(flag, CURSOR_TIMER_SIZE);
 }
 
-// increment/decrement for NORMAL
+// increment/decrement of clock
 //   flag  0:decrement, 1:increment
-static void button_xxcrement_normal(int flag) {
+void button_xxcrement_clock(int flag) {
     char d;
 
     d = RTC_read1(RTC_REG_CONTROL2);
-    switch(alarm_clock_cursor) {
-    case CURSOR_NORMAL_TIMER_FLAG:
+    switch(console_cursor) {
+    case CURSOR_CLOCK_TIMER_FLAG:
         d = d & 0x1b | (flag == 0 ? 0x00 : 0x04);
         break;
-    case CURSOR_NORMAL_ALARM_FLAG:
+    case CURSOR_CLOCK_ALARM_FLAG:
         d = d & 0x17 | (flag == 0 ? 0x00 : 0x08);
         break;
     }
     RTC_write1(RTC_REG_CONTROL2, d);
 }
 
-// increment/decrement for ADJUST
+// increment/decrement of adjust
 //   flag  0:decrement, 1:increment
-static void button_xxcrement_adjust(int flag) {
+void button_xxcrement_adjust(int flag) {
     char d;
 
-    switch(alarm_clock_cursor) {
+    switch(console_cursor) {
     case CURSOR_ADJUST_HOUR:
         d = RTC_stop_read1(RTC_REG_HOUR) & 0x3F;
         d = xxcrement_bcd(flag, d, 0x00, 0x23);
@@ -321,7 +231,7 @@
     char century, bcd_year, bcd_month, bcd_day, weekday, d;
 
     RTC_stop_read_date(century, bcd_year, bcd_month, bcd_day);
-    switch(alarm_clock_cursor) {
+    switch(console_cursor) {
     case CURSOR_ADJUST_CENTURY:
         century = xxcrement_bcd(flag, century, 0x00, 0x01);
         break;    
@@ -343,12 +253,12 @@
     RTC_write_date_start(century, bcd_year, bcd_month, bcd_day, weekday);
 }
 
-// increment/decrement for ALARM
+// increment/decrement of alarm
 //   flag  0:decrement, 1:increment
-static void button_xxcrement_alarm(int flag) {
+void button_xxcrement_alarm(int flag) {
     char d;
 
-    switch(alarm_clock_cursor) {
+    switch(console_cursor) {
     case CURSOR_ALARM_DAY_DISABLE:
         d = RTC_read1(RTC_REG_ALARM_DAY) & 0x3f | (flag == 0 ? 0x80 : 0x00);
         RTC_write1(RTC_REG_ALARM_DAY, d);
@@ -396,12 +306,12 @@
     }
 }
 
-// increment/decrement for TIMER
+// increment/decrement of timer
 //   flag  0:decrement, 1:increment
-static void button_xxcrement_timer(int flag) {
+void button_xxcrement_timer(int flag) {
     char d;
 
-    switch(alarm_clock_cursor) {
+    switch(console_cursor) {
     case CURSOR_TIMER_SELECT:
         d = RTC_read1(RTC_REG_TIMER_CONTROL);
         d = xxcrement_bcd(flag, d & 0x03, 0x00, 0x03) | d & 0x80;
@@ -410,8 +320,8 @@
     case CURSOR_TIMER_COUNT:
         d = RTC_read1(RTC_REG_TIMER);
         switch(flag) {
-        case 0: --d; break;
-        case 1: ++d; break;
+        case 0:  --d; break;
+        default: ++d;
         }
         RTC_write1(RTC_REG_TIMER, d);
         break;