Got rid of encoder library, rewrote encoder handling to use Pin Change Interrupts

This commit is contained in:
Oleksiy
2025-04-05 19:34:08 +03:00
parent fb86269b21
commit 395fa1ef1f
2 changed files with 88 additions and 16 deletions

View File

@ -1,12 +1,12 @@
#include <Wire.h>
#include <RotaryEncoder.h>
//#include <RotaryEncoder.h>
#include <FlexiTimer2.h>
#include <EEPROM.h>
#include <U8g2lib.h>
#include <avr/wdt.h>
#include <NeoHWSerial.h>
#define VERSION "V:1.1.2"
#define VERSION "V:1.1.3B"
byte memCode = 'D'; //Change to different letter if you changed the data structure
@ -151,7 +151,7 @@ int extTriggerCount;
//unsigned long lastInteractionTime; // used for display timeout
U8G2_SSD1306_128X64_NONAME_2_HW_I2C u8g2(U8G2_R2, SCL, SDA, U8X8_PIN_NONE);
RotaryEncoder encoder(ENC_D1_PIN, ENC_D2_PIN, RotaryEncoder::LatchMode::TWO03);
//RotaryEncoder encoder(ENC_D1_PIN, ENC_D2_PIN, RotaryEncoder::LatchMode::TWO03);
//Font
const PROGMEM uint8_t velvetscreen[437] U8G2_FONT_SECTION("velvetscreen") =
@ -218,9 +218,18 @@ void setup() {
calculateCycles();
calculateBPMTiming();
checkEncoderStatus();
resetClocks();
pinMode(ENC_D1_PIN, INPUT_PULLUP);
pinMode(ENC_D2_PIN, INPUT_PULLUP);
//Enabling PinChange Interrupts for the encoder
//pins 17 (PC3/PCINT11) and 4 (PD4/PCINT20), ports C and D
PCICR |= 0b00000110;
PCMSK1 |= 0b00001000;
PCMSK2 |= 0b00010000;
FlexiTimer2::set(1, 1.0 / 10000, clock); // 1.0/1000 = 1ms period. If other than 1ms calculateBPMTiming() might need tweaking
FlexiTimer2::start();
}
@ -232,6 +241,62 @@ void loop() {
checkInputs();
}
//Encoder interrupts
ISR (PCINT1_vect) {
checkEncoderStatus();
}
ISR (PCINT2_vect) {
checkEncoderStatus();
}
uint8_t encoderStatus;
uint32_t encoderCheckTime = 0;
uint32_t encoderTimeBetweenPulses = 0;
bool encoderDirectionOld; //0 = -, 1 = +, Old because current direction can be determined by encoderChange
int8_t encoderChange = 0;
void checkEncoderStatus() {
bool pin1Status = digitalRead(ENC_D1_PIN);
bool pin2Status = digitalRead(ENC_D2_PIN);
uint8_t newStatus = (pin1Status << 1) | pin2Status;
switch(encoderStatus) { // encoderStatus & 0b00000011 - to check only 2 last bits
case 0b00:
if (newStatus == 0b01) {
encoderChange++;
} else if (newStatus == 0b10) {
encoderChange--;
}
break;
case 0b01:
if (newStatus == 0b11) {
encoderChange++;
} else if (newStatus == 0b00) {
encoderChange--;
}
break;
case 0b11:
if (newStatus == 0b10) {
encoderChange++;
} else if (newStatus == 0b01) {
encoderChange--;
}
break;
case 0b10:
if (newStatus == 0b00) {
encoderChange++;
} else if (newStatus == 0b11) {
encoderChange--;
}
break;
}
//encoderStatus = (encoderStatus << 2); //previous status is now stored in bits 2 and 3
encoderStatus = bitWrite(encoderStatus, 1, pin1Status);
encoderStatus = bitWrite(encoderStatus, 0, pin2Status); //This can probably be more optimizied with bit logic
uint32_t currentTime = millis();
encoderTimeBetweenPulses = currentTime - encoderCheckTime;
encoderCheckTime = currentTime;
}
void sendMIDIClock() {
NeoSerial.write(0xF8);
}