|
| 1 | +// EEPROM replacement Lib find in "Manage Libraries"and here https://github.com/cmaglie/FlashStorage |
| 2 | +/* |
| 3 | +
|
| 4 | + Testplan: |
| 5 | + - Midi Speed |
| 6 | + - Connect both Din + USB and send a lot of data |
| 7 | + - Learn simple (single press button - root node + chromatic up |
| 8 | + - Learn advanced (double press button - assign all notes in sequence |
| 9 | + - |
| 10 | +
|
| 11 | +*/ |
| 12 | + |
| 13 | +#include <FlashAsEEPROM.h> |
| 14 | +#include <MIDI.h> |
| 15 | +#include <MIDIUSB.h> |
| 16 | +#include <SPI.h> |
| 17 | +#include <OneButton.h> |
| 18 | + |
| 19 | +// constants |
| 20 | +const int OUTPUT_PINS_COUNT = 12; //= sizeof(OUTPUT_PINS) / sizeof(OUTPUT_PINS[0]); |
| 21 | +const int LEARN_MODE_PIN = 38; // pin for the learn mode switch |
| 22 | +const int SHIFT_REGISTER_ENABLE = 27; // Output enable for shiftregister ic |
| 23 | +const int ACTIVITY_LED = 13; // activity led is still on D13 which is connected to PA17 > which means Pin 9 on MKRZero |
| 24 | + |
| 25 | +// NV Data |
| 26 | +typedef struct { |
| 27 | + byte midiChannels[12]; // 1-16 or 0 for any |
| 28 | + byte midiPins[12]; // midi notes |
| 29 | + byte alignfiller[8]; // for eeprom support |
| 30 | +} dataCFG; |
| 31 | +dataCFG nvData; |
| 32 | + |
| 33 | +FlashStorage(nvStore, dataCFG); |
| 34 | + |
| 35 | +#include "solenoidSPI.h" |
| 36 | +SOLSPI solenoids(&SPI, 30); // PB22 Pin in new layout is Pin14 on MKRZero |
| 37 | + |
| 38 | +#include "dadaStatusLED.h" |
| 39 | +dadaStatusLED statusLED(ACTIVITY_LED); // led controller |
| 40 | + |
| 41 | +#include "dadaMidiLearn.h" // learn class |
| 42 | + |
| 43 | +// Objects |
| 44 | +OneButton button(LEARN_MODE_PIN, true); // 38 Pin in new layout is Pin 38 used for SD Card on MKRZero |
| 45 | + |
| 46 | +MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, midi2); // DIN Midi Stuff |
| 47 | +dadaMidiLearn midiLearn(&nvData); // lern class + load/save from eeprom |
| 48 | + |
| 49 | +void setup() { |
| 50 | + Serial1.begin(31250); // set up MIDI baudrate |
| 51 | + pinMode(SHIFT_REGISTER_ENABLE, OUTPUT); // enable Shiftregister |
| 52 | + digitalWrite(SHIFT_REGISTER_ENABLE, LOW); |
| 53 | + pinMode(ACTIVITY_LED, OUTPUT); // pin leds to output |
| 54 | + pinMode(LEARN_MODE_PIN, INPUT_PULLUP); |
| 55 | + button.attachDoubleClick(doubleclick); // register button for learnmodes |
| 56 | + button.attachClick(singleclick); // register button for learnmodes |
| 57 | + solenoids.begin(); // start shiftregister |
| 58 | + |
| 59 | + midi2.setHandleNoteOn(handleNoteOn); // add Handler for Din MIDI |
| 60 | + midi2.setHandleNoteOff(handleNoteOff); |
| 61 | + midi2.begin(MIDI_CHANNEL_OMNI); |
| 62 | + // init(); |
| 63 | + statusLED.blink(20, 30, 32); |
| 64 | +} |
| 65 | + |
| 66 | +void loop() { |
| 67 | + midi2.read(); |
| 68 | + button.tick(); |
| 69 | + statusLED.tick(); |
| 70 | + |
| 71 | + // handle blinking port on learning in advanced mode |
| 72 | + if(midiLearn.active) { |
| 73 | + if(midiLearn.mode==1) { |
| 74 | + solenoids.singlePin(midiLearn.counter,statusLED._state ); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + // now handle usb midi and merge with DinMidi callbacks |
| 82 | + midiEventPacket_t rx; |
| 83 | + do { |
| 84 | + rx = MidiUSB.read(); |
| 85 | + if (rx.header != 0) { |
| 86 | + switch (rx.byte1 & 0xF0) { |
| 87 | + case 0x90: // note on |
| 88 | + if (rx.byte3 != 0) |
| 89 | + handleNoteOn(1 + (rx.byte1 & 0xF), rx.byte2, rx.byte3); |
| 90 | + else |
| 91 | + handleNoteOff(1 + (rx.byte1 & 0xF), rx.byte2, rx.byte3); |
| 92 | + break; |
| 93 | + case 0x80: // note off |
| 94 | + handleNoteOff(1 + (rx.byte1 & 0xF), rx.byte2, rx.byte3); |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + } while (rx.header != 0); |
| 99 | +} |
| 100 | + |
| 101 | +/************************************************************************************************************************************************/ |
| 102 | +/************************************************************************************************************************************************/ |
| 103 | +/************************************************************************************************************************************************/ |
| 104 | +/************************************************************************************************************************************************/ |
| 105 | +/************************************************************************************************************************************************/ |
| 106 | +/************************************************************************************************************************************************/ |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +void handleNoteOn(byte channel, byte note, byte velocity) { |
| 111 | + midiLearn.noteOn(channel, note, velocity); |
| 112 | + |
| 113 | + if (midiLearn.active) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + statusLED.blink(1, 2, 1); |
| 118 | + |
| 119 | + |
| 120 | + for (int i = 0 ; i < 12 ; i++) { |
| 121 | + if (nvData.midiPins[i] == note) { |
| 122 | + if (nvData.midiChannels[i] == channel || nvData.midiChannels[i] == 0) { |
| 123 | + solenoids.setOutput(i); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | +} |
| 129 | + |
| 130 | +void handleNoteOff(byte channel, byte note, byte velocity) { |
| 131 | + midiLearn.noteOff(channel, note, velocity); |
| 132 | + |
| 133 | + if (midiLearn.active) { |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + statusLED.blink(1, 2, 1); |
| 138 | + |
| 139 | + for (int i = 0 ; i < 12 ; i++) { |
| 140 | + if (nvData.midiPins[i] == note) { |
| 141 | + if (nvData.midiChannels[i] == channel || nvData.midiChannels[i] == 0) { |
| 142 | + solenoids.clearOutput(i); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// Advanced Learn |
| 149 | +void doubleclick() { |
| 150 | + statusLED.blink(20, 20, -1); // LED Settings (On Time, Off Time, Count) |
| 151 | + midiLearn.begin(1); |
| 152 | +} |
| 153 | + |
| 154 | +// Simple Learn |
| 155 | +void singleclick(void) { |
| 156 | + statusLED.blink(10, 0, -1); // LED Settings (On Time, Off Time, Count) |
| 157 | + midiLearn.begin(0); |
| 158 | +} |
| 159 | + |
| 160 | + |
0 commit comments