OSDN Git Service

[Schematic][v1.0][MCU] Fix oops on LCD-CONNECTOR.
[openi2cradio/OpenI2CRadio.git] / menu.c
1 /*
2  * OpenI2CRADIO
3  * Menu sub-routines.
4  * Copyright (C) 2013-06-21 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 "menu.h"
30 #include "menu_memoryfreq.h"
31 #include "power.h"
32 #include "commondef.h"
33 #include "backlight.h"
34
35 void toggle_amfm(void)
36 {
37     unsigned int freq;
38     if(setup.fm != 0){
39         setup.fm = 0;
40         setup.fmfreq_bank[setup.fmband] = setup.fmfreq;
41   //      akc6955_set_amband(amband);
42         freq = setup.amfreq_bank[setup.amband];
43     } else {
44         setup.fm = 0xff;
45         setup.amfreq_bank[setup.amband] = setup.amfreq;
46 //        akc6955_set_fmband(fmband);
47         freq = setup.fmfreq_bank[setup.fmband];
48     }
49     akc6955_chg_fm(setup.fm, freq);
50     idle_time_ms(500);
51 //    akc6955_set_freq(freq);
52 }
53
54
55
56 void set_stereomode(void)
57 {
58     __bitops_t mode;
59     mode.byte = 0x00;
60     if(setup.stereo == 0x00){
61             mode.b0 = 1; // Force mono
62     } else if(setup.stereo < 0x80){
63             mode.b1 = 1; // Force stereo
64     }
65     akc6955_set_stereomode(mode.byte);
66 }
67
68 void set_stereo(void)
69 {
70     
71     if(setup.stereo == 0xff) {// Force Mono
72         setup.stereo = 0x00;
73     } else if(setup.stereo == 0x00) { //Mono->Stereo
74         setup.stereo = 0x7f;
75     } else { // Default, Stereo->auto
76         setup.stereo = 0xff;
77     }
78     set_stereomode();
79 }
80
81
82 void set_volume(void)
83 {
84     unsigned char c;
85     unsigned char d;
86     unsigned char fact;
87     unsigned char dispf = 0xff;
88
89     _CLS();
90     do {
91         if(dispf != 0) {
92             _LOCATE(0,0);
93              d = akc6955_getvolumemode();
94             if(d == 0){
95                  _PUTCHAR('D');
96             } else {
97                  _PUTCHAR('K');
98             }
99             printstr(" Vol:");
100             print_numeric_nosupress(setup.volume, 2);
101             printstr("  F:Exit");
102             _LOCATE(0,1);
103             printstr("Pre:");
104             _PUTCHAR('0' + (setup.prevolume & 3));
105             _LOCATE(16-4, 1);
106             if(setup.lowboost == 0x00){
107                  printstr("   ");
108             } else {
109                  printstr("LOW");
110          }
111             _HOME();
112         }
113      c = pollkey_single_timeout(41, 1); // 943ms
114      backlight_reset(c);
115      dispf = backlight_dec(dispf);
116
117      switch(c) {
118              case charcode_6:
119                  setup.volume++;
120                  if(setup.volume > 63) setup.volume = 63;
121                  akc6955_setvolume(setup.volume);
122                  break;
123              case charcode_4:
124                  setup.volume--;
125                  if(setup.volume < 23) setup.volume = 23;
126                  akc6955_setvolume(setup.volume);
127                  break;
128              case charcode_f:
129                  _CLS();
130                  return;
131              case charcode_a:
132                  fact = 0xff;
133                  if(d != 0){
134                      fact = 0x00;
135                  }
136                  akc6955_setvolumemode(fact);
137                  akc6955_setvolume(setup.volume);
138                  break;
139              case charcode_b:
140                  setup.prevolume++;
141                  if(setup.prevolume > 3) setup.prevolume = 0;
142                  akc6955_set_prevolume(setup.prevolume);
143                  akc6955_setvolume(setup.volume);
144                  break;
145              case charcode_d:
146                  fact = 0x00;
147                  if(setup.lowboost == 0x00){
148                      fact = 0xff;
149                  }
150                  setup.lowboost = fact;
151                  akc6955_set_lowboost(setup.lowboost);
152                  akc6955_setvolume(setup.volume);
153                  break;
154              default:
155                  break;
156          }
157          if(setup.volume < 24){
158              set_examp_mute(1);
159         } else {
160              set_examp_mute(0);
161         }
162         idle_time_ms(9); // Pad.
163     } while(1);
164 }
165
166
167 void scan_start(void)
168 {
169     unsigned char c;
170     unsigned char cc;
171     unsigned char n;
172     _CLS();
173     scanflag = 0;
174     do {
175         do {
176              c = pollkey_single_timeout(20, 0) ; // 23*20 = 460ms
177              if(chk_powerbutton() != 0) shutdown(1);
178             update_status();
179             cc = ' ';
180             if((akc6955_chk_donescan() == 0) && (scanflag != 0)){
181                 cc = '>';
182             }
183             _PUTCHAR(cc);
184             printstr("Scan A/4/6/F");
185             print_freq(1);
186             _HOME();
187         } while(c == charcode_null);
188       // New Scan
189        switch(c){
190            case charcode_6:
191                scanflag = 0xff;
192                akc6955_do_scan(0xff);
193                break;
194            case charcode_4:
195                scanflag = 0xff;
196                akc6955_do_scan(0);
197                break;
198            case charcode_a:
199                akc6955_abort_scan();
200                goto _l0;
201                break;
202            case charcode_f:
203                goto _l0;
204                break;
205            default:
206                break;
207        }
208     } while(1);
209 _l0:
210     _CLS();
211 }
212
213 void setfreq_direct(void)
214 {
215     unsigned int val;
216     _CLS();
217     _LOCATE(0,0);
218     printstr("Set Freq:");
219     _LOCATE(0,1);
220     if(setup.fm != 0){
221         // FM
222         printstr("FM ");
223         val = setup.fmfreq;
224         val = read_numeric(val, 5, 7, 1);
225         setup.fmfreq = val;
226     } else {
227         // FM
228         printstr("AM ");
229         val = setup.amfreq;
230         val = read_numeric(val, 5, 7, 1);
231         setup.amfreq = val;
232     }
233     akc6955_set_freq(val);
234 }
235
236 void setband_direct(void)
237 {
238     unsigned int band;
239     unsigned int freq;
240     _CLS();
241     _LOCATE(0,0);
242     if(setup.fm != 0){
243         printstr("Set Band:FM#");
244         band = setup.fmband & 7;
245         setup.fmfreq_bank[setup.fmband] = setup.fmfreq;
246         setup.fmband = read_numeric(band, 2, 7, 1);
247         setup.fmfreq = setup.fmfreq_bank[setup.fmband];
248         akc6955_set_fmband(setup.fmband);
249         freq = setup.fmfreq;
250     } else {
251         printstr("Set Band:AM#");
252         band = setup.amband & 0x1f;
253         setup.amfreq_bank[setup.amband] = setup.amfreq;
254         setup.amband = read_numeric(band, 2, 7, 1);
255         setup.amfreq = setup.amfreq_bank[setup.amband];
256         switch(band) {
257          case AKC6955_BAND_LW:
258          case AKC6955_BAND_MW2:
259            setup.am_mode3k = 0xff;
260            break;
261          case AKC6955_BAND_MW1:
262          case AKC6955_BAND_MW3:
263          case AKC6955_BAND_MW4:
264            setup.am_mode3k = 0x00;
265            break;
266          default:
267            break;
268         }
269        
270         akc6955_set_amband(setup.amband);
271         freq = setup.amfreq;
272     }
273     akc6955_do_tune();
274     akc6955_set_freq(freq);
275 }
276
277 void call_userband(unsigned char num)
278 {
279     unsigned int freq;
280     unsigned int ch;
281     unsigned char start;
282     unsigned char end;
283     unsigned char mode3k;
284     unsigned char freq2;
285     _userband_t *p;
286
287     if(num >= USER_BAND_NUM) return;
288     
289     p = &(setup.am_usrbands[num]);
290     if(setup.fm != 0){
291         p = &(setup.fm_usrbands[num]);
292     }
293     freq = p->freq;
294     start = p->start;
295     end = p->stop;
296     mode3k = p->mode3k;
297     if(setup.fm != 0) {
298         ch = ((freq - 3000) / 5) * 2;
299         setup.fmband = AKC6955_BAND_FMUSER;
300     } else {
301         unsigned int q = 5;
302         if(mode3k != 0) q = 3;
303         ch = freq / q;
304         setup.amband = AKC6955_BAND_AMUSER;
305     }
306
307     if(start > end) {
308         unsigned char tmp;
309         tmp = start;
310         start = end;
311         end = tmp;
312     }
313     
314     freq2 = ch / 32;
315     if(freq2 > end) {
316         ch = end * 32;
317     }
318     if(freq2 < start){
319         ch = start * 32;
320     }
321     akc6955_set_userband(start, end, ch, mode3k);
322 }
323
324 void set_userband(void)
325 {
326     unsigned int from,to;
327     unsigned char c;
328     unsigned char pp;
329     unsigned char mode3k;
330     char cc;
331
332     _CLS();
333     _LOCATE(0,0);
334     printstr("User ch:");
335     c = pollkey_single();
336
337     if(c > charcode_0) return;
338     if(c < charcode_1) return;
339     if(c == charcode_0) {
340         c = 0;
341     } else {
342         c = c - charcode_1 + 1;
343     }
344     if(c >= USER_BAND_NUM) return;
345
346     if(setup.fm != 0){
347         from = setup.fm_usrbands[c].start * 80 + 3000; // 32*25/10
348         to = setup.fm_usrbands[c].stop * 80 + 3000;
349         _CLS();
350         _LOCATE(0,0);
351         _LOCATE(0,0);
352         printstr("FM#");
353         print_numeric_nosupress(c, 1);
354         _LOCATE(0,1);
355         printstr(" From:");
356         from = read_numeric(from, 5, 7, 1);
357         _LOCATE(0,1);
358         printstr(" To:  ");
359         to = read_numeric(to, 5, 7, 1);
360         setup.fm_usrbands[c].start = (from - 3000) / 80;
361         setup.fm_usrbands[c].stop = (to - 3000) / 80;
362         setup.fm_usrbands[c].freq = from * 80 + 3000;
363         setup.fm_userbandnum = c;
364     } else {
365         mode3k = setup.am_usrbands[c].mode3k;
366         pp = 96; // 3*32
367         if(mode3k == 0) pp = 160; // 5*32
368         from = setup.am_usrbands[c].start * pp;
369         to = setup.am_usrbands[c].stop * pp;
370         _CLS();
371         _LOCATE(0,0);
372         printstr("AM#");
373         print_numeric_nosupress(c, 1);
374         printstr(" Step:");
375         _LOCATE(0,1);
376         printstr("0=3k 1=5k");
377         cc = pollkey_single();
378         if(cc == charcode_0){
379             pp = 96;
380             mode3k = 0xff;
381         } else if(cc = charcode_1) {
382             pp = 160;
383             mode3k = 0;
384         }
385         _CLS();
386         _LOCATE(0,0);
387         printstr("AM#");
388         print_numeric_nosupress(c, 1);
389         _LOCATE(0,1);
390         printstr(" From:");
391         from = read_numeric(from, 5, 7, 1);
392         _LOCATE(0,1);
393         printstr(" To:  ");
394         to = read_numeric(to, 5, 7, 1);
395         setup.am_usrbands[c].start = from / pp;
396         setup.am_usrbands[c].stop = to  / pp;
397         setup.am_usrbands[c].mode3k = mode3k;
398         setup.am_usrbands[c].freq = from * pp;
399         setup.am_userbandnum = c;
400     }
401     call_userband(c);
402     _CLS();
403 }
404
405 void input_userband(void)
406 {
407     unsigned char c;
408     do{
409       c = printhelp_2lines("User Band", "   #");
410
411
412        if((c >= charcode_a) && (c <= charcode_f)){
413           break;
414        }
415        if(c == charcode_0) {
416           _PUTCHAR('0');
417           if(setup.fm != 0){
418              setup.fm_userbandnum = 0;
419           } else {
420              setup.am_userbandnum = 0;
421           }
422           call_userband(0);
423        } else {
424           c = c - charcode_1 + 1;
425           if(c < USER_BAND_NUM) {
426              _PUTCHAR(c + '0');
427              if(setup.fm != 0){
428                setup.fm_userbandnum = c;
429             } else {
430                setup.am_userbandnum = c;
431             }
432             call_userband(c);
433         }
434        }
435        idle(setup.ui_idlecount);
436     } while(1);
437     _CLS();
438 }
439
440
441 void menu_poweroff(void)
442 {
443     unsigned char c;
444     c = printhelp_2lines("Poweroff", "A=Yes");
445     if(c == charcode_a) {
446         shutdown(1);
447     }
448 }
449
450 void setup_akc6955(void)
451 {
452     akc6955_writecmd(AKC6955_POWER, 0xc0);  // You musto *not* mute, set b2 to '0".
453     akc6955_writecmd(AKC6955_VOLUME, 0xc0); // You must select to radio(b1 = '0).
454     if(setup.fm == 0) {
455         akc6955_set_amband(setup.amband);
456         if(setup.amband == AKC6955_BAND_AMUSER) call_userband(setup.am_userbandnum);
457         setup.amfreq = setup.amfreq_bank[setup.amband];
458         akc6955_chg_fm(0, setup.amfreq); // Set to AM
459         akc6955_set_freq(setup.amfreq);
460     } else {
461         akc6955_set_fmband(setup.fmband);
462         if(setup.fmband == AKC6955_BAND_FMUSER) call_userband(setup.fm_userbandnum);
463         setup.fmfreq = setup.fmfreq_bank[setup.fmband];
464         akc6955_chg_fm(0xff, setup.fmfreq); // Set to AM
465         akc6955_set_freq(setup.fmfreq);
466     }// Dummy, TBS (954KHz)
467     akc6955_set_power(0xff); // Power ON
468     akc6955_setvolume(36); // Temporally
469     akc6955_set_thresh_fmcnr(setup.threshold_fmcnr);
470     akc6955_set_thresh_amcnr(setup.threshold_amcnr);
471     akc6955_set_thresh_width(setup.threshold_width);
472     akc6955_set_thresh_fmstereo(setup.threshold_fmstereo);
473     akc6955_setvolumemode(0);
474     akc6955_set_lowboost(setup.lowboost);
475     set_stereomode();
476 }
477
478 void menu_save(void)
479 {
480     unsigned char c;
481     c = printhelp_2lines("Save settings", "A=Yes");
482     if(c == charcode_a) {
483         save_eeprom();
484     }
485 }
486
487 void menu_load(void)
488 {
489     unsigned char c;
490     c = printhelp_2lines("Load settings", "A=Yes B=Init");
491     if(c == charcode_a) {
492         c = load_eeprom();
493         if( c != 0xff) {
494             _CLS();
495             _LOCATE(0,0);
496             c = printhelp_2lines("X) Load Error", "A=Fix");
497             setdefault();
498             if(c == charcode_a){
499                 save_eeprom();
500             }
501         }
502         setup_akc6955();
503     } else if(c == charcode_b){
504         setdefault();
505         setup_akc6955();
506     }
507 }
508
509 void setup_menu(void)
510 {
511     unsigned char c;
512     unsigned int val;
513
514     c = printhelp_2lines("Setup F=HELP", "5=Return");
515     switch(c){
516         case charcode_3:
517             _CLS();
518             _LOCATE(0,0);
519             printstr("BL Level:");
520             val = read_numeric(setup.backlight_level, 3, 0,1);
521             if(val > 255) val = 255;
522             if(val < 10) val = 15;
523             setup.backlight_level = (unsigned char)val;
524             break;
525         case charcode_4:
526             _CLS();
527             _LOCATE(0,0);
528             printstr("FM Bandwidth:");
529             akc6955_get_fmbandwidth(val);
530             val = read_numeric(val, 1, 0, 1) & 3;
531             setup.fmbandwidth = (unsigned char)val;
532             akc6955_set_fmbandwidth(setup.fmbandwidth);
533             break;
534         case charcode_5:
535             break;
536         case charcode_7:
537             _CLS();
538             _LOCATE(0,0);
539             printstr("FM-CNR threshold:");
540             val = setup.threshold_fmcnr;
541             val = read_numeric(val, 1, 0, 1);
542             akc6955_set_thresh_fmcnr((unsigned char)val);
543             break;
544         case charcode_8:
545             _CLS();
546             _LOCATE(0,0);
547             printstr("AM-CNR threshold:");
548             val = setup.threshold_amcnr;
549             val = read_numeric(val, 1, 0, 1);
550             akc6955_set_thresh_amcnr((unsigned char)val);
551             break;
552         case charcode_9:
553             _CLS();
554             _LOCATE(0,0);
555             printstr("Stereo threshold:");
556             val = setup.threshold_fmstereo;
557             val = read_numeric(val, 1, 0, 1);
558             akc6955_set_thresh_fmstereo((unsigned char)val);
559             break;
560         case charcode_0:
561             menu_poweroff();
562             break;
563         case charcode_a:
564             menu_load();
565             break;
566         case charcode_c:
567             menu_save();
568             break;
569         case charcode_d:
570             setdefault();
571             break;
572         case charcode_e:
573             _CLS();
574             _LOCATE(0,0);
575             printstr("BL Long:");
576             val = read_numeric(setup.backlight_long, 3, 0,1);
577             if(val > 999) val = 999;
578             setup.backlight_long = val;
579             break;
580         case charcode_f:
581             setup_help();
582             break;
583         default:
584             break;
585     }
586 }
587 /*
588  * Main Menu : initial-screen -> 'F'.
589  */
590 void main_menu(void)
591 {
592     unsigned char c;
593     unsigned int val;
594
595     printhelp_2lines("Menu:F=HELP", "B=CANCEL");
596     c = pollkey_single(); // 1012ms
597         if(c == charcode_1){
598             // Band
599             setband_direct();
600         } else if(c == charcode_3){
601             // Band
602             setfreq_direct();
603         }else if(c == charcode_4){
604             on_call_userfreq();
605             // Scan
606         }else if(c == charcode_5){
607          //   scan_start();
608             // Scan
609         }else if(c == charcode_6){
610             on_set_userfreq();
611             // Scan
612         } else if(c == charcode_7){
613             menu_poweroff();
614         } else if(c == charcode_8){
615             // Set volume
616             set_volume();
617         } else if(c == charcode_9){
618             _CLS();
619             _LOCATE(0,0);
620             printstr("Sig width:");
621             val = setup.threshold_width;
622             val = read_numeric(val, 1, 0, 1);
623             akc6955_set_thresh_width((unsigned char)val);
624             // Set NF
625         } else if(c == charcode_0){
626             // Setup Menu
627             setup_menu();
628         } else if(c == charcode_a){
629             toggle_amfm();
630         } else if(c == charcode_c){
631             // Set userband
632             set_userband();
633         } else if(c == charcode_d){
634             // Call userband
635             input_userband();
636         } else if (c == charcode_f){
637             mainmenu_help();
638             // HELP
639         }
640        _CLS();
641        _LOCATE(0,0);
642 }
643