From c51638ee4fd86273ccd71c7278bffb372526f1d8 Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Wed, 23 Mar 2022 18:23:26 +0000 Subject: [PATCH] Knob Driver to be fixed --- lib/knob/knob | 19 +++++++++++++++++++ lib/knob/knob.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 9 +++++++-- 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 lib/knob/knob create mode 100644 lib/knob/knob.cpp diff --git a/lib/knob/knob b/lib/knob/knob new file mode 100644 index 0000000..9c035a1 --- /dev/null +++ b/lib/knob/knob @@ -0,0 +1,19 @@ +#ifndef KNOB_H +#define KNOB_H + +class Knob { + private: + int rotation; + int minimum, maximum; + bool A, B; + bool rotPlusOnePrev, rotMinOnePrev; + + public: + Knob(int minimum, int max); + + int getRotation(); + + void updateRotation(bool ANew, bool BNew); +}; + +#endif \ No newline at end of file diff --git a/lib/knob/knob.cpp b/lib/knob/knob.cpp new file mode 100644 index 0000000..25bf475 --- /dev/null +++ b/lib/knob/knob.cpp @@ -0,0 +1,48 @@ +#include + +Knob::Knob(int minimum, int maximum) { + Knob::minimum = minimum; + Knob::maximum = maximum; + Knob::A = false; + Knob::B = false; + Knob::rotPlusOnePrev = false; + Knob::rotMinOnePrev = false; + Knob::rotation = 0; +} + +int Knob::getRotation() { + return Knob::rotation; +}; + +void Knob::updateRotation(bool ANew, bool BNew) { + bool rotPlusOneNew = (!B && !A && !BNew && ANew) || + (!B && A && BNew && ANew) || + (B && !A && !BNew && !ANew) || + (B && A && BNew && !ANew); + + bool rotMinOneNew = (!B && !A && BNew && !ANew) || + (!B && A && !BNew && !ANew) || + (B && !A && BNew && ANew) || + (B && A && !BNew && ANew); + + bool impossibleState = (!B && !A && BNew && ANew) || + (!B && A && BNew && !ANew) || + (B && !A && !BNew && ANew) || + (B && A && !BNew && !ANew); + + if (rotPlusOneNew || (impossibleState && rotPlusOnePrev)) + rotation += 2; + if (rotMinOneNew || (impossibleState && rotMinOnePrev)) + rotation -= 2; + if (rotation < minimum) + rotation = minimum; + if (rotation > maximum) + rotation = maximum; + + A = ANew; + B = BNew; + if (!impossibleState) { + rotPlusOnePrev = rotPlusOneNew; + rotMinOnePrev = rotMinOneNew; + } +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 580eba1..b796a80 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include volatile std::atomic currentStepSize; @@ -48,6 +49,7 @@ const int HKOE_BIT = 6; #pragma endregion U8G2_SSD1305_128X32_NONAME_F_HW_I2C u8g2(U8G2_R0); // Display driver object +Knob K3 = Knob(0, 16); // Knob driver object // Function to set outputs using key matrix void setOutMuxBit(const uint8_t bitIdx, const bool value) { @@ -95,7 +97,7 @@ uint16_t getTopKey() { void sampleISR() { static int32_t phaseAcc = 0; phaseAcc += currentStepSize; - int32_t Vout = phaseAcc >> 24; + int32_t Vout = phaseAcc >> (32 - K3.getRotation() / 2); analogWrite(OUTR_PIN, Vout + 128); } @@ -104,12 +106,13 @@ void scanKeysTask(void *pvParameters) { TickType_t xLastWakeTime = xTaskGetTickCount(); while (1) { vTaskDelayUntil(&xLastWakeTime, xFrequency); - for (uint8_t i = 0; i < 3; i++) { + for (uint8_t i = 0; i < 7; i++) { setRow(i); delayMicroseconds(3); keyArray[i] = readCols(); } currentStepSize = notes[getTopKey()].stepSize; // Atomic Store + K3.updateRotation(keyArray[3] & 0x1, keyArray[3] & 0x2); } } @@ -127,6 +130,8 @@ void displayUpdateTask(void *pvParameters) { u8g2.print(keyArray[i], HEX); } u8g2.drawXBM(118, 0, 10, 10, icon_bits); + u8g2.setCursor(2, 30); + u8g2.print(K3.getRotation()); u8g2.sendBuffer(); // transfer internal memory to the display } }