OSDN Git Service

[UI] Fix not memory when changing frequency.
[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         _CURSOR_OFF();
190        ClrWdt();
191         _LOCATE(startx, starty);
192         print_numeric_nosupress(val, digit);
193        ClrWdt();
194         _LOCATE(startx + d -  i, starty);
195        _CURSOR_ON();
196
197        do {
198            n = pollkeys(pollkeybuf, 60, 1);
199        } while(n == 0);
200        c = pollkeybuf[0];
201
202         if(c == charcode_0){
203             val = subst_numeric(val, i, 0);
204             i--;
205         } else if((c >= charcode_1) && (c <= charcode_9)) {
206             val = subst_numeric(val, i, c - charcode_1 + 1);
207             i--;
208         } else if(c == charcode_f) {
209             // Enter
210             break;
211         } else if(c == charcode_a) {
212             // Del
213             val = val / 10;
214             i++;
215         } else if(c == charcode_b) {
216             // cancel
217             val = initial;
218             i = d;
219             break;
220         }  else if(c == charcode_e) {
221             i++;
222         } else if(c == charcode_d) {
223             i--;
224         }
225        if(i <= 0) i = 0;
226        if(i > d) i = d;
227     } while(1);
228     _CURSOR_OFF();
229     if(val > 65535) val = 65535;
230     return (unsigned int)val;
231 }
232
233 unsigned char readkey_compare(void)
234 {
235     char b;
236     char c;
237     char d;
238     char e;
239     unsigned char shift;
240     unsigned char f;
241     f = 0;
242     e = 0;
243     for(d = 0; d < 2; d++) {
244         shift = 0x01;
245         for(b = 0; b < 8; b++){
246             c = 0;
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++;
250             if(c >= 2) {
251             /*
252              * Clear older-inputs on .
253              */
254                 f |= 1;
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]);
259             }
260             shift <<= 1;
261             e++;
262         }
263     }
264     /**/
265     return f;
266 }
267
268 unsigned char readkey(void)
269 {
270     unsigned char i;
271     for(i = 0; i < 9; i++) {
272         idle_time_ms(2); // 2ms
273         readkey_io(i);
274         ClrWdt();
275     }
276     readkey_compare();
277     return pop_keyinfifo();
278 }
279
280 /*
281  * Polling key
282  * Max = 32bytes;
283  * 0 = Timeout
284  * 1~32 = Received.
285  * if((limit * 23ms) elapsed), break;
286  */
287 unsigned char pollkeys(unsigned char *p, unsigned int limit, unsigned char repeat)
288 {
289     unsigned int count = 0;
290     unsigned int lifetime = 0;
291     unsigned int penalty = 0;
292     unsigned char c;
293
294     do {
295         idle_time_ms(5); // 5ms.
296         c = readkey(); //
297         ClrWdt();
298         if(c != charcode_null) {
299             push_keyinfifo(c);
300             do {
301                 ClrWdt();
302                 c = pop_keyinfifo();
303                 if(c == charcode_null) {
304                     break;
305                 }
306                 if(c != cold) {
307                     p[count++] = c;
308                     cold = c;
309                 }
310             } while(count < 32);
311             penalty = 0;
312         } else {
313             penalty++;
314             if((limit > 3) && (penalty > (limit >> 2))){
315                 penalty = 0;
316                 cold = charcode_null;
317             }
318         }
319         if(limit != 0) lifetime++;
320         if(lifetime > limit) break;
321     } while(count < 32);
322     if(repeat != 0) cold = charcode_null;
323     return count;
324 }
325
326 unsigned char pollkey_single(void)
327 {
328     unsigned int penalty = 0;
329     unsigned char c;
330
331     cold = charcode_null;
332     do {
333         idle_time_ms(5); // 0.125 * 4 * 20 = 10ms.
334         c = readkey(); // 2 * 9 = 18ms
335         ClrWdt();
336         if(c != charcode_null) {
337             if(cold != c){
338                 cold = c;
339                 return c;
340             }
341         }
342         penalty++;
343         if(penalty > 20) cold = charcode_null;
344     } while(1);
345 }
346
347 /*
348  * Notes:
349  * Initialize sequence:
350  * keyin_init();
351  * keyin_ioinit();
352  * 
353  * Read-key-sequence:
354  * In interrupt/unsleep hook(call per Nms):
355  * readkey_io();
356  * readkey_compare();
357  *
358  * In application handler:
359  * c = pop_keyinfifo();
360  * if(c != 0) do(c);
361  */