OSDN Git Service

[SCHEMATIC] Modify SW/MW/LW Preamp, insert galbanic-isolator replace of common-mode...
[openi2cradio/OpenI2CRadio.git] / menu_memoryfreq.c
1 /*
2  * OpenI2CRADIO
3  * Menu sub-routines: Memory Freq.
4  * Copyright (C) 2013-08-14 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 "power.h"
31 #include "commondef.h"
32 #include "eeprom.h"
33 #include "backlight.h"
34 #include "rencoder.h"
35
36 void call_freqbank(unsigned int num)
37 {
38     unsigned char _fm;
39     unsigned char _band;
40     unsigned char _userband;
41     unsigned int bank = num / USER_MEMORY_NUM;
42     unsigned int n = num % USER_MEMORY_NUM;
43
44     if(num >= (USER_MEMORY_NUM * USER_MEMORY_BANKS)) return;
45
46     if(bank != setup.pagenum){ // Swap memory
47 //        save_frequencies();
48         load_frequencies(bank, 0x00);
49     }
50
51     _fm = freqset.memoryfreqs[n].fm;
52     _userband = freqset.memoryfreqs[n].band;
53     _band = _userband & 0x1f;
54     _userband = _userband >> 5;
55     if(_fm) { // AM
56         setup.fmband = _band;
57         setup.fm_userbandnum = _userband;
58     } else {
59         setup.amband = _band;
60         setup.am_userbandnum = _userband;
61     }
62     setup.memorynum = num;
63     akc6955_chg_fm(_fm, freqset.memoryfreqs[n].freq);
64 }
65
66
67 void set_freqbank(unsigned int num)
68 {
69     unsigned char _fm;
70     unsigned char _band;
71     unsigned char _userband;
72     unsigned int _freq;
73     unsigned int bank = num / USER_MEMORY_NUM;
74     unsigned int n = num % USER_MEMORY_NUM;
75
76     if(num >= (USER_MEMORY_NUM * USER_MEMORY_BANKS)) return;
77
78      if(bank != setup.pagenum){ // Save now bank, and load using bank
79         save_frequencies();
80         load_frequencies(bank, 0x00);
81     }
82
83     _fm = setup.fm;
84     _freq = setup.amfreq;
85     _userband = setup.am_userbandnum;
86     _band = setup.amband;
87     if(_fm){
88         _userband = setup.fm_userbandnum;
89         _band = setup.fmband;
90         _freq = setup.fmfreq;
91     }
92     freqset.memoryfreqs[n].fm = _fm;
93     freqset.memoryfreqs[n].band = (_userband << 5) | (_band & 0x1f);
94     freqset.memoryfreqs[n].freq = _freq;
95
96     save_frequencies();
97 }
98
99
100 void on_call_userfreq(void)
101 {
102     unsigned long val = USER_MEMORY_NUM * USER_MEMORY_BANKS;
103     _CLS();
104 //    _LOCATE(0,0);
105     printstr("Mem: 000-");
106     print_numeric_nosupress(val - 1, 3);
107     val = read_numeric(val, 3, 4, 1);
108     if(val >= 0x80000000) return;
109     call_freqbank(val);
110 }
111
112 void on_set_userfreq(void)
113 {
114     unsigned long val = USER_MEMORY_NUM * USER_MEMORY_BANKS;
115     unsigned char n;
116     unsigned char c;
117     _CLS();
118 //    _LOCATE(0,0);
119     printstr("Set Mem: 000-");
120     print_numeric_nosupress(val - 1, 3);
121     val = read_numeric(val, 3, 4, 1);
122     if(val >= 0x80000000) return;
123     _HOME();
124 //    _LOCATE(0,0);
125     printstr("OK? A=OK        ");
126     c = pollkey_single();
127     if(c != charcode_a) return;
128     set_freqbank(val);
129 //    save_eeprom();
130 }
131
132 static void disp_updown_lcd(void)
133 {
134       //         _LOCATE(0,0);
135        printstr("CH:");
136        print_numeric_nosupress(setup.memorynum, 3);
137        printstr(" HELP=F");
138        update_status();
139        print_freq(1);
140        _HOME();
141
142 }
143
144 static unsigned char renc_updown_userfreq(unsigned char dispf)
145 {
146     unsigned int count;
147     count = renc_count;
148
149     if((renc_dir == RENC_NONE) || (count == 0)) return dispf;
150     if(renc_dir == RENC_RIGHT) {
151         setup.memorynum += count;
152     } else if(renc_dir == RENC_LEFT) {
153         setup.memorynum = (setup.memorynum +
154                 USER_MEMORY_NUM * USER_MEMORY_BANKS) - count;
155     } else {
156         return dispf;
157     }
158     setup.memorynum = setup.memorynum  % (USER_MEMORY_NUM * USER_MEMORY_BANKS);
159     rencoder_start();
160     call_freqbank(setup.memorynum);
161     backlight_reset(charcode_f);
162     dispf = backlight_dec(0x00);
163     disp_updown_lcd();
164     return dispf;
165 }
166
167 void on_updown_userfreq(void)
168 {
169     unsigned int _freq;
170     unsigned char _fm;
171     unsigned char _band;
172     unsigned char _userband;
173     unsigned char c;
174     unsigned char dispf = 0xff;
175     unsigned int _ui_idle = setup.ui_idlecount / 23;
176     unsigned char _ui_pad = setup.ui_idlecount % 23;
177     unsigned char changef = 0x00;
178     
179     if(_ui_idle == 0) _ui_idle = 1;
180     _fm = setup.fm;
181     _freq = setup.amfreq;
182     _userband = setup.am_userbandnum;
183     _band = setup.amband;
184     if(_fm){
185         _userband = setup.fm_userbandnum;
186         _band = setup.fmband;
187         _freq = setup.fmfreq;
188     }
189
190     _CLS();
191     do {
192        dispf = renc_updown_userfreq(dispf);
193        if(dispf != 0)  {
194            disp_updown_lcd();
195        }
196        c = pollkey_single_timeout(_ui_idle, 1); // 23*41 = 943ms
197        backlight_reset(c);
198        dispf = backlight_dec(dispf);
199
200
201        switch(c) {
202            case charcode_7: // UP 1CH
203                 setup.memorynum++;
204                 changef = 0xff;
205                 break;
206            case charcode_1: // Down 1CH
207                 changef = 0xff;
208                setup.memorynum = setup.memorynum + (USER_MEMORY_NUM * USER_MEMORY_BANKS) - 1;
209                break;
210            case charcode_9: // Up CH FAST
211                 changef = 0xff;
212                setup.memorynum += 100;
213                break;
214            case charcode_3: // Down CH FAST
215                changef = 0xff;
216                setup.memorynum = setup.memorynum + (USER_MEMORY_NUM * USER_MEMORY_BANKS) - 100;
217                break;
218            case charcode_8: // Up CH MID
219                changef = 0xff;
220                setup.memorynum += USER_MEMORY_NUM;
221                break;
222            case charcode_2: // Down CH MID
223                changef = 0xff;
224                setup.memorynum = setup.memorynum + (USER_MEMORY_NUM * USER_MEMORY_BANKS) - USER_MEMORY_NUM;
225                break;
226             case charcode_5: // Recall
227                changef = 0xff;
228                break;
229             case charcode_d: // Reset
230                if(_fm) { // AM
231                     setup.fmband = _band;
232                     setup.fm_userbandnum = _userband;
233                     setup.fmfreq = _freq;
234                } else {
235                     setup.amband = _band;
236                     setup.am_userbandnum = _userband;
237                     setup.amfreq = _freq;
238                }
239                akc6955_chg_fm(_fm, _freq);
240                break;
241            case charcode_4:
242                on_call_userfreq();
243                break;
244            case charcode_6:
245                on_set_userfreq();
246                break;
247            case charcode_a:
248                 goto _l0;
249                 break;
250            case charcode_f:
251                userfreq_help();
252                _CLS();
253                break;
254            case charcode_e: // Set Light
255                if(dispf == 0x00){
256                    dispf = 0xff;
257                } else {
258                    dispf = 0x00;
259                }
260                break;
261            default:
262                break;
263            }
264
265           setup.memorynum = setup.memorynum % (USER_MEMORY_NUM * USER_MEMORY_BANKS);
266            if(changef != 0) {
267                 call_freqbank(setup.memorynum);
268            }
269            if(_ui_pad != 0) idle_time_ms(_ui_pad); // Pad 9ms, 1Loop = 1000ms.
270            changef = 0;
271     } while(1);
272 _l0:
273     _CLS();
274
275 }