Skip to content

Instantly share code, notes, and snippets.

@wesen
Created June 17, 2017 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesen/9430098d4aadeb38e64c9439d8bed598 to your computer and use it in GitHub Desktop.
Save wesen/9430098d4aadeb38e64c9439d8bed598 to your computer and use it in GitHub Desktop.
#include <inttypes.h>
#include <Encoder.h>
const int ShiftPin = 0; // This is the button to activate Alternate action for buttons and 8 first encoders
const int ledPin = 32; // This LED when HIGH shows the Shift Button is pressed
struct EncoderValue {
EncoderValue(uint8_t ccParameter) :
value(0)
, ccParameter(ccParameter) {
}
uint8_t value;
const uint8_t ccParameter;
void update(uint8_t value) {
if (value != this->value) {
usbMIDI.sendControlChange(ccParameter, value, 1);
this->value = value;
}
}
};
struct MidiEncoder {
MidiEncoder(int a, int b, uint8_t ccParameter, uint8_t shiftCCParameter) :
encoder(a, b)
, value(ccParameter)
, shiftValue(shiftCCParameter) {
}
void update(bool isShiftHeld) {
const int value = constrain(encoder.read(), 0, 127);
encoder.write(value);
if (isShiftHeld) {
shiftvalue.update(value);
} else {
value.update(value);
}
}
Encoder encoder;
EncoderValue value;
EncoderValue shiftValue;
};
MidiEncoder midiEncoders[16] = {
Encoder(1, 2, 66, 74),
Encoder(3, 4, 67, 75),
Encoder(5, 6, 68, 76),
Encoder(7, 8, 69, 77),
Encoder(9, 10, 70, 78),
Encoder(11, 12, 71, 79),
Encoder(14, 15, 72, 80),
Encoder(16, 17, 73, 81),
};
void setup() {
pinMode(ShiftPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
const int ShiftState = digitalRead(ShiftPin);
delay(10);
digitalWrite(ledPin, ShiftState);
for (int i = 0; i < 8; i++) {
const bool isShiftHeld = ShiftState == HIGH;
midiEncoders[i].update(isShiftHeld);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment