OSDN Git Service

[DOC][v1.0] Update documents for 1.0RC2.
[openi2cradio/OpenI2CRadio.git] / ui.c
diff --git a/ui.c b/ui.c
index 9aa45d8..6db4e20 100644 (file)
--- a/ui.c
+++ b/ui.c
@@ -63,6 +63,47 @@ char keyin_counter;
 
 unsigned char cold;
 
+// LCD Routines
+void _PUTCHAR(unsigned char c)
+{
+    acm1602_putchar(LCD_I2CADDR , c);
+}
+
+void _LOCATE(unsigned char x, unsigned char y)
+{
+    acm1602_locate_16x2(LCD_I2CADDR , x, y);
+}
+
+void _CLS(void)
+{
+    acm1602_cls(LCD_I2CADDR);
+}
+void _HOME(void)
+{
+    acm1602_home(LCD_I2CADDR);
+}
+
+void _CURSOR_ON(void)
+{
+    acm1602_dispcursor(LCD_I2CADDR, 0xff);
+}
+
+
+void _CURSOR_OFF(void)
+{
+    acm1602_dispcursor(LCD_I2CADDR, 0x00);
+}
+
+void _CURSOR_LEFT(void)
+{
+    acm1602_cursordir(LCD_I2CADDR , 0x00);
+}
+
+void _CURSOR_RIGHT(void)
+{
+    acm1602_cursordir(LCD_I2CADDR , 0xff);
+}
+
 void keyin_init(void)
 {
     char i;
@@ -93,7 +134,7 @@ void push_keyinfifo(char b)
     keyin_fifo[keyin_nowp] = b;
     keyin_nowp++;
     keyin_counter++;
-    if((keyin_nowp > 31) || (keyin_nowp < 0)) keyin_nowp = 0;
+    if(keyin_nowp > 31) keyin_nowp = 0;
 }
 
 /*
@@ -113,14 +154,14 @@ char pop_keyinfifo(void)
     c = keyin_fifo[keyin_readp];
     keyin_readp++;
     keyin_counter--;
-    if((keyin_readp > 31) || (keyin_readp < 0)) keyin_readp = 0;
+    if(keyin_readp > 31) keyin_readp = 0;
     return c;
 }
 
 void printstr(char *s)
 {
     int p = 0;
-    _CURSOR_RIGHT();
+//    _CURSOR_RIGHT();
     if(s == NULL) return;
     do {
         if(s[p] == '\0') break;
@@ -131,26 +172,26 @@ void printstr(char *s)
 
 
 
-void uint2bcd(unsigned long data, unsigned char *bcd)
+static void uint2bcd(unsigned long data, unsigned char *bcd)
 {
     unsigned char i;
     unsigned char j;
 
-    for(i = 0; i < 5; i++){
+    for(i = 0; i <= 10; i++){
         bcd[i] = data % 10;
         data = data / 10;
     }
-    bcd[i] = 0;
+    bcd[10] = 0;
 }
 
 void print_numeric_nosupress(unsigned long data, unsigned char digit)
 {
     unsigned char i;
-    unsigned char bcd[6];
+    unsigned char bcd[11];
 
 
     if(digit == 0) return;
-    if(digit >= 5) digit = 5;
+    if(digit > 10) digit = 10;
     uint2bcd(data, bcd);
     for(i = digit; i > 0; i--){
         _PUTCHAR('0' + bcd[i - 1]);
@@ -162,22 +203,24 @@ void print_numeric_nosupress(unsigned long data, unsigned char digit)
 unsigned long subst_numeric(unsigned long start, unsigned char pos, unsigned char c)
 {
     unsigned long val;
+    unsigned char bcd[11];
     char i;
-    unsigned int fact;
-    unsigned char bcd[6];
+    unsigned long fact;
 
-    if(pos > 4) pos = 4;
+    if(pos > 10) pos = 10;
     uint2bcd(start, bcd);
     bcd[pos] = c;
+    
+    fact = 1;
     val = 0;
-    for(i = 0; i < 5; i++) {
-        val = val * 10;
-        val = val + bcd[pos];
+    for(i = 0; i < 11; i++){
+        val = val + (bcd[i] * fact);
+        fact = fact * 10;
     }
     return val;
 }
 
-unsigned int read_numeric(unsigned int initial, unsigned char digit,
+unsigned long read_numeric(unsigned int initial, unsigned char digit,
         char startx, char starty)
 {
     unsigned char c;
@@ -191,16 +234,15 @@ unsigned int read_numeric(unsigned int initial, unsigned char digit,
     val =(unsigned long) initial;
     i = d;
     do {
+        _CURSOR_OFF();
        ClrWdt();
         _LOCATE(startx, starty);
         print_numeric_nosupress(val, digit);
        ClrWdt();
-        
-       do {
-           n = pollkeys(pollkeybuf, 60, 0);
-       } while(n == 0);
-       c = pollkeybuf[0];
+        _LOCATE(startx + d -  i, starty);
+       _CURSOR_ON();
 
+        c = pollkey_single();
         if(c == charcode_0){
             val = subst_numeric(val, i, 0);
             i--;
@@ -216,9 +258,7 @@ unsigned int read_numeric(unsigned int initial, unsigned char digit,
             i++;
         } else if(c == charcode_b) {
             // cancel
-            val = initial;
-            i = d;
-            break;
+            return 0xffffffff;
         }  else if(c == charcode_e) {
             i++;
         } else if(c == charcode_d) {
@@ -227,8 +267,9 @@ unsigned int read_numeric(unsigned int initial, unsigned char digit,
        if(i <= 0) i = 0;
        if(i > d) i = d;
     } while(1);
-    if(val > 65535) val = 65535;
-    return (unsigned int)val;
+    _CURSOR_OFF();
+    if(val > 0x7fffffff) val = 0x7fffffff;
+    return val;
 }
 
 unsigned char readkey_compare(void)
@@ -272,79 +313,103 @@ unsigned char readkey(void)
     for(i = 0; i < 9; i++) {
         idle_time_ms(2); // 2ms
         readkey_io(i);
-        ClrWdt();
+//        ClrWdt();
     }
     readkey_compare();
     return pop_keyinfifo();
 }
 
-/*
- * Polling key
- * Max = 32bytes;
- * 0 = Timeout
- * 1~32 = Received.
- * if((limit * 23ms) elapsed), break;
- */
-unsigned char pollkeys(unsigned char *p, unsigned int limit, unsigned char repeat)
+
+unsigned char pollkey_single(void)
 {
-    unsigned int count = 0;
-    unsigned int lifetime = 0;
     unsigned int penalty = 0;
     unsigned char c;
 
+//    cold = charcode_null;
     do {
         idle_time_ms(5); // 5ms.
-        c = readkey(); //
+        c = readkey(); // 2 * 9 = 18ms
         ClrWdt();
         if(c != charcode_null) {
-            push_keyinfifo(c);
-            do {
-                ClrWdt();
-                c = pop_keyinfifo();
-                if(c == charcode_null) {
-                    break;
-                }
-                if(c != cold) {
-                    p[count++] = c;
-                    cold = c;
-                }
-            } while(count < 32);
+            if(cold != c){
+                cold = c;
+                return c;
+            }
             penalty = 0;
         } else {
             penalty++;
-            if((limit > 3) && (penalty > (limit >> 2))){
+            if(penalty > 4) {
                 penalty = 0;
-                cold = charcode_null;
+                cold = charcode_null; // About 100ms
             }
         }
-        if(limit != 0) lifetime++;
-        if(lifetime > limit) break;
-    } while(count < 32);
-    if(repeat != 0) cold = charcode_null;
-    return count;
+    } while(1);
+}
+unsigned char pollkey_numeric(unsigned char init)
+{
+    unsigned char c;
+    c = pollkey_single();
+    if(c == charcode_0) {
+        return 0;
+    } else if((c >= charcode_1) && (c <= charcode_9)){
+        return c;
+    } else {
+        return init;
+    }
 }
 
