mirror of
https://github.com/supleed2/ELEC60013-ES-CW2.git
synced 2024-12-22 21:55:50 +00:00
Knob Driver to be fixed
This commit is contained in:
parent
d5f583cefa
commit
c51638ee4f
19
lib/knob/knob
Normal file
19
lib/knob/knob
Normal file
|
@ -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
|
48
lib/knob/knob.cpp
Normal file
48
lib/knob/knob.cpp
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
#include <knob>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
#include <STM32FreeRTOS.h>
|
#include <STM32FreeRTOS.h>
|
||||||
#include <U8g2lib.h>
|
#include <U8g2lib.h>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <knob>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
volatile std::atomic<int32_t> currentStepSize;
|
volatile std::atomic<int32_t> currentStepSize;
|
||||||
|
@ -48,6 +49,7 @@ const int HKOE_BIT = 6;
|
||||||
#pragma endregion
|
#pragma endregion
|
||||||
|
|
||||||
U8G2_SSD1305_128X32_NONAME_F_HW_I2C u8g2(U8G2_R0); // Display driver object
|
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
|
// Function to set outputs using key matrix
|
||||||
void setOutMuxBit(const uint8_t bitIdx, const bool value) {
|
void setOutMuxBit(const uint8_t bitIdx, const bool value) {
|
||||||
|
@ -95,7 +97,7 @@ uint16_t getTopKey() {
|
||||||
void sampleISR() {
|
void sampleISR() {
|
||||||
static int32_t phaseAcc = 0;
|
static int32_t phaseAcc = 0;
|
||||||
phaseAcc += currentStepSize;
|
phaseAcc += currentStepSize;
|
||||||
int32_t Vout = phaseAcc >> 24;
|
int32_t Vout = phaseAcc >> (32 - K3.getRotation() / 2);
|
||||||
analogWrite(OUTR_PIN, Vout + 128);
|
analogWrite(OUTR_PIN, Vout + 128);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,12 +106,13 @@ void scanKeysTask(void *pvParameters) {
|
||||||
TickType_t xLastWakeTime = xTaskGetTickCount();
|
TickType_t xLastWakeTime = xTaskGetTickCount();
|
||||||
while (1) {
|
while (1) {
|
||||||
vTaskDelayUntil(&xLastWakeTime, xFrequency);
|
vTaskDelayUntil(&xLastWakeTime, xFrequency);
|
||||||
for (uint8_t i = 0; i < 3; i++) {
|
for (uint8_t i = 0; i < 7; i++) {
|
||||||
setRow(i);
|
setRow(i);
|
||||||
delayMicroseconds(3);
|
delayMicroseconds(3);
|
||||||
keyArray[i] = readCols();
|
keyArray[i] = readCols();
|
||||||
}
|
}
|
||||||
currentStepSize = notes[getTopKey()].stepSize; // Atomic Store
|
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.print(keyArray[i], HEX);
|
||||||
}
|
}
|
||||||
u8g2.drawXBM(118, 0, 10, 10, icon_bits);
|
u8g2.drawXBM(118, 0, 10, 10, icon_bits);
|
||||||
|
u8g2.setCursor(2, 30);
|
||||||
|
u8g2.print(K3.getRotation());
|
||||||
u8g2.sendBuffer(); // transfer internal memory to the display
|
u8g2.sendBuffer(); // transfer internal memory to the display
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue