-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
607 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// EEPROM replacement Lib find in "Manage Libraries"and here https://github.com/cmaglie/FlashStorage | ||
/* | ||
Testplan: | ||
- Midi Speed | ||
- Connect both Din + USB and send a lot of data | ||
- Learn simple (single press button - root node + chromatic up | ||
- Learn advanced (double press button - assign all notes in sequence | ||
- | ||
*/ | ||
|
||
#include <FlashAsEEPROM.h> | ||
#include <MIDI.h> | ||
#include <MIDIUSB.h> | ||
#include <SPI.h> | ||
#include <OneButton.h> | ||
|
||
// constants | ||
const int OUTPUT_PINS_COUNT = 12; //= sizeof(OUTPUT_PINS) / sizeof(OUTPUT_PINS[0]); | ||
const int LEARN_MODE_PIN = 38; // pin for the learn mode switch | ||
const int SHIFT_REGISTER_ENABLE = 27; // Output enable for shiftregister ic | ||
const int ACTIVITY_LED = 13; // activity led is still on D13 which is connected to PA17 > which means Pin 9 on MKRZero | ||
|
||
// NV Data | ||
typedef struct { | ||
byte midiChannels[12]; // 1-16 or 0 for any | ||
byte midiPins[12]; // midi notes | ||
byte alignfiller[8]; // for eeprom support | ||
} dataCFG; | ||
dataCFG nvData; | ||
|
||
FlashStorage(nvStore, dataCFG); | ||
|
||
#include "solenoidSPI.h" | ||
SOLSPI solenoids(&SPI, 30); // PB22 Pin in new layout is Pin14 on MKRZero | ||
|
||
#include "dadaStatusLED.h" | ||
dadaStatusLED statusLED(ACTIVITY_LED); // led controller | ||
|
||
#include "dadaMidiLearn.h" // learn class | ||
|
||
// Objects | ||
OneButton button(LEARN_MODE_PIN, true); // 38 Pin in new layout is Pin 38 used for SD Card on MKRZero | ||
|
||
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, midi2); // DIN Midi Stuff | ||
dadaMidiLearn midiLearn(&nvData); // lern class + load/save from eeprom | ||
|
||
void setup() { | ||
Serial1.begin(31250); // set up MIDI baudrate | ||
pinMode(SHIFT_REGISTER_ENABLE, OUTPUT); // enable Shiftregister | ||
digitalWrite(SHIFT_REGISTER_ENABLE, LOW); | ||
pinMode(ACTIVITY_LED, OUTPUT); // pin leds to output | ||
pinMode(LEARN_MODE_PIN, INPUT_PULLUP); | ||
button.attachDoubleClick(doubleclick); // register button for learnmodes | ||
button.attachClick(singleclick); // register button for learnmodes | ||
solenoids.begin(); // start shiftregister | ||
|
||
midi2.setHandleNoteOn(handleNoteOn); // add Handler for Din MIDI | ||
midi2.setHandleNoteOff(handleNoteOff); | ||
midi2.begin(MIDI_CHANNEL_OMNI); | ||
// init(); | ||
statusLED.blink(20, 30, 32); | ||
} | ||
|
||
void loop() { | ||
midi2.read(); | ||
button.tick(); | ||
statusLED.tick(); | ||
|
||
// handle blinking port on learning in advanced mode | ||
if(midiLearn.active) { | ||
if(midiLearn.mode==1) { | ||
solenoids.singlePin(midiLearn.counter,statusLED._state ); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
// now handle usb midi and merge with DinMidi callbacks | ||
midiEventPacket_t rx; | ||
do { | ||
rx = MidiUSB.read(); | ||
if (rx.header != 0) { | ||
switch (rx.byte1 & 0xF0) { | ||
case 0x90: // note on | ||
if (rx.byte3 != 0) | ||
handleNoteOn(1 + (rx.byte1 & 0xF), rx.byte2, rx.byte3); | ||
else | ||
handleNoteOff(1 + (rx.byte1 & 0xF), rx.byte2, rx.byte3); | ||
break; | ||
case 0x80: // note off | ||
handleNoteOff(1 + (rx.byte1 & 0xF), rx.byte2, rx.byte3); | ||
break; | ||
} | ||
} | ||
} while (rx.header != 0); | ||
} | ||
|
||
/************************************************************************************************************************************************/ | ||
/************************************************************************************************************************************************/ | ||
/************************************************************************************************************************************************/ | ||
/************************************************************************************************************************************************/ | ||
/************************************************************************************************************************************************/ | ||
/************************************************************************************************************************************************/ | ||
|
||
|
||
|
||
void handleNoteOn(byte channel, byte note, byte velocity) { | ||
midiLearn.noteOn(channel, note, velocity); | ||
|
||
if (midiLearn.active) { | ||
return; | ||
} | ||
|
||
statusLED.blink(1, 2, 1); | ||
|
||
|
||
for (int i = 0 ; i < 12 ; i++) { | ||
if (nvData.midiPins[i] == note) { | ||
if (nvData.midiChannels[i] == channel || nvData.midiChannels[i] == 0) { | ||
solenoids.setOutput(i); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
void handleNoteOff(byte channel, byte note, byte velocity) { | ||
midiLearn.noteOff(channel, note, velocity); | ||
|
||
if (midiLearn.active) { | ||
return; | ||
} | ||
|
||
statusLED.blink(1, 2, 1); | ||
|
||
for (int i = 0 ; i < 12 ; i++) { | ||
if (nvData.midiPins[i] == note) { | ||
if (nvData.midiChannels[i] == channel || nvData.midiChannels[i] == 0) { | ||
solenoids.clearOutput(i); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Advanced Learn | ||
void doubleclick() { | ||
statusLED.blink(20, 20, -1); // LED Settings (On Time, Off Time, Count) | ||
midiLearn.begin(1); | ||
} | ||
|
||
// Simple Learn | ||
void singleclick(void) { | ||
statusLED.blink(10, 0, -1); // LED Settings (On Time, Off Time, Count) | ||
midiLearn.begin(0); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
Copyright (c) 2017, DADAMACHINES | ||
Author: Sven Braun | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without modification, | ||
are permitted provided that the following conditions are met: | ||
1. Redistributions of source code must retain the above copyright notice, | ||
this list of conditions and the following disclaimer. | ||
2. Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
3. Neither the name of DADAMACHINES nor the names of its contributors may be used | ||
to endorse or promote products derived from this software without | ||
specific prior written permission. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
|
||
#ifndef _DADALERN_H | ||
#define _DADALERN_H | ||
|
||
class dadaMidiLearn { | ||
public: | ||
|
||
bool active; | ||
int8_t counter; | ||
uint8_t mode; // 0 = basic, 1 = advanced | ||
dataCFG * nv; | ||
dadaMidiLearn(dataCFG * mynv) { | ||
nv = mynv; | ||
active = false; | ||
mode = 0; | ||
counter = 0; | ||
loadEEPROM(); | ||
}; | ||
|
||
void begin(uint8_t learnMode) { | ||
counter = 0; | ||
mode = learnMode; | ||
active = true; | ||
clearMap(); | ||
}; | ||
|
||
void clearMap() { | ||
for (int i = 0 ; i < 12 ; i++) { | ||
nv->midiChannels[i] = 0; // no midi filter | ||
nv->midiPins[i]=128; // invalid note | ||
} | ||
}; | ||
|
||
void noteOn(byte ch, byte note, byte velocity) { | ||
if (!active) return ; | ||
|
||
// nv->midiChannel = ch; | ||
|
||
if (mode == 0) { | ||
for (byte i = 0 ; i < 12 ; i++) { | ||
nv->midiPins[i] = note + i; | ||
} | ||
active = false; | ||
saveEEPROM(); | ||
return; | ||
} | ||
|
||
if (mode == 1) { | ||
nv->midiPins[counter] = note; | ||
nv->midiChannels[counter] = ch; | ||
counter++; | ||
if (counter > 11) { | ||
active = false; | ||
saveEEPROM(); | ||
} | ||
return; | ||
} | ||
|
||
}; | ||
|
||
void noteOff(byte ch, byte note, byte velocity) { | ||
// nothing todo | ||
}; | ||
|
||
// load & save stuff for eeprom addr=0 .. midiChannel, addr=1-128 mapping table | ||
void saveEEPROM() { | ||
nvStore.write(nvData); | ||
statusLED.blink(8, 14, 16); | ||
}; | ||
|
||
void loadEEPROM() { | ||
nvData = nvStore.read(); | ||
statusLED.blink(1, 3, 32); | ||
}; | ||
|
||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
Copyright (c) 2017, DADAMACHINES | ||
Author: Sven Braun | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without modification, | ||
are permitted provided that the following conditions are met: | ||
1. Redistributions of source code must retain the above copyright notice, | ||
this list of conditions and the following disclaimer. | ||
2. Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
3. Neither the name of DADAMACHINES nor the names of its contributors may be used | ||
to endorse or promote products derived from this software without | ||
specific prior written permission. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
|
||
#ifndef _DARALED13_H | ||
#define _DARALED13_H | ||
|
||
#define loopTime 200 // speed depend on CPU Frequency | ||
|
||
class dadaStatusLED { | ||
public: | ||
int _ledPin; | ||
long _on_time; | ||
long _off_time; | ||
int _times; | ||
bool _state; | ||
long _countDown; | ||
|
||
dadaStatusLED(int pin) { | ||
_ledPin = pin; | ||
_times = 0; | ||
pinMode(_ledPin, OUTPUT); // pin leds to output | ||
}; | ||
|
||
void blink( long on_time, long off_time, int count = -1) { | ||
if (_times > 0) { // accept only requests wen finished | ||
return; | ||
} | ||
_on_time = on_time * loopTime; | ||
_off_time = off_time * loopTime; | ||
_times = count; | ||
}; | ||
|
||
void tick() { // call this in MainLoop | ||
if (_times == 0) { | ||
return; | ||
} | ||
|
||
_countDown--; | ||
if (_state) { | ||
if (_countDown < 0) { | ||
_state = ! _state; | ||
_countDown = _off_time; | ||
digitalWrite(_ledPin, LOW); | ||
if (_times > 0 ) { | ||
_times--; | ||
} | ||
} | ||
} else { | ||
if (_countDown < 0) { | ||
_state = ! _state; | ||
_countDown = _on_time; | ||
digitalWrite(_ledPin, HIGH); | ||
} | ||
} | ||
}; | ||
|
||
}; | ||
|
||
#endif |
Oops, something went wrong.