-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoders.cpp
More file actions
590 lines (522 loc) · 17.1 KB
/
Encoders.cpp
File metadata and controls
590 lines (522 loc) · 17.1 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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#ifndef BEENHERE
#include "SDT.h"
#endif
/*****
Purpose: EncoderFilter
Parameter list:
void
Return value;
void
Modified AFP21-12-15
*****/
void FilterSetSSB() {
long filter_change;
// SSB
if (filter_pos != last_filter_pos) {
tft.writeTo(L2); // Clear layer 2. KF5N July 31, 2023
tft.clearMemory();
if (xmtMode == CW_MODE) BandInformation();
tft.fillRect((MAX_WATERFALL_WIDTH + SPECTRUM_LEFT_X) / 2 - filterWidth, SPECTRUM_TOP_Y + 17, filterWidth, SPECTRUM_HEIGHT - 20, RA8875_BLACK); // Erase old filter background
filter_change = (filter_pos - last_filter_pos);
if (filter_change >= 1) {
filterWidth--;
if (filterWidth < 10)
filterWidth = 10;
}
if (filter_change <= -1) {
filterWidth++;
if (filterWidth > 100)
filterWidth = 50;
}
last_filter_pos = filter_pos;
// ============= AFP 10-27-22
switch (bands[currentBand].mode) {
case DEMOD_LSB:
if (switchFilterSideband == 0) // "0" = normal, "1" means change opposite filter
{
bands[currentBand].FLoCut = bands[currentBand].FLoCut + filter_change * 50 * ENCODER_FACTOR;
//fHiCutOld= bands[currentBand].FHiCut;
FilterBandwidth();
} else if (switchFilterSideband == 1) {
//if (abs(bands[currentBand].FHiCut) < 500) {
bands[currentBand].FHiCut = bands[currentBand].FHiCut + filter_change * 50 * ENCODER_FACTOR;
fLoCutOld = bands[currentBand].FLoCut;
}
break;
case DEMOD_USB:
if (switchFilterSideband == 0) {
bands[currentBand].FHiCut = bands[currentBand].FHiCut - filter_change * 50 * ENCODER_FACTOR;
//bands[currentBand].FLoCut= fLoCutOld;
FilterBandwidth();
} else if (switchFilterSideband == 1) {
bands[currentBand].FLoCut = bands[currentBand].FLoCut - filter_change * 50 * ENCODER_FACTOR;
// bands[currentBand].FHiCut= fHiCutOld;
}
break;
case DEMOD_AM:
bands[currentBand].FHiCut = bands[currentBand].FHiCut - filter_change * 50 * ENCODER_FACTOR;
bands[currentBand].FLoCut = -bands[currentBand].FHiCut;
FilterBandwidth();
InitFilterMask();
break;
case DEMOD_SAM: // AFP 11-03-22
bands[currentBand].FHiCut = bands[currentBand].FHiCut - filter_change * 50 * ENCODER_FACTOR;
bands[currentBand].FLoCut = -bands[currentBand].FHiCut;
FilterBandwidth();
InitFilterMask();
break;
case DEMOD_FM: // G0ORX 02/21/24
bands[currentBand].FHiCut = bands[currentBand].FHiCut - filter_change * 50 * ENCODER_FACTOR;
bands[currentBand].FLoCut = -bands[currentBand].FHiCut;
FilterBandwidth();
InitFilterMask();
break;
}
// ============= AFP 10-27-22
//ControlFilterF();
Menu2 = MENU_F_LO_CUT; // set Menu2 to MENU_F_LO_CUT
FilterBandwidth();
ShowBandwidth();
DrawFrequencyBarValue();
// centerTuneFlag = 1; //AFP 10-03-22
//SetFreq(); // AFP 10-04-22
// ShowFrequency();
}
//notchPosOld = filter_pos;
tft.writeTo(L1); // Exit function in layer 1. KF5N August 3, 2023
}
#ifndef G0ORX_FRONTPANEL_2
/*****
Purpose: EncoderCenterTune
Parameter list:
void
Return value;
void
*****/
void EncoderCenterTune() {
long tuneChange = 0L;
// long oldFreq = centerFreq;
#ifdef G0ORX_FRONTPANEL
int result = tuneEncoder.process(); // Read the encoder
#else
unsigned char result = tuneEncoder.process(); // Read the encoder
#endif
if (result == 0) // Nothing read
return;
//centerTuneFlag = 1; //AFP 10-03-22 Not used in revised tuning scheme. KF5N July 22, 2023
if (xmtMode == CW_MODE && decoderFlag == DECODE_ON) { // No reason to reset if we're not doing decoded CW AFP 09-27-22
ResetHistograms();
}
#ifdef G0ORX_FRONTPANEL
tuneChange = result;
#else
switch (result) {
case DIR_CW: // Turned it clockwise, 16
tuneChange = 1L;
break;
case DIR_CCW: // Turned it counter-clockwise
tuneChange = -1L;
break;
}
#endif
// newFreq = (long)freqIncrement * tuneChange;
centerFreq += ((long)freqIncrement * tuneChange); // tune the master vfo
// if (centerFreq != oldFreq) { // If the frequency has changed...
//=== AFP 10-19-22
TxRxFreq = centerFreq + NCOFreq;
SetFreq(); // Change to receiver tuning process. KF5N July 22, 2023
//currentFreqA= centerFreq + NCOFreq;
DrawBandWidthIndicatorBar(); // AFP 10-20-22
//FilterOverlay(); // AFP 10-20-22
ShowFrequency();
BandInformation();
// }
}
/*****
Purpose: Encoder volume control
Parameter list:
void
Return value;
int 0 means encoder didn't move; otherwise it moved
*****/
void EncoderVolume() //============================== AFP 10-22-22 Begin new
{
#ifdef G0ORX_FRONTPANEL
int result;
#else
char result;
#endif
int adjustVolEncoder = 0;
result = volumeEncoder.process(); // Read the encoder
if (result == 0) { // Nothing read
return;
}
#ifdef G0ORX_FRONTPANEL
adjustVolEncoder = result;
#else
switch (result) {
case DIR_CW: // Turned it clockwise, 16
adjustVolEncoder = 1;
break;
case DIR_CCW: // Turned it counter-clockwise
adjustVolEncoder = -1;
break;
}
#endif
#ifdef G0ORX_FRONTPANEL
switch(volumeFunction) {
case AUDIO_VOLUME:
audioVolume += adjustVolEncoder;
if (audioVolume > 100) { // In range?
audioVolume = 100;
} else {
if (audioVolume < 0) {
audioVolume = 0;
}
}
break;
case AGC_GAIN:
bands[currentBand].AGC_thresh += adjustVolEncoder;
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 += adjustVolEncoder;
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 += adjustVolEncoder;
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] += adjustVolEncoder;
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;
}
#else
audioVolume += adjustVolEncoder;
// simulate log taper. As we go higher in volume, the increment increases.
if (audioVolume < (MIN_AUDIO_VOLUME + 10)) increment = 2;
else if (audioVolume < (MIN_AUDIO_VOLUME + 20)) increment = 3;
else if (audioVolume < (MIN_AUDIO_VOLUME + 30)) increment = 4;
else if (audioVolume < (MIN_AUDIO_VOLUME + 40)) increment = 5;
else if (audioVolume < (MIN_AUDIO_VOLUME + 50)) increment = 6;
else if (audioVolume < (MIN_AUDIO_VOLUME + 60)) increment = 7;
else increment = 8;
if (audioVolume > MAX_AUDIO_VOLUME) {
audioVolume = MAX_AUDIO_VOLUME;
} else {
if (audioVolume < MIN_AUDIO_VOLUME)
audioVolume = MIN_AUDIO_VOLUME;
}
#endif
volumeChangeFlag = true; // Need this because of unknown timing in display updating.
} //============================== AFP 10-22-22 End new
#endif
/*****
Purpose: Use the encoder to change the value of a number in some other function
Parameter list:
int minValue the lowest value allowed
int maxValue the largest value allowed
int startValue the numeric value to begin the count
int increment the amount by which each increment changes the value
char prompt[] the input prompt
Return value;
int the new value
*****/
float GetEncoderValueLive(float minValue, float maxValue, float startValue, float increment, char prompt[]) //AFP 10-22-22
{
float currentValue = startValue;
tft.setFontScale((enum RA8875tsize)1);
tft.setTextColor(RA8875_WHITE);
tft.fillRect(250, 0, 285, CHAR_HEIGHT, RA8875_BLACK); // Increased rectangle size to full erase value. KF5N August 12, 2023
tft.setCursor(257, 1);
tft.print(prompt);
tft.setCursor(440, 1);
if (abs(startValue) > 2) {
tft.print(startValue, 0);
} else {
tft.print(startValue, 3);
}
//while (true) {
if (filterEncoderMove != 0) {
currentValue += filterEncoderMove * increment; // Bump up or down...
if (currentValue < minValue)
currentValue = minValue;
else if (currentValue > maxValue)
currentValue = maxValue;
// tft.fillRect(449, 0, 90, CHAR_HEIGHT, RA8875_BLACK); // This is not required. KF5N August 12, 2023
tft.setCursor(440, 1);
if (abs(startValue) > 2) {
tft.print(startValue, 0);
} else {
tft.print(startValue, 3);
}
filterEncoderMove = 0;
}
//tft.setTextColor(RA8875_WHITE);
return currentValue;
}
/*****
Purpose: Use the encoder to change the value of a number in some other function
Parameter list:
int minValue the lowest value allowed
int maxValue the largest value allowed
int startValue the numeric value to begin the count
int increment the amount by which each increment changes the value
char prompt[] the input prompt
Return value;
int the new value
*****/
int GetEncoderValue(int minValue, int maxValue, int startValue, int increment, char prompt[]) {
int currentValue = startValue;
int val;
tft.setFontScale((enum RA8875tsize)1);
tft.setTextColor(RA8875_WHITE);
tft.fillRect(250, 0, 280, CHAR_HEIGHT, RA8875_MAGENTA);
tft.setCursor(257, 1);
tft.print(prompt);
tft.setCursor(470, 1);
tft.print(startValue);
while (true) {
if (filterEncoderMove != 0) {
currentValue += filterEncoderMove * increment; // Bump up or down...
if (currentValue < minValue)
currentValue = minValue;
else if (currentValue > maxValue)
currentValue = maxValue;
tft.fillRect(465, 0, 65, CHAR_HEIGHT, RA8875_MAGENTA);
tft.setCursor(470, 1);
tft.print(currentValue);
filterEncoderMove = 0;
}
val = ReadSelectedPushButton(); // Read the ladder value
//MyDelay(100L); //AFP 09-22-22
if (val != -1 && val < (EEPROMData.switchValues[0] + WIGGLE_ROOM)) {
val = ProcessButtonPress(val); // Use ladder value to get menu choice
if (val == MENU_OPTION_SELECT) { // Make a choice??
return currentValue;
}
}
}
}
/*****
Purpose: Allows quick setting of WPM without going through a menu
Parameter list:
void
Return value;
int the current WPM
*****/
int SetWPM() {
int val;
long lastWPM = currentWPM;
tft.setFontScale((enum RA8875tsize)1);
tft.fillRect(SECONDARY_MENU_X, MENUS_Y, EACH_MENU_WIDTH, CHAR_HEIGHT, RA8875_MAGENTA);
tft.setTextColor(RA8875_WHITE);
tft.setCursor(SECONDARY_MENU_X + 1, MENUS_Y + 1);
tft.print("current WPM:");
tft.setCursor(SECONDARY_MENU_X + 200, MENUS_Y + 1);
tft.print(currentWPM);
#ifdef G0ORX_FRONTPANEL_2
calibrateFlag = 1;
#endif
while (true) {
if (filterEncoderMove != 0) { // Changed encoder?
currentWPM += filterEncoderMove; // Yep
lastWPM = currentWPM;
if (lastWPM < 5) // Set minimum keyer speed to 5 wpm. KF5N August 20, 2023
lastWPM = 5;
else if (lastWPM > MAX_WPM)
lastWPM = MAX_WPM;
tft.fillRect(SECONDARY_MENU_X + 200, MENUS_Y + 1, 50, CHAR_HEIGHT, RA8875_MAGENTA);
tft.setCursor(SECONDARY_MENU_X + 200, MENUS_Y + 1);
tft.print(lastWPM);
filterEncoderMove = 0;
}
val = ReadSelectedPushButton(); // Read pin that controls all switches
val = ProcessButtonPress(val);
if (val == MENU_OPTION_SELECT) { // Make a choice??
currentWPM = lastWPM;
EEPROMData.currentWPM = currentWPM;
EEPROMWrite();
UpdateWPMField();
break;
}
}
// UpdateEEPROMSyncIndicator(syncEEPROM = 0); // EEPROM and current values not the same
tft.setTextColor(RA8875_WHITE);
// EraseMenus();
#ifdef G0ORX_FRONTPANEL_2
calibrateFlag = 0;
#endif
return currentWPM;
}
/*****
Purpose: Determines how long the transmit relay remains on after last CW atom is sent.
Parameter list:
void
Return value;
long the delay length in milliseconds
*****/
long SetTransmitDelay() // new function JJP 9/1/22
{
int val;
long lastDelay = cwTransmitDelay;
long increment = 250; // Means a quarter second change per detent
tft.setFontScale((enum RA8875tsize)1);
tft.fillRect(SECONDARY_MENU_X - 150, MENUS_Y, EACH_MENU_WIDTH + 150, CHAR_HEIGHT, RA8875_MAGENTA); // scoot left cuz prompt is long
tft.setTextColor(RA8875_WHITE);
tft.setCursor(SECONDARY_MENU_X - 149, MENUS_Y + 1);
tft.print("current delay:");
tft.setCursor(SECONDARY_MENU_X + 79, MENUS_Y + 1);
tft.print(cwTransmitDelay);
#ifdef G0ORX_FRONTPANEL_2
calibrateFlag = 1;
#endif
while (true) {
if (filterEncoderMove != 0) { // Changed encoder?
lastDelay += filterEncoderMove * increment; // Yep
if (lastDelay < 0L)
lastDelay = 250L;
tft.fillRect(SECONDARY_MENU_X + 80, MENUS_Y + 1, 200, CHAR_HEIGHT, RA8875_MAGENTA);
tft.setCursor(SECONDARY_MENU_X + 79, MENUS_Y + 1);
tft.print(lastDelay);
filterEncoderMove = 0;
}
val = ReadSelectedPushButton(); // Read pin that controls all switches
val = ProcessButtonPress(val);
//MyDelay(150L); //ALF 09-22-22
if (val == MENU_OPTION_SELECT) { // Make a choice??
cwTransmitDelay = lastDelay;
EEPROMData.cwTransmitDelay = cwTransmitDelay;
EEPROMWrite();
break;
}
}
tft.setTextColor(RA8875_WHITE);
EraseMenus();
#ifdef G0ORX_FRONTPANEL_2
calibrateFlag = 0;
#endif
return cwTransmitDelay;
}
#ifndef G0ORX_FRONTPANEL_2
/*****
Purpose: Fine tune control.
Parameter list:
void
Return value;
void cannot return value from interrupt
*****/
FASTRUN // Causes function to be allocated in RAM1 at startup for fastest performance.
void
EncoderFineTune() {
#ifdef G0ORX_FRONTPANEL
int result;
#else
char result;
#endif
result = fineTuneEncoder.process(); // Read the encoder
if (result == 0) { // Nothing read
fineTuneEncoderMove = 0L;
return;
} else {
#ifdef G0ORX_FRONTPANEL
fineTuneEncoderMove=result;
#else
if (result == DIR_CW) { // 16 = CW, 32 = CCW
fineTuneEncoderMove = 1L;
} else {
fineTuneEncoderMove = -1L;
}
#endif
}
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))) { // 47500 with 2x zoom.
centerTuneFlag = 0;
resetTuningFlag = 1;
return;
}
} else {
if (NCOFreq > 142000 || NCOFreq < -43000) { // Offset tuning window in zoom 1x
centerTuneFlag = 0;
resetTuningFlag = 1;
return;
}
}
fineTuneEncoderMove = 0L;
TxRxFreq = centerFreq + NCOFreq; // KF5N
}
FASTRUN // Causes function to be allocated in RAM1 at startup for fastest performance.
void
EncoderFilter() {
#ifdef G0ORX_FRONTPANEL
int result;
#else
char result;
#endif
result = filterEncoder.process(); // Read the encoder
if (result == 0) {
// filterEncoderMove = 0;// Nothing read
return;
}
#ifdef G0ORX_FRONTPANEL
filterEncoderMove = result;
#else
switch (result) {
case DIR_CW: // Turned it clockwise, 16
filterEncoderMove = 1;
//filter_pos = last_filter_pos - 5 * filterEncoderMove; // AFP 10-22-22
break;
case DIR_CCW: // Turned it counter-clockwise
filterEncoderMove = -1;
// filter_pos = last_filter_pos - 5 * filterEncoderMove; // AFP 10-22-22
break;
}
#endif
if (calibrateFlag == 0) { // AFP 10-22-22
filter_pos = last_filter_pos - 5 * filterEncoderMove; // AFP 10-22-22
} // AFP 10-22-22
}
#endif