-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSAMD-Beats.ino
162 lines (151 loc) · 3.95 KB
/
SAMD-Beats.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// TESTED ON SEEEDUINO WIO LITE MG126
#include <Arduino.h>
#include <MIDIUSB.h>
#include <MIDIUSB_Defs.h>
#include <frequencyToNote.h>
#include <pitchToFrequency.h>
#include <pitchToNote.h>
#include "Colors.h"
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#define PIN 13
#define USER_BUTTON 38 // User button is mapped to toggle between Beats/SMPTE
Adafruit_NeoMatrix lc = Adafruit_NeoMatrix(32, 8, PIN,
NEO_MATRIX_BOTTOM + NEO_MATRIX_RIGHT + NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
NEO_GRB + NEO_KHZ800);
const uint16_t colors[] = {
lc.Color(pink.r, pink.g, pink.b), lc.Color(blue.r, blue.g, blue.b), lc.Color(yellow.r, yellow.g, yellow.b)
};
String beatCounter;
String barCounter;
midiEventPacket_t rx;
void displayPosition();
void noteOn(byte channel, byte pitch, byte velocity);
boolean dots;
int buttonState;
int lastButtonState = LOW;
int displayMode = 1;
int x = lc.width();
int16_t xPos0 = x - 32;
int16_t dot1 = x - 28;
int16_t xPos1 = x - 26;
int16_t xPos2 = x - 20;
int16_t dot2 = x - 15;
int16_t xPos3 = x - 14;
int16_t xPos4 = x - 8;
int16_t xPos5 = x - 2;
void setup() {
SerialUSB.begin(115200);
lc.begin();
lc.setTextWrap(true);
lc.setBrightness(32);
lc.setTextColor(colors[1]);
dots = false;
lc.show();
pinMode(USER_BUTTON,INPUT);
}
void loop() {
rx = MidiUSB.read();
displayPosition();
readButton();
}
void readButton() {
int reading = digitalRead(buttonPin);
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
displayMode = !displayMode;
if (displayMode == 1) {
noteOn(0, 0x35, 0x7F);
MidiUSB.flush();
}
if (displayMode == 0) {
noteOn(0, 0x35, 0);
MidiUSB.flush();
}
}
}
lastButtonState = reading;
}
void displayPosition() {
if (rx.byte1 >> 4 == 0x0B) {
if (dots) {
lc.drawPixel(dot1, 6, 50);
lc.drawPixel(dot2, 6, 50);
}
if (rx.byte2 >= 0x40 && rx.byte2 <= 0x41) {
lc.fillRect(xPos5, 0, 5, 8, 0);
if ((rx.byte3 & 0x10) == 0) {
beatCounter = 1;
} else {
beatCounter = rx.byte3 & 0x0F;
}
lc.setCursor(xPos5, 0);
lc.print(beatCounter);
lc.show();
}
if (rx.byte2 >= 0x42 && rx.byte2 <= 0x43) {
lc.fillRect(xPos4, 0, 5, 8, 0);
if ((rx.byte3 & 0x10) == 0) {
beatCounter = 1;
} else {
beatCounter = rx.byte3 & 0x0F;
}
lc.setCursor(xPos4, 0);
lc.print(beatCounter);
lc.show();
}
if (rx.byte2 >= 0x43 && rx.byte2 <= 0x44) {
lc.fillRect(xPos3, 0, 6, 8, 0);
if ((rx.byte3 & 0x10) == 0) {
beatCounter = " ";
} else {
beatCounter = rx.byte3 & 0x0F;
}
lc.setCursor(xPos3, 0);
lc.print(beatCounter);
lc.show();
}
if (rx.byte2 >= 0x45 && rx.byte2 <= 0x46) {
lc.fillRect(xPos2, 0, 6, 8, 0);
if ((rx.byte3 & 0x10) == 0) {
beatCounter = " ";
} else {
beatCounter = rx.byte3 & 0x0F;
}
lc.setCursor(xPos2, 0);
lc.print(beatCounter);
lc.show();
}
if (rx.byte2 >= 0x46 && rx.byte2 <= 0x47) {
lc.fillRect(xPos1, 0, 6, 8, 0);
if ((rx.byte3 & 0x10) == 0) {
beatCounter = " ";
} else {
beatCounter = rx.byte3 & 0x0F;
}
lc.setCursor(xPos1, 0);
lc.print(beatCounter);
lc.show();
}
if (rx.byte2 >= 0x48 && rx.byte2 <= 0x49) { // position display message
lc.fillRect(xPos0, 0, 6, 8, 0);
if ((rx.byte3 & 0x10) == 0) {
barCounter = " ";
} else {
barCounter = rx.byte3 & 0x0F;
}
lc.setCursor(xPos0, 0);
lc.print(barCounter);
lc.show();
}
}
}
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);
}