RotaryEncoder.h with interrupts seems to work fine (hopefully this version is not messed up with merge conflicts)

This commit is contained in:
Oleksiy
2025-05-09 00:42:03 +03:00
parent 663b723ddb
commit d8bcbabad5
2 changed files with 43 additions and 97 deletions

View File

@ -1,11 +1,12 @@
#include <Wire.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.3B"
#define VERSION "V:1.1.3B3"
byte memCode = 'D'; //Change to different letter if you changed the data structure
@ -50,7 +51,7 @@ bool rotateScreen = true;
uint16_t CV1Calibration = 512;
uint16_t CV2Calibration = 512;
bool showDone = false;
const int subDivs[20] = { -24, -12, -8, -6, -4, -3, -2, 1, 2, 3, 4, 5, 6, 7, 8, 16, 24, 32, 64, 128 }; //positive - divide, negative - multiply, 0 - off
@ -127,7 +128,6 @@ byte menuItem = 0;
bool menuItemSelected = false;
byte lastMenuItem = 3;
byte displayScreen = 0; //0 - main, 1 - sequencer, 2 - settings
bool showDone = false;
bool playBtnPushed = false;
bool shiftBtnPushed = false;
@ -151,6 +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::FOUR3);
//Font
const PROGMEM uint8_t velvetscreen[437] U8G2_FONT_SECTION("velvetscreen") =
@ -208,10 +209,7 @@ void setup() {
}
pinMode(clockOutPin, OUTPUT);
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;
@ -226,7 +224,6 @@ void setup() {
calculateCycles();
calculateBPMTiming();
checkEncoderStatus();
resetClocks();
@ -243,56 +240,10 @@ void loop() {
//Encoder interrupts
ISR (PCINT1_vect) {
checkEncoderStatus();
encoder.tick();
}
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;
//uint8_t encoderBurstCount = 0;
void checkEncoderStatus() {
uint8_t newStatus = (digitalRead(ENC_D1_PIN) << 1) | digitalRead(ENC_D2_PIN);
switch(encoderStatus) {
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 = newStatus;
uint32_t currentTime = millis();
encoderTimeBetweenPulses = currentTime - encoderCheckTime;
encoderCheckTime = currentTime;
encoder.tick();
}
void sendMIDIClock() {
@ -387,8 +338,8 @@ void externalClock() {
//reset cycles if there were no pulses for a while
if ((newExtPulseTime - lastExtPulseTime) > 125) { //125ms is 20bpm
resetClocks();
sendMIDIStart();
}
tickCount = 0; //to make things happen in the main clock function
@ -403,7 +354,6 @@ void externalClock() {
}
if ((newExtPulseTime - lastExtPulseTime) > 750) {
resetClocks();
sendMIDIStart();
extResetCountdown = 0;
extTriggerCount = 0;
}