Skip to content

Commit d0a0a71

Browse files
authored
Merge pull request #174 from infrafast/dynamicExample
Example demonstrating how to create dynamic instance instead of macro
2 parents e0abb86 + 874b14a commit d0a0a71

File tree

3 files changed

+254
-0
lines changed

3 files changed

+254
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <WiFi.h>
2+
3+
#include "ETH_Helper.h"
4+
5+
#define SerialMon Serial
6+
#include "midiHelpers.h"
7+
8+
9+
#define ethernet true; // set to false to demonstrate WiFi usage
10+
11+
char ssid[] = "ssid"; // your network SSID (name)
12+
char pass[] = "pass"; // your network password (use for WPA, or use as key for WEP)
13+
14+
MidiClient* midiClient; // generic class offered as an alternative of the MACRO
15+
16+
void OnAppleMidiException(const APPLEMIDI_NAMESPACE::ssrc_t&, const APPLEMIDI_NAMESPACE::Exception&, const int32_t);
17+
18+
void WIFI_startup(){
19+
WiFi.begin(ssid, pass);
20+
while (WiFi.status() != WL_CONNECTED) {
21+
delay(500);
22+
AM_DBG(F("Establishing connection to WiFi.."));
23+
}
24+
}
25+
26+
void setup(){
27+
28+
AM_DBG_SETUP(115200);
29+
30+
bool useEth = false;
31+
32+
if (useEth){
33+
ETH_startup();
34+
midiClient = new AppleMidiWithInterfaceWrapper<EthernetUDP>("APPLE_MIDIETHCLIENT",DEFAULT_CONTROL_PORT);
35+
}else{
36+
WIFI_startup();
37+
midiClient = new AppleMidiWithInterfaceWrapper<WiFiUDP>("APPLE_MIDIWIFICLIENT",DEFAULT_CONTROL_PORT);
38+
}
39+
40+
midiClient->begin();
41+
42+
midiClient->setHandleConnected([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc, const char* name) {
43+
AM_DBG(F("Connected to session"), ssrc, name);
44+
});
45+
midiClient->setHandleDisconnected([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc) {
46+
AM_DBG(F("Disconnected"), ssrc);
47+
});
48+
49+
AM_DBG(F("OK, now make sure you have an rtpMIDI session that is Enabled"));
50+
AM_DBG(F("Add device named Arduino with Host"), useEth?Ethernet.localIP():WiFi.localIP(), "Port", midiClient->getPort(), "(Name", midiClient->getName(), ")");
51+
AM_DBG(F("Select and then press the Connect button"));
52+
AM_DBG(F("Then open a MIDI listener and monitor incoming notes"));
53+
}
54+
55+
void loop(){
56+
midiClient->read();
57+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#ifdef ETHERNET3
2+
#include <Ethernet3.h>
3+
#else
4+
#include <Ethernet.h>
5+
#include <EthernetBonjour.h> // https://github.com/TrippyLighting/EthernetBonjour
6+
#endif
7+
8+
// to get the Mac address
9+
#include <WiFi.h>
10+
11+
#define RESET_PIN D7
12+
#define CS_PIN D5
13+
14+
// Enter a MAC address for your controller below.
15+
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
16+
byte mac[] = {
17+
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
18+
};
19+
20+
/*
21+
Wiz W5500 reset function. Change this for the specific reset
22+
sequence required for your particular board or module.
23+
*/
24+
void hardreset() {
25+
pinMode(RESET_PIN, OUTPUT);
26+
digitalWrite(RESET_PIN, HIGH);
27+
delay(150);
28+
29+
digitalWrite(RESET_PIN, LOW);
30+
delay(500);
31+
digitalWrite(RESET_PIN, HIGH);
32+
delay(150);
33+
}
34+
35+
bool ETH_startup()
36+
{
37+
#ifdef ETHERNET3
38+
Ethernet.setRstPin(RESET_PIN);
39+
Ethernet.setCsPin(CS_PIN);
40+
Ethernet.init(4); // maxSockNum = 4 Socket 0...3 -> RX/TX Buffer 4k
41+
Serial.println("Resetting Wiz W5500 Ethernet Board... ");
42+
Ethernet.hardreset();
43+
#else
44+
Ethernet.init(CS_PIN);
45+
Serial.println("Resetting Wiz Ethernet Board... ");
46+
hardreset();
47+
#endif
48+
49+
esp_read_mac(mac, ESP_MAC_WIFI_STA);
50+
51+
/*
52+
Network configuration - all except the MAC are optional.
53+
54+
IMPORTANT NOTE - The mass-produced W5500 boards do -not-
55+
have a built-in MAC address (depite
56+
comments to the contrary elsewhere). You
57+
-must- supply a MAC address here.
58+
*/
59+
#ifdef ETHERNET3
60+
Serial.println("Starting Ethernet3 connection...");
61+
#else
62+
Serial.println("Starting Ethernet connection...");
63+
//if (Ethernet.hardwareStatus() == EthernetNoHardware) Serial.println("No EthernetHW");
64+
#endif
65+
Ethernet.begin(mac);
66+
Serial.print("Ethernet IP is: ");
67+
Serial.println(Ethernet.localIP());
68+
69+
/*
70+
Sanity checks for W5500 and cable connection.
71+
*/
72+
Serial.println("Checking connection.");
73+
bool rdy_flag = false;
74+
for (uint8_t i = 0; i <= 20; i++) {
75+
#ifdef ETHERNET3
76+
if ((Ethernet.link() == 0)) {
77+
#else
78+
if ((Ethernet.linkStatus() == Unknown)) {
79+
#endif
80+
Serial.print(".");
81+
rdy_flag = false;
82+
delay(80);
83+
} else {
84+
rdy_flag = true;
85+
break;
86+
}
87+
}
88+
if (rdy_flag == false) {
89+
Serial.println("\n\r\tHardware fault, or cable problem... cannot continue.");
90+
while (true) {
91+
delay(10); // Halt.
92+
}
93+
} else {
94+
Serial.println("OK");
95+
}
96+
97+
#ifndef ETHERNET3
98+
// Initialize the Bonjour/MDNS library. You can now reach or ping this
99+
// Arduino via the host name "arduino.local", provided that your operating
100+
// system is Bonjour-enabled (such as MacOS X).
101+
// Always call this before any other method!
102+
EthernetBonjour.begin("arduino");
103+
104+
EthernetBonjour.addServiceRecord("apple-midi", //Arduino._apple-midi doesnt work
105+
5004,
106+
MDNSServiceUDP);
107+
#endif
108+
109+
return true;
110+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#ifndef MIDIHELPERS_h
2+
#define MIDIHELPERS_h
3+
4+
#define USE_EXT_CALLBACKS // as from example => required for MIDI callbacks
5+
#include <AppleMIDI.h> //https://github.com/lathoub/Arduino-AppleMIDI-Library
6+
7+
using namespace APPLEMIDI_NAMESPACE;
8+
9+
/*
10+
class Utilities for creating a generic pointer to AppleMIDISession and MidiInterface
11+
from a WiFiUDP or EThernetUDP (template) and be able to manipulate it as a generic instance
12+
and manipulating the pointer generically
13+
*/
14+
15+
class MidiClient {
16+
public:
17+
virtual void read() = 0;
18+
virtual ~MidiClient() {}
19+
virtual void setHandleConnected(void (*fptr)(const ssrc_t &, const char *))= 0;
20+
virtual void setHandleDisconnected(void (*fptr)(const ssrc_t &)) = 0;
21+
virtual void setHandleException(void (*fptr)(const ssrc_t &, const Exception &, const int32_t value))=0;
22+
virtual const char *getName() = 0;
23+
virtual const uint16_t getPort() = 0;
24+
virtual void begin() = 0;
25+
// all methods you need to be wrapped below
26+
virtual void sendNoteOn(byte note, byte velocity, byte channel) = 0;
27+
virtual void sendNoteOff(byte note, byte velocity, byte channel) = 0;
28+
// etc...
29+
30+
};
31+
32+
template <typename UdpType>
33+
class AppleMidiWithInterfaceWrapper : public MidiClient {
34+
public:
35+
AppleMIDISession<UdpType>* session;
36+
MidiInterface<AppleMIDISession<UdpType>, AppleMIDISettings>* midi;
37+
38+
AppleMidiWithInterfaceWrapper<UdpType>(const char* sessionName, uint16_t port) {
39+
session = new AppleMIDISession<UdpType>(sessionName, port);
40+
midi = new MidiInterface<AppleMIDISession<UdpType>, AppleMIDISettings>(*session);
41+
}
42+
43+
virtual void begin(){
44+
session->begin();
45+
}
46+
47+
virtual const char *getName(){
48+
return session->getName();
49+
}
50+
51+
virtual const uint16_t getPort(){
52+
return session->getPort();
53+
}
54+
55+
virtual void setHandleConnected(void (*fptr)(const ssrc_t &, const char *)){
56+
session->setHandleConnected(fptr);
57+
}
58+
59+
virtual void setHandleDisconnected(void (*fptr)(const ssrc_t &)){
60+
session->setHandleDisconnected(fptr);
61+
}
62+
63+
virtual void setHandleException(void (*fptr)(const ssrc_t &, const Exception &, const int32_t value)){
64+
session->setHandleException(fptr);
65+
}
66+
67+
68+
void read() override {
69+
midi->read();
70+
}
71+
72+
void sendNoteOn(byte note, byte velocity, byte channel) override {
73+
midi->sendNoteOn(note, velocity, channel);
74+
}
75+
76+
void sendNoteOff(byte note, byte velocity, byte channel) override {
77+
midi->sendNoteOff(note, velocity, channel);
78+
}
79+
80+
81+
~AppleMidiWithInterfaceWrapper() {
82+
delete midi;
83+
delete session;
84+
}
85+
};
86+
87+
#endif

0 commit comments

Comments
 (0)