OSDN Git Service

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