OSDN Git Service

[UI][AKC6955] Fix correct AM<->FM changing.
[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 "power.h"
52 #include "adc_int.h"
53
54 void setfreq_updown(unsigned char ctlword)
55 {
56     unsigned int freq;
57     if((ctlword != charcode_e) && (ctlword >= charcode_1)
58             && (ctlword <= charcode_f)) {
59             backlight_counter = backlight_long;
60             lcd_setbacklight(0xff, backlight_level); // Turn ON
61     } else if(ctlword == charcode_e){
62         if(backlight_counter != 0){
63             backlight_counter = 0;
64         } else {
65             backlight_counter = backlight_long;
66         }
67         return;
68     }
69
70     update_status();
71     switch(ctlword){
72         case charcode_8: // Change band
73             if(fm == 0){
74                 amfreq_bank[amband] = amfreq;
75                 if(amband >= AKC6955_BAND_MW4) {
76                    amband = 0;
77                 } else {
78                     amband++;
79                 }
80                 akc6955_set_amband(amband);
81                 freq = amfreq_bank[amband];
82             } else {
83               fmfreq_bank[fmband] = fmfreq;
84               if(fmband >= AKC6955_BAND_FMUSER) {
85                   fmband = 0;
86               } else {
87                   fmband++;
88               }
89               freq = fmfreq_bank[fmband];
90               akc6955_set_fmband(fmband);
91             }
92             idle_time_62_5ms(); // 62.5ms
93             ClrWdt();
94             akc6955_set_freq(freq);
95             break;
96         case charcode_2: // Change band
97             if(fm == 0){
98                 amfreq_bank[amband] = amfreq;
99                 if(amband <= 0 ) {
100                    amband = AKC6955_BAND_MW4;
101                 } else {
102                     amband--;
103                 }
104                 akc6955_set_amband(amband);
105                 freq = amfreq_bank[amband];
106             } else {
107               fmfreq_bank[fmband] = fmfreq;
108               if(fmband <= 0) {
109                   fmband = AKC6955_BAND_FMUSER;
110               } else {
111                   fmband--;
112               }
113               freq = fmfreq_bank[fmband];
114               akc6955_set_fmband(fmband);
115             }
116             idle_time_62_5ms(); // 62.5ms
117             ClrWdt();
118             akc6955_set_freq(freq);
119             break;
120         case charcode_4: // Down Freq;
121             akc6955_down_freq(30);
122             break;
123         case charcode_6: // Down Freq;
124             akc6955_up_freq(30);
125             break;
126         case charcode_9: // Down Fast;
127             akc6955_up_freq(100);
128             break;
129         case charcode_7: // Down Fast;
130             akc6955_down_freq(100);
131             break;
132         case charcode_1: // Down Slow;
133             if(fm != 0){
134                 freq = 3; // DOWN 30KHz
135             } else {
136                 if(amband == AKC6955_BAND_MW2) {
137                     freq = 9;
138                 } else if(amband == AKC6955_BAND_MW3) {
139                     freq = 10;
140                 } else if(am_mode3k == 0x00) {
141                     freq = 3; // DOWN 50KHz
142                 } else {
143                     freq = 5; // DOWN 50KHz
144                 }
145             }
146             akc6955_down_freq(3);
147             break;
148         case charcode_3: // Down Slow;
149             if(fm != 0){
150                 freq = 3; // DOWN 30KHz
151             } else {
152                 if(amband == AKC6955_BAND_MW2) {
153                     freq = 9;
154                 } else if(amband == AKC6955_BAND_MW3) {
155                     freq = 10;
156                 } else if(am_mode3k == 0x00) {
157                     freq = 3; // DOWN 50KHz
158                 } else {
159                     freq = 5; // DOWN 50KHz
160                 }
161             }
162             akc6955_up_freq(3);
163             break;
164         case charcode_0: // Step
165             if(fm == 0){
166                 if(am_mode3k == 0) {
167                     am_mode3k = 0xff;
168                 } else {
169                     am_mode3k = 0;
170                 }
171                 akc6955_mode3k(am_mode3k);
172             }
173             break;
174         case charcode_a: // Toggle FM
175             toggle_amfm();
176             break;
177         case charcode_b:
178             set_volume();
179             break;
180         case charcode_c:
181             // Scan
182             scan_start();
183             break;
184         case charcode_d:
185             // FM Narrow/Wide
186             set_stereo();
187             break;
188         case charcode_5:
189             main_menu();
190             break;
191         case charcode_f:
192             updown_help();
193             _CLS();
194             _LOCATE(0,0);
195             break;
196         default:
197             break;
198     }
199     if(fm == 0){
200         amband = akc6955_get_amband();
201         amfreq = akc6955_get_freq();
202         amfreq_bank[amband] = amfreq;
203     } else {
204         fmband = akc6955_get_fmband();
205         fmfreq = akc6955_get_freq();
206         fmfreq_bank[fmband] = fmfreq;
207     }
208 }