OSDN Git Service

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