-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWifiNINA_NoteOnOffEverySec.ino
148 lines (121 loc) · 4.57 KB
/
WifiNINA_NoteOnOffEverySec.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
// Hardware: Arduino MKR WiFi 1010
// https://store.arduino.cc/usa/mkr-wifi-1010
#include <SPI.h>
#include <WiFiNINA.h>
#define FIRMWARE_VERSION "1.2.1"
#include <WiFiUdp.h>
#include "AppleMidi.h"
int status = WL_IDLE_STATUS;
char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP)
unsigned long t0 = millis();
bool isConnected = false;
APPLEMIDI_CREATE_INSTANCE(WiFiUDP, AppleMIDI); // see definition in AppleMidi_Defs.h
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void setup()
{
// Serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Getting IP address...");
// check for the presence of the shield:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("WiFi module not present");
// don't continue:
while (true);
}
String fv = WiFi.firmwareVersion();
if ( fv < FIRMWARE_VERSION )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println();
Serial.print("IP address is ");
Serial.println(WiFi.localIP());
Serial.println("OK, now make sure you an rtpMIDI session that is Enabled");
Serial.print("Add device named Arduino with Host/Port ");
Serial.print(WiFi.localIP());
Serial.println(":5004");
Serial.println("Then press the Connect button");
Serial.println("Then open a MIDI listener (eg MIDI-OX) and monitor incoming notes");
// Create a session and wait for a remote host to connect to us
AppleMIDI.begin("Arduino1");
AppleMIDI.OnConnected(OnAppleMidiConnected);
AppleMIDI.OnDisconnected(OnAppleMidiDisconnected);
AppleMIDI.OnReceiveNoteOn(OnAppleMidiNoteOn);
AppleMIDI.OnReceiveNoteOff(OnAppleMidiNoteOff);
Serial.println("Sending NoteOn/Off of note 45, every second");
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void loop()
{
// Listen to incoming notes
AppleMIDI.read();
// send a note every second
// (dont cáll delay(1000) as it will stall the pipeline)
if (isConnected && (millis() - t0) > 1000)
{
t0 = millis();
// Serial.print(".");
int note = 45;
int velocity = 55;
int channel = 1;
AppleMIDI.sendNoteOn(note, velocity, channel);
AppleMIDI.sendNoteOff(note, velocity, channel);
}
}
// ====================================================================================
// Event handlers for incoming MIDI messages
// ====================================================================================
// -----------------------------------------------------------------------------
// rtpMIDI session. Device connected
// -----------------------------------------------------------------------------
void OnAppleMidiConnected(uint32_t ssrc, char* name) {
isConnected = true;
Serial.print("Connected to session ");
Serial.println(name);
}
// -----------------------------------------------------------------------------
// rtpMIDI session. Device disconnected
// -----------------------------------------------------------------------------
void OnAppleMidiDisconnected(uint32_t ssrc) {
isConnected = false;
Serial.println("Disconnected");
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void OnAppleMidiNoteOn(byte channel, byte note, byte velocity) {
Serial.print("Incoming NoteOn from channel:");
Serial.print(channel);
Serial.print(" note:");
Serial.print(note);
Serial.print(" velocity:");
Serial.print(velocity);
Serial.println();
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void OnAppleMidiNoteOff(byte channel, byte note, byte velocity) {
Serial.print("Incoming NoteOff from channel:");
Serial.print(channel);
Serial.print(" note:");
Serial.print(note);
Serial.print(" velocity:");
Serial.print(velocity);
Serial.println();
}