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);
107 void keyin_init(void)
111 for(i = 0; i < 2; i++) {
112 keyin_old[0].byte[i] = 0x00;
113 keyin_old[1].byte[i] = 0x00;
114 keyin_now.byte[i] = 0x00;
116 for(i = 0; i < 32; i++) keyin_fifo[i] = 0x00;
120 cold = charcode_null;
123 * Push to keyin fifo; not used atomic-writing.
126 void push_keyinfifo(char b) __critical
128 void push_keyinfifo(char b)
131 if(keyin_counter >= 31) {
132 return; // Discard data.
134 keyin_fifo[keyin_nowp] = b;
137 if(keyin_nowp > 31) keyin_nowp = 0;
141 * Pop from keyin fifo; not used atomic-reading.
144 char pop_keyinfifo(void) __critical
146 char pop_keyinfifo(void)
150 if(keyin_counter <= 0) {
152 return charcode_null ;
154 c = keyin_fifo[keyin_readp];
157 if(keyin_readp > 31) keyin_readp = 0;
161 void printstr(char *s)
165 if(s == NULL) return;
167 if(s[p] == '\0') break;
175 static void uint2bcd(unsigned long data, unsigned char *bcd)
180 for(i = 0; i <= 10; i++){
187 void print_numeric_nosupress(unsigned long data, unsigned char digit)
190 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) {
262 } else if(c == charcode_e) {
264 } else if(c == charcode_d) {
271 if(val > 0x7fffffff) val = 0x7fffffff;
275 unsigned char readkey_compare(void)
285 for(d = 0; d < 2; d++) {
287 for(b = 0; b < 8; b++){
289 if((keyin_now.byte[d] & shift) != 0) c++;
290 if((keyin_old[0].byte[d] & shift) != 0) c++;
291 if((keyin_old[1].byte[d] & shift) != 0) c++;
294 * Clear older-inputs on .
297 keyin_old[0].byte[d] &= ~shift;
298 keyin_old[1].byte[d] &= ~shift;
299 keyin_now.byte[d] &= ~shift;
300 push_keyinfifo(charcodemap[e]);
310 unsigned char readkey(void)
313 for(i = 0; i < 9; i++) {
314 idle_time_ms(2); // 2ms
319 return pop_keyinfifo();
323 unsigned char pollkey_single(void)
325 unsigned int penalty = 0;
328 // cold = charcode_null;
330 idle_time_ms(5); // 5ms.
331 c = readkey(); // 2 * 9 = 18ms
333 if(c != charcode_null) {
343 cold = charcode_null; // About 100ms
348 unsigned char pollkey_numeric(unsigned char init)
351 c = pollkey_single();
352 if(c == charcode_0) {
354 } else if((c >= charcode_1) && (c <= charcode_9)){
361 unsigned char pollkey_single_timeout(unsigned int limit, unsigned char repeat)
364 unsigned int ticks = 0;
365 unsigned char penalty = 0;
366 unsigned char count = 0;
369 cold = charcode_null;
370 pollkeybuf[0] = charcode_null;
378 idle_time_ms(5); // 5ms.
379 c = readkey(); // 2 * 9 = 18ms
382 if(c != charcode_null){
384 pollkeybuf[count++] = c;
396 cold = charcode_null;
405 while(ticks <= limit) {
406 idle_time_ms(5 + 18);
415 * Initialize sequence:
420 * In interrupt/unsleep hook(call per Nms):
424 * In application handler:
425 * c = pop_keyinfifo();