forked from ttrftech/CentSDR
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ui.c
379 lines (341 loc) · 9.53 KB
/
ui.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* Copyright (c) 2014-2015, TAKAHASHI Tomohiro (TTRFTECH) edy555@gmail.com
* All rights reserved.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* The software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "ch.h"
#include "hal.h"
#include "nanosdr.h"
#include "si5351.h"
#include <stdlib.h>
#include <string.h>
#define set_volume(gain) tlv320aic3204_set_volume(gain)
int fetch_encoder_tick(void);
#define NO_EVENT 0
#define EVT_BUTTON_SINGLE_CLICK 0x01
#define EVT_BUTTON_DOUBLE_CLICK 0x02
#define EVT_BUTTON_DOWN_LONG 0x04
#define EVT_UP 0x10
#define EVT_DOWN 0x20
#define EVT_PUSH_UP 0x30
#define EVT_PUSH_DOWN 0x40
#define BUTTON_DOWN_LONG_TICKS 16000
#define BUTTON_DOUBLE_TICKS 500
#define BUTTON_DEBOUNCE_TICKS 10
#define BUTTON_NO_ACTION 0
#define BIT_PUSH 0
#define BIT_ENCODER0 1
#define BIT_ENCODER1 2
static uint16_t last_button = 0;
static uint16_t button_event_inhibited = 0;
static uint32_t last_button_down_ticks;
//static uint8_t dragged = 0; // encoder changed while button pressed
int
read_buttons(void)
{
return (palReadPort(GPIOA) & 0x1) ^ config.button_polarity;
}
void
inhibit_button_event(void)
{
button_event_inhibited = 1;
}
int btn_check(void)
{
int cur_button = read_buttons();
int changed = last_button ^ cur_button;
int status = 0;
uint32_t ticks = chVTGetSystemTime();
if (changed & (1<<BIT_PUSH)) {
if (ticks >= last_button_down_ticks + BUTTON_DEBOUNCE_TICKS) {
if (!(cur_button & (1<<BIT_PUSH))) {
// button released
if (button_event_inhibited) {
button_event_inhibited = 0;
} else {
status |= EVT_BUTTON_SINGLE_CLICK;
}
} else {
// button pushed
if (ticks < last_button_down_ticks + BUTTON_DOUBLE_TICKS) {
status |= EVT_BUTTON_DOUBLE_CLICK;
} else {
last_button_down_ticks = ticks;
}
}
}
} else {
// button unchanged
if ((cur_button & (1<<BIT_PUSH))
&& !button_event_inhibited
&& ticks >= last_button_down_ticks + BUTTON_DOWN_LONG_TICKS) {
status |= EVT_BUTTON_DOWN_LONG;
button_event_inhibited = 1;
}
}
last_button = cur_button;
return status;
}
#define VOLUME_MAX 29
#define VOLUME_MIN -7
#define RFGAIN_MAX 95
static void
set_gain(int gain)
{
int dgain = 0;
if (gain > RFGAIN_MAX) {
dgain = gain - RFGAIN_MAX;
gain = RFGAIN_MAX;
}
if (gain < 0) {
dgain = gain;
gain = 0;
}
tlv320aic3204_set_gain(gain, gain);
tlv320aic3204_set_digital_gain(dgain, dgain);
}
void
update_frequency(void)
{
set_tune(uistat.freq);
}
int enc_status = 0;
int enc_count = 0;
int
fetch_encoder_tick(void)
{
int n = enc_count;
enc_count = 0;
return n;
}
void ext_callback(EXTDriver *extp, expchannel_t channel)
{
(void)extp;
int cur = palReadPort(GPIOB);
const int trans_tbl[4][4] = {
/*falling A */ /*rising A */ /*falling B */ /*rising B */
{ 0, 0, 3, 3 }, { 1, 1, 2, 2 }, { 0, 1, 1, 0 }, { 3, 2, 2, 3 }
};
int s = (channel - 1) * 2; // A: 0, B: 2
if (cur & (1 << channel))
s |= 1; // rising
if (enc_status == 0 && s == 3) // rising B
enc_count -= config.rotary_encoder_direction;
if (enc_status == 3 && s == 2) // falling B
enc_count += config.rotary_encoder_direction;
enc_status = trans_tbl[s][enc_status];
#if 0
if (channel == 0) {
enc_count = 0;
}
#endif
}
static const EXTConfig extconf = {
{
{ 0, NULL }, //{ EXT_MODE_GPIOA | EXT_CH_MODE_RISING_EDGE | EXT_CH_MODE_AUTOSTART, ext_callback },
{ EXT_MODE_GPIOB | EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART, ext_callback },
{ EXT_MODE_GPIOB | EXT_CH_MODE_BOTH_EDGES | EXT_CH_MODE_AUTOSTART, ext_callback },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL },
{ 0, NULL }
}
};
void
recall_channel(unsigned int channel)
{
// apply settings at channel
uistat.freq = config.channels[channel].freq;
//uistat.rfgain = config.channels[channel].rfgain;
uistat.modulation = config.channels[channel].modulation;
//set_gain(uistat.rfgain);
set_modulation(uistat.modulation);
update_frequency();
}
static unsigned int next_valid_channel(unsigned int channel, int offs)
{
#define SKIP_EMPTY_CHANNELS 0
int ch = (int)channel;
int dir = (offs < 0) ? -1 : 1;
offs = abs(offs);
while (offs > 0) {
do {
ch += dir;
if (ch < 0)
ch = CHANNEL_MAX - 1;
if (ch >= CHANNEL_MAX)
ch = 0;
#if SKIP_EMPTY_CHANNELS
} while (config.channels[ch].freq == 0);
#else
} while (false);
#endif
offs--;
}
return (unsigned int)ch;
}
void
ui_init(void)
{
#if 1
extStart(&EXTD1, &extconf);
//chCondObjectInit(&condvar_button);
#endif
set_volume(uistat.volume);
set_gain(uistat.rfgain);
set_agc_mode(uistat.agcmode);
set_modulation(uistat.modulation);
//recall_channel(uistat.channel);
update_frequency();
}
static int minmax(int x, int min, int max)
{
if (x >= max)
return max - 1;
if (x < min)
return min;
return x;
}
void
ui_process(void)
{
int status = btn_check();
int tick = fetch_encoder_tick();
int n;
if (status & EVT_BUTTON_SINGLE_CLICK) {
uistat.mode++;
if (uistat.agcmode != 0 && uistat.mode == RFGAIN)
uistat.mode++;
// skip CWTONE, IQBAL on click
if (uistat.mode == AGC_MAXGAIN)
uistat.mode = SPDISP;
uistat.mode %= MODE_MAX;
disp_update();
} else if (status & EVT_BUTTON_DOWN_LONG) {
tlv320aic3204_beep();
save_config_current_channel();
}
if (tick != 0) {
if (read_buttons() != 0) {
// button pressed
if (uistat.mode == FREQ) {
if (tick < 0) {
if (uistat.digit < 7)
uistat.digit++;
else
uistat.mode--;
}
if (tick > 0) {
if (uistat.digit > 0)
uistat.digit--;
else
uistat.mode++;
}
} else {
if (tick < 0) {
uistat.mode--;
if (uistat.mode == IQBAL || uistat.mode == RFGAIN)
disp_clear_aux();
// skip rfgain if agc is enabled
if (uistat.agcmode != 0 && uistat.mode == RFGAIN)
uistat.mode--;
}
if (tick > 0) {
uistat.mode++;
// skip rfgain if agc is enabled
if (uistat.agcmode != 0 && uistat.mode == RFGAIN)
uistat.mode++;
if (uistat.mode == AGC_MAXGAIN || uistat.mode == SPDISP)
disp_clear_aux();
}
uistat.mode = uistat.mode % MODE_MAX;
}
disp_update();
inhibit_button_event();
} else {
if (uistat.mode == CHANNEL) {
uistat.channel = next_valid_channel(uistat.channel, tick);
recall_channel(uistat.channel);
} else if (uistat.mode == VOLUME) {
uistat.volume = minmax(uistat.volume + tick, VOLUME_MIN, VOLUME_MAX+1);
set_volume(uistat.volume);
} else if (uistat.mode == FREQ) {
int32_t step = 1;
for (n = uistat.digit; n > 0; n--)
step *= 10;
int32_t freq = uistat.freq + step * tick;
if (freq > 0)
uistat.freq = freq;
update_frequency();
} else if (uistat.mode == RFGAIN) {
uistat.rfgain = minmax((int)uistat.rfgain + tick, -24, RFGAIN_MAX + 40+1);
set_gain(uistat.rfgain);
} else if (uistat.mode == AGC) {
uistat.agcmode = minmax(uistat.agcmode + tick, 0, AGC_MAX);
set_agc_mode(uistat.agcmode);
} else if (uistat.mode == MOD) {
if (tick > 0 && uistat.modulation < MOD_MAX-1) {
uistat.modulation++;
}
if (tick < 0 && uistat.modulation > 0) {
uistat.modulation--;
}
set_modulation(uistat.modulation);
update_frequency();
} else if (uistat.mode == CWTONE) {
uistat.cw_tone_freq = minmax(uistat.cw_tone_freq + tick, -2000, 2000);
update_cwtone();
} else if (uistat.mode == IQBAL) {
uistat.iqbal = minmax(uistat.iqbal + tick * 10, -4000, 4000);
update_iqbal();
} else if (uistat.mode == AGC_MAXGAIN) {
config.agc.maximum_gain = minmax(config.agc.maximum_gain + tick, 0, 128);
update_agc();
} else if (uistat.mode == SPDISP) {
uistat.spdispmode = minmax(uistat.spdispmode + tick, 0, SPDISP_MODE_MAX);
} else if (uistat.mode == WFDISP) {
uistat.wfdispmode = minmax(uistat.wfdispmode + tick, 0, WFDISP_MODE_MAX);
}
}
disp_update();
}
}