Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rotation calculation improvements #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Brixcer_Box/include/KnobControl.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
class KnobControl {

// Interval of time to count the number of rotations
static const int ROTATION_COUNT_INTERVAL_MS = 200;

int knobPinA;
int knobPinB;
int buttonPin;

int state;

int rotationCounter;
unsigned long timeOfLastUpdate;

public:
KnobControl(int knobPinA, int knobPinB, int buttonPin);
int readKnobValues();
int getNumberOfRotations();
int readButtonValue();
};
35 changes: 29 additions & 6 deletions Brixcer_Box/src/KnobControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ enum position {
END_CCW = 5,
};

int CW_ROT = 0x10;
int CCW_ROT = 0xFFF0;
int16_t CW_ROT = 0x10;
int16_t CCW_ROT = 0xFFF0;

const int transitions[6][4] = {
// Starting Position
Expand All @@ -38,20 +38,43 @@ KnobControl::KnobControl(int knobPinA, int knobPinB, int buttonPin) {
pinMode(this->knobPinB, INPUT);
pinMode(this->buttonPin, INPUT_PULLUP);

this->state = START;
state = START;
rotationCounter = 0;
}

int KnobControl::readKnobValues() {
// Grab state of input pins.
// Read the current value of the pins
int pinstate = (digitalRead(knobPinB) << 1) | digitalRead(knobPinA);

// Determine new state from the pins and state table.
// Determine the current state from the transition table
state = transitions[state & 0x7][pinstate];

// Return emit bits, ie the generated event.
// Return the direction of rotation (0: rotation hasn't completed, 1: CW, -1: CCW)
return state >> 4;
}

int KnobControl::getNumberOfRotations() {
// Get the direction of the latest rotation
int direction = this->readKnobValues();

// Add the rotation to the directional rotation count
rotationCounter += direction;

// If the counting period has ended, send the current rotation count
if (millis() - timeOfLastUpdate > ROTATION_COUNT_INTERVAL_MS) {
// Get the number of directional rotations
int rotations = rotationCounter;

// Reset rotation counter
rotationCounter = 0;
timeOfLastUpdate = millis();

return rotations;
}

return 0;
}

int KnobControl::readButtonValue() {
// Check if this button was the activated one
if (!digitalRead(buttonPin)) {
Expand Down
4 changes: 2 additions & 2 deletions Brixcer_Box/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ LedControl* leds = (LedControl*) malloc(sizeof(LedControl) * KnobCount);
//MediaControl mediaControl(PlayPause, Previous, Next);

// Define array for Serial input
const int numChars = 33; // 32 chars + 1 for `\0`
const int numChars = 33; // 32 chars + 1 for \0
char receivedChars[numChars];

// Define Serial interface
Expand Down Expand Up @@ -162,7 +162,7 @@ void buttonClick() {

void knobPinTrigger() {
for (int i = 0; i < KnobCount; i++) {
int value = knobs[i].readKnobValues();
int value = knobs[i].getNumberOfRotations();
if (value != 0) {
sendData(i, 0, value);
}
Expand Down