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 void uint2bcd(unsigned long data, unsigned char *bcd)
139 for(i = 0; i < 5; i++){
146 void print_numeric_nosupress(unsigned long data, unsigned char digit)
149 unsigned char bcd[6];
152 if(digit == 0) return;
153 if(digit >= 5) digit = 5;
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[6];
169 uint2bcd(start, bcd);
171 val = bcd[0] + bcd[1] * 10 + bcd[2] * 100 + bcd[3] * 1000 + bcd[4] * 10000;
175 unsigned int read_numeric(unsigned int initial, unsigned char digit,
176 char startx, char starty)
186 val =(unsigned long) initial;
191 _LOCATE(startx, starty);
192 print_numeric_nosupress(val, digit);
194 _LOCATE(startx + d - i, starty);
197 c = pollkey_single();
199 val = subst_numeric(val, i, 0);
201 } else if((c >= charcode_1) && (c <= charcode_9)) {
202 val = subst_numeric(val, i, c - charcode_1 + 1);
204 } else if(c == charcode_f) {
207 } else if(c == charcode_a) {
211 } else if(c == charcode_b) {
216 } else if(c == charcode_e) {
218 } else if(c == charcode_d) {
225 if(val > 65535) val = 65535;
226 return (unsigned int)val;
229 unsigned char readkey_compare(void)
239 for(d = 0; d < 2; d++) {
241 for(b = 0; b < 8; b++){
243 if((keyin_now.byte[d] & shift) != 0) c++;
244 if((keyin_old[0].byte[d] & shift) != 0) c++;
245 if((keyin_old[1].byte[d] & shift) != 0) c++;
248 * Clear older-inputs on .
251 keyin_old[0].byte[d] &= ~shift;
252 keyin_old[1].byte[d] &= ~shift;
253 keyin_now.byte[d] &= ~shift;
254 push_keyinfifo(charcodemap[e]);
264 unsigned char readkey(void)
267 for(i = 0; i < 9; i++) {
268 idle_time_ms(2); // 2ms
273 return pop_keyinfifo();
281 * if((limit * 23ms) elapsed), break;
283 unsigned char pollkeys(unsigned char *p, unsigned int limit, unsigned char repeat)
285 unsigned char count = 0;
286 unsigned int lifetime = 0;
287 unsigned int penalty = 0;
292 idle_time_ms(5); // 5ms.
293 c = readkey(); //18ms
295 if(c != charcode_null){
309 cold = charcode_null;
312 if(limit != 0) lifetime++;
313 if(lifetime > limit) break;
315 p[count] = charcode_null;
319 unsigned char pollkey_single(void)
321 unsigned int penalty = 0;
324 // cold = charcode_null;
326 idle_time_ms(5); // 5ms.
327 c = readkey(); // 2 * 9 = 18ms
329 if(c != charcode_null) {
339 cold = charcode_null; // About 100ms
345 unsigned char pollkey_single_timeout(unsigned int limit, unsigned char repeat)
348 unsigned int ticks = 0;
349 unsigned char penalty = 0;
350 unsigned char count = 0;
353 cold = charcode_null;
354 pollkeybuf[0] = charcode_null;
362 idle_time_ms(5); // 5ms.
363 c = readkey(); // 2 * 9 = 18ms
366 if(c != charcode_null){
368 pollkeybuf[count++] = c;
380 cold = charcode_null;
389 while(ticks <= limit) {
390 idle_time_ms(5 + 18);
399 * Initialize sequence:
404 * In interrupt/unsleep hook(call per Nms):
408 * In application handler:
409 * c = pop_keyinfifo();