OSDN Git Service

[Menu][v1.0] Remove tedious function-callings.Save program-memory about 400bytes...
[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 #include "backlight.h"
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 dispf = 0xff;
87     unsigned char fact;
88
89     _CLS();
90     do {
91         if(dispf != 0x00){
92             _LOCATE(0,0);
93             d = akc6955_getvolumemode();
94             if(d == 0){
95                  _PUTCHAR('D');
96             } else {
97                 _PUTCHAR('K');
98             }
99             printstr(" Vol:");
100             print_numeric_nosupress(setup.volume, 2);
101             printstr("  F:Exit");
102             _LOCATE(0,1);
103             printstr("Pre:");
104             _PUTCHAR('0' + (setup.prevolume & 3));
105             _LOCATE(16-4, 1);
106             if(setup.lowboost == 0x00){
107                  printstr("   ");
108             } else {
109                 printstr("LOW");
110             }
111             _HOME();
112          }
113      c = pollkey_single_timeout(41, 1); // 506ms
114      backlight_reset(c);
115      dispf = backlight_dec(dispf);
116      switch(c) {
117              case charcode_6:
118                  setup.volume++;
119                  if(setup.volume > 63) setup.volume = 63;
120                  break;
121              case charcode_4:
122                  setup.volume--;
123                  if(setup.volume < 23) setup.volume = 23;
124                  break;
125              case charcode_f:
126                  _CLS();
127                  return;
128              case charcode_a:
129                  fact = 0xff;
130                  if(d != 0){
131                      fact = 0x00;
132                  }
133                  akc6955_setvolumemode(fact);
134                  break;
135              case charcode_b:
136                  setup.prevolume++;
137                  if(setup.prevolume > 3) setup.prevolume = 0;
138                  akc6955_set_prevolume(setup.prevolume);
139                  break;
140              case charcode_d:
141                  fact = 0x00;
142                  if(setup.lowboost == 0x00){
143                      fact = 0xff;
144                  }
145                  setup.lowboost = fact;
146                  akc6955_set_lowboost(setup.lowboost);
147                  break;
148              default:
149                  break;
150          }
151          if(setup.volume < 24){
152              set_examp_mute(1);
153         } else {
154              set_examp_mute(0);
155         }
156         akc6955_setvolume(setup.volume);
157         idle_time_ms(9); // Adjust to 1S
158     } while(1);
159 }
160
161
162 void scan_start(void)
163 {
164     unsigned char c;
165     unsigned char cc;
166     _CLS();
167     scanflag = 0;
168     do {
169         do {
170              c = pollkey_single_timeout(20, 0) ; // 23*20 = 460ms
171             update_status();
172             cc = ' ';
173             if((akc6955_chk_donescan() == 0) && (scanflag != 0)){
174                 cc = '>';
175             }
176             _PUTCHAR(cc);
177             printstr("Scan A/4/6/F");
178             print_freq(1);
179             _HOME();
180         } while(c == charcode_null);
181          switch(c){
182            case charcode_6:
183                scanflag = 0xff;
184                akc6955_do_scan(0xff);
185                break;
186            case charcode_4:
187                scanflag = 0xff;
188                akc6955_do_scan(0);
189                break;
190            case charcode_a:
191                akc6955_abort_scan();
192                goto _l0;
193                break;
194            case charcode_f:
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         printstr("FM#");
330         print_numeric_nosupress(c, 1);
331         _LOCATE(0,1);
332         printstr(" From:");
333         from = read_numeric(from, 5, 7, 1);
334         _LOCATE(0,1);
335         printstr(" To:  ");
336         to = read_numeric(to, 5, 7, 1);
337         setup.fm_usrbands[c].start = (from - 3000) / 80;
338         setup.fm_usrbands[c].stop = (to - 3000) / 80;
339         setup.fm_usrbands[c].freq = from * 80 + 3000;
340         setup.fm_userbandnum = c;
341     } else {
342         mode3k = setup.am_usrbands[c].mode3k;
343         pp = 96; // 3*32
344         if(mode3k == 0) pp = 160; // 5*32
345         from = setup.am_usrbands[c].start * pp;
346         to = setup.am_usrbands[c].stop * pp;
347         _CLS();
348         _LOCATE(0,0);
349         printstr("AM#");
350         print_numeric_nosupress(c, 1);
351         printstr(" Step:");
352         _LOCATE(0,1);
353         printstr("0=3k 1=5k");
354         cc = pollkey_single();
355         if(cc == charcode_0){
356             pp = 96;
357             mode3k = 0xff;
358         } else if(cc = charcode_1) {
359             pp = 160;
360             mode3k = 0;
361         }
362         _CLS();
363         _LOCATE(0,0);
364         printstr("AM#");
365         print_numeric_nosupress(c, 1);
366         _LOCATE(0,1);
367         printstr(" From:");
368         from = read_numeric(from, 5, 7, 1);
369         _LOCATE(0,1);
370         printstr(" To:  ");
371         to = read_numeric(to, 5, 7, 1);
372         setup.am_usrbands[c].start = from / pp;
373         setup.am_usrbands[c].stop = to  / pp;
374         setup.am_usrbands[c].mode3k = mode3k;
375         setup.am_usrbands[c].freq = from * pp;
376         setup.am_userbandnum = c;
377     }
378     call_userband(c);
379     _CLS();
380 }
381
382 void input_userband(void)
383 {
384     unsigned char c;
385     do{
386     c = printhelp_2lines("User Band", "   #");
387     if((c >= charcode_a) && (c <= charcode_f)){
388         break;
389     }
390     if(c == charcode_0) {
391         _PUTCHAR('0');
392         if(setup.fm != 0){
393            setup.fm_userbandnum = 0;
394         } else {
395            setup.am_userbandnum = 0;
396         }
397         call_userband(0);
398     } else {
399         c = c - charcode_1 + 1;
400         if(c < USER_BAND_NUM) {
401             _PUTCHAR(c + '0');
402             if(setup.fm != 0){
403                setup.fm_userbandnum = c;
404             } else {
405                 setup.am_userbandnum = c;
406             }
407             call_userband(c);
408         }
409     }
410     idle(setup.ui_idlecount);
411     } while(1);
412     _CLS();
413 }
414
415
416 void menu_poweroff(void)
417 {
418     unsigned char c;
419     c = printhelp_2lines("Poweroff", "A=Yes");
420     if(c == charcode_a) {
421         shutdown(1);
422     }
423 }
424
425 void setup_akc6955(void)
426 {
427     akc6955_writecmd(AKC6955_POWER, 0xc0);  // You musto *not* mute, set b2 to '0".
428     akc6955_writecmd(AKC6955_VOLUME, 0xc0); // You must select to radio(b1 = '0).
429     if(setup.fm == 0) {
430         akc6955_set_amband(setup.amband);
431         if(setup.amband == AKC6955_BAND_AMUSER) call_userband(setup.am_userbandnum);
432         setup.amfreq = setup.amfreq_bank[setup.amband];
433         akc6955_chg_fm(0, setup.amfreq); // Set to AM
434         akc6955_set_freq(setup.amfreq);
435     } else {
436         akc6955_set_fmband(setup.fmband);
437         if(setup.fmband == AKC6955_BAND_FMUSER) call_userband(setup.fm_userbandnum);
438         setup.fmfreq = setup.fmfreq_bank[setup.fmband];
439         akc6955_chg_fm(0xff, setup.fmfreq); // Set to AM
440         akc6955_set_freq(setup.fmfreq);
441     }// Dummy, TBS (954KHz)
442     akc6955_set_power(0xff); // Power ON
443     akc6955_setvolume(36); // Temporally
444     akc6955_set_thresh_fmcnr(setup.threshold_fmcnr);
445     akc6955_set_thresh_amcnr(setup.threshold_amcnr);
446     akc6955_set_thresh_width(setup.threshold_width);
447     akc6955_set_thresh_fmstereo(setup.threshold_fmstereo);
448     akc6955_setvolumemode(0);
449     akc6955_set_lowboost(setup.lowboost);
450     set_stereomode();
451 }
452
453 void menu_save(void)
454 {
455     unsigned char c;
456     c = printhelp_2lines("Save settings", "A=Yes");
457     if(c == charcode_a) {
458         save_eeprom();
459     }
460 }
461
462 void menu_load(void)
463 {
464     unsigned char c;
465     c = printhelp_2lines("Load settings", "A=Yes B=Init");
466     if(c == charcode_a) {
467         c = load_eeprom();
468         if( c != 0xff) {
469             _CLS();
470             _LOCATE(0,0);
471             c = printhelp_2lines("X) Load Error", "A=Fix");
472             setdefault();
473             if(c == charcode_a){
474                 save_eeprom();
475             }
476         }
477         setup_akc6955();
478     } else if(c == charcode_b){
479         setdefault();
480         setup_akc6955();
481     }
482 }
483
484 void setup_menu(void)
485 {
486     unsigned char c;
487     unsigned int val;
488
489     c = printhelp_2lines("Setup F=HELP", "5=Return");
490     _CLS();
491     _LOCATE(0,0);
492     switch(c){
493         case charcode_3:
494             printstr("BL Level:");
495             val = read_numeric(setup.backlight_level, 3, 0,1);
496             if(val > 255) val = 255;
497             if(val < 10) val = 15;
498             setup.backlight_level = (unsigned char)val;
499             break;
500         case charcode_4:
501             printstr("FM Bandwidth:");
502             akc6955_get_fmbandwidth(val);
503             val = read_numeric(val, 1, 0, 1) & 3;
504             setup.fmbandwidth = (unsigned char)val;
505             akc6955_set_fmbandwidth(setup.fmbandwidth);
506             break;
507         case charcode_5:
508             break;
509         case charcode_7:
510             printstr("FM-CNR threshold:");
511             val = setup.threshold_fmcnr;
512             val = read_numeric(val, 1, 0, 1);
513             akc6955_set_thresh_fmcnr((unsigned char)val);
514             break;
515         case charcode_8:
516             printstr("AM-CNR threshold:");
517             val = setup.threshold_amcnr;
518             val = read_numeric(val, 1, 0, 1);
519             akc6955_set_thresh_amcnr((unsigned char)val);
520             break;
521         case charcode_9:
522             printstr("Stereo threshold:");
523             val = setup.threshold_fmstereo;
524             val = read_numeric(val, 1, 0, 1);
525             akc6955_set_thresh_fmstereo((unsigned char)val);
526             break;
527         case charcode_0:
528             menu_poweroff();
529             break;
530         case charcode_a:
531             menu_load();
532             break;
533         case charcode_c:
534             menu_save();
535             break;
536         case charcode_d:
537             setdefault();
538             break;
539         case charcode_e:
540             printstr("BL Long:");
541             val = read_numeric(setup.backlight_long, 3, 0,1);
542             if(val > 999) val = 999;
543             setup.backlight_long = val;
544             break;
545         case charcode_f:
546             setup_help();
547             break;
548         default:
549             break;
550     }
551     _CLS();
552     _LOCATE(0,0);
553 }
554 /*
555  * Main Menu : initial-screen -> 'F'.
556  */
557 void main_menu(void)
558 {
559     unsigned char c;
560     unsigned int val;
561
562     c = printhelp_2lines("Menu: F=HELP", "B=CANCEL");
563       if(c == charcode_1){
564             // Band
565             setband_direct();
566        } else if(c == charcode_3){
567             // Band
568             setfreq_direct();
569        }else if(c == charcode_4){
570             on_call_userfreq();
571             // Scan
572         }else if(c == charcode_5){
573          //   scan_start();
574             // Scan
575         }else if(c == charcode_6){
576             on_set_userfreq();
577             // Scan
578         } else if(c == charcode_7){
579             menu_poweroff();
580         } else if(c == charcode_8){
581             // Set volume
582             set_volume();
583         } else if(c == charcode_9){
584             _CLS();
585             _LOCATE(0,0);
586             printstr("Sig width:");
587             val = setup.threshold_width;
588             val = read_numeric(val, 1, 0, 1);
589             akc6955_set_thresh_width((unsigned char)val);
590             // Set NF
591         } else if(c == charcode_0){
592             // Setup Menu
593             setup_menu();
594         } else if(c == charcode_a){
595             toggle_amfm();
596         } else if(c == charcode_c){
597             // Set userband
598             set_userband();
599         } else if(c == charcode_d){
600             // Call userband
601             input_userband();
602         } else if (c == charcode_f){
603             mainmenu_help();
604             // HELP
605         }
606        _CLS();
607        _LOCATE(0,0);
608 }
609