|
| 1 | +#include <Control_Surface.h> |
| 2 | +#include <AH/Arduino-Wrapper.h> |
| 3 | +#include <AH/Hardware/ExtendedInputOutput/ExtendedInputOutput.hpp> |
| 4 | +#include <AH/Hardware/RegisterEncoders.hpp> |
| 5 | +#include <Adafruit_MCP23X17.h> |
| 6 | +#include <Adafruit_NeoPixel.h> |
| 7 | + |
| 8 | +// MCP configuration |
| 9 | +const int MCP_PIN_INTA = 7; |
| 10 | +const int MCP_PIN_INTB = 7; |
| 11 | + |
| 12 | +// Encoder pins [A, B] |
| 13 | +const int ENCODER_COUNT = 4; |
| 14 | +const int ENCODER_PINS[ENCODER_COUNT][2] = { |
| 15 | + {0, 1}, |
| 16 | + {2, 3}, |
| 17 | + {4, 5}, |
| 18 | + {6, 7}, |
| 19 | +}; |
| 20 | + |
| 21 | +// Key matrix |
| 22 | +const int MATRIX_ROW_COUNT = 3; |
| 23 | +const int MATRIX_COL_COUNT = 4; |
| 24 | +const int MATRIX_ROWS[MATRIX_ROW_COUNT] = { |
| 25 | + 8, |
| 26 | + 9, |
| 27 | + 10, |
| 28 | +}; |
| 29 | +const int MATRIX_COLS[MATRIX_COL_COUNT] = { |
| 30 | + 11, |
| 31 | + 12, |
| 32 | + 13, |
| 33 | + 14, |
| 34 | +}; |
| 35 | +const MIDIAddress BUTTON_ADDRESSES[3][4] { |
| 36 | + { |
| 37 | + {MIDI_Notes::A[0], Channel_1}, |
| 38 | + {MIDI_Notes::B[0], Channel_1}, |
| 39 | + {MIDI_Notes::C[0], Channel_1}, |
| 40 | + {MIDI_Notes::D[0], Channel_1}, |
| 41 | + }, |
| 42 | + { |
| 43 | + {MIDI_Notes::E[0], Channel_1}, |
| 44 | + {MIDI_Notes::F[0], Channel_1}, |
| 45 | + {MIDI_Notes::G[0], Channel_1}, |
| 46 | + {MIDI_Notes::A[1], Channel_1}, |
| 47 | + }, |
| 48 | + { |
| 49 | + {MIDI_Notes::B[1], Channel_1}, |
| 50 | + {MIDI_Notes::C[1], Channel_1}, |
| 51 | + {MIDI_Notes::D[1], Channel_1}, |
| 52 | + {MIDI_Notes::E[1], Channel_1}, |
| 53 | + }, |
| 54 | +}; |
| 55 | + |
| 56 | +// Encoder switch pins |
| 57 | +const int ENCODER_SW_PINS[4] = { |
| 58 | + 0, |
| 59 | + 1, |
| 60 | + 2, |
| 61 | + 3, |
| 62 | +}; |
| 63 | + |
| 64 | +// LED configuration |
| 65 | +const int LED_COUNT = 16; |
| 66 | +const int LED_PIN = 6; |
| 67 | + |
| 68 | +////////// |
| 69 | + |
| 70 | +// MIDI interface |
| 71 | +USBMIDI_Interface midi; |
| 72 | + |
| 73 | +// Hardware objects |
| 74 | +Adafruit_MCP23X17 mcp; |
| 75 | +Adafruit_NeoPixel pixels(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); |
| 76 | + |
| 77 | +// Encoders |
| 78 | +using RegisterEncoderType = RegisterEncoders<uint16_t, 4, int32_t, false>; |
| 79 | + |
| 80 | +RegisterEncoderType encs; |
| 81 | + |
| 82 | +void setup() { |
| 83 | + // Set up LEDs |
| 84 | + pixels.begin(); |
| 85 | + |
| 86 | + // Set up the MCP |
| 87 | + if (!mcp.begin_I2C()) { |
| 88 | + // If I2C setup failed, flash the first knob red forever. |
| 89 | + while (true) { |
| 90 | + pixels.setPixelColor(0, pixels.Color(255, 0, 0)); |
| 91 | + pixels.show(); |
| 92 | + delay(1000); |
| 93 | + |
| 94 | + pixels.setPixelColor(0, pixels.Color(0, 0, 0)); |
| 95 | + pixels.show(); |
| 96 | + delay(1000); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + mcp.setupInterrupts(false, false, LOW); |
| 101 | + |
| 102 | + // Set up the MCP interrupt pins |
| 103 | + pinMode(MCP_PIN_INTA, INPUT); |
| 104 | + pinMode(MCP_PIN_INTB, INPUT); |
| 105 | + |
| 106 | + // Set up the encoder pins |
| 107 | + for (int encoderIndex = 0; encoderIndex < ENCODER_COUNT; encoderIndex++) { |
| 108 | + for (int pinIndex = 0; pinIndex <= 1; pinIndex++) { |
| 109 | + int pin = ENCODER_PINS[encoderIndex][pinIndex]; |
| 110 | + mcp.pinMode(pin, INPUT_PULLUP); |
| 111 | + mcp.setupInterruptPin(pin, CHANGE); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +void loop() { |
| 117 | + // Read from encoders |
| 118 | + if (!digitalRead(MCP_PIN_INTA)) { |
| 119 | + encs.update(mcp.getCapturedInterrupt()); |
| 120 | + delay(100); // Debounce |
| 121 | + mcp.clearInterrupts(); |
| 122 | + } |
| 123 | + |
| 124 | + for (uint8_t row = 0; row < MATRIX_ROW_COUNT; row++) { |
| 125 | + mcp.digitalWrite(MATRIX_ROWS[row], HIGH); |
| 126 | + |
| 127 | + if (!digitalRead(MCP_PIN_INTB)) { |
| 128 | + uint16_t pins = mcp.getCapturedInterrupt(); |
| 129 | + |
| 130 | + for (uint8_t col = 0; col < MATRIX_COL_COUNT; col++) { |
| 131 | + if (pins & (1 << MATRIX_COLS[col])) { |
| 132 | + midi.sendNoteOn(BUTTON_ADDRESSES[row][col], 0x7F); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + mcp.digitalWrite(row, LOW); |
| 138 | + mcp.clearInterrupts(); |
| 139 | + } |
| 140 | +} |
0 commit comments