Merge pull request #1 from SmittyHalibut/pdq_gfx_update

Interrupt Encoder Update
This commit is contained in:
reedbn 2020-01-14 21:50:44 -08:00 committed by GitHub
commit baf880c7ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 14 deletions

View File

@ -179,6 +179,7 @@ void cwKeyer(void);
void switchVFO(int vfoSelect);
int enc_read(void); // returns the number of ticks in a short interval, +ve in clockwise, -ve in anti-clockwise
void enc_setup(void); // Setups up initial values and interrupts.
int btnDown(); //returns true if the encoder button is pressed
/* these functions are called universally to update the display */

View File

@ -565,7 +565,6 @@ void updateDisplay() {
displayVFO(vfoActive);
}
int enc_prev_state = 3;
/**
* The A7 And A6 are purely analog lines on the Arduino Nano
@ -585,11 +584,15 @@ int enc_prev_state = 3;
* at which the enccoder was spun
*/
/*
int enc_prev_state = 3;
byte enc_state (void) {
//Serial.print(digitalRead(ENC_A)); Serial.print(":");Serial.println(digitalRead(ENC_B));
return (digitalRead(ENC_A) == 1 ? 1 : 0) + (digitalRead(ENC_B) == 1 ? 2: 0);
}
int enc_read(void) {
int result = 0;
byte newState;
@ -627,6 +630,82 @@ int enc_read(void) {
// Serial.println(result);
return(result);
}
*/
/*
* SmittyHalibut's encoder handling, using interrupts. Should be quicker, smoother handling.
*/
int8_t enc_count;
uint8_t prev_enc;
uint8_t enc_state (void) {
return (digitalRead(ENC_A)?1:0 + digitalRead(ENC_B)?2:0);
}
/*
* Setup the encoder interrupts and global variables.
*/
void pci_setup(byte pin) {
*digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
}
void enc_setup(void) {
enc_count = 0;
// This is already done in setup() ?
//pinMode(ENC_A, INPUT);
//pinMode(ENC_B, INPUT);
prev_enc = enc_state();
// Setup Pin Change Interrupts for the encoder inputs
pci_setup(ENC_A);
pci_setup(ENC_B);
}
/*
* The Interrupt Service Routine for Pin Change Interrupts on A0-A5.
*/
ISR (PCINT1_vect) {
uint8_t cur_enc = enc_state();
if (prev_enc == cur_enc) {
//Serial.println("unnecessary ISR");
return;
}
//Serial.print(prev_enc);
//Serial.println(cur_enc);
//these transitions point to the enccoder being rotated anti-clockwise
if ((prev_enc == 0 && cur_enc == 2) ||
(prev_enc == 2 && cur_enc == 3) ||
(prev_enc == 3 && cur_enc == 1) ||
(prev_enc == 1 && cur_enc == 0))
{
enc_count-=1;
}
//these transitions point to the enccoder being rotated clockwise
else if ((prev_enc == 0 && cur_enc == 1) ||
(prev_enc == 1 && cur_enc == 3) ||
(prev_enc == 3 && cur_enc == 2) ||
(prev_enc == 2 && cur_enc == 0))
{
enc_count+=1;
}
else {
// A change to two states, we can't tell whether it was forward or backward, so we skip it.
//Serial.println("skip");
}
prev_enc = cur_enc; // Record state for next pulse interpretation
}
int enc_read(void) {
int8_t ret = enc_count;
enc_count = 0;
return int(ret);
}
void ritToggle(struct Button *button){
if (ritOn == 0){
@ -1021,4 +1100,3 @@ void doCommands(){
checkCAT();
}

View File

@ -524,7 +524,7 @@ void doTuning(){
if (now >= nextFrequencyUpdate && prev_freq != frequency){
updateDisplay();
nextFrequencyUpdate = now + 500;
nextFrequencyUpdate = now + 100;
prev_freq = frequency;
}
@ -532,22 +532,26 @@ void doTuning(){
if (!s)
return;
//Serial.println(s);
doingCAT = 0; // go back to manual mode if you were doing CAT
prev_freq = frequency;
if (s > 10)
frequency += 200l * s;
else if (s > 5)
frequency += 100l * s;
else if (s > 0)
// TODO With the new more responsive tuning, 5 and 10 are nearly useless
// thresholds for acceleration. I rarely see above 2 or 3 even when turning
// the knob quickly.
if (s < 5 && s > -5) {
frequency += 50l * s;
else if (s < -10)
frequency += 200l * s;
else if (s < -5)
//Serial.println(" 5");
}
else if (s < 10 && s > -10) {
frequency += 100l * s;
else if (s < 0)
frequency += 50l * s;
//Serial.println(" 10");
}
else { // if (s >= 10 || s <= -10)
frequency += 200l * s;
//Serial.println(" <");
}
if (prev_freq < 10000000l && frequency > 10000000l)
isUSB = true;
@ -676,6 +680,7 @@ void initPorts(){
pinMode(ENC_A, INPUT_PULLUP);
pinMode(ENC_B, INPUT_PULLUP);
pinMode(FBUTTON, INPUT_PULLUP);
enc_setup();
//configure the function button to use the external pull-up
// pinMode(FBUTTON, INPUT);