#define WOKWI // For use with keyboard.h just make this line a comment -> //#define WOKWI
#ifdef WOKWI
#define KEY_TAB "KEY_TAB"
#define KEY_LEFT_SHIFT "KEY_LEFT_SHIFT"
#define KEY_RETURN "KEY_RETURN"
struct kbt {
void begin();
void write(String aKey);
void press(String aKey);
void release(String aKey);
} Keyboard;
void kbt::begin() {}
void kbt::write(String aKey) {
Serial.println(aKey + "_press&released");
}
void kbt::press(String aKey) {
Serial.println(aKey + "_pressed");
}
void kbt::release(String aKey) {
Serial.println(aKey + "_released");
}
#else
#include <Keyboard.h>
#endif
const int encoderClk = 2; // Rotary encoder pin 1
const int encoderDT = 3; // Rotary encoder pin 2
const int buttonPin = 4; // Rotary encoder button pin
int section = 0;
const int numSections = 7;
const char* sectionNames[] = {"Text Entry", "Types", "Instruments", "Styles", "Banks", "User", "Blank Section"};
enum Direction {NONE, LEFT, RIGHT};
struct encoderType {
byte buttonPin;
byte encoderClk;
byte encoderDT;
boolean buttonReleased();
Direction turned();
void init(byte bPin, byte ClkPin, byte DTPin);
};
void encoderType::init(byte bPin, byte ClkPin, byte DTPin) {
encoderType::encoderClk = ClkPin;
encoderType::encoderDT = DTPin;
encoderType::buttonPin = bPin;
pinMode(ClkPin, INPUT_PULLUP);
pinMode(DTPin, INPUT_PULLUP);
pinMode(bPin, INPUT_PULLUP);
}
boolean encoderType::buttonReleased() {
static unsigned lastChange = 0;
static byte state = HIGH;
static byte lastState = HIGH;
byte actState = digitalRead(encoderType::buttonPin);
if (actState != lastState) {
lastChange = millis();
lastState = actState;
}
if (state != lastState && millis() - lastChange > 30) {
state = lastState;
if (state) return true;
}
return false;
}
Direction encoderType::turned() {
static byte lastClk = HIGH;
Direction returnValue = NONE;
byte newClk = digitalRead(encoderType::encoderClk);
if (newClk != lastClk) {
// There was a change on the CLK pin
lastClk = newClk;
byte dtValue = digitalRead(encoderType::encoderDT);
if (newClk == LOW && dtValue == HIGH) { // clockwise
returnValue = RIGHT;
}
if (newClk == LOW && dtValue == LOW) { // counterclockwise
returnValue = LEFT;
}
}
return returnValue;
};
encoderType myEncoder;
void setup() {
Serial.begin(115200);
myEncoder.init(buttonPin, encoderClk, encoderDT);
Keyboard.begin();
}
void loop() {
Direction d = myEncoder.turned();
if (d == LEFT) DecreaseSection();
if (d == RIGHT) IncreaseSection();
if (myEncoder.buttonReleased()) SendReturn();
}
void SendReturn() {
Keyboard.write(KEY_RETURN);
}
void IncreaseSection() {
section++;
if (section >= numSections) {
section = 0;
}
Serial.println("section++\t" + String(section));
if (section == 6) {
// Send 5 tabs for Blank Section
for (int i = 0; i < 5; i++) {
Keyboard.write(KEY_TAB);
}
} else {
Keyboard.write(KEY_TAB);
}
Serial.print("Section: ");
Serial.println(sectionNames[section]);
}
void DecreaseSection() {
section--;
if (section < 0) {
section = numSections - 1;
}
Serial.println("section--\t" + String(section));
if (section == 6) {
// Send 5 shift+tabs for Blank Section
Keyboard.press(KEY_LEFT_SHIFT);
for (int i = 0; i < 5; i++) {
Keyboard.write(KEY_TAB);
}
Keyboard.release(KEY_LEFT_SHIFT);
} else {
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.write(KEY_TAB);
Keyboard.release(KEY_LEFT_SHIFT);
}
Serial.print("Section: ");
Serial.println(sectionNames[section]);
}