OSDN Git Service

[UI] Stable update_display().
[openi2cradio/OpenI2CRadio.git] / main.c
1 /*
2  * OpenI2CRADIO
3  * Config & Main routine.
4  * Copyright (C) 2013-06-10 K.Ohta <whatisthis.sowhat ai gmail.com>
5  * License: GPL2+LE
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2,
10  *  or (at your option) any later version.
11  *  This library / program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  *  See the GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this library; see the file COPYING. If not, write to the
18  *  Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
19  *  MA 02110-1301, USA.
20  *
21  *  As a special exception, if you link this(includeed from sdcc) library
22  *  with other files, some of which are compiled with SDCC,
23  *  to produce an executable, this library does not by itself cause
24  *  the resulting executable to be covered by the GNU General Public License.
25  *  This exception does not however invalidate any other reasons why
26  *  the executable file might be covered by the GNU General Public License.
27  */
28
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sdcc-lib.h>
34 #include <pic18fregs.h> /* ONLY FOR PIC18x */
35 #include <signal.h>
36 #include <delay.h>
37
38 #include "iodef.h"
39 #include "idle.h"
40 #include "i2c_io.h"
41 #include "akc6955.h"
42 #include "lcd_acm1602.h"
43 #include "ui.h"
44 #include "eeprom.h"
45 #include "ioports.h"
46
47 /*
48  * Config words.
49  */
50 #if defined(pic18f23k22) || defined(pic18f24k22) || defined(pic18f25k22) || defined(pic18f26k22)
51 //#pragma stack 0x200 256 // Set stack size to 256bytes.
52 #pragma config FOSC=INTIO67,BORV=190,BOREN=ON,PWRTEN=ON
53 #pragma config WDTEN=ON,WDTPS=32768
54 #pragma config PBADEN=OFF,MCLRE=EXTMCLR,STVREN=ON,LVP=OFF,DEBUG=ON//,XINST=ON
55 //#pragma config PBADEN=OFF,MCLRE=EXTMCLR,STVREN=ON,LVP=OFF//,XINST=ON
56 #pragma config CP0=OFF,CP1=OFF,CPB=OFF,CPD=OFF
57 #pragma config WRT0=OFF,WRT1=OFF,WRTB=OFF,WRTC=OFF,WRTD=OFF
58 #pragma config EBTR0=OFF,EBTR1=OFF,EBTRB=OFF
59 #endif
60 // For 4xK20 or 2xK20 Series
61 #if defined(pic18f43k20) || defined(pic18f44k20) || defined(pic18f45k20) || defined(pic18f46k20) || \
62     defined(pic18f23k20) || defined(pic18f24k20) || defined(pic18f25k20) || defined(pic18f26k20)
63
64 #pragma stack 0x200 256
65 #pragma config FOSC=HS,FCMEN=ON,PWRT=ON,BOREN=ON,BORV=22
66 #pragma config WDTEN=ON,WDTPS=32768,PBADEN=OFF,HFOFST=ON,LPT1OSC=OFF,MCLRE=ON
67 #pragma config STVREN=ON,DEBUG=ON
68 //#pragma config CP0=OFF,CP1=OFF,CP2=OFF,CP3=OFF
69 //#pragma config CPB=OFF,CPD=OFF
70 //#pragma config WRT0=OFF,WRT1=OFF,WRT2=OFF,WRT3=OFF
71 //#pragma config WRTC=OFF,WRTB=OFF,WRTD=OFF
72 //#pragma config EBTR0=OFF,EBTR1=OFF,EBTR2=OFF,EBTR3=OFF,EBTRB=OFF
73 #endif
74
75 //#define _LCD_DEBUG 1
76
77 SIGHANDLER(TMR0_handler)
78 {
79    unsigned char tmr0f;
80    unsigned char t0con;
81
82    // Stop timer0
83    t0con = T0CON;
84    t0con &= ~_IDLEN;
85    T0CON = t0con;
86
87
88    // Clear interrupt flag
89    tmr0f = INTCON;
90    tmr0f &= ~(_TMR0IF | _TMR0IE);
91    INTCON = tmr0f;
92
93    return;
94 }
95
96 /*
97  * Interrupt wake up every 1ms.
98  */
99 SIGHANDLER(TMR3_Handler)
100 {
101     if(statecount >= 8) statecount = 0;
102     readkey_io(statecount);
103     statecount++;
104     if(statecount > 7) readkey_compare(); // Compare and push to fifo.
105     PIR2bits.TMR3IF  = 0;
106     PIE2bits.TMR3IE  = 1;
107     TMR3_set();
108 }
109
110 DEF_INTLOW(intlow_handler)
111   DEF_HANDLER(SIG_TMR0, TMR0_handler)
112 //  DEF_HANDLER(SIG_TMR3, TMR3_Handler)
113 END_DEF
114
115
116 unsigned int amfreq;
117 unsigned int fmfreq;
118 unsigned char amband;
119 unsigned char fmband;
120 unsigned char fm;
121 unsigned char am_mode3k;
122 unsigned char am_userbandnum;
123 unsigned char fm_userbandnum;
124 typedef struct {
125     unsigned char mode3k; // mode3k if am
126     unsigned char start;
127     unsigned char stop;
128     unsigned int freq;
129 } _userband_t;
130 #define USER_BAND_NUM 4
131 _userband_t am_usrbands[USER_BAND_NUM];
132 _userband_t fm_usrbands[USER_BAND_NUM];
133
134 unsigned char enter_mode;
135 unsigned char numeric_mode;
136 unsigned char menu_node;
137 int backlight_long;
138 unsigned char help_flag;
139 int help_line;
140 int help_section;
141 int ui_language;
142 unsigned int ui_idlecount;
143 unsigned char scanflag;
144
145 int recv_signal;
146 int backlight_counter;
147 unsigned char backlight_level;
148 unsigned char pollkeybuf[33];
149
150
151 unsigned int writeword_eeprom(unsigned int p, unsigned int *sum, unsigned int word)
152 {
153     ClrWdt();
154     if(eeprom_writebyte(p, word >> 8) == 0) return p; // Error
155     *sum = calcsum_byte(*sum, (word >> 8));
156
157     if(eeprom_writebyte(p, word & 0xff) == 0) return p+1; // Error
158     *sum = calcsum_byte(*sum, word & 0xff);
159     return 0xffff;
160 }
161
162 unsigned int writebyte_eeprom(unsigned int p, unsigned int *sum, unsigned char b)
163 {
164     ClrWdt();
165     if(eeprom_writebyte(p, b) == 0) return p; // Error
166     *sum = calcsum_byte(*sum, b);
167     return 0xffff;
168 }
169
170 void save_eeprom(void)
171 {
172     unsigned int p = 0;
173     unsigned int sum = 0x0000;
174     unsigned char i;
175
176     // Magic word
177     writeword_eeprom(p, &sum, 0x1298);
178     p+= 2;
179     // amfreq
180     writeword_eeprom(p, &sum, amfreq);
181     p+= 2;
182     // amfreq
183     writeword_eeprom(p, &sum, fmfreq);
184     p+= 2;
185
186     writebyte_eeprom(p, &sum, amband);
187     p++;
188     writebyte_eeprom(p, &sum, fmband);
189     p++;
190     writebyte_eeprom(p, &sum, fm);
191     p++;
192     writebyte_eeprom(p, &sum, am_mode3k);
193     p++;
194     writebyte_eeprom(p, &sum, am_userbandnum);
195     p++;
196     writebyte_eeprom(p, &sum, fm_userbandnum);
197     p++;
198
199     for(i = 0 ; i < USER_BAND_NUM; i++){
200         writebyte_eeprom(p, &sum, am_usrbands[i].mode3k);
201         writebyte_eeprom(p + 1, &sum, am_usrbands[i].start);
202         writebyte_eeprom(p + 2, &sum, am_usrbands[i].stop);
203         writeword_eeprom(p + 3, &sum, am_usrbands[i].freq);
204         p += 5;
205     }
206     for(i = 0 ; i < USER_BAND_NUM; i++){
207         writebyte_eeprom(p, &sum, fm_usrbands[i].mode3k);
208         writebyte_eeprom(p + 1, &sum, fm_usrbands[i].start);
209         writebyte_eeprom(p + 2, &sum, fm_usrbands[i].stop);
210         writeword_eeprom(p + 3, &sum, fm_usrbands[i].freq);
211         p += 5;
212     }
213     // Write checksum
214     eeprom_writebyte(p, sum >> 8);
215     eeprom_writebyte(p + 1, sum & 0xff);
216     p+= 2;
217 }
218
219 unsigned int readword_eeprom(unsigned int p, unsigned int *sum)
220 {
221     unsigned char h,l;
222     unsigned int s;
223
224     ClrWdt();
225
226     h = eeprom_readbyte(p);
227     *sum = calcsum_byte(*sum, h);
228
229     l = eeprom_readbyte(p + 1);
230     *sum = calcsum_byte(*sum, l);
231
232     s = (h << 8) + l;
233     return s;
234 }
235
236 unsigned char readbyte_eeprom(unsigned int p, unsigned int *sum)
237 {
238     unsigned char b;
239
240     ClrWdt();
241
242     b = eeprom_readbyte(p);
243     *sum = calcsum_byte(*sum, b);
244
245     return b;
246 }
247
248
249 unsigned char load_eeprom(void)
250 {
251     unsigned int p = 0;
252     unsigned int sum = 0x0000;
253     unsigned char i;
254     unsigned int magic;
255
256     // Magic word
257     magic = readword_eeprom(p, &sum);
258     if(magic != 0x1298) return 0x01; // NO MAGICWORD
259     p+= 2;
260     // amfreq
261     amfreq = readword_eeprom(p, &sum);
262     p+= 2;
263     // fmfreq
264     fmfreq = readword_eeprom(p, &sum);
265     p+= 2;
266
267     amband = readbyte_eeprom(p, &sum);
268     p++;
269     fmband = readbyte_eeprom(p, &sum);
270     p++;
271     fm = readbyte_eeprom(p, &sum);
272     p++;
273     am_mode3k = readbyte_eeprom(p, &sum);
274     p++;
275     am_userbandnum = readbyte_eeprom(p, &sum);
276     p++;
277     fm_userbandnum = readbyte_eeprom(p, &sum);
278     p++;
279
280     for(i = 0 ; i < USER_BAND_NUM; i++){
281         am_usrbands[i].mode3k = readbyte_eeprom(p, &sum);
282         am_usrbands[i].start  = readbyte_eeprom(p + 1, &sum);
283         am_usrbands[i].stop   = readbyte_eeprom(p + 2, &sum);
284         am_usrbands[i].freq   = readword_eeprom(p + 3, &sum);
285         p += 5;
286     }
287     for(i = 0 ; i < USER_BAND_NUM; i++){
288         fm_usrbands[i].mode3k = readbyte_eeprom(p, &sum);
289         fm_usrbands[i].start  = readbyte_eeprom(p + 1, &sum);
290         fm_usrbands[i].stop   = readbyte_eeprom(p + 2, &sum);
291         fm_usrbands[i].freq   = readword_eeprom(p + 3, &sum);
292         p += 5;
293     }
294     // Write checksum
295     magic = (eeprom_readbyte(p) << 8) + eeprom_readbyte(p+1);
296
297     p+= 2;
298     if(sum != magic) return 0x00;
299     return 0xff;
300 }
301
302
303 void toggle_amfm(void)
304 {
305     if(fm != 0){
306         fm = 0;
307 //        akc6955_chg_fm(fm);
308 //        akc6955_set_amband(amband);
309 //        akc6955_set_freq(amfreq);
310     } else {
311         fm = 0xff;
312 //        akc6955_chg_fm(fm);
313 //        akc6955_set_fmband(fmband);
314 //        akc6955_set_freq(fmfreq);
315     }
316 }
317
318 static void update_status(void)
319 {
320 #ifndef _LCD_DEBUG
321         recv_signal = akc6955_read_level();
322         if(fm != 0){
323             fmfreq = akc6955_get_freq();
324         } else {
325             amfreq = akc6955_get_freq();
326         }
327 #endif
328
329 }
330
331
332 void set_volume(void)
333 {
334 }
335
336 void update_display(void)
337 {
338 //    _HOME();
339     _LOCATE(0,0);
340      printstr("S=");
341      print_numeric_nosupress(recv_signal, 3);
342     _LOCATE(0,1);
343 //    _PUTCHAR(' ');
344     if(fm != 0){ // FM
345         if(fmband < AKC6955_BAND_TV1) {
346             printstr("FM");
347             _PUTCHAR('1' + (fmband & 7));
348             printstr("  ");
349         } else if(fmband < AKC6955_BAND_FMUSER){
350             printstr("TV");
351             _PUTCHAR('1' + fmband - AKC6955_BAND_TV1);
352             printstr("  ");
353         } else { // USER
354             printstr("FMUSR");
355         }
356     } else { // AM
357         if(amband == AKC6955_BAND_LW) {
358             printstr("LW   ");
359         } else if(amband <AKC6955_BAND_SW1) { //MW
360             printstr("MW");
361             _PUTCHAR('1' + amband - AKC6955_BAND_MW1);
362             printstr("  ");
363         } else if(amband <AKC6955_BAND_SW10) { //MW
364             printstr("SW");
365             _PUTCHAR('1' + amband - AKC6955_BAND_SW1);
366             printstr("  ");
367         } else if(amband < AKC6955_BAND_AMUSER) { //MW
368             printstr("SW1");
369             _PUTCHAR('0' + amband - AKC6955_BAND_SW10);
370             _PUTCHAR(' ');
371         } else if(amband == AKC6955_BAND_MW4){
372             printstr("MW4  ");
373         } else {
374             printstr("AMUSR");
375         }
376      }
377 //     _LOCATE(15-5 ,1);
378      _LOCATE(15-4-6, 1);
379      if(fm != 0){
380          int freq_lo = fmfreq % 100;
381          int freq_hi = fmfreq / 100;
382          print_numeric_nosupress(freq_hi, 3);
383          _PUTCHAR('.');
384          print_numeric_nosupress(freq_lo, 2);
385      } else {
386          _PUTCHAR(' ');
387          print_numeric_nosupress(amfreq, 5);
388      }
389      // Signal
390      _LOCATE(15-4, 1);
391      if(fm != 0){
392          printstr("MHz");
393      } else {
394          printstr("KHz");
395      }
396     _HOME();
397 }
398
399 void scan_start()
400 {
401     unsigned char input_flag;
402     unsigned char c;
403
404     do {
405         if(scanflag == 0){
406         // New Scan
407             _CLS();
408             printstr("Scan A=ABORT");
409             _LOCATE(0,1);
410             printstr("U=6, D=4");
411             do {
412                 input_flag = readkey_compare();
413                 idle(0xff80);
414             } while(input_flag == 0);
415             c = pop_keyinfifo();
416             if(c == charcode_6){
417                     akc6955_do_scan(0xff);
418             } else if(c == charcode_4){
419                     akc6955_do_scan(0);
420             } else {
421                 break;
422             }
423             scanflag = 0xff;
424         } else {
425             do {
426                 input_flag = readkey_compare();
427                 idle(0xff80);
428             } while(input_flag == 0);
429             c = pop_keyinfifo();
430
431             if(c == charcode_a){
432                 akc6955_abort_scan();
433                 break;
434             } else if(c == charcode_4){
435                 akc6955_abort_scan();
436                 akc6955_do_scan(0);
437                 continue;
438             } else if(c == charcode_6){
439                 akc6955_abort_scan();
440                 akc6955_do_scan(0xff);
441                 continue;
442             }
443             if(akc6955_chk_donescan() != 0) break;
444         }
445         idle(0xff00);
446     } while(1);
447     scanflag=0;
448     _CLS();
449     update_status();
450     update_display();
451 }
452
453 void setfreq_direct(void)
454 {
455     unsigned int val;
456     _CLS();
457     if(fm != 0){
458         // FM
459         _LOCATE(0,0);
460         printstr("Set Freq:FM");
461         val = fmfreq;
462         val = read_numeric(val, 5, 7, 1);
463         fmfreq = val;
464         akc6955_set_freq(val);
465     } else {
466         // FM
467         _LOCATE(0,0);
468         printstr("Set Freq:AM");
469         val = amfreq;
470         val = read_numeric(val, 5, 7, 1);
471         amfreq = val;
472         akc6955_set_freq(val);
473     }
474     idle(0xff00);
475     update_status();
476     update_display();
477 }
478
479 void setband_direct(void)
480 {
481     unsigned int band;
482     _CLS();
483     _LOCATE(0,0);
484     if(fm != 0){
485         printstr("Set Band:FM");
486         band = fmband & 7;
487         band = read_numeric(band, 2, 7, 1);
488         akc6955_set_fmband(band);
489         akc6955_do_tune();
490         idle(0xff00);
491     } else {
492         printstr("Set Band:AM");
493         band = amband & 0x1f;
494         band = read_numeric(band, 2, 7, 1);
495         akc6955_set_amband(band);
496         akc6955_do_tune();
497     }
498     idle(0xff00);
499     update_status();
500     update_display();
501 }
502
503 void call_userband(unsigned char num)
504 {
505     unsigned int freq;
506     unsigned int ch;
507     if(num >= USER_BAND_NUM) return;
508     if(fm != 0){
509         freq = fm_usrbands[num].freq;
510         ch = ((freq - 3000) / 25) * 10;
511         akc6955_set_userband(fm_usrbands[num].start, fm_usrbands[num].stop, ch,
512                             fm_usrbands[num].mode3k);
513     } else {
514         unsigned int p = 5;
515         if(am_usrbands[num].mode3k != 0) p = 3;
516         freq = am_usrbands[num].freq;
517         ch = freq / p;
518         akc6955_set_userband(am_usrbands[num].start, am_usrbands[num].stop, ch,
519                             am_usrbands[num].mode3k);
520     }
521     if(fm != 0) {
522         fmband = AKC6955_BAND_AMUSER;
523     } else {
524         amband = AKC6955_BAND_AMUSER;
525     }
526     idle(0xff00);
527     update_status();
528     update_display();
529 }
530
531 void set_userband(void)
532 {
533     unsigned int from,to;
534     unsigned char c;
535     unsigned char p;
536     unsigned char mode3k;
537     unsigned int input_flag;
538     char cc;
539
540     _CLS();
541     _LOCATE(0,0);
542     printstr("User ch:");
543     do {
544         input_flag = readkey_compare();
545         idle(0xff80);
546     } while(input_flag == 0);
547     c = pop_keyinfifo();
548
549     if(c > charcode_0) return;
550     if(c < charcode_1) return;
551     if(c == charcode_0) {
552         c = 0;
553     } else {
554         c = c - charcode_1 + 1;
555     }
556     if(c >= USER_BAND_NUM) return;
557     if(fm != 0){
558         from = fm_usrbands[c].start * 80 + 3000; // 32*25/10
559         to = fm_usrbands[c].stop * 80 + 3000;
560         _CLS();
561         _LOCATE(0,0);
562         printstr("FM #");
563         print_numeric_nosupress(c, 1);
564         printstr(" From:");
565         from = read_numeric(from, 5, 7, 1);
566         _CLS();
567         _LOCATE(0,0);
568         printstr("FM #");
569         print_numeric_nosupress(c, 1);
570         printstr(" To:");
571         to = read_numeric(to, 5, 7, 1);
572         fm_usrbands[c].start = (from - 3000) / 80;
573         fm_usrbands[c].stop = (to - 3000) / 80;
574         fm_usrbands[c].freq = from * 80 + 3000;
575         fm_userbandnum = c;
576     } else {
577         mode3k = am_usrbands[c].mode3k;
578         p = 96; // 3*32
579         if(mode3k == 0) p = 160; // 5*32
580         from = am_usrbands[c].start * p; 
581         to = am_usrbands[c].stop * p;
582         _CLS();
583         _LOCATE(0,0);
584         printstr("AM #");
585         print_numeric_nosupress(c, 1);
586         printstr(" Step:");
587         _LOCATE(0,1);
588         printstr("0=3k 1=5k");
589         do {
590             input_flag = readkey_compare();
591             idle(0xff80);
592         } while(input_flag == 0);
593         cc = pop_keyinfifo();
594
595         if(cc == charcode_0){
596             p = 96;
597             mode3k = 0xff;
598         } else if(cc = charcode_1) {
599             p = 160;
600             mode3k = 0;
601         }
602         _CLS();
603         _LOCATE(0,0);
604         printstr("AM #");
605         print_numeric_nosupress(c, 1);
606         printstr(" From:");
607         from = read_numeric(from, 5, 7, 1);
608         _CLS();
609         _LOCATE(0,0);
610         printstr("AM #");
611         print_numeric_nosupress(c, 1);
612         printstr(" To:");
613         to = read_numeric(to, 5, 7, 1);
614         am_usrbands[c].start = from / p;
615         am_usrbands[c].stop = to  / p;
616         am_usrbands[c].mode3k = mode3k;
617         am_usrbands[c].freq = from * p;
618         am_userbandnum = c;
619     }
620     call_userband(c);
621 }
622
623 void input_userband(void)
624 {
625     unsigned char c;
626     unsigned char input_flag;
627     do{
628     _CLS();
629     _LOCATE(0,0);
630     printstr("User Band");
631     _LOCATE(0,1);
632     printstr("   #");
633     do {
634         input_flag = readkey_compare();
635         idle(0xff80);
636     } while(input_flag == 0);
637     c = pop_keyinfifo();
638
639     if((c >= charcode_a) && (c <= charcode_f)){
640         break;
641     }
642     if(c == charcode_0) {
643         _PUTCHAR('0');
644         if(fm != 0){
645            fm_userbandnum = 0;
646         } else {
647            am_userbandnum = 0;
648         }
649         call_userband(0);
650     } else {
651         c = c - charcode_1 + 1;
652         if(c < USER_BAND_NUM) {
653             _PUTCHAR(c + '0');
654             if(fm != 0){
655                fm_userbandnum = c;
656             } else {
657                 am_userbandnum = c;
658             }
659             call_userband(c);
660         }
661     }
662     idle(0xff00);
663     } while(1);
664     _CLS();
665 }
666
667
668 void main_menu(void)
669 {
670     unsigned char c;
671     unsigned int input_flag;
672     _CLS();
673     _LOCATE(0,0);
674     printstr("Menu:F=HELP");
675     _LOCATE(1,0);
676     printstr("A=CANCEL");
677     do{
678         do {
679             input_flag = readkey_compare();
680             idle(0xff80);
681         } while(input_flag == 0);
682
683         c = pop_keyinfifo();
684         if((c < charcode_1) || ( c > charcode_0)) {
685             idle(0xff00);
686             continue; // Error
687         }
688         if(c == charcode_f){
689             // HELP
690         } else if(c == charcode_a){
691             // Cancel
692             break;
693         } else if(c == charcode_1){
694             // AM
695             fm = 0;
696             akc6955_chg_fm(fm);
697             akc6955_set_amband(amband);
698             akc6955_set_freq(amfreq);
699             break;
700         } else if(c == charcode_2){
701             // Band
702             setband_direct();
703             break;
704         } else if(c == charcode_3){
705             // Band
706             setfreq_direct();
707             break;
708         } else if(c == charcode_4){
709             // fm
710             fm = 0xff;
711             akc6955_chg_fm(fm);
712             akc6955_set_fmband(fmband);
713             akc6955_set_freq(fmfreq);
714             break;
715         } else if(c == charcode_5){
716             // Scan
717             break;
718         } else if(c == charcode_6){
719             // Set gain
720             break;
721         } else if(c == charcode_7){
722             // Set volume
723             break;
724         } else if(c == charcode_8){
725             // Set sensitivity
726             break;
727         } else if(c == charcode_9){
728             // Set NF
729             break;
730         } else if(c == charcode_0){
731             // Setup Menu
732             break;
733         } else if(c == charcode_b){
734             // Call userband
735             input_userband();
736             break;
737         } else if(c == charcode_c){
738             // Set userband
739             set_userband();
740             break;
741         } else if(c == charcode_d){
742             // Reserve
743             break;
744         } else if(c == charcode_e){
745             // Reserve
746             break;
747         }
748         idle(0xff00);
749     } while(1);
750 }
751
752 void setfreq_updown(unsigned char ctlword)
753 {
754     switch(ctlword){
755         case charcode_8: // Change band
756             if(fm == 0){
757                 amband++;
758                 if(amband > 18) amband = 0;
759 //                amfreq = akc6955_setfreq(amfreq)
760 //                akc6955_set_amband(amband);
761                 _AKC6955_WAIT_62_5MS(); // 62.5ms
762 //                amband = akc6955_get_amband();
763 //                amfreq = akc6955_get_freq();
764             } else {
765                 fmband++;
766                 if(fmband > 7) fmband = 0;
767 //                amfreq = akc6955_setfreq(amfreq)
768 //                akc6955_set_fmband(fmband);
769                 _AKC6955_WAIT_62_5MS(); // 62.5ms
770 //                fmband = akc6955_get_fmband();
771 //                fmfreq = akc6955_get_freq();
772             }
773             break;
774         case charcode_2: // Change band
775             if(fm == 0){
776                 amband--;
777                 if(amband == 0) amband = 18;
778                 if(amband >= 18) amband = 18;
779 //                amfreq = akc6955_setfreq(amfreq)
780 //                akc6955_set_amband(amband);
781                 _AKC6955_WAIT_62_5MS(); // 62.5ms
782 //                amband = akc6955_get_amband();
783 //                amfreq = akc6955_get_freq();
784             } else {
785                 fmband--;
786                 if(fmband == 0) fmband = 7;
787                 if(fmband >= 7) fmband = 7;
788 //                amfreq = akc6955_setfreq(amfreq)
789 //                akc6955_set_fmband(fmband);
790                 _AKC6955_WAIT_62_5MS(); // 62.5ms
791 //                fmband = akc6955_get_fmband();
792 //                fmfreq = akc6955_get_freq();
793             }
794             break;
795         case charcode_4: // Down Freq;
796             if(fm != 0){
797                 fmfreq -= 10;
798   //              fmfreq = akc6955_down_freq(10); // DOWN 100KHz
799             } else {
800                 amfreq -= 10;
801   //              amfreq = akc6955_down_freq(10); // DOWN 10KHz
802             }
803             break;
804         case charcode_6: // Down Freq;
805             if(fm != 0){
806                 fmfreq += 10;
807     //            fmfreq = akc6955_up_freq(10); // UP 100KHz
808             } else {
809                 amfreq += 10;
810
811     //            amfreq = akc6955_up_freq(10); // UP 10KHz
812             }
813             break;
814         case charcode_7: // Down Fast;
815             if(fm != 0){
816       //          fmfreq = akc6955_down_freq(50); // DOWN 500KHz
817             } else {
818       //          amfreq = akc6955_down_freq(50); // DOWN 50KHz
819             }
820             break;
821         case charcode_9: // Down Fast;
822             if(fm != 0){
823         //        fmfreq = akc6955_up_freq(50); // UP 100KHz
824             } else {
825         //        amfreq = akc6955_up_freq(50); // UP 10KHz
826             }
827             break;
828         case charcode_1: // Down Slow;
829             if(fm != 0){
830         //        fmfreq = akc6955_down_freq(5); // DOWN 50KHz
831             } else {
832           //      amfreq = akc6955_down_freq(5); // DOWN 50KHz
833             }
834             break;
835         case charcode_3: // Down Slow;
836             if(fm != 0){
837         //        fmfreq = akc6955_up_freq(5); // UP 50KHz
838             } else {
839         //        amfreq = akc6955_up_freq(5); // UP 5KHz
840             }
841             break;
842         case charcode_0: // Step
843             if(fm == 0){
844                 if(am_mode3k == 0) {
845                     am_mode3k = 0xff;
846                 } else {
847                     am_mode3k = 0;
848                 }
849           //      amfreq = akc6955_mode3k(am_mode3k);
850             }
851             break;
852         case charcode_a: // Toggle FM
853             toggle_amfm();
854             break;
855         case charcode_b:
856             //input_userband();
857             break;
858         case charcode_c:
859             //set_userband();
860             break;
861         case charcode_d:
862             //set_volume();
863             break;
864         case charcode_e: // Backlight ON/OFF
865             if(backlight_counter > 0) {
866                backlight_counter = 0;
867             } else {
868                backlight_counter = backlight_long;
869             }
870             break;
871         case charcode_f:
872             //main_menu();
873             break;
874         default:
875             break;
876     }
877 }
878 /*
879  * 
880  */
881 static void setdefault(void)
882 {
883     char i;
884     amfreq = 954;
885     fmfreq = 8000; // 10KHz order.
886     amband = AKC6955_BAND_MW2;
887     fmband = AKC6955_BAND_FM2;
888     am_mode3k = 0xff;
889     fm = 0;
890     recv_signal = 0;
891     am_userbandnum = 0;
892     fm_userbandnum = 0;
893     for(i = 0; i < 4; i++){
894         am_usrbands[i].start = 0x19;
895         am_usrbands[i].stop  = 0x32;
896     }
897     for(i = 0; i < 4; i++){
898         fm_usrbands[i].start = 0x19;
899         fm_usrbands[i].stop  = 0x32;
900     }
901
902
903 }
904
905 int main(void)
906 {
907     unsigned char c;
908     unsigned int sum = 0;
909     unsigned char p;
910
911 #ifdef _LCD_DEBUG
912     unsigned char power_flag;
913 #endif
914 //    OSCCON =  (_IDLEN & 0b11111100) | 0b00111000;
915     idle_init();
916     keyin_init();
917     keyin_ioinit();
918
919     //i2c1_init();
920
921     _AKC6955_WAIT_125_0MS(); // Wait 125ms
922 #ifdef _LCD_DEBUG
923     power_flag = 0xff;
924 #endif
925     backlight_long = 256;
926     backlight_counter = backlight_long;
927     backlight_level = 255;
928     ui_idlecount = 65535 - 7182/4 + 1; // 0.25Sec
929
930     acm1602_init(0xa0, 1); //Init LCD
931     _AKC6955_WAIT_125_0MS(); // Wait 125ms
932     _LOCATE(0,0);  // It's BAD-KNOWHOW, but needs AKIZUKI'S LCD :(
933     _PUTCHAR(' '); //
934     _LOCATE(0,1);
935     printstr("Hello;-)");
936     lcd_setbacklight(0xff, 100);
937 //   PORTD |= _LCDPORT_CONT_RS;
938 #if 0
939     i = 10001;
940     do {
941         ClrWdt();
942         set_fmlamp(i & 1);
943         set_amlamp(i & 1);
944         set_powerlamp(i & 1);
945         i = i + 1;
946 //        idle(65536 - 7182/10 + 1);
947         c = pollkeys(pollkeybuf, 60, 1); // Poll about 600ms
948         if(c != 0) {
949             unsigned char sp;
950 //            _LOCATE(0,0);
951             for(sp = 0; sp < c; sp++) _PUTCHAR(pollkeybuf[sp] + '0');
952         } else {
953             _CLS();
954             _LOCATE(0,0);
955             _PUTCHAR(' ');
956         }
957     }while(1);
958 #else
959     idle(0xf800);
960     switch(load_eeprom()) {
961         case 0x01: // No magic-word
962             idle(65535-7128*2+1);
963             _CLS();
964             setdefault();
965             _LOCATE(0,0);
966             printstr("EEPROM FORMATTING");
967             _LOCATE(0,1);
968             printstr("Press any key");
969             c = pollkey_single();
970             _CLS();
971             _LOCATE(0,0);
972             printstr("Formatting...");
973 //            format_eeprom(2,200);
974 //            writeword_eeprom(0, &sum, 0x1298);
975
976   //          _CLS();
977             _LOCATE(0,0);
978             printstr("Save defaults");
979             setdefault();
980 //            save_eeprom();
981             break;
982         case 0x00: // Checksum error
983             idle(65535-7128*2+1);
984             _CLS();
985             _LOCATE(0,0);
986             printstr("X-) Sum Error");
987             _LOCATE(0,1);
988             printstr("Press any key to format");
989             c = pollkey_single();
990             _CLS();
991             _LOCATE(0,0);
992             printstr("Formatting...");
993             format_eeprom(2,250);
994             writeword_eeprom(0, &sum, 0x1298);
995             _CLS();
996             _LOCATE(0,0);
997             printstr("Save defaults");
998             setdefault();
999             save_eeprom();
1000             break;
1001         case 0xff: // Success
1002             break;
1003         default: // Unknown error
1004             setdefault();
1005             break;
1006     }
1007     // Init AKC6955
1008     /* Check EEPROM */
1009     /* Push default parameters to AKC6955*/
1010     scanflag = 0;
1011 #if 0
1012     akc6955_chg_fm(fm); // Set to AM
1013     akc6955_set_amband(amband);
1014     akc6955_set_freq(amfreq); // Dummy, TBS (954KHz)
1015     akc6955_set_power(0xff); // Power ON
1016 #endif
1017 #if 0
1018     idle(0xf000);
1019 #else
1020     idle(0xff00);
1021 #endif
1022     _CLS();
1023     _LOCATE(0,0);
1024     _PUTCHAR(' ');
1025     //update_status();
1026 //    update_display();
1027     ClrWdt();
1028     idle(ui_idlecount);
1029     _LOCATE(0,0);
1030     printstr("OK");
1031     do {
1032         /* Main routine*/
1033        c = pollkeys(pollkeybuf, 60, 1);
1034        p = 0;
1035        while(c > 0) {
1036            setfreq_updown(pollkeybuf[p]);
1037            c--;
1038            p++;
1039        }
1040         // Check battery (include idle?)
1041         // Read AKJC6955's status
1042 //      update_status();
1043         // Putstring to LCD.
1044         _LOCATE(0,0);
1045         update_display();
1046         if(backlight_counter > 0) {
1047             backlight_counter--;
1048             lcd_setbacklight(0xff, backlight_level); // Turn ON
1049         } else {
1050             lcd_setbacklight(0x00, 0); // Turn OFF
1051         }
1052 //        idle(ui_idlecount);
1053     } while(1);
1054 #endif
1055 }
1056