Skip to content

Commit b8145b0

Browse files
authored
Update the CI dispatch example.
1 parent 90de310 commit b8145b0

File tree

1 file changed

+60
-37
lines changed

1 file changed

+60
-37
lines changed

README.md

+60-37
Original file line numberDiff line numberDiff line change
@@ -181,51 +181,74 @@ int main() {
181181
182182
### Example: Process MIDI-CI Messages
183183
184-
> THIS EXAMPLE IS OBSOLETE
184+
MIDI-CI requires a lot of SysEx messages. This library abstracts the complexity of building and parsing MIDI-CI Messages.
185185
186-
MIDI-CI requires a lot of SysEx messages. This library abstracts the complexity of building and parsing most MIDI-CI Messages.
186+
~~~cpp
187+
#include "midi2/ci_dispatcher.hpp"
187188
188-
```C++
189+
#include <array>
190+
#include <cstdlib>
191+
#include <format>
192+
#include <iostream>
193+
#include <type_traits>
189194
190-
#include "midiCIProcessor.h"
191-
midi2Processor midiCIProcess;
192-
uint32_t localMUID;
193-
uint8_t sysexId[3] = {0x00 , 0x02, 0x22};
194-
uint8_t famId[2] = {0x7F, 0x00};
195-
uint8_t modelId[2] = {0x7F, 0x00};
196-
uint8_t ver[4];
197-
unint8_t sysexBuffer[512];
198-
199-
bool checkMUID(uint8_t group, uint32_t muid){
200-
return (localMUID==muid);
201-
}
195+
using namespace midi2::ci::literals;
202196
203-
void recvDiscovery(uint8_t group, struct MIDICI ciDetails, uint8_t* remotemanuId, uint8_t* remotefamId, uint8_t* remotemodelId, uint8_t *remoteverId, uint8_t remoteciSupport, uint16_t remotemaxSysex){
204-
Serial.print("->Discovery: remoteMuid ");Serial.println(ciDetails.remoteMUID);
205-
uint16_t sBuffLen = sendDiscoveryReply(sysexBuffer, localMUID, ciDetails.remoteMUID, sysexId, famId, modelId, ver, 0b11100, 512);
206-
sendSysExOutOfDevice(sysexBuffer, sBuffLen);
197+
namespace {
198+
// Display the header fields
199+
void print_header(std::ostream &os, midi2::ci::header const &h) {
200+
os << std::format("device-id=0x{:X}\nversion=0x{:X}\n", h.device_id, h.version)
201+
<< std::format("remote-MUID=0x{:X}\nlocal-MUID=0x{:X}\n\n", h.remote_muid, h.local_muid);
202+
}
203+
// Display the discovery data fields
204+
void print_discovery(std::ostream &os, midi2::ci::discovery const &d) {
205+
os << std::format("manufacturer={}\nfamily=0x{:X}\nmodel=0x{:X}\n", d.manufacturer, d.family, d.model)
206+
<< std::format("version={}\ncapability=0x{:X}\n", d.version, d.capability)
207+
<< std::format("max-sysex-size=0x{:X}\noutput-path-id=0x{:X}\n", d.max_sysex_size, d.output_path_id);
207208
}
208209
209-
void setup()
210-
{
211-
localMUID = random(0xFFFFEFF);
212-
213-
midiCIProcess.setRecvDiscovery(recvDiscovery);
214-
midiCIProcess.setCheckMUID(checkMUID);
215-
216-
uint16_t sBuffLen = sendDiscoveryRequest(sysexBuffer,1, sysexId, famId, modelId, ver,12, 512);
217-
sendSysExOutOfDevice(sysexBuffer, sBuffLen);
210+
// We must pass a “context” to the dispatcher, which will be forwarded to
211+
// each of the dispatcher's callbacks. The context lets message handlers share
212+
// state but we don’t need that here, so a struct with no members will suffice.
213+
struct context {};
214+
215+
dispatcher setup_ci_dispatcher(midi2::ci::muid const my_muid) {
216+
// Create a CI dispatcher instance using std::function<> for
217+
// all of its handler functions.
218+
auto dispatcher = midi2::ci::make_function_dispatcher<context, buffer_size>();
219+
auto &config = dispatcher.config();
220+
221+
// Register a handler for checking whether a message is addressed
222+
// to this receiver.
223+
config.system.on_check_muid(
224+
[my_muid](context, std::uint8_t /*group*/, midi2::ci::muid const m) { return m == my_muid; });
225+
226+
// Register a handler for Discovery messages.
227+
config.management.on_discovery([](context, midi2::ci::header const &h, midi2::ci::discovery const &d) {
228+
print_header(std::cout, h);
229+
print_discovery(std::cout, d);
230+
// Send a reply to this message...
231+
});
232+
return dispatcher;
218233
}
234+
} // end anonymous namespace
219235
220-
void loop()
221-
{
222-
...
223-
while(uint8_t sysexByte = getNextSysexByte()){
224-
midiCIProcess.processUMP(sysexByte);
236+
int main() {
237+
// Use a proper random number!
238+
constexpr auto my_muid = midi2::ci::muid{0x01234567U};
239+
constexpr auto my_group = std::uint8_t{0};
240+
constexpr auto device_id = 0_b7;
241+
auto dispatcher = setup_ci_dispatcher(my_muid);
242+
243+
// A system exclusive message containing a CI discovery request.
244+
constexpr std::array message{0x7E, 0x7F, 0x0D, 0x70, 0x02, 0x00, 0x00, 0x00, 0x00, 0x7F,
245+
0x7F, 0x7F, 0x7F, 0x12, 0x23, 0x34, 0x79, 0x2E, 0x5D, 0x56,
246+
0x01, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x02, 0x00, 0x00, 0x00};
247+
dispatcher.start(my_group, device_id);
248+
for (auto const b : message) {
249+
dispatcher.processMIDICI(static_cast<std::byte>(b));
225250
}
226-
...
251+
dispatcher.finish();
227252
}
228-
229-
```
230-
253+
~~~
231254
---

0 commit comments

Comments
 (0)