Skip to content

Commit 3ee12b7

Browse files
committed
add automat sw_1.0.0 2
1 parent 46f638a commit 3ee12b7

9 files changed

+607
-0
lines changed

automat-sw_1.0.0/automat-sw_1.0.0.ino

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+

automat-sw_1.0.0/dadaMidiLearn.h

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
Copyright (c) 2017, DADAMACHINES
3+
Author: Sven Braun
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without modification,
7+
are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice,
10+
this list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
3. Neither the name of DADAMACHINES nor the names of its contributors may be used
17+
to endorse or promote products derived from this software without
18+
specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
33+
#ifndef _DADALERN_H
34+
#define _DADALERN_H
35+
36+
class dadaMidiLearn {
37+
public:
38+
39+
bool active;
40+
int8_t counter;
41+
uint8_t mode; // 0 = basic, 1 = advanced
42+
dataCFG * nv;
43+
dadaMidiLearn(dataCFG * mynv) {
44+
nv = mynv;
45+
active = false;
46+
mode = 0;
47+
counter = 0;
48+
loadEEPROM();
49+
};
50+
51+
void begin(uint8_t learnMode) {
52+
counter = 0;
53+
mode = learnMode;
54+
active = true;
55+
clearMap();
56+
};
57+
58+
void clearMap() {
59+
for (int i = 0 ; i < 12 ; i++) {
60+
nv->midiChannels[i] = 0; // no midi filter
61+
nv->midiPins[i]=128; // invalid note
62+
}
63+
};
64+
65+
void noteOn(byte ch, byte note, byte velocity) {
66+
if (!active) return ;
67+
68+
// nv->midiChannel = ch;
69+
70+
if (mode == 0) {
71+
for (byte i = 0 ; i < 12 ; i++) {
72+
nv->midiPins[i] = note + i;
73+
}
74+
active = false;
75+
saveEEPROM();
76+
return;
77+
}
78+
79+
if (mode == 1) {
80+
nv->midiPins[counter] = note;
81+
nv->midiChannels[counter] = ch;
82+
counter++;
83+
if (counter > 11) {
84+
active = false;
85+
saveEEPROM();
86+
}
87+
return;
88+
}
89+
90+
};
91+
92+
void noteOff(byte ch, byte note, byte velocity) {
93+
// nothing todo
94+
};
95+
96+
// load & save stuff for eeprom addr=0 .. midiChannel, addr=1-128 mapping table
97+
void saveEEPROM() {
98+
nvStore.write(nvData);
99+
statusLED.blink(8, 14, 16);
100+
};
101+
102+
void loadEEPROM() {
103+
nvData = nvStore.read();
104+
statusLED.blink(1, 3, 32);
105+
};
106+
107+
};
108+
109+
#endif

automat-sw_1.0.0/dadaStatusLED.h

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright (c) 2017, DADAMACHINES
3+
Author: Sven Braun
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without modification,
7+
are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice,
10+
this list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
3. Neither the name of DADAMACHINES nor the names of its contributors may be used
17+
to endorse or promote products derived from this software without
18+
specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
33+
#ifndef _DARALED13_H
34+
#define _DARALED13_H
35+
36+
#define loopTime 200 // speed depend on CPU Frequency
37+
38+
class dadaStatusLED {
39+
public:
40+
int _ledPin;
41+
long _on_time;
42+
long _off_time;
43+
int _times;
44+
bool _state;
45+
long _countDown;
46+
47+
dadaStatusLED(int pin) {
48+
_ledPin = pin;
49+
_times = 0;
50+
pinMode(_ledPin, OUTPUT); // pin leds to output
51+
};
52+
53+
void blink( long on_time, long off_time, int count = -1) {
54+
if (_times > 0) { // accept only requests wen finished
55+
return;
56+
}
57+
_on_time = on_time * loopTime;
58+
_off_time = off_time * loopTime;
59+
_times = count;
60+
};
61+
62+
void tick() { // call this in MainLoop
63+
if (_times == 0) {
64+
return;
65+
}
66+
67+
_countDown--;
68+
if (_state) {
69+
if (_countDown < 0) {
70+
_state = ! _state;
71+
_countDown = _off_time;
72+
digitalWrite(_ledPin, LOW);
73+
if (_times > 0 ) {
74+
_times--;
75+
}
76+
}
77+
} else {
78+
if (_countDown < 0) {
79+
_state = ! _state;
80+
_countDown = _on_time;
81+
digitalWrite(_ledPin, HIGH);
82+
}
83+
}
84+
};
85+
86+
};
87+
88+
#endif

0 commit comments

Comments
 (0)