transfered encoder implementation from 1.1.3 beta 1 (it takes 170 bytes less than with RotaryEncoder library)

This commit is contained in:
Oleksiy
2025-04-08 22:17:52 +03:00
parent 91511da688
commit 29c187bd01
2 changed files with 114 additions and 44 deletions

View File

@ -1,5 +1,4 @@
#include <Wire.h>
#include <RotaryEncoder.h>
#include <FlexiTimer2.h>
#include <EEPROM.h>
#include <U8g2lib.h>
@ -148,7 +147,6 @@ int extTriggerCount;
//uint32_t 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);
String version;
@ -170,6 +168,16 @@ 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;
PCMSK1 |= 0b00001000;
PCMSK2 |= 0b00010000;
loadState();
u8g2.begin();
@ -178,6 +186,7 @@ void setup() {
calculateCycles();
calculateBPMTiming();
checkEncoderStatus();
resetClocks();
@ -192,6 +201,64 @@ 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;
uint8_t encoderBurstCount = 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);
}