Skip to content

Instantly share code, notes, and snippets.

@ianthehenry
Created November 27, 2023 03:09
Show Gist options
  • Save ianthehenry/4c75f4931d1dffed0144678b7a470784 to your computer and use it in GitHub Desktop.
Save ianthehenry/4c75f4931d1dffed0144678b7a470784 to your computer and use it in GitHub Desktop.
#include "MIDIUSB.h"
#define STATUS_LED 13
int pressed[72] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int changed[72] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
#define ROW_COUNT 9
#define COL_COUNT 8
int rows[ROW_COUNT] = {5, 6, 9, 10, 11, 12, 20, 19, 18};
int cols[COL_COUNT] = {21, 22, 23, 15, 16, 14, 0, 1};
#define E1 28
#define C2 36
void setup() {
for (int row = 0; row < ROW_COUNT; row++) {
pinMode(rows[row], OUTPUT);
digitalWrite(rows[row], HIGH);
}
for (int col = 0; col < COL_COUNT; col++) {
pinMode(cols[col], INPUT_PULLUP);
}
pinMode(STATUS_LED, OUTPUT);
}
int layout(int ix, int root, int row_step, int col_step) {
int physical_row = ix / COL_COUNT;
int physical_col = ix % COL_COUNT;
int logical_row = physical_row / 3;
int logical_col = COL_COUNT * (2 - physical_row % 3) + physical_col;
return root + logical_row * row_step + logical_col * col_step;
}
int bayan(int ix) {
return layout(ix, E1, 1, 3);
}
int italian(int ix) {
return layout(ix, E1, -1, 3);
}
int janko(int ix) {
return layout(ix, E1, 1, 2);
}
int guitar_m3(int ix) {
return layout(ix, E1 + 24, -3, 1);
}
int guitar_M3(int ix) {
return layout(ix, E1 + 24, -4, 1);
}
int note(int ix) {
return bayan(ix);
}
void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOn);
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
}
void loop() {
int light = 0;
for (int row = 0; row < ROW_COUNT; row++) {
digitalWrite(rows[row], LOW);
for (int col = 0; col < COL_COUNT; col++) {
int i = row * COL_COUNT + col;
int was = pressed[i];
int now = digitalRead(cols[col]) == LOW;
pressed[i] = now;
changed[i] = was != now;
}
digitalWrite(rows[row], HIGH);
}
for (int i = 0; i < ROW_COUNT * COL_COUNT; i++) {
if (pressed[i]) {
light = 1;
}
if (changed[i]) {
if (pressed[i]) {
noteOn(0, note(i), 64);
} else {
noteOff(0, note(i), 64);
}
}
}
if (light) {
digitalWrite(STATUS_LED, HIGH);
} else {
digitalWrite(STATUS_LED, LOW);
}
MidiUSB.flush();
delay(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment