@@ -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+
588590byte 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+
593596int 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
631710void ritToggle (struct Button *button){
632711 if (ritOn == 0 ){
@@ -1021,4 +1100,3 @@ void doCommands(){
10211100
10221101 checkCAT ();
10231102}
1024-
0 commit comments