OSDN Git Service

[SCHEMATIC] Modify SW/MW/LW Preamp, insert galbanic-isolator replace of common-mode...
[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 #include "rencoder.h"
56
57 static void setfreq_updown_amsub(void)
58 {
59     if(setup.amband >= AKC6955_BAND_AMEND) {
60         setup.amband = 0;
61     }
62     akc6955_set_amband(setup.amband);
63     setup.amfreq = setup.amfreq_bank[setup.amband];
64 //    idle_time_62_5ms(); // 62.5ms
65     ClrWdt();
66     akc6955_mode3k(setup.am_mode3k);
67     akc6955_set_freq(setup.amfreq);
68 }
69
70 static void setfreq_updown_fmsub(void)
71 {
72     if(setup.fmband >= AKC6955_BAND_FMEND) {
73         setup.fmband = 0;
74     }
75     akc6955_set_fmband(setup.fmband);
76     setup.fmfreq = setup.fmfreq_bank[setup.fmband];
77 //    idle_time_62_5ms(); // 62.5ms
78     ClrWdt();
79     akc6955_set_freq(setup.fmfreq);
80 }
81
82 static void setband_updown(unsigned char updown)
83 {
84     unsigned char band;
85     if(setup.fm == 0){  // MW
86         band = setup.amband + 1;
87         if(setup.amband == AKC6955_BAND_AMUSER){
88             userband.am_usrbands[setup.am_userbandnum].freq = setup.amfreq;
89         }
90         setup.amfreq_bank[setup.amband] = setup.amfreq;
91         if(updown == 0) { // down
92             if(band <= 1) {
93                 band = AKC6955_BAND_AMEND + 1;
94             }
95             band -= 2;
96         }
97         setup.amband = band;
98         switch(setup.amband) {
99          case AKC6955_BAND_LW:
100          case AKC6955_BAND_MW2:
101            setup.am_mode3k = 0xff;
102            break;
103          case AKC6955_BAND_AMUSER:
104            call_userband(setup.am_userbandnum);
105            break;
106          default:
107            setup.am_mode3k = 0x00;
108            break;
109         }
110         setfreq_updown_amsub();
111     } else { // FM
112         if(setup.fmband == AKC6955_BAND_FMUSER){
113             userband.fm_usrbands[setup.fm_userbandnum].freq = setup.fmfreq;
114         }
115         band = setup.fmband + 1;
116         setup.fmfreq_bank[setup.fmband] = setup.fmfreq;
117         if(updown == 0) { // down
118             if(band <= 1) {
119                 band = AKC6955_BAND_FMEND + 1;
120             }
121             band -= 2;
122         }
123         setup.fmband = band;
124         if(setup.fmband == AKC6955_BAND_FMUSER){
125    //         setup.fmfreq = fm_usrbands[fm_userbandnum].freq;
126             call_userband(setup.fm_userbandnum);
127         }
128         setfreq_updown_fmsub();
129     }
130 }
131
132 unsigned char setfreq_renc_updown(unsigned char dispf)
133 {
134     unsigned int freq;
135
136     if(setup.fm != 0) {
137         switch(setup.fm_tunepitch & 3) {
138             case 0:
139                 freq = 1; // 25KHz
140                 break;
141             case 1:
142                 freq = 2; // 50KHz
143                 break;
144             case 2:
145                 freq = 4; // 100KHz
146                 break;
147             default:
148                 freq = 8; // 200KHz excepts TV1/TV2.
149                 if((setup.fmband == AKC6955_BAND_TV1) || (setup.fmband == AKC6955_BAND_TV2)) {
150                     freq = 4; // 100KHz
151                 }
152                 break;
153             }
154         freq = freq * renc_count;
155     } else {
156         if(setup.amband == AKC6955_BAND_MW2) {
157             freq = renc_count * 3;
158         } else {
159             freq = renc_count;
160         }
161     }
162
163     if(freq != 0) {  // WORKAROUND: IF STEP=0 then go maximum X-)
164         if(renc_dir == RENC_LEFT) {
165             akc6955_down_freq(freq);
166         } else if(renc_dir == RENC_RIGHT) {
167             akc6955_up_freq(freq);
168         }
169         if(dispf == 0) {
170              acm1602_resume(LCD_I2CADDR);
171              dispf = 0xff;
172         }
173        backlight_reset(charcode_f);
174        update_status();
175        update_display();
176        rencoder_start();
177     }
178     return dispf;
179 }
180
181 void setfreq_updown(unsigned char ctlword)
182 {
183     unsigned int freq;
184
185     backlight_reset(ctlword);
186
187     update_status();
188     switch(ctlword){
189         case charcode_8: // Up band
190             setband_updown(0xff);
191             break;
192         case charcode_2: // Down band
193             setband_updown(0x00);
194             break;
195         case charcode_6: // Up Middle;
196         case charcode_4: // Down Middle;
197             freq = 9; // 27KHz@3K/9KStep.
198             if(setup.fm != 0) {
199                switch(setup.fm_tunepitch & 3) {
200                 case 3:
201                   freq = 20; // 500KHz excepts TV1/TV2.
202                   if((setup.fmband == AKC6955_BAND_TV1) || (setup.fmband == AKC6955_BAND_TV2)) {
203                        freq = 10; // 250KHz
204                   }
205                   break;
206                 default:
207                   freq = 10; // 250KHz
208                   break;
209                }
210             } else if(setup.am_mode3k == 0){
211                 freq = 6; // 30KHz
212             }
213             if(ctlword == charcode_6) {
214                akc6955_up_freq(freq); // 225 or 27 or 45KHz
215             } else {
216                akc6955_down_freq(freq); // 225 or 27 or 45KHz
217             }
218             break;
219         case charcode_9: // Up Fast;
220         case charcode_7: // Down Fast;
221             freq = 33; // 99KHz@3K/9KStep.
222             if(setup.fm != 0){
223                switch(setup.fm_tunepitch & 3) {
224                 case 3:
225                   freq = 80; // 2MHz excepts TV1/TV2.
226                   if((setup.fmband == AKC6955_BAND_TV1) || (setup.fmband == AKC6955_BAND_TV2)) {
227                        freq = 40; // 1MHz
228                   }
229                   break;
230                 default:
231                   freq = 40; // 1MHz
232                   break;
233                }
234             } else if(setup.am_mode3k == 0){
235                 freq = 20; // 100KHz
236             }
237             if(ctlword == charcode_9) {
238                akc6955_up_freq(freq); // 825 or 90 or 165KHz
239             } else {
240                akc6955_down_freq(freq); // 825 or 90 or 165KHz
241             }
242             break;
243         case charcode_3: // Up Slow;
244         case charcode_1: // Down Slow;
245             if(setup.fm != 0){
246                switch(setup.fm_tunepitch & 3) {
247                 case 0:
248                   freq = 1; // 25KHz
249                   break;
250                 case 1:
251                   freq = 2; // 50KHz
252                   break;
253                 case 2:
254                   freq = 4; // 100KHz
255                   break;
256                 default:
257                   freq = 8; // 200KHz excepts TV1/TV2.
258                   if((setup.fmband == AKC6955_BAND_TV1) || (setup.fmband == AKC6955_BAND_TV2)) {
259                        freq = 4; // 100KHz
260                   }
261                   break;
262                }
263             } else {
264                 freq = 1; // 5KHz or 3KHz
265                 if(setup.amband == AKC6955_BAND_MW2) {
266                     freq = 3; // 9KHz
267                 } else if((setup.amband == AKC6955_BAND_MW3) || (setup.amband == AKC6955_BAND_MW4)) {
268                     freq = 2; // 10KHz
269                 }
270             }
271             if(ctlword == charcode_1) {
272                akc6955_down_freq(freq);
273             } else {
274                akc6955_up_freq(freq);
275             }
276             break;
277         case charcode_0: // Step
278             setup.threshold_width++;
279             akc6955_set_thresh_width(setup.threshold_width);
280             akc6955_do_tune();
281             break;
282         case charcode_a: // Toggle FM
283             toggle_amfm();
284             break;
285         case charcode_b:
286 //            set_volume();
287              on_updown_userfreq();
288             break;
289         case charcode_c:
290             // Scan
291             scan_start();
292             break;
293         case charcode_d:
294             // FM Narrow/Wide
295             set_stereo();
296             break;
297         case charcode_5:
298             main_menu();
299             break;
300         case charcode_f:
301             updown_help();
302             _CLS();
303 //            _LOCATE(0,0);
304             break;
305         default:
306             break;
307     }
308 }