OSDN Git Service

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