Skip to content

Commit 214a5c2

Browse files
author
Franz Detro
committed
add examples for stream messages
1 parent 82c8c37 commit 214a5c2

File tree

1 file changed

+167
-1
lines changed

1 file changed

+167
-1
lines changed

docs/stream_message.examples.cpp

Lines changed: 167 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,169 @@
11
#include <midi/stream_message.h>
22

3-
void run_stream_message_examples() { /* TODO */ }
3+
#include <midi/manufacturer.h>
4+
5+
/* Most of the code in these examples is taken from the FreeRTOS firmware for ProtoZOA, take a look here:
6+
https://github.com/midi2-dev/AmeNote_Protozoa/blob/master/UUT_FreeRTOS/FreeRTOS_Tasks/UMPProcessing.cpp#L158
7+
*/
8+
static void reply_with_packet(const midi::universal_packet& p)
9+
{
10+
using namespace midi;
11+
12+
if (is_stream_message(p))
13+
{
14+
// you may want to inspect the other replies, too
15+
16+
// Remark: in real world implementations you have to aggregate the name and serial strings in case of multi-part
17+
// replies!
18+
if (auto reply = as_endpoint_name_view(p))
19+
{
20+
auto name = reply->payload();
21+
}
22+
else if (auto reply = as_function_block_name_view(p))
23+
{
24+
auto name = reply->payload();
25+
}
26+
else if (auto reply = as_product_instance_id_view(p))
27+
{
28+
auto serial = reply->payload();
29+
}
30+
}
31+
}
32+
33+
void endpoint_discovery_example()
34+
{
35+
using namespace midi;
36+
37+
auto discovery = make_endpoint_discovery_message(discovery_filter::endpoint_all);
38+
39+
if (auto m = as_endpoint_discovery_view(discovery))
40+
{
41+
const auto identity = device_identity{ manufacturer::native_instruments, 0x1730, 49, 0x00010005 };
42+
const auto ep_name = std::string{ "Kontrol S49" };
43+
const auto serial = std::string{ "12345678" };
44+
const auto protocol = protocol::midi1;
45+
const auto extensions = extensions_t{ 0 };
46+
47+
if (m->requests_info())
48+
{
49+
constexpr auto endpoint_info = make_endpoint_info_message(3, true, protocol::midi1 + protocol::midi2, 0);
50+
reply_with_packet(endpoint_info);
51+
}
52+
53+
if (m->requests_device_identity())
54+
{
55+
const auto device_identity = make_device_identity_message(identity);
56+
reply_with_packet(device_identity);
57+
}
58+
59+
if (m->requests_name())
60+
{
61+
send_endpoint_name(ep_name, reply_with_packet);
62+
}
63+
64+
if (m->requests_product_instance_id())
65+
{
66+
send_product_instance_id(serial, reply_with_packet);
67+
}
68+
69+
if (m->requests_stream_configuration())
70+
{
71+
reply_with_packet(make_stream_configuration_notification(protocol, extensions));
72+
}
73+
}
74+
}
75+
76+
void function_block_discovery_example()
77+
{
78+
using namespace midi;
79+
80+
auto discovery = make_function_block_discovery_message(0xFF, discovery_filter::function_block_all);
81+
82+
if (auto m = as_function_block_discovery_view(discovery))
83+
{
84+
constexpr uint8_t main = 0;
85+
constexpr uint8_t din_port_out = 1;
86+
constexpr uint8_t din_port_in = 2;
87+
88+
if (m->requests_info())
89+
{
90+
if (m->requests_function_block(main))
91+
{
92+
constexpr auto reply =
93+
midi::make_function_block_info_message(0, midi::function_block_options::bidirectional, 0);
94+
reply_with_packet(reply);
95+
}
96+
97+
if (m->requests_function_block(din_port_out))
98+
{
99+
constexpr auto reply = midi::make_function_block_info_message(
100+
din_port_out,
101+
midi::function_block_options{ true,
102+
midi::function_block_options::direction_input,
103+
midi::function_block_options::midi1_31250,
104+
midi::function_block_options::ui_hint_as_direction,
105+
0x00,
106+
0 },
107+
din_port_out);
108+
reply_with_packet(reply);
109+
}
110+
111+
if (m->requests_function_block(din_port_in))
112+
{
113+
constexpr auto reply = midi::make_function_block_info_message(
114+
din_port_in,
115+
midi::function_block_options{ true,
116+
midi::function_block_options::direction_output,
117+
midi::function_block_options::midi1_31250,
118+
midi::function_block_options::ui_hint_as_direction,
119+
0x00,
120+
0 },
121+
din_port_in);
122+
reply_with_packet(reply);
123+
}
124+
}
125+
126+
if (m->requests_name())
127+
{
128+
if (m->requests_function_block(main))
129+
{
130+
constexpr auto reply = midi::make_function_block_name_message(midi::packet_format::complete, 0, "Main");
131+
reply_with_packet(reply);
132+
}
133+
134+
if (m->requests_function_block(din_port_out))
135+
{
136+
constexpr auto reply =
137+
midi::make_function_block_name_message(midi::packet_format::complete, din_port_out, "EXT OUT");
138+
reply_with_packet(reply);
139+
}
140+
141+
if (m->requests_function_block(din_port_in))
142+
{
143+
constexpr auto reply =
144+
midi::make_function_block_name_message(midi::packet_format::complete, din_port_in, "EXT IN");
145+
reply_with_packet(reply);
146+
}
147+
}
148+
}
149+
}
150+
151+
void stream_configuration_example()
152+
{
153+
using namespace midi;
154+
155+
auto r = make_stream_configuration_request(protocol::midi2);
156+
157+
if (auto m = as_stream_configuration_view(r))
158+
{
159+
auto new_protocol = m->protocol();
160+
reply_with_packet(midi::make_stream_configuration_notification(m->protocol(), m->extensions()));
161+
}
162+
}
163+
164+
void run_stream_message_examples()
165+
{
166+
endpoint_discovery_example();
167+
function_block_discovery_example();
168+
stream_configuration_example();
169+
}

0 commit comments

Comments
 (0)