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];
70 for(i = 0; i < 2; i++) {
71 keyin_old[0].byte[i] = 0x00;
72 keyin_old[1].byte[i] = 0x00;
73 keyin_now.byte[i] = 0x00;
75 for(i = 0; i < 32; i++) keyin_fifo[i] = 0x00;
82 * Push to keyin fifo; not used atomic-writing.
85 void push_keyinfifo(char b) __critical
87 void push_keyinfifo(char b)
90 if(keyin_counter >= 31) {
91 return; // Discard data.
93 keyin_fifo[keyin_nowp] = b;
96 if((keyin_nowp > 31) || (keyin_nowp < 0)) keyin_nowp = 0;
100 * Pop from keyin fifo; not used atomic-reading.
103 char pop_keyinfifo(void) __critical
105 char pop_keyinfifo(void)
109 if(keyin_counter <= 0) {
111 return charcode_null ;
113 c = keyin_fifo[keyin_readp];
116 if((keyin_readp > 31) || (keyin_readp < 0)) keyin_readp = 0;
120 void printstr(char *s)
124 if(s == NULL) return;
126 if(s[p] == '\0') break;
134 static void uint2bcd(unsigned long data, unsigned char *bcd)
139 for(i = 0; i <= 10; i++){
146 void print_numeric_nosupress(unsigned long data, unsigned char digit)
149 unsigned char bcd[11];
152 if(digit == 0) return;
153 if(digit > 10) digit = 10;
155 for(i = digit; i > 0; i--){
156 _PUTCHAR('0' + bcd[i - 1]);
162 unsigned long subst_numeric(unsigned long start, unsigned char pos, unsigned char c)
165 unsigned char bcd[11];
169 if(pos > 10) pos = 10;
170 uint2bcd(start, bcd);
175 for(i = 0; i < 11; i++){
176 val = val + (bcd[i] * fact);
182 unsigned long read_numeric(unsigned int initial, unsigned char digit,
183 char startx, char starty)
193 val =(unsigned long) initial;
198 _LOCATE(startx, starty);
199 print_numeric_nosupress(val, digit);
201 _LOCATE(startx + d - i, starty);
204 c = pollkey_single();
206 val = subst_numeric(val, i, 0);
208 } else if((c >= charcode_1) && (c <= charcode_9)) {
209 val = subst_numeric(val, i, c - charcode_1 + 1);
211 } else if(c == charcode_f) {
214 } else if(c == charcode_a) {
218 } else if(c == charcode_b) {
222 } else if(c == charcode_e) {
224 } else if(c == charcode_d) {
231 if(val > 0x7fffffff) val = 0x7fffffff;
235 unsigned char readkey_compare(void)
245 for(d = 0; d < 2; d++) {
247 for(b = 0; b < 8; b++){
249 if((keyin_now.byte[d] & shift) != 0) c++;
250 if((keyin_old[0].byte[d] & shift) != 0) c++;
251 if((keyin_old[1].byte[d] & shift) != 0) c++;
254 * Clear older-inputs on .
257 keyin_old[0].byte[d] &= ~shift;
258 keyin_old[1].byte[d] &= ~shift;
259 keyin_now.byte[d] &= ~shift;
260 push_keyinfifo(charcodemap[e]);
270 unsigned char readkey(void)
273 for(i = 0; i < 9; i++) {
274 idle_time_ms(2); // 2ms
279 return pop_keyinfifo();
283 unsigned char pollkey_single(void)
285 unsigned int penalty = 0;
288 // cold = charcode_null;
290 idle_time_ms(5); // 5ms.
291 c = readkey(); // 2 * 9 = 18ms
293 if(c != charcode_null) {
303 cold = charcode_null; // About 100ms
310 unsigned char pollkey_numeric(unsigned char init)
313 c = pollkey_single();
314 if(c == charcode_0) {
316 } else if((c >= charcode_1) && (c <= charcode_9)){
323 unsigned char pollkey_single_timeout(unsigned int limit, unsigned char repeat)
326 unsigned int ticks = 0;
327 unsigned char penalty = 0;
328 unsigned char count = 0;
331 cold = charcode_null;
332 pollkeybuf[0] = charcode_null;
340 idle_time_ms(5); // 5ms.
341 c = readkey(); // 2 * 9 = 18ms
344 if(c != charcode_null){
346 pollkeybuf[count++] = c;
358 cold = charcode_null;
367 while(ticks <= limit) {
368 idle_time_ms(5 + 18);
377 * Initialize sequence:
382 * In interrupt/unsleep hook(call per Nms):
386 * In application handler:
387 * c = pop_keyinfifo();