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.
33 const char charcodemap[] = {charcode_0,
54 extern unsigned char pollkeybuf[32];
56 keyin_defs keyin_old[2];
69 for(i = 0; i < 2; i++) {
70 keyin_old[0].byte[i] = 0x00;
71 keyin_old[1].byte[i] = 0x00;
72 keyin_now.byte[i] = 0x00;
74 for(i = 0; i < 32; i++) keyin_fifo[i] = 0x00;
81 * Push to keyin fifo; not used atomic-writing.
83 void push_keyinfifo(char b) __critical
85 if(keyin_counter >= 31) {
87 keyin_readp++; // Discard data
89 if((keyin_readp > 31) || (keyin_readp < 0)) keyin_readp = 0;
91 keyin_fifo[keyin_nowp] = b;
94 if((keyin_nowp > 31) || (keyin_nowp < 0))keyin_nowp = 0;
98 * Pop from keyin fifo; not used atomic-reading.
100 char pop_keyinfifo(void) __critical
103 if(keyin_counter <= 0) {
105 return charcode_null ;
107 c = keyin_fifo[keyin_readp];
110 if((keyin_readp > 31) || (keyin_readp < 0)) keyin_readp = 0;
114 void printstr(char *s)
118 if(s == NULL) return;
120 if(s[p] == '\0') break;
126 void print_numeric_nosupress(unsigned int data, unsigned char digit)
132 if(digit == 0) return;
133 if(digit >= 5) digit = 5;
167 unsigned int subst_numeric(unsigned int start, unsigned char pos, unsigned int c)
172 unsigned char bcd[5];
175 if((pos == 4) && (c > 6)) return start;
179 for(i = 4; i >= 0; i--){
187 for(i = 3; i >= 0; i--){
191 if((bcd[4] == 6) && (val > 5535)) return val;
192 val = val + bcd[4] * 10000;
196 unsigned int read_numeric(unsigned int initial, unsigned char digit,
197 char startx, char starty)
209 _LOCATE(startx, starty);
210 print_numeric_nosupress(val, digit);
213 n = pollkeys(pollkeybuf, 60, 0);
218 val = subst_numeric(val, i, 0);
220 } else if((c >= charcode_1) && (c <= charcode_9)) {
221 val = subst_numeric(val, i, c - charcode_1 + 1);
223 } else if(c == charcode_f) {
226 } else if(c == charcode_a) {
230 } else if(c == charcode_b) {
235 } else if(c == charcode_e) {
237 } else if(c == charcode_d) {
246 unsigned char readkey_compare(void)
256 for(d = 0; d < 2; d++) {
258 for(b = 0; b < 8; b++){
260 if((keyin_now.byte[d] & shift) != 0) c++;
261 if((keyin_old[0].byte[d] & shift) != 0) c++;
262 if((keyin_old[1].byte[d] & shift) != 0) c++;
265 * Clear older-inputs on .
268 keyin_old[0].byte[d] &= ~shift;
269 keyin_old[1].byte[d] &= ~shift;
270 keyin_now.byte[d] &= ~shift;
271 push_keyinfifo(charcodemap[e]);
281 unsigned char readkey(void)
284 for(i = 0; i < 9; i++) {
285 idle_time_ms(2); // 2ms
289 return pop_keyinfifo();
297 * if((limit * 19.6ms) elapsed), break;
299 unsigned char pollkeys(unsigned char *p, unsigned int limit, unsigned char repeat)
301 unsigned int count = 0;
302 unsigned int lifetime = 0;
303 unsigned int penalty = 0;
307 idle_time_ms(10); // 0.78*20 = 15.6ms.
310 if(c != charcode_null) {
314 if(c == charcode_null) {
325 if((limit > 3) && (penalty > limit / 4)){
327 cold = charcode_null;
330 if(limit != 0) lifetime++;
331 if(lifetime > limit) break;
333 if(repeat != 0) cold = charcode_null;
337 unsigned char pollkey_single(void)
339 unsigned int count = 0;
340 unsigned int lifetime = 0;
341 unsigned int penalty = 0;
345 idle_time_ms(10); // 0.125 * 4 * 20 = 10ms.
346 c = readkey(); // 2 * 9 = 16ms
348 if(c != charcode_null) {
355 if(penalty > 20) cold = charcode_null;
361 * Initialize sequence:
366 * In interrupt/unsleep hook(call per Nms):
370 * In application handler:
371 * c = pop_keyinfifo();