Skip to content

Commit

Permalink
working encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
rabidaudio committed Jan 1, 2023
1 parent 92ede29 commit ef9f7a4
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 25 deletions.
6 changes: 5 additions & 1 deletion clock/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,9 @@ upload_command = avrdude $UPLOAD_FLAGS -U flash:w:$SOURCE:i
[env:mega]
platform = atmelavr
framework = arduino
board = ATmega2560
board = ATmega2560
; board = megaatmega2560
; board_build.mcu = atmega2560
lib_deps = adafruit/Adafruit LED Backpack Library@^1.3.2
upload_protocol = wiring
; board_build.f_cpu = 16000000L
91 changes: 73 additions & 18 deletions clock/src/RotaryEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,34 @@

#include <Arduino.h>

#define ROT_A bit(1)
#define ROT_B bit(2)

void enableInterrupts(pin)
void enableInterrupts(uint8_t pin)
{
*digitalPinToPCMSK(pin) |= bit(digitalPinToPCMSKbit(pin)); // enable pin
PCIFR |= bit(digitalPinToPCICRbit(pin)); // clear outstanding interrupt
PCICR |= bit(digitalPinToPCICRbit(pin)); // enable interrupt for group
}

// Source:
// https://github.com/PaulStoffregen/Encoder/blob/master/Encoder.h#L160
const int8_t ENCODER_STATE_TABLE[] = {
// old B old A new B new A
0, // 0 0 0 0
1, // 0 0 0 1
-1, // 0 0 1 0
2, // 0 0 1 1
-1, // 0 1 0 0
0, // 0 1 0 1
-2, // 0 1 1 0
1, // 0 1 1 1
1, // 1 0 0 0
-2, // 1 0 0 1
0, // 1 0 1 0
-1, // 1 0 1 1
2, // 1 1 0 0
-1, // 1 1 0 1
0, // 1 1 1 1
};

// A class for reading incremental rotary encoder rotations
// and reporting changes using pin change interrupts.
// Assumes pins are active low.
Expand All @@ -20,17 +38,17 @@ class _RotaryEncoder
private:
uint8_t _aPin;
uint8_t _bPin;
bool _aState;
bool _aPrevState;
uint8_t _state;
bool _changing;
volatile int8_t _counter;

public:
void begin(aPin, bPin)
void begin(uint8_t aPin, uint8_t bPin)
{
_aPin = aPin;
_bPin = bPin;
_aState = 0;
_aPrevState = 0;
_state = 0;
_changing = false;
_counter = 0;
pinMode(aPin, INPUT);
pinMode(bPin, INPUT);
Expand All @@ -42,17 +60,54 @@ class _RotaryEncoder
interrupts();
}

void tick()
inline void tick()
{
_aPrevState = _aState;
_aState = digitalRead(_aPin) == LOW;
if (_aState != _aPrevState) {
if (digitalRead(_bPin) == LOW) {
_counter++;
} else {
_counter--;
// Serial.print(_state, BIN); Serial.print("\t");
_state = _state >> 2;
uint8_t aState = (digitalRead(_aPin) == LOW);
uint8_t bState = (digitalRead(_bPin) == LOW);
_state |= (aState << 2) | (bState << 3);
// Serial.print(aState); Serial.print("\t");
// Serial.print(bState); Serial.print("\t");
// Serial.println(_state, BIN);
// uint8_t change = _state & 0b00000101;
if (!_changing) {
int8_t v = ENCODER_STATE_TABLE[_state];
if (v != 0) {
_counter += v;
_changing = true;
}
}

if (_changing && aState && bState) {
_changing = false;
}

// if (change != 0 && change != 0b00000101) {
// _counter += ENCODER_STATE_TABLE[_state];
// Serial.print(_state, BIN);
// Serial.print("\t");
// Serial.println(ENCODER_STATE_TABLE[_state]);
// }
// _aPrevState = _aState;
// _aState = digitalRead(_aPin) == LOW;
// Serial.print("a\t");
// Serial.print(_aPrevState);
// Serial.print("\t->\t");
// Serial.print(_aState);
// if (_aState != _aPrevState) {
// bool _bState = digitalRead(_bPin) == LOW;
// Serial.print("\tb\t");
// Serial.print(_bState);
// if (_bState) {
// _counter++;
// Serial.print("\t+1");
// } else {
// _counter--;
// Serial.print("\t-1");
// }
// }
// Serial.println();
}

// Compare the current state to where it was last and return the number
Expand All @@ -63,7 +118,7 @@ class _RotaryEncoder
_counter = 0;
return v;
}
}
};

// Global instance to assign
_RotaryEncoder Knob;
Expand Down
14 changes: 8 additions & 6 deletions clock/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
// Mega
#define CLOCK_PIN 10
#define SUBDIV_PIN 13
// #define KNOB_A_PIN A0
// #define KNOB_B_PIN A0
#define KNOB_A_PIN A14
#define KNOB_B_PIN A15
#define CV_IN_PIN A9
#define A_BUTTON_PIN 8
#define B_BUTTON_PIN 9
Expand Down Expand Up @@ -53,7 +53,7 @@ TapTempo tapTempo;

void setup()
{
// Serial.begin(9600);
Serial.begin(9600);
#ifdef BACKPACK_DISPLAY
display.begin();
#else
Expand Down Expand Up @@ -94,13 +94,15 @@ void setup()
display.tick();
}

int8_t v = 0;

void loop()
{

int8_t knobMotion = knob.readChanges();
int8_t knobMotion = Knob.readChanges();
if (knobMotion != 0)
{

v += knobMotion;
Serial.println(v);
}

// uint16_t tapBpm = tapTempo.tick(aButton.isPressed());
Expand Down

0 comments on commit ef9f7a4

Please sign in to comment.