4 * Copyright (C) 2013-06-10 K.Ohta <whatisthis.sowhat ai gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2,
9 * or (at your option) any later version.
10 * This library / program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this library; see the file COPYING. If not, write to the
17 * Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
20 * As a special exception, if you link this(includeed from sdcc) library
21 * with other files, some of which are compiled with SDCC,
22 * to produce an executable, this library does not by itself cause
23 * the resulting executable to be covered by the GNU General Public License.
24 * This exception does not however invalidate any other reasons why
25 * the executable file might be covered by the GNU General Public License.
31 #include "commondef.h"
34 const char charcodemap[] = {charcode_0,
55 extern unsigned char pollkeybuf[32];
57 keyin_defs keyin_old[2];
67 void _PUTCHAR(unsigned char c)
69 acm1602_putchar(LCD_I2CADDR , c);
72 void _LOCATE(unsigned char x, unsigned char y)
74 acm1602_locate_16x2(LCD_I2CADDR , x, y);
79 acm1602_cls(LCD_I2CADDR);
83 acm1602_home(LCD_I2CADDR);
88 acm1602_dispcursor(LCD_I2CADDR, 0xff);
92 void _CURSOR_OFF(void)
94 acm1602_dispcursor(LCD_I2CADDR, 0x00);
97 void _CURSOR_LEFT(void)
99 acm1602_cursordir(LCD_I2CADDR , 0x00);
102 void _CURSOR_RIGHT(void)
104 acm1602_cursordir(LCD_I2CADDR , 0xff);
108 void keyin_init(void)
112 for(i = 0; i < 2; i++) {
113 keyin_old[0].byte[i] = 0x00;
114 keyin_old[1].byte[i] = 0x00;
115 keyin_now.byte[i] = 0x00;
117 for(i = 0; i < 32; i++) keyin_fifo[i] = 0x00;
121 cold = charcode_null;
124 * Push to keyin fifo; not used atomic-writing.
127 void push_keyinfifo(char b) __critical
129 void push_keyinfifo(char b)
132 if(keyin_counter >= 31) {
133 return; // Discard data.
135 keyin_fifo[keyin_nowp] = b;
138 if((keyin_nowp > 31) || (keyin_nowp < 0)) keyin_nowp = 0;
142 * Pop from keyin fifo; not used atomic-reading.
145 char pop_keyinfifo(void) __critical
147 char pop_keyinfifo(void)
151 if(keyin_counter <= 0) {
153 return charcode_null ;
155 c = keyin_fifo[keyin_readp];
158 if((keyin_readp > 31) || (keyin_readp < 0)) keyin_readp = 0;
162 void printstr(char *s)
166 if(s == NULL) return;
168 if(s[p] == '\0') break;
176 static void uint2bcd(unsigned long data, unsigned char *bcd)
181 for(i = 0; i <= 10; i++){
188 void print_numeric_nosupress(unsigned long data, unsigned char digit)
191 unsigned char bcd[11];
193 if(digit == 0) return;
194 if(digit > 10) digit = 10;
196 for(i = digit; i > 0; i--){
197 _PUTCHAR('0' + bcd[i - 1]);
203 unsigned long subst_numeric(unsigned long start, unsigned char pos, unsigned char c)
206 unsigned char bcd[11];
210 if(pos > 10) pos = 10;
211 uint2bcd(start, bcd);
216 for(i = 0; i < 11; i++){
217 val = val + (bcd[i] * fact);
223 unsigned long read_numeric(unsigned int initial, unsigned char digit,
224 char startx, char starty)
234 val =(unsigned long) initial;
239 _LOCATE(startx, starty);
240 print_numeric_nosupress(val, digit);
242 _LOCATE(startx + d - i, starty);
245 c = pollkey_single();
247 val = subst_numeric(val, i, 0);
249 } else if((c >= charcode_1) && (c <= charcode_9)) {
250 val = subst_numeric(val, i, c - charcode_1 + 1);
252 } else if(c == charcode_f) {
255 } else if(c == charcode_a) {
259 } else if(c == charcode_b) {
263 } else if(c == charcode_e) {
265 } else if(c == charcode_d) {
272 if(val > 0x7fffffff) val = 0x7fffffff;
276 unsigned char readkey_compare(void)
286 for(d = 0; d < 2; d++) {
288 for(b = 0; b < 8; b++){
290 if((keyin_now.byte[d] & shift) != 0) c++;
291 if((keyin_old[0].byte[d] & shift) != 0) c++;
292 if((keyin_old[1].byte[d] & shift) != 0) c++;
295 * Clear older-inputs on .
298 keyin_old[0].byte[d] &= ~shift;
299 keyin_old[1].byte[d] &= ~shift;
300 keyin_now.byte[d] &= ~shift;
301 push_keyinfifo(charcodemap[e]);
311 unsigned char readkey(void)
314 for(i = 0; i < 9; i++) {
315 idle_time_ms(2); // 2ms
320 return pop_keyinfifo();
324 unsigned char pollkey_single(void)
326 unsigned int penalty = 0;
329 // cold = charcode_null;
331 idle_time_ms(5); // 5ms.
332 c = readkey(); // 2 * 9 = 18ms
334 if(c != charcode_null) {
344 cold = charcode_null; // About 100ms
351 unsigned char pollkey_numeric(unsigned char init)
354 c = pollkey_single();
355 if(c == charcode_0) {
357 } else if((c >= charcode_1) && (c <= charcode_9)){
364 unsigned char pollkey_single_timeout(unsigned int limit, unsigned char repeat)
367 unsigned int ticks = 0;
368 unsigned char penalty = 0;
369 unsigned char count = 0;
372 cold = charcode_null;
373 pollkeybuf[0] = charcode_null;
381 idle_time_ms(5); // 5ms.
382 c = readkey(); // 2 * 9 = 18ms
385 if(c != charcode_null){
387 pollkeybuf[count++] = c;
399 cold = charcode_null;
408 while(ticks <= limit) {
409 idle_time_ms(5 + 18);
418 * Initialize sequence:
423 * In interrupt/unsleep hook(call per Nms):
427 * In application handler:
428 * c = pop_keyinfifo();