OSDN Git Service

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