Got rid of encoder library, rewrote encoder handling to use Pin Change Interrupts
This commit is contained in:
@ -1,12 +1,12 @@
|
|||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#include <RotaryEncoder.h>
|
//#include <RotaryEncoder.h>
|
||||||
#include <FlexiTimer2.h>
|
#include <FlexiTimer2.h>
|
||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
#include <U8g2lib.h>
|
#include <U8g2lib.h>
|
||||||
#include <avr/wdt.h>
|
#include <avr/wdt.h>
|
||||||
#include <NeoHWSerial.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
|
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
|
//unsigned long lastInteractionTime; // used for display timeout
|
||||||
|
|
||||||
U8G2_SSD1306_128X64_NONAME_2_HW_I2C u8g2(U8G2_R2, SCL, SDA, U8X8_PIN_NONE);
|
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
|
//Font
|
||||||
const PROGMEM uint8_t velvetscreen[437] U8G2_FONT_SECTION("velvetscreen") =
|
const PROGMEM uint8_t velvetscreen[437] U8G2_FONT_SECTION("velvetscreen") =
|
||||||
@ -218,9 +218,18 @@ void setup() {
|
|||||||
|
|
||||||
calculateCycles();
|
calculateCycles();
|
||||||
calculateBPMTiming();
|
calculateBPMTiming();
|
||||||
|
checkEncoderStatus();
|
||||||
|
|
||||||
resetClocks();
|
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::set(1, 1.0 / 10000, clock); // 1.0/1000 = 1ms period. If other than 1ms calculateBPMTiming() might need tweaking
|
||||||
FlexiTimer2::start();
|
FlexiTimer2::start();
|
||||||
}
|
}
|
||||||
@ -232,6 +241,62 @@ void loop() {
|
|||||||
checkInputs();
|
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() {
|
void sendMIDIClock() {
|
||||||
NeoSerial.write(0xF8);
|
NeoSerial.write(0xF8);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -88,23 +88,29 @@ void checkInputs() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//encoder
|
//encoder
|
||||||
encoder.tick();
|
// things to try:
|
||||||
int encPosition = encoder.getPosition();
|
// 1 - make checkInputs one if-else chain and move the encoder part to the first condition
|
||||||
int encDirection = (int)(encoder.getDirection());
|
// 2 - count "burst" changes and apply acceleration
|
||||||
if (encPositionOld != encPosition) {
|
// 3 - pinchange interrupts? https://github.com/mathertel/RotaryEncoder/blob/master/examples/InterruptRotator/InterruptRotator.ino
|
||||||
int change = encPositionOld - encPosition;
|
//encoder.tick();
|
||||||
if (reverseEnc) {
|
//int encPosition = 0;//encoder.getPosition();
|
||||||
|
//int8_t encDirection = 0;//(int)(encoder.getDirection());
|
||||||
|
if (encoderChange != 0) {
|
||||||
|
int change = encoderChange;
|
||||||
|
if (!reverseEnc) {
|
||||||
change = change * -1;
|
change = change * -1;
|
||||||
}
|
}
|
||||||
unsigned long ms = encoder.getMillisBetweenRotations();
|
/*unsigned long ms = 0;//encoder.getMillisBetweenRotations();
|
||||||
/*if (encDirectionOld == encDirection && ms < 20) { //encoder acceleration
|
|
||||||
|
if (encDirectionOld == encDirection && ms < 18) { //encoder acceleration
|
||||||
change = change * 5;
|
change = change * 5;
|
||||||
} else if (encDirectionOld == encDirection && ms < 80) {
|
} /* else if (encDirectionOld == encDirection && ms < 80) {
|
||||||
change = change * 2;
|
change = change * 2;
|
||||||
} else */ if (encDirectionOld != encDirection && ms < 200) { //filter out encoder "jumps"
|
} else */
|
||||||
|
if (((encoderChange > 0) != encoderDirectionOld) && encoderTimeBetweenPulses < 200) { //filter out encoder "jumps"
|
||||||
change = 0;
|
change = 0;
|
||||||
}
|
}
|
||||||
encDirectionOld = encDirection;
|
encoderDirectionOld = (encoderChange > 0);
|
||||||
if (displayScreen == 0) {
|
if (displayScreen == 0) {
|
||||||
byte channelCV;
|
byte channelCV;
|
||||||
if (!insideTab && !shiftBtnPushed) { //Change tab
|
if (!insideTab && !shiftBtnPushed) { //Change tab
|
||||||
@ -364,7 +370,8 @@ void checkInputs() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateScreen();
|
updateScreen();
|
||||||
encPositionOld = encPosition;
|
//encPositionOld = encPosition;
|
||||||
|
encoderChange = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//play button
|
//play button
|
||||||
|
|||||||
Reference in New Issue
Block a user