7 Commits

9 changed files with 436 additions and 3 deletions

View File

@ -0,0 +1,116 @@
#include <Wire.h>
#include <U8g2lib.h>
#define SCREEN_ADDRESS 0x3C
// Rev 2+ Config
#define ENC_BTN_PIN 14
#define ENC_D1_PIN 17
#define ENC_D2_PIN 4
#define START_STOP_BTN_PIN 5
#define SHIFT_BTN_PIN 12
#define EXT_INPUT_PIN 2 //needs to be an interrupt pin
#define ANALOGUE_INPUT_1_PIN A7
#define ANALOGUE_INPUT_2_PIN A6
const byte outsPins[6] = { 7, 8, 10, 6, 9, 11 };
const byte clockOutPin = 3;
bool rotateScreen = false;
bool reverseEnc = false;
//
/* Rev 1 Config
#define ENC_BTN_PIN 14
#define ENC_D1_PIN 17
#define ENC_D2_PIN 4
#define START_STOP_BTN_PIN 5
#define SHIFT_BTN_PIN 100
#define EXT_INPUT_PIN 2 //needs to be an interrupt pin
#define ANALOGUE_INPUT_1_PIN A2
#define ANALOGUE_INPUT_2_PIN A1
const byte clockOutPin = 13;
const byte outsPins[6] = {6, 11, 7, 10, 8, 9};
bool rotateScreen = true;
*/
U8G2_SSD1306_128X64_NONAME_2_HW_I2C u8g2(U8G2_R2, SCL, SDA, U8X8_PIN_NONE);
int16_t counter = 0;
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;
u8g2.begin();
updateScreen();
}
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() {
//noInterrupts();
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;
//interrupts();
}

View File

@ -0,0 +1,29 @@
void checkInputs() {
//encoder
if (encoderChange != 0) {
if (!reverseEnc) {
encoderChange = encoderChange * -1;
}
counter += encoderChange;
/*if (((encoderChange > 0) != encoderDirectionOld) && encoderTimeBetweenPulses < 60) { //filter out encoder "jumps".
encoderChange = 0; //Comented out because it seems like sometimes it was preventing normal scroll
} //if it works ok without it delete encoderDirectionOld var altogether
encoderDirectionOld = (encoderChange > 0);*/
//encoder acceleration
/*if (encoderTimeBetweenPulses < 15) { // <--
encoderBurstCount++;
} else {
encoderBurstCount = 0;
}
if (encoderBurstCount > 3) { // <--
encoderChange = encoderChange * 3; // <-- The three params need to be finetuned to feel natural
}*/
updateScreen();
//encPositionOld = encPosition;
encoderChange = 0;
}
}

View File

@ -0,0 +1,13 @@
void updateScreen() {
u8g2.firstPage();
do {
String valueStr;
u8g2.setDrawColor(1);
valueStr = String(counter);
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(0,15,valueStr.c_str());
} while ( u8g2.nextPage() );
}

View File

@ -0,0 +1,100 @@
#include <Wire.h>
#include <U8g2lib.h>
#define SCREEN_ADDRESS 0x3C
// Rev 2+ Config
#define ENC_BTN_PIN 14
#define ENC_D1_PIN 17
#define ENC_D2_PIN 4
#define START_STOP_BTN_PIN 5
#define SHIFT_BTN_PIN 12
#define EXT_INPUT_PIN 2 //needs to be an interrupt pin
#define ANALOGUE_INPUT_1_PIN A7
#define ANALOGUE_INPUT_2_PIN A6
const byte outsPins[6] = { 7, 8, 10, 6, 9, 11 };
const byte clockOutPin = 3;
bool rotateScreen = false;
bool reverseEnc = false;
//
/* Rev 1 Config
#define ENC_BTN_PIN 14
#define ENC_D1_PIN 17
#define ENC_D2_PIN 4
#define START_STOP_BTN_PIN 5
#define SHIFT_BTN_PIN 100
#define EXT_INPUT_PIN 2 //needs to be an interrupt pin
#define ANALOGUE_INPUT_1_PIN A2
#define ANALOGUE_INPUT_2_PIN A1
const byte clockOutPin = 13;
const byte outsPins[6] = {6, 11, 7, 10, 8, 9};
bool rotateScreen = true;
*/
U8G2_SSD1306_128X64_NONAME_2_HW_I2C u8g2(U8G2_R2, SCL, SDA, U8X8_PIN_NONE);
int16_t counter = 0;
void setup() {
pinMode(clockOutPin, OUTPUT);
pinMode(ENC_D1_PIN, INPUT_PULLUP);
pinMode(ENC_D2_PIN, INPUT_PULLUP);
u8g2.begin();
updateScreen();
}
void loop() {
checkInputs();
}
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;
}