-unsigned char pollkey_single(void)
+unsigned char pollkey_single_timeout(unsigned int limit, unsigned char repeat)
 {
-    unsigned int penalty = 0;
     unsigned char c;
+    unsigned int ticks = 0;
+    unsigned char penalty = 0;
+    unsigned char count = 0;
+
 
     cold = charcode_null;
+    pollkeybuf[0] = charcode_null;
     do {
-        idle_time_ms(5); // 0.125 * 4 * 20 = 10ms.
+        if(limit != 0) {
+            ticks++;
+            if(ticks > limit) {
+                break;
+            }
+        }
+        idle_time_ms(5); // 5ms.
         c = readkey(); // 2 * 9 = 18ms
         ClrWdt();
-        if(c != charcode_null) {
-            if(cold != c){
+        ticks++;
+        if(c != charcode_null){
+            if(cold != c) {
+                pollkeybuf[count++] = c;
                 cold = c;
-                return c;
+                count++;
+                if(repeat == 0) {
+                        break;
+                }
+                penalty = 0;
+            }
+        }  else {
+            penalty++;
+            if(penalty > 4){
+                penalty = 0;
+                cold = charcode_null;
             }
         }
-        penalty++;
-        if(penalty > 20) cold = charcode_null;
-    } while(1);
-}
+    } while(count < 32);
 
+    /*
+     * Set Deadzone.
+     */
+    if(limit == 0) {
+        while(ticks <= limit) {
+            idle_time_ms(5 + 18);
+            ticks++;
+        }
+    }
+    c = pollkeybuf[0];
+    return c;
+}
 /*
  * Notes:
  * Initialize sequence: