OSDN Git Service

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