#include "ioports.h"
#include "ui.h"
#include "idle.h"
+#include "commondef.h"
const char charcodemap[] = {charcode_0,
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 _LOCATE_0_0(void)
+{
+ acm1602_locate_16x2(LCD_I2CADDR , 0, 0);
+}
+
+void _LOCATE_0_1(void)
+{
+ acm1602_locate_16x2(LCD_I2CADDR , 0, 1);
+}
+
+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;
#endif
{
if(keyin_counter >= 31) {
- keyin_counter = 31;
- keyin_readp++; // Discard data
-// return;
- if((keyin_readp > 31) || (keyin_readp < 0)) keyin_readp = 0;
+ return; // Discard data.
}
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;
}
/*
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)
+void printstr(const char *s)
{
int p = 0;
- _CURSOR_RIGHT();
+// _CURSOR_RIGHT();
if(s == NULL) return;
do {
if(s[p] == '\0') break;
_PUTCHAR(s[p]);
p++;
- } while(p < 255);
+ } while(p < 32);
+}
+
+
+
+static void uint2bcd(unsigned long data, unsigned char *bcd)
+{
+ unsigned char i;
+ unsigned char j;
+
+ for(i = 0; i <= 10; i++){
+ bcd[i] = data % 10;
+ data = data / 10;
+ }
+ bcd[10] = 0;
}
-void print_numeric_nosupress(unsigned int data, unsigned char digit)
+void print_numeric_nosupress(unsigned long data, unsigned char digit)
{
- unsigned int i;
- int ref = 10;
- int div = 1;
+ unsigned char i;
+ unsigned char bcd[11];
if(digit == 0) return;
- if(digit >= 5) digit = 5;
- if(digit == 5){
- i = data / 10000;
- data = data % 10000;
- _PUTCHAR(i + '0');
- digit--;
- }
- if(digit == 4){
- i = data / 1000;
- data = data % 1000;
- _PUTCHAR(i + '0');
- digit--;
- }
- if(digit == 3){
- i = data / 100;
- data = data % 100;
- _PUTCHAR(i + '0');
- digit--;
- }
- if(digit == 2){
- i = data / 10;
- data = data % 10;
- _PUTCHAR(i + '0');
- digit--;
- }
- if(digit == 1){
- i = data;
- _PUTCHAR(i + '0');
- digit--;
+ if(digit > 10) digit = 10;
+ uint2bcd(data, bcd);
+ for(i = digit; i > 0; i--){
+ _PUTCHAR('0' + bcd[i - 1]);
}
}
/*
* Read Numeric(int)
*/
-unsigned int subst_numeric(unsigned int start, unsigned char pos, unsigned int c)
+unsigned long subst_numeric(unsigned long start, unsigned char pos, unsigned char c)
{
- unsigned int val;
+ unsigned long val;
+ unsigned char bcd[11];
char i;
- unsigned int fact;
- unsigned char bcd[5];
-
- if(pos > 4) pos = 4;
- if((pos == 4) && (c > 6)) return start;
- val = start;
- // binary to BCD
- fact = 10000;
- for(i = 4; i >= 0; i--){
- bcd[i] = val / fact;
- val = val % fact;
- fact = fact / 10;
- }
- // subst
+ unsigned long fact;
+
+ if(pos > 10) pos = 10;
+ uint2bcd(start, bcd);
bcd[pos] = c;
+
+ fact = 1;
val = 0;
- for(i = 3; i >= 0; i--){
- val = val * 10;
- val = val + bcd[i];
+ for(i = 0; i < 11; i++){
+ val = val + (bcd[i] * fact);
+ fact = fact * 10;
}
- if((bcd[4] == 6) && (val > 5535)) return val;
- val = val + bcd[4] * 10000;
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;
unsigned char n;
char i;
- unsigned int val;
+ unsigned long val;
+ unsigned long v;
char d;
d = digit - 1;
- val = initial;
+ 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--;
i++;
} else if(c == charcode_b) {
// cancel
- val = initial;
- i = d;
- break;
+ _CURSOR_OFF();
+ return 0xffffffff;
} else if(c == charcode_e) {
i++;
} else if(c == charcode_d) {
if(i <= 0) i = 0;
if(i > d) i = d;
} while(1);
+ _CURSOR_OFF();
+ if(val > 0x7fffffff) val = 0x7fffffff;
return val;
}
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 * 19.6ms) 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(10); // 0.78*20 = 15.6ms.
- c = readkey(); //
+ idle_time_ms(5); // 5ms.
+ 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 / 4)){
+ 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_single(void)
+
+unsigned char pollkey_numeric(unsigned char init)
+{
+ unsigned char c;
+ _PUTCHAR('0' + init);
+ 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_timeout(unsigned int limit, unsigned char repeat)
{
- unsigned int count = 0;
- unsigned int lifetime = 0;
- 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(10); // 0.125 * 4 * 20 = 10ms.
- c = readkey(); // 2 * 9 = 16ms
+ 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: