OSDN Git Service

[UI] Fix typos...(-_-;
[openi2cradio/OpenI2CRadio.git] / ui.c
1 /*
2  * OpenI2CRADIO
3  * UI Handler
4  * Copyright (C) 2013-06-10 K.Ohta <whatisthis.sowhat ai gmail.com>
5  *
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.
14  *
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,
18  *  MA 02110-1301, USA.
19  *
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.
26  */
27
28 #include "ioports.h"
29 #include "ui.h"
30 #include "idle.h"
31 #include "commondef.h"
32
33
34 const char charcodemap[] = {charcode_0,
35                             charcode_1,
36                             charcode_4,
37                             charcode_7,
38
39                             charcode_f,
40                             charcode_2,
41                             charcode_5,
42                             charcode_8,
43
44                             charcode_e,
45                             charcode_3,
46                             charcode_6,
47                             charcode_9,
48
49                             charcode_d,
50                             charcode_c,
51                             charcode_b,
52                             charcode_a,
53 };
54
55 extern unsigned char pollkeybuf[32];
56
57 keyin_defs keyin_old[2];
58 keyin_defs keyin_now;
59 char keyin_fifo[32];
60 char keyin_nowp;
61 char keyin_readp;
62 char keyin_counter;
63
64 unsigned char cold;
65
66 void keyin_init(void)
67 {
68     char i;
69     /* Initialize vars*/
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;
74     }
75     for(i = 0; i < 32; i++) keyin_fifo[i] = 0x00;
76     keyin_nowp = 0;
77     keyin_readp = 0;
78     keyin_counter = 0;
79     cold = charcode_null;
80 }
81 /*
82  * Push to keyin fifo; not used atomic-writing.
83  */
84 #ifdef __SDCC
85 void push_keyinfifo(char b) __critical
86 #else
87 void push_keyinfifo(char b)
88 #endif
89 {
90     if(keyin_counter >= 31) {
91         return; // Discard data.
92     }
93     keyin_fifo[keyin_nowp] = b;
94     keyin_nowp++;
95     keyin_counter++;
96     if((keyin_nowp > 31) || (keyin_nowp < 0)) keyin_nowp = 0;
97 }
98
99 /*
100  * Pop from keyin fifo; not used atomic-reading.
101  */
102 #ifdef __SDCC
103 char pop_keyinfifo(void) __critical
104 #else
105 char pop_keyinfifo(void)
106 #endif
107 {
108     char c;
109     if(keyin_counter <= 0) {
110         keyin_counter = 0;
111         return charcode_null ;
112     }
113     c = keyin_fifo[keyin_readp];
114     keyin_readp++;
115     keyin_counter--;
116     if((keyin_readp > 31) || (keyin_readp < 0)) keyin_readp = 0;
117     return c;
118 }
119
120 void printstr(char *s)
121 {
122     int p = 0;
123     _CURSOR_RIGHT();
124     if(s == NULL) return;
125     do {
126         if(s[p] == '\0') break;
127         _PUTCHAR(s[p]);
128         p++;
129     } while(p < 255);
130 }
131
132
133
134 void uint2bcd(unsigned long data, unsigned char *bcd)
135 {
136     unsigned char i;
137     unsigned char j;
138
139     for(i = 0; i < 5; i++){
140         bcd[i] = data % 10;
141         data = data / 10;
142     }
143     bcd[5] = 0;
144 }
145
146 void print_numeric_nosupress(unsigned long data, unsigned char digit)
147 {
148     unsigned char i;
149     unsigned char bcd[6];
150
151
152     if(digit == 0) return;
153     if(digit >= 5) digit = 5;
154     uint2bcd(data, bcd);
155     for(i = digit; i > 0; i--){
156         _PUTCHAR('0' + bcd[i - 1]);
157     }
158 }
159 /*
160  * Read Numeric(int)
161  */
162 unsigned long subst_numeric(unsigned long start, unsigned char pos, unsigned char c)
163 {
164     unsigned long val;
165     unsigned char bcd[6];
166     char i;
167
168     if(pos > 4) pos = 4;
169     uint2bcd(start, bcd);
170     bcd[pos] = c;
171     val = bcd[0] + bcd[1] * 10 + bcd[2] * 100 + bcd[3] * 1000 + bcd[4] * 10000;
172     return val;
173 }
174
175 unsigned int read_numeric(unsigned int initial, unsigned char digit,
176         char startx, char starty)
177 {
178     unsigned char c;
179     unsigned char n;
180     char i;
181     unsigned long val;
182     unsigned long v;
183     char d;
184
185     d = digit - 1;
186     val =(unsigned long) initial;
187     i = d;
188     do {
189        ClrWdt();
190         _LOCATE(startx, starty);
191         print_numeric_nosupress(val, digit);
192        ClrWdt();
193         
194        do {
195            n = pollkeys(pollkeybuf, 60, 1);
196        } while(n == 0);
197        c = pollkeybuf[0];
198
199         if(c == charcode_0){
200             val = subst_numeric(val, i, 0);
201             i--;
202         } else if((c >= charcode_1) && (c <= charcode_9)) {
203             val = subst_numeric(val, i, c - charcode_1 + 1);
204             i--;
205         } else if(c == charcode_f) {
206             // Enter
207             break;
208         } else if(c == charcode_a) {
209             // Del
210             val = val / 10;
211             i++;
212         } else if(c == charcode_b) {
213             // cancel
214             val = initial;
215             i = d;
216             break;
217         }  else if(c == charcode_e) {
218             i++;
219         } else if(c == charcode_d) {
220             i--;
221         }
222        if(i <= 0) i = 0;
223        if(i > d) i = d;
224     } while(1);
225     if(val > 65535) val = 65535;
226     return (unsigned int)val;
227 }
228
229 unsigned char readkey_compare(void)
230 {
231     char b;
232     char c;
233     char d;
234     char e;
235     unsigned char shift;
236     unsigned char f;
237     f = 0;
238     e = 0;
239     for(d = 0; d < 2; d++) {
240         shift = 0x01;
241         for(b = 0; b < 8; b++){
242             c = 0;
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++;
246             if(c >= 2) {
247             /*
248              * Clear older-inputs on .
249              */
250                 f |= 1;
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]);
255             }
256             shift <<= 1;
257             e++;
258         }
259     }
260     /**/
261     return f;
262 }
263
264 unsigned char readkey(void)
265 {
266     unsigned char i;
267     for(i = 0; i < 9; i++) {
268         idle_time_ms(2); // 2ms
269         readkey_io(i);
270         ClrWdt();
271     }
272     readkey_compare();
273     return pop_keyinfifo();
274 }
275
276 /*
277  * Polling key
278  * Max = 32bytes;
279  * 0 = Timeout
280  * 1~32 = Received.
281  * if((limit * 23ms) elapsed), break;
282  */
283 unsigned char pollkeys(unsigned char *p, unsigned int limit, unsigned char repeat)
284 {
285     unsigned int count = 0;
286     unsigned int lifetime = 0;
287     unsigned int penalty = 0;
288     unsigned char c;
289
290     do {
291         idle_time_ms(5); // 5ms.
292         c = readkey(); //
293         ClrWdt();
294         if(c != charcode_null) {
295             push_keyinfifo(c);
296             do {
297                 ClrWdt();
298                 c = pop_keyinfifo();
299                 if(c == charcode_null) {
300                     break;
301                 }
302                 if(c != cold) {
303                     p[count++] = c;
304                     cold = c;
305                 }
306             } while(count < 32);
307             penalty = 0;
308         } else {
309             penalty++;
310             if((limit > 3) && (penalty > (limit >> 2))){
311                 penalty = 0;
312                 cold = charcode_null;
313             }
314         }
315         if(limit != 0) lifetime++;
316         if(lifetime > limit) break;
317     } while(count < 32);
318     if(repeat != 0) cold = charcode_null;
319     return count;
320 }
321
322 unsigned char pollkey_single(void)
323 {
324     unsigned int penalty = 0;
325     unsigned char c;
326
327     cold = charcode_null;
328     do {
329         idle_time_ms(5); // 0.125 * 4 * 20 = 10ms.
330         c = readkey(); // 2 * 9 = 18ms
331         ClrWdt();
332         if(c != charcode_null) {
333             if(cold != c){
334                 cold = c;
335                 return c;
336             }
337         }
338         penalty++;
339         if(penalty > 20) cold = charcode_null;
340     } while(1);
341 }
342
343 /*
344  * Notes:
345  * Initialize sequence:
346  * keyin_init();
347  * keyin_ioinit();
348  * 
349  * Read-key-sequence:
350  * In interrupt/unsleep hook(call per Nms):
351  * readkey_io();
352  * readkey_compare();
353  *
354  * In application handler:
355  * c = pop_keyinfifo();
356  * if(c != 0) do(c);
357  */