OSDN Git Service

[Schematic][v2.0] For v2.00. Some of circuit is changed(see diff).
[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             update_status();
178             cc = ' ';
179             if((akc6955_chk_donescan() == 0) && (scanflag != 0)){
180                 cc = '>';
181             }
182             _PUTCHAR(cc);
183             printstr("Scan A/4/6/F");
184             print_freq(1);
185             _HOME();
186         } while(c == charcode_null);
187       // New Scan
188        switch(c){
189            case charcode_6:
190                scanflag = 0xff;
191                akc6955_do_scan(0xff);
192                break;
193            case charcode_4:
194                scanflag = 0xff;
195                akc6955_do_scan(0);
196                break;
197            case charcode_a:
198                akc6955_abort_scan();
199                goto _l0;
200                break;
201            case charcode_f:
202                goto _l0;
203                break;
204            default:
205                break;
206        }
207     } while(1);
208 _l0:
209     _CLS();
210 }
211
212 void setfreq_direct(void)
213 {
214     unsigned int val;
215     _CLS();
216     _LOCATE(0,0);
217     printstr("Set Freq:");
218     _LOCATE(0,1);
219     if(setup.fm != 0){
220         // FM
221         printstr("FM ");
222         val = setup.fmfreq;
223         val = read_numeric(val, 5, 7, 1);
224         setup.fmfreq = val;
225     } else {
226         // FM
227         printstr("AM ");
228         val = setup.amfreq;
229         val = read_numeric(val, 5, 7, 1);
230         setup.amfreq = val;
231     }
232     akc6955_set_freq(val);
233 }
234
235 void setband_direct(void)
236 {
237     unsigned int band;
238     unsigned int freq;
239     _CLS();
240     _LOCATE(0,0);
241     if(setup.fm != 0){
242         printstr("Set Band:FM#");
243         band = setup.fmband & 7;
244         setup.fmfreq_bank[setup.fmband] = setup.fmfreq;
245         setup.fmband = read_numeric(band, 2, 7, 1);
246         setup.fmfreq = setup.fmfreq_bank[setup.fmband];
247         akc6955_set_fmband(setup.fmband);
248         freq = setup.fmfreq;
249     } else {
250         printstr("Set Band:AM#");
251         band = setup.amband & 0x1f;
252         setup.amfreq_bank[setup.amband] = setup.amfreq;
253         setup.amband = read_numeric(band, 2, 7, 1);
254         setup.amfreq = setup.amfreq_bank[setup.amband];
255         akc6955_set_amband(setup.amband);
256         freq = setup.amfreq;
257     }
258     akc6955_do_tune();
259     akc6955_set_freq(freq);
260 }
261
262 void call_userband(unsigned char num)
263 {
264     unsigned int freq;
265     unsigned int ch;
266     unsigned char start;
267     unsigned char end;
268     unsigned char mode3k;
269     unsigned char freq2;
270     _userband_t *p;
271
272     if(num >= USER_BAND_NUM) return;
273     
274     p = &(setup.am_usrbands[num]);
275     if(setup.fm != 0){
276         p = &(setup.fm_usrbands[num]);
277     }
278     freq = p->freq;
279     start = p->start;
280     end = p->stop;
281     mode3k = p->mode3k;
282     if(setup.fm != 0) {
283         ch = ((freq - 3000) / 5) * 2;
284         setup.fmband = AKC6955_BAND_FMUSER;
285     } else {
286         unsigned int q = 5;
287         if(mode3k != 0) q = 3;
288         ch = freq / q;
289         setup.amband = AKC6955_BAND_AMUSER;
290     }
291
292     if(start > end) {
293         unsigned char tmp;
294         tmp = start;
295         start = end;
296         end = tmp;
297     }
298     
299     freq2 = ch / 32;
300     if(freq2 > end) {
301         ch = end * 32;
302     }
303     if(freq2 < start){
304         ch = start * 32;
305     }
306     akc6955_set_userband(start, end, ch, mode3k);
307 }
308
309 void set_userband(void)
310 {
311     unsigned int from,to;
312     unsigned char c;
313     unsigned char pp;
314     unsigned char mode3k;
315     char cc;
316
317     _CLS();
318     _LOCATE(0,0);
319     printstr("User ch:");
320     c = pollkey_single();
321
322     if(c > charcode_0) return;
323     if(c < charcode_1) return;
324     if(c == charcode_0) {
325         c = 0;
326     } else {
327         c = c - charcode_1 + 1;
328     }
329     if(c >= USER_BAND_NUM) return;
330
331     if(setup.fm != 0){
332         from = setup.fm_usrbands[c].start * 80 + 3000; // 32*25/10
333         to = setup.fm_usrbands[c].stop * 80 + 3000;
334         _CLS();
335         _LOCATE(0,0);
336         _LOCATE(0,0);
337         printstr("FM#");
338         print_numeric_nosupress(c, 1);
339         _LOCATE(0,1);
340         printstr(" From:");
341         from = read_numeric(from, 5, 7, 1);
342         _LOCATE(0,1);
343         printstr(" To:  ");
344         to = read_numeric(to, 5, 7, 1);
345         setup.fm_usrbands[c].start = (from - 3000) / 80;
346         setup.fm_usrbands[c].stop = (to - 3000) / 80;
347         setup.fm_usrbands[c].freq = from * 80 + 3000;
348         setup.fm_userbandnum = c;
349     } else {
350         mode3k = setup.am_usrbands[c].mode3k;
351         pp = 96; // 3*32
352         if(mode3k == 0) pp = 160; // 5*32
353         from = setup.am_usrbands[c].start * pp;
354         to = setup.am_usrbands[c].stop * pp;
355         _CLS();
356         _LOCATE(0,0);
357         printstr("AM#");
358         print_numeric_nosupress(c, 1);
359         printstr(" Step:");
360         _LOCATE(0,1);
361         printstr("0=3k 1=5k");
362         cc = pollkey_single();
363         if(cc == charcode_0){
364             pp = 96;
365             mode3k = 0xff;
366         } else if(cc = charcode_1) {
367             pp = 160;
368             mode3k = 0;
369         }
370         _CLS();
371         _LOCATE(0,0);
372         printstr("AM#");
373         print_numeric_nosupress(c, 1);
374         _LOCATE(0,1);
375         printstr(" From:");
376         from = read_numeric(from, 5, 7, 1);
377         _LOCATE(0,1);
378         printstr(" To:  ");
379         to = read_numeric(to, 5, 7, 1);
380         setup.am_usrbands[c].start = from / pp;
381         setup.am_usrbands[c].stop = to  / pp;
382         setup.am_usrbands[c].mode3k = mode3k;
383         setup.am_usrbands[c].freq = from * pp;
384         setup.am_userbandnum = c;
385     }
386     call_userband(c);
387     _CLS();
388 }
389
390 void input_userband(void)
391 {
392     unsigned char c;
393     do{
394       c = printhelp_2lines("User Band", "   #");
395
396
397        if((c >= charcode_a) && (c <= charcode_f)){
398           break;
399        }
400        if(c == charcode_0) {
401           _PUTCHAR('0');
402           if(setup.fm != 0){
403              setup.fm_userbandnum = 0;
404           } else {
405              setup.am_userbandnum = 0;
406           }
407           call_userband(0);
408        } else {
409           c = c - charcode_1 + 1;
410           if(c < USER_BAND_NUM) {
411              _PUTCHAR(c + '0');
412              if(setup.fm != 0){
413                setup.fm_userbandnum = c;
414             } else {
415                setup.am_userbandnum = c;
416             }
417             call_userband(c);
418         }
419        }
420        idle(setup.ui_idlecount);
421     } while(1);
422     _CLS();
423 }
424
425
426 void menu_poweroff(void)
427 {
428     unsigned char c;
429     c = printhelp_2lines("Poweroff", "A=Yes");
430     if(c == charcode_a) {
431         shutdown(1);
432     }
433 }
434
435 void setup_akc6955(void)
436 {
437     akc6955_writecmd(AKC6955_POWER, 0xc0);  // You musto *not* mute, set b2 to '0".
438     akc6955_writecmd(AKC6955_VOLUME, 0xc0); // You must select to radio(b1 = '0).
439     if(setup.fm == 0) {
440         akc6955_set_amband(setup.amband);
441         if(setup.amband == AKC6955_BAND_AMUSER) call_userband(setup.am_userbandnum);
442         setup.amfreq = setup.amfreq_bank[setup.amband];
443         akc6955_chg_fm(0, setup.amfreq); // Set to AM
444         akc6955_set_freq(setup.amfreq);
445     } else {
446         akc6955_set_fmband(setup.fmband);
447         if(setup.fmband == AKC6955_BAND_FMUSER) call_userband(setup.fm_userbandnum);
448         setup.fmfreq = setup.fmfreq_bank[setup.fmband];
449         akc6955_chg_fm(0xff, setup.fmfreq); // Set to AM
450         akc6955_set_freq(setup.fmfreq);
451     }// Dummy, TBS (954KHz)
452     akc6955_set_power(0xff); // Power ON
453     akc6955_setvolume(36); // Temporally
454     akc6955_set_thresh_fmcnr(setup.threshold_fmcnr);
455     akc6955_set_thresh_amcnr(setup.threshold_amcnr);
456     akc6955_set_thresh_width(setup.threshold_width);
457     akc6955_set_thresh_fmstereo(setup.threshold_fmstereo);
458     akc6955_setvolumemode(0);
459     akc6955_set_lowboost(setup.lowboost);
460     set_stereomode();
461 }
462
463 void menu_save(void)
464 {
465     unsigned char c;
466     c = printhelp_2lines("Save settings", "A=Yes");
467     if(c == charcode_a) {
468         save_eeprom();
469     }
470 }
471
472 void menu_load(void)
473 {
474     unsigned char c;
475     c = printhelp_2lines("Load settings", "A=Yes B=Init");
476     if(c == charcode_a) {
477         c = load_eeprom();
478         if( c != 0xff) {
479             _CLS();
480             _LOCATE(0,0);
481             c = printhelp_2lines("X) Load Error", "A=Fix");
482             setdefault();
483             if(c == charcode_a){
484                 save_eeprom();
485             }
486         }
487         setup_akc6955();
488     } else if(c == charcode_b){
489         setdefault();
490         setup_akc6955();
491     }
492 }
493
494 void setup_menu(void)
495 {
496     unsigned char c;
497     unsigned int val;
498
499     c = printhelp_2lines("Setup F=HELP", "5=Return");
500     switch(c){
501         case charcode_3:
502             _CLS();
503             _LOCATE(0,0);
504             printstr("BL Level:");
505             val = read_numeric(setup.backlight_level, 3, 0,1);
506             if(val > 255) val = 255;
507             if(val < 10) val = 15;
508             setup.backlight_level = (unsigned char)val;
509             break;
510         case charcode_4:
511             _CLS();
512             _LOCATE(0,0);
513             printstr("FM Bandwidth:");
514             akc6955_get_fmbandwidth(val);
515             val = read_numeric(val, 1, 0, 1) & 3;
516             setup.fmbandwidth = (unsigned char)val;
517             akc6955_set_fmbandwidth(setup.fmbandwidth);
518             break;
519         case charcode_5:
520             break;
521         case charcode_7:
522             _CLS();
523             _LOCATE(0,0);
524             printstr("FM-CNR threshold:");
525             val = setup.threshold_fmcnr;
526             val = read_numeric(val, 1, 0, 1);
527             akc6955_set_thresh_fmcnr((unsigned char)val);
528             break;
529         case charcode_8:
530             _CLS();
531             _LOCATE(0,0);
532             printstr("AM-CNR threshold:");
533             val = setup.threshold_amcnr;
534             val = read_numeric(val, 1, 0, 1);
535             akc6955_set_thresh_amcnr((unsigned char)val);
536             break;
537         case charcode_9:
538             _CLS();
539             _LOCATE(0,0);
540             printstr("Stereo threshold:");
541             val = setup.threshold_fmstereo;
542             val = read_numeric(val, 1, 0, 1);
543             akc6955_set_thresh_fmstereo((unsigned char)val);
544             break;
545         case charcode_0:
546             menu_poweroff();
547             break;
548         case charcode_a:
549             menu_load();
550             break;
551         case charcode_c:
552             menu_save();
553             break;
554         case charcode_d:
555             setdefault();
556             break;
557         case charcode_e:
558             _CLS();
559             _LOCATE(0,0);
560             printstr("BL Long:");
561             val = read_numeric(setup.backlight_long, 3, 0,1);
562             if(val > 999) val = 999;
563             setup.backlight_long = val;
564             break;
565         case charcode_f:
566             setup_help();
567             break;
568         default:
569             break;
570     }
571 }
572 /*
573  * Main Menu : initial-screen -> 'F'.
574  */
575 void main_menu(void)
576 {
577     unsigned char c;
578     unsigned int val;
579
580     printhelp_2lines("Menu:F=HELP", "B=CANCEL");
581     c = pollkey_single(); // 1012ms
582         if(c == charcode_1){
583             // Band
584             setband_direct();
585         } else if(c == charcode_3){
586             // Band
587             setfreq_direct();
588         }else if(c == charcode_4){
589             on_call_userfreq();
590             // Scan
591         }else if(c == charcode_5){
592          //   scan_start();
593             // Scan
594         }else if(c == charcode_6){
595             on_set_userfreq();
596             // Scan
597         } else if(c == charcode_7){
598             menu_poweroff();
599         } else if(c == charcode_8){
600             // Set volume
601             set_volume();
602         } else if(c == charcode_9){
603             _CLS();
604             _LOCATE(0,0);
605             printstr("Sig width:");
606             val = setup.threshold_width;
607             val = read_numeric(val, 1, 0, 1);
608             akc6955_set_thresh_width((unsigned char)val);
609             // Set NF
610         } else if(c == charcode_0){
611             // Setup Menu
612             setup_menu();
613         } else if(c == charcode_a){
614             toggle_amfm();
615         } else if(c == charcode_c){
616             // Set userband
617             set_userband();
618         } else if(c == charcode_d){
619             // Call userband
620             input_userband();
621         } else if (c == charcode_f){
622             mainmenu_help();
623             // HELP
624         }
625        _CLS();
626        _LOCATE(0,0);
627 }
628