OSDN Git Service

[UI][UPDOWN] Fix unselected AMU/MW4 on up-band.
[openi2cradio/OpenI2CRadio.git] / ui_updown.c
1 /*
2  * OpenI2CRADIO
3  * Up-down UI routine.
4  * Copyright (C) 2013-06-10 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 <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #if defined(__SDCC)
34 #include <sdcc-lib.h>
35 #include <pic18fregs.h> /* ONLY FOR PIC18x */
36 #include <delay.h>
37 #else
38 #include <xc.h>
39 #endif
40 #include <signal.h>
41
42 #include "iodef.h"
43 #include "idle.h"
44 #include "i2c_io.h"
45 #include "akc6955.h"
46 #include "lcd_acm1602.h"
47 #include "ui.h"
48 #include "eeprom.h"
49 #include "ioports.h"
50 #include "menu.h"
51 #include "menu_memoryfreq.h"
52 #include "power.h"
53 #include "adc_int.h"
54 #include "backlight.h"
55
56 static void setfreq_updown_amsub(void)
57 {
58     if(setup.amband >= AKC6955_BAND_AMEND) {
59         setup.amband = 0;
60     }
61     akc6955_set_amband(setup.amband);
62     setup.amfreq = setup.amfreq_bank[setup.amband];
63 //    idle_time_62_5ms(); // 62.5ms
64     ClrWdt();
65     akc6955_set_freq(setup.amfreq);
66 }
67
68 static void setfreq_updown_fmsub(void)
69 {
70     if(setup.fmband >= AKC6955_BAND_FMEND) {
71         setup.fmband = 0;
72     }
73     akc6955_set_fmband(setup.fmband);
74     setup.fmfreq = setup.fmfreq_bank[setup.fmband];
75 //    idle_time_62_5ms(); // 62.5ms
76     ClrWdt();
77     akc6955_set_freq(setup.fmfreq);
78 }
79
80 static void setband_updown(unsigned char updown)
81 {
82     unsigned char band;
83     if(setup.fm == 0){  // MW
84         band = setup.amband + 1;
85         if(setup.amband == AKC6955_BAND_AMUSER){
86             setup.am_usrbands[setup.am_userbandnum].freq = setup.amfreq;
87         }
88         setup.amfreq_bank[setup.amband] = setup.amfreq;
89         if(updown == 0) { // down
90             if(band <= 1) {
91                 band = AKC6955_BAND_AMEND + 1;
92             }
93             band -= 2;
94         }
95         setup.amband = band;
96         switch(setup.amband) {
97          case AKC6955_BAND_LW:
98          case AKC6955_BAND_MW2:
99            setup.am_mode3k = 0xff;
100            break;
101          case AKC6955_BAND_MW1:
102          case AKC6955_BAND_MW3:
103          case AKC6955_BAND_MW4:
104            setup.am_mode3k = 0x00;
105            break;
106          case AKC6955_BAND_AMUSER:
107            call_userband(setup.am_userbandnum);
108            break;
109          default:
110            break;
111         }
112         setfreq_updown_amsub();
113     } else { // FM
114         if(setup.fmband == AKC6955_BAND_FMUSER){
115             setup.fm_usrbands[setup.fm_userbandnum].freq = setup.fmfreq;
116         }
117         band = setup.fmband + 1;
118         setup.fmfreq_bank[setup.fmband] = setup.fmfreq;
119         if(updown == 0) { // down
120             if(band <= 1) {
121                 band = AKC6955_BAND_FMEND - 1;
122             }
123             band -= 2;
124         }
125         setup.fmband = band;
126         if(setup.fmband == AKC6955_BAND_FMUSER){
127    //         setup.fmfreq = fm_usrbands[fm_userbandnum].freq;
128             call_userband(setup.fm_userbandnum);
129         }
130         setfreq_updown_fmsub();
131     }
132 }
133
134 void setfreq_updown(unsigned char ctlword)
135 {
136     unsigned int freq;
137
138     backlight_reset(ctlword);
139     update_status();
140     switch(ctlword){
141         case charcode_8: // Up band
142             setband_updown(0xff);
143             break;
144         case charcode_2: // Down band
145             setband_updown(0x00);
146             break;
147         case charcode_6: // Up Fast;
148         case charcode_4: // Down Freq;
149             freq = 9; // 27KHz@3K/9KStep.
150             if(setup.fm != 0) {
151                 freq = 10; // 250KHz
152             } else if(setup.am_mode3k == 0){
153                 freq = 6; // 30KHz
154             }
155             if(ctlword == charcode_6) {
156                akc6955_up_freq(freq); // 225 or 27 or 45KHz
157             } else {
158                akc6955_down_freq(freq); // 225 or 27 or 45KHz
159             }
160             break;
161         case charcode_9: // Up Fast;
162         case charcode_7: // Down Fast;
163             freq = 33; // 99KHz@3K/9KStep.
164             if(setup.fm != 0){
165                 freq = 40; // 1MHz
166             } else if(setup.am_mode3k == 0){
167                 freq = 20; // 100KHz
168             }
169             if(ctlword == charcode_9) {
170                akc6955_up_freq(freq); // 825 or 90 or 165KHz
171             } else {
172                akc6955_down_freq(freq); // 825 or 90 or 165KHz
173             }
174             break;
175         case charcode_3: // Up Slow;
176         case charcode_1: // Down Slow;
177             if(setup.fm != 0){
178                 freq = 4; // DOWN 100KHz
179             } else {
180                 freq = 1; // 5KHz or 3KHz
181                 if(setup.amband == AKC6955_BAND_MW2) {
182                     freq = 3; // 9KHz
183                 } else if(setup.amband == AKC6955_BAND_MW3) {
184                     freq = 2; // 10KHz
185                 } else if(setup.amband == AKC6955_BAND_MW4) {
186                     freq = 2; // 10KHz
187                 }
188             }
189             if(ctlword == charcode_1) {
190                akc6955_down_freq(freq);
191             } else {
192                akc6955_up_freq(freq);
193             }
194        
195             break;
196         case charcode_0: // Step
197             setup.threshold_width++;
198             akc6955_set_thresh_width(setup.threshold_width);
199             akc6955_do_tune();
200             break;
201         case charcode_a: // Toggle FM
202             toggle_amfm();
203             break;
204         case charcode_b:
205 //            set_volume();
206              on_updown_userfreq();
207             break;
208         case charcode_c:
209             // Scan
210             scan_start();
211             break;
212         case charcode_d:
213             // FM Narrow/Wide
214             set_stereo();
215             break;
216         case charcode_5:
217             main_menu();
218             break;
219         case charcode_f:
220             updown_help();
221             _CLS();
222             _LOCATE(0,0);
223             break;
224         default:
225             break;
226     }
227 }