OSDN Git Service

[POWER][v2.0] Fix right shutdown sequence.
[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_mode3k(setup.am_mode3k);
66     akc6955_set_freq(setup.amfreq);
67 }
68
69 static void setfreq_updown_fmsub(void)
70 {
71     if(setup.fmband >= AKC6955_BAND_FMEND) {
72         setup.fmband = 0;
73     }
74     akc6955_set_fmband(setup.fmband);
75     setup.fmfreq = setup.fmfreq_bank[setup.fmband];
76 //    idle_time_62_5ms(); // 62.5ms
77     ClrWdt();
78     akc6955_set_freq(setup.fmfreq);
79 }
80
81 static void setband_updown(unsigned char updown)
82 {
83     unsigned char band;
84     if(setup.fm == 0){  // MW
85         band = setup.amband + 1;
86         if(setup.amband == AKC6955_BAND_AMUSER){
87             userband.am_usrbands[setup.am_userbandnum].freq = setup.amfreq;
88         }
89         setup.amfreq_bank[setup.amband] = setup.amfreq;
90         if(updown == 0) { // down
91             if(band <= 1) {
92                 band = AKC6955_BAND_AMEND + 1;
93             }
94             band -= 2;
95         }
96         setup.amband = band;
97         switch(setup.amband) {
98          case AKC6955_BAND_LW:
99          case AKC6955_BAND_MW2:
100            setup.am_mode3k = 0xff;
101            break;
102          case AKC6955_BAND_AMUSER:
103            call_userband(setup.am_userbandnum);
104            break;
105          default:
106            setup.am_mode3k = 0x00;
107            break;
108         }
109         setfreq_updown_amsub();
110     } else { // FM
111         if(setup.fmband == AKC6955_BAND_FMUSER){
112             userband.fm_usrbands[setup.fm_userbandnum].freq = setup.fmfreq;
113         }
114         band = setup.fmband + 1;
115         setup.fmfreq_bank[setup.fmband] = setup.fmfreq;
116         if(updown == 0) { // down
117             if(band <= 1) {
118                 band = AKC6955_BAND_FMEND + 1;
119             }
120             band -= 2;
121         }
122         setup.fmband = band;
123         if(setup.fmband == AKC6955_BAND_FMUSER){
124    //         setup.fmfreq = fm_usrbands[fm_userbandnum].freq;
125             call_userband(setup.fm_userbandnum);
126         }
127         setfreq_updown_fmsub();
128     }
129 }
130
131 void setfreq_updown(unsigned char ctlword)
132 {
133     unsigned int freq;
134
135     backlight_reset(ctlword);
136
137     update_status();
138     switch(ctlword){
139         case charcode_8: // Up band
140             setband_updown(0xff);
141             break;
142         case charcode_2: // Down band
143             setband_updown(0x00);
144             break;
145         case charcode_6: // Up Middle;
146         case charcode_4: // Down Middle;
147             freq = 9; // 27KHz@3K/9KStep.
148             if(setup.fm != 0) {
149                switch(setup.fm_tunepitch & 3) {
150                 case 3:
151                   freq = 20; // 500KHz excepts TV1/TV2.
152                   if((setup.fmband == AKC6955_BAND_TV1) || (setup.fmband == AKC6955_BAND_TV2)) {
153                        freq = 10; // 250KHz
154                   }
155                   break;
156                 default:
157                   freq = 10; // 250KHz
158                   break;
159                }
160             } else if(setup.am_mode3k == 0){
161                 freq = 6; // 30KHz
162             }
163             if(ctlword == charcode_6) {
164                akc6955_up_freq(freq); // 225 or 27 or 45KHz
165             } else {
166                akc6955_down_freq(freq); // 225 or 27 or 45KHz
167             }
168             break;
169         case charcode_9: // Up Fast;
170         case charcode_7: // Down Fast;
171             freq = 33; // 99KHz@3K/9KStep.
172             if(setup.fm != 0){
173                switch(setup.fm_tunepitch & 3) {
174                 case 3:
175                   freq = 80; // 2MHz excepts TV1/TV2.
176                   if((setup.fmband == AKC6955_BAND_TV1) || (setup.fmband == AKC6955_BAND_TV2)) {
177                        freq = 40; // 1MHz
178                   }
179                   break;
180                 default:
181                   freq = 40; // 1MHz
182                   break;
183                }
184             } else if(setup.am_mode3k == 0){
185                 freq = 20; // 100KHz
186             }
187             if(ctlword == charcode_9) {
188                akc6955_up_freq(freq); // 825 or 90 or 165KHz
189             } else {
190                akc6955_down_freq(freq); // 825 or 90 or 165KHz
191             }
192             break;
193         case charcode_3: // Up Slow;
194         case charcode_1: // Down Slow;
195             if(setup.fm != 0){
196                switch(setup.fm_tunepitch & 3) {
197                 case 0:
198                   freq = 1; // 25KHz
199                   break;
200                 case 1:
201                   freq = 2; // 50KHz
202                   break;
203                 case 2:
204                   freq = 4; // 100KHz
205                   break;
206                 default:
207                   freq = 8; // 200KHz excepts TV1/TV2.
208                   if((setup.fmband == AKC6955_BAND_TV1) || (setup.fmband == AKC6955_BAND_TV2)) {
209                        freq = 4; // 100KHz
210                   }
211                   break;
212                }
213             } else {
214                 freq = 1; // 5KHz or 3KHz
215                 if(setup.amband == AKC6955_BAND_MW2) {
216                     freq = 3; // 9KHz
217                 } else if((setup.amband == AKC6955_BAND_MW3) || (setup.amband == AKC6955_BAND_MW4)) {
218                     freq = 2; // 10KHz
219                 }
220             }
221             if(ctlword == charcode_1) {
222                akc6955_down_freq(freq);
223             } else {
224                akc6955_up_freq(freq);
225             }
226             break;
227         case charcode_0: // Step
228             setup.threshold_width++;
229             akc6955_set_thresh_width(setup.threshold_width);
230             akc6955_do_tune();
231             break;
232         case charcode_a: // Toggle FM
233             toggle_amfm();
234             break;
235         case charcode_b:
236 //            set_volume();
237              on_updown_userfreq();
238             break;
239         case charcode_c:
240             // Scan
241             scan_start();
242             break;
243         case charcode_d:
244             // FM Narrow/Wide
245             set_stereo();
246             break;
247         case charcode_5:
248             main_menu();
249             break;
250         case charcode_f:
251             updown_help();
252             _CLS();
253 //            _LOCATE(0,0);
254             break;
255         default:
256             break;
257     }
258 }