Skip to content

Commit baf880c

Browse files
authored
Merge pull request afarhan#1 from SmittyHalibut/pdq_gfx_update
Interrupt Encoder Update
2 parents 3598328 + 50a00ff commit baf880c

File tree

3 files changed

+98
-14
lines changed

3 files changed

+98
-14
lines changed

ubitx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ void cwKeyer(void);
179179
void switchVFO(int vfoSelect);
180180

181181
int enc_read(void); // returns the number of ticks in a short interval, +ve in clockwise, -ve in anti-clockwise
182+
void enc_setup(void); // Setups up initial values and interrupts.
182183
int btnDown(); //returns true if the encoder button is pressed
183184

184185
/* these functions are called universally to update the display */

ubitx_ui.cpp

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,6 @@ void updateDisplay() {
565565
displayVFO(vfoActive);
566566
}
567567

568-
int enc_prev_state = 3;
569568

570569
/**
571570
* The A7 And A6 are purely analog lines on the Arduino Nano
@@ -585,11 +584,15 @@ int enc_prev_state = 3;
585584
* at which the enccoder was spun
586585
*/
587586

587+
/*
588+
int enc_prev_state = 3;
589+
588590
byte enc_state (void) {
589591
//Serial.print(digitalRead(ENC_A)); Serial.print(":");Serial.println(digitalRead(ENC_B));
590592
return (digitalRead(ENC_A) == 1 ? 1 : 0) + (digitalRead(ENC_B) == 1 ? 2: 0);
591593
}
592594
595+
593596
int enc_read(void) {
594597
int result = 0;
595598
byte newState;
@@ -627,6 +630,82 @@ int enc_read(void) {
627630
// Serial.println(result);
628631
return(result);
629632
}
633+
*/
634+
635+
/*
636+
* SmittyHalibut's encoder handling, using interrupts. Should be quicker, smoother handling.
637+
*/
638+
int8_t enc_count;
639+
uint8_t prev_enc;
640+
641+
uint8_t enc_state (void) {
642+
return (digitalRead(ENC_A)?1:0 + digitalRead(ENC_B)?2:0);
643+
}
644+
645+
/*
646+
* Setup the encoder interrupts and global variables.
647+
*/
648+
void pci_setup(byte pin) {
649+
*digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
650+
PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
651+
PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
652+
}
653+
654+
void enc_setup(void) {
655+
enc_count = 0;
656+
// This is already done in setup() ?
657+
//pinMode(ENC_A, INPUT);
658+
//pinMode(ENC_B, INPUT);
659+
prev_enc = enc_state();
660+
661+
// Setup Pin Change Interrupts for the encoder inputs
662+
pci_setup(ENC_A);
663+
pci_setup(ENC_B);
664+
}
665+
666+
/*
667+
* The Interrupt Service Routine for Pin Change Interrupts on A0-A5.
668+
*/
669+
ISR (PCINT1_vect) {
670+
uint8_t cur_enc = enc_state();
671+
if (prev_enc == cur_enc) {
672+
//Serial.println("unnecessary ISR");
673+
return;
674+
}
675+
//Serial.print(prev_enc);
676+
//Serial.println(cur_enc);
677+
678+
//these transitions point to the enccoder being rotated anti-clockwise
679+
if ((prev_enc == 0 && cur_enc == 2) ||
680+
(prev_enc == 2 && cur_enc == 3) ||
681+
(prev_enc == 3 && cur_enc == 1) ||
682+
(prev_enc == 1 && cur_enc == 0))
683+
{
684+
enc_count-=1;
685+
}
686+
//these transitions point to the enccoder being rotated clockwise
687+
else if ((prev_enc == 0 && cur_enc == 1) ||
688+
(prev_enc == 1 && cur_enc == 3) ||
689+
(prev_enc == 3 && cur_enc == 2) ||
690+
(prev_enc == 2 && cur_enc == 0))
691+
{
692+
enc_count+=1;
693+
}
694+
else {
695+
// A change to two states, we can't tell whether it was forward or backward, so we skip it.
696+
//Serial.println("skip");
697+
}
698+
prev_enc = cur_enc; // Record state for next pulse interpretation
699+
}
700+
701+
int enc_read(void) {
702+
int8_t ret = enc_count;
703+
enc_count = 0;
704+
return int(ret);
705+
}
706+
707+
708+
630709

631710
void ritToggle(struct Button *button){
632711
if (ritOn == 0){
@@ -1021,4 +1100,3 @@ void doCommands(){
10211100

10221101
checkCAT();
10231102
}
1024-

ubitx_v6.3.1_code.ino

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -524,30 +524,34 @@ void doTuning(){
524524

525525
if (now >= nextFrequencyUpdate && prev_freq != frequency){
526526
updateDisplay();
527-
nextFrequencyUpdate = now + 500;
527+
nextFrequencyUpdate = now + 100;
528528
prev_freq = frequency;
529529
}
530530

531531
s = enc_read();
532532
if (!s)
533533
return;
534534

535+
//Serial.println(s);
536+
535537
doingCAT = 0; // go back to manual mode if you were doing CAT
536538
prev_freq = frequency;
537539

538-
539-
if (s > 10)
540-
frequency += 200l * s;
541-
else if (s > 5)
542-
frequency += 100l * s;
543-
else if (s > 0)
540+
// TODO With the new more responsive tuning, 5 and 10 are nearly useless
541+
// thresholds for acceleration. I rarely see above 2 or 3 even when turning
542+
// the knob quickly.
543+
if (s < 5 && s > -5) {
544544
frequency += 50l * s;
545-
else if (s < -10)
546-
frequency += 200l * s;
547-
else if (s < -5)
545+
//Serial.println(" 5");
546+
}
547+
else if (s < 10 && s > -10) {
548548
frequency += 100l * s;
549-
else if (s < 0)
550-
frequency += 50l * s;
549+
//Serial.println(" 10");
550+
}
551+
else { // if (s >= 10 || s <= -10)
552+
frequency += 200l * s;
553+
//Serial.println(" <");
554+
}
551555

552556
if (prev_freq < 10000000l && frequency > 10000000l)
553557
isUSB = true;
@@ -676,6 +680,7 @@ void initPorts(){
676680
pinMode(ENC_A, INPUT_PULLUP);
677681
pinMode(ENC_B, INPUT_PULLUP);
678682
pinMode(FBUTTON, INPUT_PULLUP);
683+
enc_setup();
679684

680685
//configure the function button to use the external pull-up
681686
// pinMode(FBUTTON, INPUT);

0 commit comments

Comments
 (0)