-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG0ORX_FrontPanel_2.cpp
More file actions
515 lines (465 loc) · 13.2 KB
/
G0ORX_FrontPanel_2.cpp
File metadata and controls
515 lines (465 loc) · 13.2 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
* G0ORX New Front Panel.
*
* i2c interface to Raspberry Pi Pico acting as a i2c slave device.
*
* Supports 4 Encoders, 4 Encoder Switches, Touch Screen
*
*/
#ifndef BEENHERE
#include "SDT.h"
#endif
#ifdef G0ORX_FRONTPANEL_2
#include <Wire.h>
#define I2C_ADDRESS 0x20
bool found_pico = false;
bool configured_pico = false;
#define CONFIG_REG 0x00
#define RESET_REG 0x01
#define READ_INTERRUPT_MASK_REG 0x02
#define READ_ENCODER_REG 0x03
#define READ_SWITCH_REG 0x04
#define READ_TOUCH_REG 0x05
#define SET_LED_REG 0x06
#define EN0_INT 0x01
#define EN1_INT 0x02
#define EN2_INT 0x04
#define EN3_INT 0x08
#define SW0_INT 0x10
#define SW1_INT 0x20
#define SW2_INT 0x40
#define SW3_INT 0x80
#define TS_INT 0x100
#define LED0 0x01
#define LED1 0x02
#define LED_ON 0x04
#define LED_OFF 0x00
#define PICO_INTERRUPT_PIN 15
volatile uint16_t config=0x0F0; // Active Low Interrupt, Invert SW, Invert SW, Invert SW, Invert SW, FWD, FWD, FWD, FWD
volatile uint16_t interrupt_mask;
#define TOUCH_PRESSED 0x02
#define TOUCH_RELEASED 0x01
volatile uint8_t touch_status;
volatile int16_t touch_X;
volatile int16_t touch_Y;
volatile bool pressed = false;
volatile bool has_moved = false;
volatile long start_freq;
volatile long new_freq;
static long zoomBandwidth[5]= {192000L,96000L,48000L,24000L,12000L};
volatile int16_t last_X;
volatile int16_t encoder_value[4] = {0,0,0,0};
int G0ORXButtonPressed = -1;
//bool keypadHit=false;
static const uint8_t my_keymap[22] = { // note 18 and 19 are not connected
0,
0, 4, 8, 12, 16,
1, 5, 9, 13, 17,
2, 6, 10, 14, 18,
3, 7, 11, 15, 19,
255
};
static void KeypadInit() {
tft.KeypadInit(true, false, 0, 0, 0, false, false);
tft.SetKeyMap(my_keymap);
}
// initial processing only to handle frequency change
static void update_frequency(long new_freq) {
Debug(String(__FUNCTION__)+": "+String(new_freq));
centerTuneFlag = 1;
if (T41State == CW_XMIT && decoderFlag == DECODE_ON) {
ResetHistograms();
}
centerFreq = new_freq;
TxRxFreq = centerFreq + NCOFreq;
centerTuneFlag = 1;
}
FASTRUN
static void process_touch() {
Debug(String(__FUNCTION__));
if(menuStatus==NO_MENUS_ACTIVE) {
long hzPerPixel = zoomBandwidth[spectrum_zoom]/SPECTRUM_RES;;
if(touch_status==TOUCH_PRESSED) {
if(!pressed) {
if(touch_X<SPECTRUM_RES) {
pressed = true;
has_moved = false;
last_X = touch_X;
}
} else {
long moved = (long)last_X - (long)touch_X;
if(has_moved || moved>=10 || moved <=-10) {
has_moved = true;
last_X = touch_X;
if (activeVFO == VFO_A) {
start_freq = currentFreqA;
} else {
start_freq = currentFreqB;
}
new_freq = start_freq + (moved * hzPerPixel);
update_frequency(new_freq);
}
}
} else if(touch_status==TOUCH_RELEASED) {
if(pressed) {
if(has_moved) {
if (activeVFO == VFO_A) {
start_freq = currentFreqA;
} else {
start_freq = currentFreqB;
}
new_freq = (start_freq/freqIncrement)*freqIncrement;
update_frequency(new_freq);
} else {
int x=last_X-SPECTRUM_LEFT_X;
if (activeVFO == VFO_A) {
start_freq = currentFreqA - (zoomBandwidth[spectrum_zoom]/2);
} else {
start_freq = currentFreqB - (zoomBandwidth[spectrum_zoom]/2);
}
new_freq = start_freq + (x * hzPerPixel);
new_freq = (new_freq/freqIncrement)*freqIncrement;
update_frequency(new_freq);
}
pressed = false;
has_moved = false;
}
}
} else {
if(touch_status==TOUCH_RELEASED) {
// ignore touch pressed - activate when released
int i=(touch_Y-115)/25;
if(touch_X<250) {
//touchSelectPrimaryMenuIndex(i);
touchPrimaryMenuIndex=i;
} else if(touch_X>300) {
touchSecondaryMenuIndex=i;
}
}
}
}
FASTRUN
void KeypadInterrupt() {
//uint8_t status;
tft.getINTC2();
G0ORXButtonPressed = tft.KeypadGetKey();
}
FASTRUN
void getKeypad() {
if(G0ORXButtonPressed==-1) {
int val=tft.KeypadGetKey();
if(val!=0xFF) {
G0ORXButtonPressed=val;
} else {
G0ORXButtonPressed=-1;
}
}
}
FASTRUN
uint8_t get_byte() {
uint8_t byte=0;
while(!Wire1.available()) {
delay(10);
}
byte = Wire1.read();
return byte;
}
FASTRUN
uint16_t get_interrupt_mask() {
uint16_t mask;
uint8_t res;
Debug(String(__FUNCTION__));
mask=0;
Wire1.beginTransmission(I2C_ADDRESS);
Wire1.write(READ_INTERRUPT_MASK_REG);
res=Wire1.endTransmission(true);
if(res==0) {
Wire1.requestFrom(I2C_ADDRESS, 2);
mask=get_byte()&0xFF;
mask|=(get_byte()&0xFF)<<8;
} else {
Debug(String(__FUNCTION__)+": endTransmission: "+String(res,HEX));
}
Debug(String(__FUNCTION__)+": 0x"+String(mask,HEX));
return mask;
}
FASTRUN
int16_t get_encoder_value(uint8_t encoder) {
int16_t result=0;
uint8_t res;
Debug(String(__FUNCTION__)+": encoder="+String(encoder));
Wire1.beginTransmission(I2C_ADDRESS);
Wire1.write(READ_ENCODER_REG);
Wire1.write(encoder);
res=Wire1.endTransmission(true);
if(res==0) {
Wire1.requestFrom(I2C_ADDRESS, 2); // request 2 bytes from Pico}
result=get_byte()&0xFF;
result|=(get_byte()&0xFF)<<8;
} else {
Debug(String(__FUNCTION__)+": endTransmission: "+String(res,HEX));
}
Debug(String(__FUNCTION__)+": "+String(result));
return result;
}
FASTRUN
uint8_t get_switch_state(uint8_t sw) {
uint8_t state=0;
uint8_t res;
Debug(String(__FUNCTION__)+": sw="+String(sw));
Wire1.beginTransmission(I2C_ADDRESS);
Wire1.write(READ_SWITCH_REG);
Wire1.write(sw);
res=Wire1.endTransmission(true);
if(res==0) {
Wire1.requestFrom(I2C_ADDRESS, 1); // request 1 bytes from Pico
state = get_byte();
} else {
Debug(String(__FUNCTION__)+": endTransmission: "+String(res,HEX));
}
return state;
}
FASTRUN
void get_touch() {
uint8_t res;
Wire1.beginTransmission(I2C_ADDRESS);
Wire1.write(READ_TOUCH_REG);
res=Wire1.endTransmission(true);
if(res==0) {
Wire1.requestFrom(I2C_ADDRESS, 5);
touch_status = get_byte();
touch_X=get_byte()&0xFF;
touch_X|=(get_byte()&0xFF)<<8;
touch_Y=get_byte()&0xFF;
touch_Y|=(get_byte()&0xFF)<<8;
}
Debug(String(__FUNCTION__)+": 0x"+String(touch_status,HEX)+" X="+String(touch_X)+" Y="+String(touch_Y));
}
FASTRUN
void FrontPanelSetLed(int led,bool state) {
uint8_t res;
Debug(String(__FUNCTION__)+": led+"+String(led)+" state="+String(state));
Wire1.beginTransmission(I2C_ADDRESS);
Wire1.write(SET_LED_REG);
Wire1.write((uint8_t)led);
Wire1.write((uint8_t)state);
res=Wire1.endTransmission(true);
if(res!=0) {
Debug(String(__FUNCTION__)+": endTransmission: "+String(res,HEX));
}
}
FASTRUN
static void send_config() {
uint8_t res;
Debug("Configure Pico config=0x"+String(config,HEX));
configured_pico=false;
while(!configured_pico) {
Wire1.beginTransmission(I2C_ADDRESS);
Wire1.write(CONFIG_REG);
Wire1.write(config&0xFF);
Wire1.write((config>>8)&0xFF);
res=Wire1.endTransmission(true);
if(res!=0) {
Debug(String(__FUNCTION__)+": endTransmission: "+String(res,HEX));
} else {
configured_pico = true;
}
delay(100);
}
}
FASTRUN
void front_panel_interrupt() {
int16_t encoder_val;
long tuneChange = 0L;
uint8_t switch_state;
Debug(String(__FUNCTION__));
__disable_irq();
uint16_t interrupt_mask=get_interrupt_mask();
Debug(String(__FUNCTION__)+": mask=0x"+String(interrupt_mask,HEX));
switch(interrupt_mask) {
case 0: // nothing
break;
case 0x8000: // ready
//send_config();
break;
case 0x01: // Volume
encoder_value[0]=get_encoder_value(0);
switch(volumeFunction) {
case AUDIO_VOLUME:
audioVolume += encoder_value[0];
if (audioVolume > 100) { // In range?
audioVolume = 100;
} else {
if (audioVolume < 0) {
audioVolume = 0;
}
}
break;
case AGC_GAIN:
bands[currentBand].AGC_thresh += encoder_value[0];
if(bands[currentBand].AGC_thresh < -20) {
bands[currentBand].AGC_thresh = -20;
} else if(bands[currentBand].AGC_thresh > 120) {
bands[currentBand].AGC_thresh = 120;
}
AGCLoadValues();
break;
case MIC_GAIN:
currentMicGain += encoder_value[0];
if(currentMicGain < -40) {
currentMicGain = -40;
} else if(currentMicGain > 30) {
currentMicGain = 30;
}
if(radioState == SSB_TRANSMIT_STATE ) {
comp1.setPreGain_dB(currentMicGain);
comp2.setPreGain_dB(currentMicGain);
}
break;
case SIDETONE_VOLUME:
sidetoneVolume += encoder_value[0];
if(sidetoneVolume < 0.0 ) {
sidetoneVolume = 0.0;
} else if(sidetoneVolume > 100.0) {
sidetoneVolume = 100.0;
}
if(radioState == CW_TRANSMIT_STRAIGHT_STATE || radioState == CW_TRANSMIT_KEYER_STATE) {
modeSelectOutL.gain(1, sidetoneVolume/100.0);
modeSelectOutR.gain(1, sidetoneVolume/100.0);
}
break;
case NOISE_FLOOR_LEVEL:
currentNoiseFloor[currentBand] += encoder_value[0];
if(currentNoiseFloor[currentBand]<0) {
currentNoiseFloor[currentBand]=0;
} else if(currentNoiseFloor[currentBand]>100) {
currentNoiseFloor[currentBand]=100;
}
break;
case SQUELCH_LEVEL:
squelch += adjustVolEncoder;
if(squelch<0) {
squelch=0;
} else if(squelch>99) {
squelch=99;
}
break;
}
volumeChangeFlag = true;
break;
case 0x02: // Filter/Menu
encoder_value[1]=get_encoder_value(1);
if (calibrateFlag == 0) {
filter_pos = last_filter_pos - 5 * encoder_value[1];
} else {
filterEncoderMove=encoder_value[1];
}
break;
case 0x04: // Center Freq
encoder_val=get_encoder_value(2);
if (encoder_val == 0) // Nothing read
break;
//centerTuneFlag = 1; //AFP 10-03-22
if (T41State == CW_XMIT && decoderFlag == DECODE_ON) { // No reason to reset if we're not doing decoded CW AFP 09-27-22
ResetHistograms();
}
tuneChange=(long)encoder_val;
centerFreq += ((long)freqIncrement * tuneChange);
TxRxFreq = centerFreq + NCOFreq;
centerTuneFlag = 1;
break;
case 0x08: // Fine Tune
encoder_value[3]=get_encoder_value(3);
fineTuneEncoderMove=encoder_value[3];
NCOFreq += stepFineTune * fineTuneEncoderMove; //AFP 11-01-22
centerTuneFlag = 1;
// ============ AFP 10-28-22
if (activeVFO == VFO_A) {
currentFreqA = centerFreq + NCOFreq; //AFP 10-05-22
} else {
currentFreqB = centerFreq + NCOFreq; //AFP 10-05-22
}
// =============== Recentering at band edges ==========
if (spectrum_zoom != 0) {
if (NCOFreq > (95000 / (1 << spectrum_zoom)) || NCOFreq < (-93000 / (1 << spectrum_zoom))) {
NCOFreq = 0L;
centerFreq = TxRxFreq = currentFreqA;
}
} else {
if (NCOFreq > (142000) || NCOFreq < (-43000)) { // Offset tuning window in zoom 1x
NCOFreq = 0L;
centerFreq = TxRxFreq = currentFreqA; //AFP 10-28-22
}
centerTuneFlag = 1;
}
fineTuneEncoderMove = 0L;
TxRxFreq = centerFreq + NCOFreq; // KF5N
break;
case 0x10:
switch_state=get_switch_state(0);
if(switch_state==1) {
G0ORXButtonPressed = NUMBER_OF_SWITCHES+0;
}
break;
case 0x20:
switch_state=get_switch_state(1);
if(switch_state==1) {
G0ORXButtonPressed = NUMBER_OF_SWITCHES+1;
}
break;
case 0x40:
switch_state=get_switch_state(2);
if(switch_state==1) {
G0ORXButtonPressed = NUMBER_OF_SWITCHES+2;
}
break;
case 0x80:
switch_state=get_switch_state(3);
if(switch_state==1) {
G0ORXButtonPressed = NUMBER_OF_SWITCHES+3;
}
break;
case 0x100:
get_touch();
process_touch();
break;
default:
Debug(String(__FUNCTION__)+": "+String(interrupt_mask,HEX)+"!");
break;
}
__enable_irq();
}
void FrontPanel2Init() {
uint8_t res;
int count=0;
KeypadInit();
Debug(String(__FUNCTION__));
Debug("Setup Wire1");
Wire1.setClock(100000);
Wire1.begin();
Debug("Finding Pico...");
found_pico=false;
while(!found_pico && count<10) {
Wire1.beginTransmission(I2C_ADDRESS);
res=Wire1.endTransmission(true);
if(res!=0) {
Debug("endTransmission: "+String(res,HEX));
++count;
} else {
found_pico = true;
}
delay(50);
}
if(found_pico) {
Debug("Found Pico @0x"+String(I2C_ADDRESS,HEX));
delay(100);
send_config();
// Setup Pico interrupt
pinMode(PICO_INTERRUPT_PIN,INPUT);
attachInterrupt(digitalPinToInterrupt(PICO_INTERRUPT_PIN), front_panel_interrupt, FALLING);
} else {
Debug("Pico Not Found @0x"+String(I2C_ADDRESS,HEX));
ShowMessageOnWaterfall("Pico Not Found at 0x"+String(I2C_ADDRESS,HEX));
}
}
#endif