From a9f8cef4682b2d13e19faaeef4176c0964a97577 Mon Sep 17 00:00:00 2001 From: Michael Schuetze Date: Sat, 14 Oct 2023 13:46:03 -0400 Subject: [PATCH 1/2] Switched rotation bitwise int to int16 for boards with larger int storage --- Brixcer_Box/src/KnobControl.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Brixcer_Box/src/KnobControl.cpp b/Brixcer_Box/src/KnobControl.cpp index d7a71a5..3861bf0 100644 --- a/Brixcer_Box/src/KnobControl.cpp +++ b/Brixcer_Box/src/KnobControl.cpp @@ -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 From b9faff92d042486f18919582a2c3c025947735ac Mon Sep 17 00:00:00 2001 From: Michael Schuetze Date: Sat, 14 Oct 2023 13:46:50 -0400 Subject: [PATCH 2/2] Coagulated rotary encoder rotations --- Brixcer_Box/include/KnobControl.h | 7 +++++++ Brixcer_Box/src/KnobControl.cpp | 31 +++++++++++++++++++++++++++---- Brixcer_Box/src/main.cpp | 4 ++-- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/Brixcer_Box/include/KnobControl.h b/Brixcer_Box/include/KnobControl.h index a4bc276..9b2821e 100644 --- a/Brixcer_Box/include/KnobControl.h +++ b/Brixcer_Box/include/KnobControl.h @@ -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(); }; diff --git a/Brixcer_Box/src/KnobControl.cpp b/Brixcer_Box/src/KnobControl.cpp index 3861bf0..c5cd58f 100644 --- a/Brixcer_Box/src/KnobControl.cpp +++ b/Brixcer_Box/src/KnobControl.cpp @@ -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)) { diff --git a/Brixcer_Box/src/main.cpp b/Brixcer_Box/src/main.cpp index 1e92ee6..5d514f2 100644 --- a/Brixcer_Box/src/main.cpp +++ b/Brixcer_Box/src/main.cpp @@ -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 @@ -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); }