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);
198 n = pollkeys(pollkeybuf, 60, 1);
203 val = subst_numeric(val, i, 0);
205 } else if((c >= charcode_1) && (c <= charcode_9)) {
206 val = subst_numeric(val, i, c - charcode_1 + 1);
208 } else if(c == charcode_f) {
211 } else if(c == charcode_a) {
215 } else if(c == charcode_b) {
220 } else if(c == charcode_e) {
222 } else if(c == charcode_d) {
229 if(val > 65535) val = 65535;
230 return (unsigned int)val;
233 unsigned char readkey_compare(void)
243 for(d = 0; d < 2; d++) {
245 for(b = 0; b < 8; b++){
247 if((keyin_now.byte[d] & shift) != 0) c++;
248 if((keyin_old[0].byte[d] & shift) != 0) c++;
249 if((keyin_old[1].byte[d] & shift) != 0) c++;
252 * Clear older-inputs on .
255 keyin_old[0].byte[d] &= ~shift;
256 keyin_old[1].byte[d] &= ~shift;
257 keyin_now.byte[d] &= ~shift;
258 push_keyinfifo(charcodemap[e]);
268 unsigned char readkey(void)
271 for(i = 0; i < 9; i++) {
272 idle_time_ms(2); // 2ms
277 return pop_keyinfifo();
285 * if((limit * 23ms) elapsed), break;
287 unsigned char pollkeys(unsigned char *p, unsigned int limit, unsigned char repeat)
289 unsigned int count = 0;
290 unsigned int lifetime = 0;
291 unsigned int penalty = 0;
295 idle_time_ms(5); // 5ms.
298 if(c != charcode_null) {
303 if(c == charcode_null) {
314 if((limit > 3) && (penalty > (limit >> 2))){
316 cold = charcode_null;
319 if(limit != 0) lifetime++;
320 if(lifetime > limit) break;
322 if(repeat != 0) cold = charcode_null;
326 unsigned char pollkey_single(void)
328 unsigned int penalty = 0;
331 cold = charcode_null;
333 idle_time_ms(5); // 0.125 * 4 * 20 = 10ms.
334 c = readkey(); // 2 * 9 = 18ms
336 if(c != charcode_null) {
343 if(penalty > 20) cold = charcode_null;
349 * Initialize sequence:
354 * In interrupt/unsleep hook(call per Nms):
358 * In application handler:
359 * c = pop_keyinfifo();