View File

@ -0,0 +1,33 @@
void checkInputs() {
//encoder
if (millis() > encoderCheckTime + 20) { //debouncing. this approach doesn't work. very noisy
checkEncoderStatus();
}
if (encoderChange != 0) {
if (!reverseEnc) {
encoderChange = encoderChange * -1;
}
counter += encoderChange;
/*if (((encoderChange > 0) != encoderDirectionOld) && encoderTimeBetweenPulses < 60) { //filter out encoder "jumps".
encoderChange = 0; //Comented out because it seems like sometimes it was preventing normal scroll
} //if it works ok without it delete encoderDirectionOld var altogether
encoderDirectionOld = (encoderChange > 0);*/
//encoder acceleration
/*if (encoderTimeBetweenPulses < 15) { // <--
encoderBurstCount++;
} else {
encoderBurstCount = 0;
}
if (encoderBurstCount > 3) { // <--
encoderChange = encoderChange * 3; // <-- The three params need to be finetuned to feel natural
}*/
updateScreen();
//encPositionOld = encPosition;
encoderChange = 0;
}
}

View File

@ -0,0 +1,13 @@
void updateScreen() {
u8g2.firstPage();
do {
String valueStr;
u8g2.setDrawColor(1);
valueStr = String(counter);
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(0,15,valueStr.c_str());
} while ( u8g2.nextPage() );
}

View File

@ -0,0 +1,115 @@
#define ENC_D1_PIN 17
#define ENC_D2_PIN 4
bool reverseEnc = false;
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 setup() {
Serial.begin(9600);
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;
checkEncoderStatus();
}
void loop() {
checkInputs();
}
//Encoder interrupts
ISR (PCINT1_vect) {
checkEncoderStatus();
}
ISR (PCINT2_vect) {
checkEncoderStatus();
}
void checkInputs() {
//encoder
// things to try:
// count "burst" changes and apply acceleration (test the period between ticks in a new sketch)
if (encoderChange != 0) {
if (!reverseEnc) {
encoderChange = encoderChange * -1;
}
if (((encoderChange > 0) != encoderDirectionOld) && encoderTimeBetweenPulses < 200) { //filter out encoder "jumps"
encoderChange = 0;
}
encoderDirectionOld = (encoderChange > 0);
//encoder acceleration
if (encoderTimeBetweenPulses < 50) {
encoderBurstCount++;
} else {
encoderBurstCount = 0;
}
if (encoderBurstCount > 2) {
encoderChange = encoderChange * 2;
}
Serial.print("Change: " + String(encoderChange));
Serial.println(", Period " + String(encoderTimeBetweenPulses));
}
}

View File

@ -6,7 +6,7 @@
#include <avr/wdt.h>
#include <NeoHWSerial.h>
#define VERSION "V:1.1.2"
#define VERSION "V:1.1.3B3"
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::FOUR3);
//Font
const PROGMEM uint8_t velvetscreen[437] U8G2_FONT_SECTION("velvetscreen") =
@ -210,6 +210,12 @@ void setup() {
pinMode(clockOutPin, OUTPUT);
//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();
@ -232,6 +238,14 @@ void loop() {
checkInputs();
}
//Encoder interrupts
ISR (PCINT1_vect) {
encoder.tick();
}
ISR (PCINT2_vect) {
encoder.tick();
}
void sendMIDIClock() {
NeoSerial.write(0xF8);
}

View File

@ -88,7 +88,7 @@ void checkInputs() {
}
//encoder
encoder.tick();
//encoder.tick();
int encPosition = encoder.getPosition();
int encDirection = (int)(encoder.getDirection());
if (encPositionOld != encPosition) {