Skip to content

Commit 37bac76

Browse files
committedDec 14, 2024·
Fixed #291
1 parent ac66c8e commit 37bac76

21 files changed

+164
-143
lines changed
 

‎lib-ltc/include/arm/ltcmidisystemrealtime.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file ltcmidisystemrealtime.h
33
*
44
*/
5-
/* Copyright (C) 2020-2023 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2020-2024 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -42,8 +42,6 @@ class LtcMidiSystemRealtime {
4242

4343
void Start();
4444
void Stop();
45-
void Run();
46-
4745

4846
void SendStart() {
4947
Send(midi::Types::START);
@@ -59,6 +57,8 @@ class LtcMidiSystemRealtime {
5957

6058
void SetBPM(uint32_t nBPM);
6159

60+
void Input(const uint8_t *pBuffer, uint32_t nSize, uint32_t nFromIp, uint16_t nFromPort);
61+
6262
static LtcMidiSystemRealtime *Get() {
6363
return s_pThis;
6464
}
@@ -78,11 +78,14 @@ class LtcMidiSystemRealtime {
7878
LtcOutputs::Get()->ShowBPM(nBPM);
7979
}
8080

81+
void static staticCallbackFunctionInput(const uint8_t *pBuffer, uint32_t nSize, uint32_t nFromIp, uint16_t nFromPort) {
82+
s_pThis->Input(pBuffer, nSize, nFromIp, nFromPort);
83+
}
84+
8185
private:
8286
int32_t m_nHandle { -1 };
8387
uint32_t m_nBPMPrevious { 999 };
8488

85-
static inline char *s_pUdpBuffer;
8689
static inline LtcMidiSystemRealtime *s_pThis;
8790
};
8891

‎lib-ltc/include/arm/ltcoutputs.h

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class LtcOutputs {
5353
bool m_bShowSysTime;
5454
ltc::Type m_TypePrevious { ltc::Type::INVALID };
5555
uint32_t m_nMidiQuarterFramePiece { 0 };
56+
uint32_t m_nRtpMidiQuarterFramePiece { 0 };
5657
char m_aTimeCode[ltc::timecode::CODE_MAX_LENGTH];
5758
char m_aSystemTime[ltc::timecode::SYSTIME_MAX_LENGTH];
5859
int32_t m_nSecondsPrevious { 60 };

‎lib-ltc/src/arm/artnetreader.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ void ArtNetReader::Handler(const struct artnet::TimeCode *ArtNetTimeCode) {
8282
LtcSender::Get()->SetTimeCode(reinterpret_cast<const struct ltc::TimeCode *>(ArtNetTimeCode));
8383
}
8484

85-
if (!ltc::g_DisabledOutputs.bRtpMidi) {
86-
RtpMidi::Get()->SendTimeCode(reinterpret_cast<const struct midi::Timecode *>(ArtNetTimeCode));
87-
}
88-
8985
if (!ltc::g_DisabledOutputs.bEtc) {
9086
LtcEtc::Get()->Send(reinterpret_cast<const struct midi::Timecode *>(ArtNetTimeCode));
9187
}

‎lib-ltc/src/arm/ltcetcreader.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ void LtcEtcReader::Handler(const midi::Timecode *pTimeCode) {
8484
ArtNetNode::Get()->SendTimeCode(reinterpret_cast<const struct artnet::TimeCode *>(pTimeCode));
8585
}
8686

87-
if (!ltc::g_DisabledOutputs.bRtpMidi) {
88-
RtpMidi::Get()->SendTimeCode(pTimeCode);
89-
}
90-
9187
memcpy(&m_MidiTimeCode, pTimeCode, sizeof(struct midi::Timecode));
9288

9389
LtcOutputs::Get()->Update(reinterpret_cast<const struct ltc::TimeCode*>(pTimeCode));

‎lib-ltc/src/arm/ltcgenerator.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -748,10 +748,6 @@ void LtcGenerator::Update() {
748748
ArtNetNode::Get()->SendTimeCode(reinterpret_cast<const struct artnet::TimeCode*>(&g_ltc_LtcTimeCode));
749749
}
750750

751-
if (!ltc::g_DisabledOutputs.bRtpMidi) {
752-
RtpMidi::Get()->SendTimeCode(reinterpret_cast<const struct midi::Timecode *>(&g_ltc_LtcTimeCode));
753-
}
754-
755751
if (!ltc::g_DisabledOutputs.bEtc) {
756752
LtcEtc::Get()->Send(reinterpret_cast<const struct midi::Timecode *>(&g_ltc_LtcTimeCode));
757753
}

‎lib-ltc/src/arm/ltcmidisystemrealtime.cpp

+27-29
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
* THE SOFTWARE.
2424
*/
2525

26+
#if defined (DEBUG_ARM_LTCMIDISYSTEMREALTIME)
27+
# undef NDEBUG
28+
#endif
29+
2630
#pragma GCC push_options
2731
#pragma GCC optimize ("O2")
2832
#pragma GCC optimize ("no-tree-loop-distribute-patterns")
@@ -74,7 +78,7 @@ static void timer_handler() {
7478
#endif
7579

7680
void LtcMidiSystemRealtime::Start() {
77-
m_nHandle = Network::Get()->Begin(udp::PORT);
81+
m_nHandle = Network::Get()->Begin(udp::PORT, staticCallbackFunctionInput);
7882
assert(m_nHandle != -1);
7983
}
8084

@@ -104,67 +108,61 @@ void LtcMidiSystemRealtime::SetBPM(uint32_t nBPM) {
104108
}
105109
}
106110

107-
void LtcMidiSystemRealtime::Run() {
108-
uint32_t nIPAddressFrom;
109-
uint16_t nForeignPort;
110-
111-
auto nBytesReceived = Network::Get()->RecvFrom(m_nHandle, const_cast<const void **>(reinterpret_cast<void **>(&s_pUdpBuffer)), &nIPAddressFrom, &nForeignPort);
112-
113-
if (__builtin_expect((nBytesReceived < 9), 1)) {
114-
return;
115-
}
116-
117-
if (__builtin_expect((memcmp("midi!", s_pUdpBuffer, 5) != 0), 0)) {
111+
void LtcMidiSystemRealtime::Input(const uint8_t *pBuffer, uint32_t nSize, [[maybe_unused]] uint32_t nFromIp, [[maybe_unused]] uint16_t nFromPort) {
112+
if (__builtin_expect((memcmp("midi!", pBuffer, 5) != 0), 0)) {
118113
return;
119114
}
120115

121-
if (s_pUdpBuffer[nBytesReceived - 1] == '\n') {
122-
nBytesReceived--;
116+
if (pBuffer[nSize - 1] == '\n') {
117+
nSize--;
123118
}
124119

125-
debug_dump(s_pUdpBuffer, nBytesReceived);
120+
debug_dump(pBuffer, nSize);
126121

127-
if (nBytesReceived == (5 + length::START)) {
128-
if (memcmp(&s_pUdpBuffer[5], cmd::START, length::START) == 0) {
122+
if (nSize == (5 + length::START)) {
123+
if (memcmp(&pBuffer[5], cmd::START, length::START) == 0) {
129124
SendStart();
130125
DEBUG_PUTS("Start");
131126
return;
132127
}
133128
}
134129

135-
if (nBytesReceived == (5 + length::STOP)) {
136-
if (memcmp(&s_pUdpBuffer[5], cmd::STOP, length::STOP) == 0) {
130+
if (nSize == (5 + length::STOP)) {
131+
if (memcmp(&pBuffer[5], cmd::STOP, length::STOP) == 0) {
137132
SendStop();
138133
DEBUG_PUTS("Stop");
139134
return;
140135
}
141136
}
142137

143-
if (nBytesReceived == (5 + length::CONTINUE)) {
144-
if (memcmp(&s_pUdpBuffer[5], cmd::CONTINUE, length::CONTINUE) == 0) {
138+
if (nSize == (5 + length::CONTINUE)) {
139+
if (memcmp(&pBuffer[5], cmd::CONTINUE, length::CONTINUE) == 0) {
145140
SendContinue();
146141
DEBUG_PUTS("Continue");
147142
return;
148143
}
149144
}
150145

151-
if (nBytesReceived == (5 + length::BPM + 3)) {
152-
if (memcmp(&s_pUdpBuffer[5], cmd::BPM, length::BPM) == 0) {
146+
if (nSize == (5 + length::BPM + 3)) {
147+
if (memcmp(&pBuffer[5], cmd::BPM, length::BPM) == 0) {
153148
uint32_t nOfffset = 5 + length::BPM;
154149
uint32_t nBPM;
155150

156-
if (isdigit(s_pUdpBuffer[nOfffset])) {
157-
nBPM = 100U * static_cast<uint32_t>(s_pUdpBuffer[nOfffset++] - '0');
158-
if (isdigit(s_pUdpBuffer[nOfffset])) {
159-
nBPM += 10U * static_cast<uint32_t>(s_pUdpBuffer[nOfffset++] - '0');
160-
if (isdigit(s_pUdpBuffer[nOfffset])) {
161-
nBPM += static_cast<uint32_t>(s_pUdpBuffer[nOfffset++] - '0');
151+
if (isdigit(pBuffer[nOfffset])) {
152+
nBPM = 100U * static_cast<uint32_t>(pBuffer[nOfffset++] - '0');
153+
154+
if (isdigit(pBuffer[nOfffset])) {
155+
nBPM += 10U * static_cast<uint32_t>(pBuffer[nOfffset++] - '0');
156+
157+
if (isdigit(pBuffer[nOfffset])) {
158+
nBPM += static_cast<uint32_t>(pBuffer[nOfffset++] - '0');
162159
SetBPM(nBPM);
163160
ShowBPM(nBPM);
164161
DEBUG_PRINTF("BPM: %u", nBPM);
165162
}
166163
}
167164
}
165+
168166
return;
169167
}
170168
}

‎lib-ltc/src/arm/ltcoutputs.cpp

+18-6
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ LtcOutputs::LtcOutputs(const ltc::Source source, const bool bShowSysTime): m_bSh
103103
}
104104

105105
void LtcOutputs::Init() {
106-
if (!ltc::g_DisabledOutputs.bMidi) {
106+
if ((!ltc::g_DisabledOutputs.bMidi) || (!ltc::g_DisabledOutputs.bRtpMidi)) {
107107
#if defined (H3)
108108
irq_timer_set(IRQ_TIMER_1, static_cast<thunk_irq_timer_t>(irq_timer1_midi_handler));
109109
#elif defined (GD32)
@@ -119,13 +119,13 @@ void LtcOutputs::Init() {
119119
void LtcOutputs::Update(const struct ltc::TimeCode *ptLtcTimeCode) {
120120
assert(ptLtcTimeCode != nullptr);
121121

122-
if (!ltc::g_DisabledOutputs.bNtp) {
123-
NtpServer::Get()->SetTimeCode(ptLtcTimeCode);
124-
}
125-
126122
if (ptLtcTimeCode->nType != static_cast<uint8_t>(m_TypePrevious)) {
127123
m_TypePrevious = static_cast<ltc::Type>(ptLtcTimeCode->nType);
128124

125+
if (!ltc::g_DisabledOutputs.bRtpMidi) {
126+
RtpMidi::Get()->SendTimeCode(reinterpret_cast<const struct midi::Timecode *>(ptLtcTimeCode));
127+
}
128+
129129
if (!ltc::g_DisabledOutputs.bMidi) {
130130
Midi::Get()->SendTimeCode(reinterpret_cast<const struct midi::Timecode *>(ptLtcTimeCode));
131131
}
@@ -140,6 +140,7 @@ void LtcOutputs::Update(const struct ltc::TimeCode *ptLtcTimeCode) {
140140
#endif
141141

142142
m_nMidiQuarterFramePiece = 0;
143+
m_nRtpMidiQuarterFramePiece = 0;
143144

144145
if (!ltc::g_DisabledOutputs.bOled) {
145146
Display::Get()->TextLine(2, ltc::get_type(static_cast<ltc::Type>(ptLtcTimeCode->nType)), ltc::timecode::TYPE_MAX_LENGTH);
@@ -154,6 +155,10 @@ void LtcOutputs::Update(const struct ltc::TimeCode *ptLtcTimeCode) {
154155
m_aTimeCode[ltc::timecode::index::COLON_3] = (ptLtcTimeCode->nType != static_cast<uint8_t>(ltc::Type::DF) ? ':' : ';');
155156
}
156157

158+
if (!ltc::g_DisabledOutputs.bNtp) {
159+
NtpServer::Get()->SetTimeCode(ptLtcTimeCode);
160+
}
161+
157162
ltc::itoa_base10(ptLtcTimeCode, m_aTimeCode);
158163

159164
if (!ltc::g_DisabledOutputs.bOled) {
@@ -176,7 +181,14 @@ void LtcOutputs::UpdateMidiQuarterFrameMessage(const struct ltc::TimeCode *pltcT
176181

177182
if (__builtin_expect((sv_isMidiQuarterFrameMessage), 0)) {
178183
sv_isMidiQuarterFrameMessage = false;
179-
Midi::Get()->SendQf(reinterpret_cast<const struct midi::Timecode *>(pltcTimeCode), m_nMidiQuarterFramePiece);
184+
185+
if (!ltc::g_DisabledOutputs.bRtpMidi) {
186+
RtpMidi::Get()->SendQf(reinterpret_cast<const struct midi::Timecode *>(pltcTimeCode), m_nRtpMidiQuarterFramePiece);
187+
}
188+
189+
if (!ltc::g_DisabledOutputs.bMidi) {
190+
Midi::Get()->SendQf(reinterpret_cast<const struct midi::Timecode *>(pltcTimeCode), m_nMidiQuarterFramePiece);
191+
}
180192
}
181193
}
182194

‎lib-ltc/src/arm/ltcreader.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -292,19 +292,13 @@ void LtcReader::Run() {
292292
ArtNetNode::Get()->SendTimeCode(reinterpret_cast<const struct artnet::TimeCode*>(&ltcTimeCode));
293293
}
294294

295-
if (!ltc::g_DisabledOutputs.bRtpMidi) {
296-
RtpMidi::Get()->SendTimeCode(reinterpret_cast<const struct midi::Timecode *>(const_cast<struct midi::Timecode *>(&s_midiTimeCode)));
297-
}
298-
299295
if (!ltc::g_DisabledOutputs.bEtc) {
300296
LtcEtc::Get()->Send(reinterpret_cast<const struct midi::Timecode *>(const_cast<struct midi::Timecode *>(&s_midiTimeCode)));
301297
}
302298

303299
if (m_nTypePrevious != TimeCodeType) {
304300
m_nTypePrevious = TimeCodeType;
305301

306-
Midi::Get()->SendTimeCode(reinterpret_cast<const struct midi::Timecode *>(const_cast<struct midi::Timecode *>(&s_midiTimeCode)));
307-
308302
#if defined (H3)
309303
H3_TIMER->TMR1_INTV = TimeCodeConst::TMR_INTV[static_cast<uint32_t>(TimeCodeType)] / 4;
310304
H3_TIMER->TMR1_CTRL |= (TIMER_CTRL_EN_START | TIMER_CTRL_RELOAD);
@@ -314,7 +308,7 @@ void LtcReader::Run() {
314308
#endif
315309
}
316310

317-
LtcOutputs::Get()->Update(reinterpret_cast<const struct ltc::TimeCode*>(&ltcTimeCode));
311+
LtcOutputs::Get()->Update(reinterpret_cast<const struct ltc::TimeCode *>(&ltcTimeCode));
318312
}
319313

320314
__DMB();

‎lib-ltc/src/arm/midireader.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,6 @@ void MidiReader::Update() {
153153
ArtNetNode::Get()->SendTimeCode(reinterpret_cast<const struct artnet::TimeCode*>(&m_MidiTimeCode));
154154
}
155155

156-
if (!ltc::g_DisabledOutputs.bRtpMidi) {
157-
RtpMidi::Get()->SendTimeCode(&m_MidiTimeCode);
158-
}
159-
160156
if (!ltc::g_DisabledOutputs.bEtc) {
161157
LtcEtc::Get()->Send(&m_MidiTimeCode);
162158
}

‎lib-ltc/src/arm/systimereader.cpp

-8
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,6 @@ void SystimeReader::ActionSetRate(const char *pTimeCodeRate) {
175175
ArtNetNode::Get()->SendTimeCode(reinterpret_cast<const struct artnet::TimeCode*>(&m_MidiTimeCode));
176176
}
177177

178-
if (!ltc::g_DisabledOutputs.bRtpMidi) {
179-
RtpMidi::Get()->SendTimeCode(reinterpret_cast<const struct midi::Timecode *>(&m_MidiTimeCode));
180-
}
181-
182178
if (!ltc::g_DisabledOutputs.bEtc) {
183179
LtcEtc::Get()->Send(&m_MidiTimeCode);
184180
}
@@ -285,10 +281,6 @@ void SystimeReader::Run() {
285281
ArtNetNode::Get()->SendTimeCode(reinterpret_cast<const struct artnet::TimeCode *>(&m_MidiTimeCode));
286282
}
287283

288-
if (!ltc::g_DisabledOutputs.bRtpMidi) {
289-
RtpMidi::Get()->SendTimeCode(reinterpret_cast<const struct midi::Timecode *>(&m_MidiTimeCode));
290-
}
291-
292284
if (!ltc::g_DisabledOutputs.bEtc) {
293285
LtcEtc::Get()->Send(&m_MidiTimeCode);
294286
}

‎lib-ltc/src/arm/tcnetreader.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@ void TCNetReader::Handler(const struct tcnet::TimeCode *pTimeCode) {
119119
ArtNetNode::Get()->SendTimeCode(reinterpret_cast<const struct artnet::TimeCode*>(pTimeCode));
120120
}
121121

122-
if (!ltc::g_DisabledOutputs.bRtpMidi) {
123-
RtpMidi::Get()->SendTimeCode(reinterpret_cast<const struct midi::Timecode *>(pTimeCode));
124-
}
125-
126122
if (!ltc::g_DisabledOutputs.bEtc) {
127123
LtcEtc::Get()->Send(&m_MidiTimeCode);
128124
}

‎lib-midi/.cproject

+3
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@
207207
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-h3/CMSIS/Core_A/Include}&quot;"/>
208208
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-h3/include}&quot;"/>
209209
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-arm/include}&quot;"/>
210+
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-network/include}&quot;"/>
210211
</option>
211212
<inputType id="ilg.gnuarmeclipse.managedbuild.cross.tool.assembler.input.57520852" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.assembler.input"/>
212213
</tool>
@@ -223,6 +224,7 @@
223224
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-h3/CMSIS/Core_A/Include}&quot;"/>
224225
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-h3/include}&quot;"/>
225226
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-arm/include}&quot;"/>
227+
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-network/include}&quot;"/>
226228
</option>
227229
<inputType id="ilg.gnuarmeclipse.managedbuild.cross.tool.c.compiler.input.749519629" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.c.compiler.input"/>
228230
</tool>
@@ -242,6 +244,7 @@
242244
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-hal/include}&quot;"/>
243245
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/include}&quot;"/>
244246
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-arm/include}&quot;"/>
247+
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lib-network/include}&quot;"/>
245248
</option>
246249
<inputType id="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.compiler.input.290390095" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.compiler.input"/>
247250
</tool>

‎lib-midi/.settings/language.settings.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
66
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
77
<provider copy-of="extension" id="org.eclipse.cdt.managedbuilder.core.GCCBuildCommandParser"/>
8-
<provider class="org.eclipse.cdt.internal.build.crossgcc.CrossGCCBuiltinSpecsDetector" console="false" env-hash="-868907429938189684" id="org.eclipse.cdt.build.crossgcc.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
8+
<provider class="org.eclipse.cdt.internal.build.crossgcc.CrossGCCBuiltinSpecsDetector" console="false" env-hash="-868850457862413684" id="org.eclipse.cdt.build.crossgcc.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
99
<language-scope id="org.eclipse.cdt.core.gcc"/>
1010
<language-scope id="org.eclipse.cdt.core.g++"/>
1111
</provider>
@@ -14,7 +14,7 @@
1414
</configuration>
1515
<configuration id="ilg.gnuarmeclipse.managedbuild.cross.toolchain.base.309283989.303033930.1102356485.534880544.1199730404" name="H3">
1616
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
17-
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="true" env-hash="1410606877957839909" id="org.eclipse.embedcdt.managedbuild.cross.arm.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Arm Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
17+
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="true" env-hash="1326063933164894935" id="org.eclipse.embedcdt.managedbuild.cross.arm.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Arm Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
1818
<language-scope id="org.eclipse.cdt.core.gcc"/>
1919
<language-scope id="org.eclipse.cdt.core.g++"/>
2020
</provider>

‎lib-midi/include/applemidi.h

+30-7
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
#include "debug.h"
4545

4646
namespace applemidi {
47-
static constexpr auto UPD_PORT_CONTROL_DEFAULT = 5004U;
48-
static constexpr auto UPD_PORT_MIDI_DEFAULT = UPD_PORT_CONTROL_DEFAULT + 1U;
4947
static constexpr auto SESSION_NAME_LENGTH_MAX = 24;
5048
static constexpr auto VERSION = 2;
5149

@@ -73,6 +71,9 @@ static constexpr auto EXCHANGE_PACKET_MIN_LENGTH = sizeof(struct applemidi::Exch
7371
} // namespace applemidi
7472

7573
class AppleMidi {
74+
static constexpr uint16_t UPD_PORT_CONTROL_DEFAULT = 5004;
75+
static constexpr uint16_t UPD_PORT_MIDI_DEFAULT = UPD_PORT_CONTROL_DEFAULT + 1;
76+
static constexpr uint16_t SIGNATURE = 0xffff;
7677
public:
7778
AppleMidi();
7879

@@ -82,8 +83,6 @@ class AppleMidi {
8283

8384
void Start() {
8485
DEBUG_ENTRY
85-
// assert(MDNS::Get() != nullptr);
86-
// MDNS::Get()->ServiceRecordAdd(nullptr, mdns::Services::MIDI, nullptr, m_nPort);
8786
mdns_service_record_add(nullptr, mdns::Services::MIDI, nullptr, m_nPort);
8887

8988
m_nHandleControl = Network::Get()->Begin(m_nPort);
@@ -108,9 +107,33 @@ class AppleMidi {
108107
DEBUG_EXIT
109108
}
110109

111-
void Run();
110+
void Run() {
111+
m_nBytesReceived = Network::Get()->RecvFrom(m_nHandleMidi, const_cast<const void **>(reinterpret_cast<void **>(&m_pBuffer)), &m_nRemoteIp, &m_nRemotePort);
112112

113-
void SetPort(uint16_t nPort) {
113+
if (__builtin_expect((m_nBytesReceived >= 12), 0)) {
114+
if (m_SessionStatus.nRemoteIp == m_nRemoteIp) {
115+
HandleMidiMessage();
116+
}
117+
}
118+
119+
m_nBytesReceived = Network::Get()->RecvFrom(m_nHandleControl, const_cast<const void **>(reinterpret_cast<void **>(&m_pBuffer)), &m_nRemoteIp, &m_nRemotePort);
120+
121+
if (__builtin_expect((m_nBytesReceived >= applemidi::EXCHANGE_PACKET_MIN_LENGTH), 0)) {
122+
if (*reinterpret_cast<uint16_t *>(m_pBuffer) == SIGNATURE) {
123+
HandleControlMessage();
124+
}
125+
}
126+
127+
if (m_SessionStatus.sessionState == applemidi::SessionState::ESTABLISHED) {
128+
if (__builtin_expect((Hardware::Get()->Millis() - m_SessionStatus.nSynchronizationTimestamp > (90 * 1000)), 0)) {
129+
m_SessionStatus.sessionState = applemidi::SessionState::WAITING_IN_CONTROL;
130+
m_SessionStatus.nRemoteIp = 0;
131+
DEBUG_PUTS("End Session {time-out}");
132+
}
133+
}
134+
}
135+
136+
void SetPort(const uint16_t nPort) {
114137
assert(nPort > 1024);
115138
m_nPort = nPort;
116139
}
@@ -165,7 +188,7 @@ class AppleMidi {
165188
uint32_t m_nRemoteIp { 0 };
166189
uint32_t m_nBytesReceived { 0 };
167190
uint16_t m_nExchangePacketReplySize;
168-
uint16_t m_nPort { applemidi::UPD_PORT_CONTROL_DEFAULT };
191+
uint16_t m_nPort { UPD_PORT_CONTROL_DEFAULT };
169192
uint16_t m_nRemotePort { 0 };
170193
applemidi::ExchangePacket m_ExchangePacketReply;
171194
applemidi::SessionStatus m_SessionStatus;

‎lib-midi/include/rtpmidi.h

+54-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file rtpmidi.h
33
*
44
*/
5-
/* Copyright (C) 2019-2023 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2019-2024 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -86,14 +86,14 @@ class RtpMidi final: public AppleMidi {
8686
AppleMidi::Run();
8787
}
8888

89-
void SendRaw(uint8_t nByte) {
89+
void SendRaw(const uint8_t nByte) {
9090
auto *data = &m_pSendBuffer[rtpmidi::COMMAND_OFFSET + 1];
9191
data[0] = nByte;
9292
Send(1);
9393
}
9494

95-
void SendRaw(midi::Types tType) {
96-
SendRaw(static_cast<uint8_t>(tType));
95+
void SendRaw(const midi::Types type) {
96+
SendRaw(static_cast<uint8_t>(type));
9797
}
9898

9999
void SendTimeCode(const midi::Timecode *tTimeCode) {
@@ -113,6 +113,52 @@ class RtpMidi final: public AppleMidi {
113113
Send(10);
114114
}
115115

116+
void SendQf(const uint8_t nValue) {
117+
auto *data = &m_pSendBuffer[rtpmidi::COMMAND_OFFSET + 1];
118+
119+
data[0] = 0xF1;
120+
data[1] = nValue;
121+
122+
Send(2);
123+
}
124+
125+
void SendQf(const struct midi::Timecode *timeCode, uint32_t& nMidiQuarterFramePiece) {
126+
auto data = static_cast<uint8_t>(nMidiQuarterFramePiece << 4);
127+
128+
switch (nMidiQuarterFramePiece) {
129+
case 0:
130+
data = data | (timeCode->nFrames & 0x0F);
131+
break;
132+
case 1:
133+
data = data | static_cast<uint8_t>((timeCode->nFrames & 0x10) >> 4);
134+
break;
135+
case 2:
136+
data = data | (timeCode->nSeconds & 0x0F);
137+
break;
138+
case 3:
139+
data = data | static_cast<uint8_t>((timeCode->nSeconds & 0x30) >> 4);
140+
break;
141+
case 4:
142+
data = data | (timeCode->nMinutes & 0x0F);
143+
break;
144+
case 5:
145+
data = data | static_cast<uint8_t>((timeCode->nMinutes & 0x30) >> 4);
146+
break;
147+
case 6:
148+
data = data | (timeCode->nHours & 0x0F);
149+
break;
150+
case 7:
151+
data = static_cast<uint8_t>(data | (timeCode->nType << 1) | ((timeCode->nHours & 0x10) >> 4));
152+
break;
153+
default:
154+
break;
155+
}
156+
157+
SendQf(data);
158+
159+
nMidiQuarterFramePiece = (nMidiQuarterFramePiece + 1) & 0x07;
160+
}
161+
116162
void SetHandler(RtpMidiHandler *pRtpMidiHandler) {
117163
m_pRtpMidiHandler = pRtpMidiHandler;
118164
}
@@ -121,7 +167,7 @@ class RtpMidi final: public AppleMidi {
121167
AppleMidi::Print();
122168
}
123169

124-
static RtpMidi* Get() {
170+
static RtpMidi *Get() {
125171
return s_pThis;
126172
}
127173

@@ -131,7 +177,7 @@ class RtpMidi final: public AppleMidi {
131177
int32_t DecodeTime(uint32_t nCommandLength, uint32_t nOffset);
132178
int32_t DecodeMidi(uint32_t nCommandLength, uint32_t nOffset);
133179

134-
midi::Types GetTypeFromStatusByte(uint8_t nStatusByte) {
180+
midi::Types GetTypeFromStatusByte(const uint8_t nStatusByte) {
135181
if ((nStatusByte < 0x80) || (nStatusByte == 0xf4) || (nStatusByte == 0xf5) || (nStatusByte == 0xf9) || (nStatusByte == 0xfD)) {
136182
return midi::Types::INVALIDE_TYPE;
137183
}
@@ -143,7 +189,7 @@ class RtpMidi final: public AppleMidi {
143189
return static_cast<midi::Types>(nStatusByte);
144190
}
145191

146-
uint8_t GetChannelFromStatusByte(uint8_t nStatusByte) {
192+
uint8_t GetChannelFromStatusByte(const uint8_t nStatusByte) {
147193
return static_cast<uint8_t>((nStatusByte & 0x0F) + 1);
148194
}
149195

@@ -165,7 +211,7 @@ class RtpMidi final: public AppleMidi {
165211
uint8_t *m_pSendBuffer { nullptr };
166212
uint16_t m_nSequenceNumber { 0 };
167213

168-
static RtpMidi *s_pThis;
214+
static inline RtpMidi *s_pThis;
169215
};
170216

171217
#endif /* RTPMIDI_H_ */

‎lib-midi/src/net/applemidi.cpp

+4-30
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
#include "debug.h"
4747

4848
namespace applemidi {
49-
static constexpr uint16_t SIGNATURE = 0xffff;
49+
5050
} // namespace applemidi
5151

5252
enum TAppleMidiCommand {
@@ -71,7 +71,7 @@ struct TTimestampSynchronization {
7171
AppleMidi::AppleMidi() : m_nSSRC(Network::Get()->GetIp()), m_nExchangePacketReplySize(applemidi::EXCHANGE_PACKET_MIN_LENGTH) {
7272
DEBUG_ENTRY
7373

74-
m_ExchangePacketReply.nSignature = applemidi::SIGNATURE;
74+
m_ExchangePacketReply.nSignature = SIGNATURE;
7575
m_ExchangePacketReply.nProtocolVersion = __builtin_bswap32(applemidi::VERSION);
7676
m_ExchangePacketReply.nSSRC = m_nSSRC;
7777

@@ -148,14 +148,14 @@ void AppleMidi::HandleMidiMessage() {
148148

149149
debug_dump(m_pBuffer, m_nBytesReceived);
150150

151-
if (*reinterpret_cast<uint16_t*>(m_pBuffer) == 0x6180) {
151+
if (*reinterpret_cast<uint16_t *>(m_pBuffer) == 0x6180) {
152152
HandleRtpMidi(m_pBuffer);
153153
return;
154154
}
155155

156156
if (m_nBytesReceived >= applemidi::EXCHANGE_PACKET_MIN_LENGTH) {
157157

158-
if (*reinterpret_cast<uint16_t *>(m_pBuffer) == applemidi::SIGNATURE) {
158+
if (*reinterpret_cast<uint16_t *>(m_pBuffer) == SIGNATURE) {
159159

160160
if (m_SessionStatus.sessionState == applemidi::SessionState::WAITING_IN_MIDI) {
161161
DEBUG_PUTS("SESSION_STATE_WAITING_IN_MIDI");
@@ -219,29 +219,3 @@ void AppleMidi::HandleMidiMessage() {
219219

220220
DEBUG_EXIT
221221
}
222-
223-
void AppleMidi::Run() {
224-
m_nBytesReceived = Network::Get()->RecvFrom(m_nHandleMidi, const_cast<const void **>(reinterpret_cast<void **>(&m_pBuffer)), &m_nRemoteIp, &m_nRemotePort);
225-
226-
if (__builtin_expect((m_nBytesReceived >= 12), 0)) {
227-
if (m_SessionStatus.nRemoteIp == m_nRemoteIp) {
228-
HandleMidiMessage();
229-
}
230-
}
231-
232-
m_nBytesReceived = Network::Get()->RecvFrom(m_nHandleControl, const_cast<const void **>(reinterpret_cast<void **>(&m_pBuffer)), &m_nRemoteIp, &m_nRemotePort);
233-
234-
if (__builtin_expect((m_nBytesReceived >= applemidi::EXCHANGE_PACKET_MIN_LENGTH), 0)) {
235-
if (*reinterpret_cast<uint16_t *>(m_pBuffer) == applemidi::SIGNATURE) {
236-
HandleControlMessage();
237-
}
238-
}
239-
240-
if (m_SessionStatus.sessionState == applemidi::SessionState::ESTABLISHED) {
241-
if (__builtin_expect((Hardware::Get()->Millis() - m_SessionStatus.nSynchronizationTimestamp > (90 * 1000)), 0)) {
242-
m_SessionStatus.sessionState = applemidi::SessionState::WAITING_IN_CONTROL;
243-
m_SessionStatus.nRemoteIp = 0;
244-
DEBUG_PUTS("End Session {time-out}");
245-
}
246-
}
247-
}

‎lib-midi/src/net/rtpmidi.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file rtpmidi.cpp
33
*
44
*/
5-
/* Copyright (C) 2019-2023 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2019-2024 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -47,8 +47,6 @@
4747
#define RTP_MIDI_CS_MASK_SHORTLEN 0x0f
4848
#define RTP_MIDI_CS_MASK_LONGLEN 0x0fff
4949

50-
RtpMidi *RtpMidi::s_pThis = nullptr;
51-
5250
int32_t RtpMidi::DecodeTime([[maybe_unused]] uint32_t nCommandLength, uint32_t nOffset) {
5351
DEBUG_ENTRY
5452

‎opi_emac_ltc_smpte/.settings/language.settings.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
66
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
77
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuildCommandParser" id="org.eclipse.cdt.managedbuilder.core.GCCBuildCommandParser" keep-relative-paths="false" name="CDT GCC Build Output Parser" parameter="([^/\\\\]*)((g?cc)|([gc]\+\+)|(clang))" prefer-non-shared="true"/>
8-
<provider class="org.eclipse.cdt.internal.build.crossgcc.CrossGCCBuiltinSpecsDetector" console="false" env-hash="-868907429938189684" id="org.eclipse.cdt.build.crossgcc.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
8+
<provider class="org.eclipse.cdt.internal.build.crossgcc.CrossGCCBuiltinSpecsDetector" console="false" env-hash="-868850457862413684" id="org.eclipse.cdt.build.crossgcc.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
99
<language-scope id="org.eclipse.cdt.core.gcc"/>
1010
<language-scope id="org.eclipse.cdt.core.g++"/>
1111
</provider>

‎opi_emac_ltc_smpte/Makefile.H3

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ PLATFORM=ORANGE_PI
33
DEFINES =NODE_LTC_SMPTE ARM_ALLOW_MULTI_CORE
44
DEFINES+=CONFIG_LTC_USE_DAC
55

6-
DEFINES+=CONFIG_NTP_CLIENT_POLL_POWER=3
6+
#DEFINES+=CONFIG_NTP_CLIENT_POLL_POWER=3
77

88
DEFINES+=CONFIG_DISPLAY_ENABLE_SSD1311 CONFIG_DISPLAY_ENABLE_HD44780 CONFIG_DISPLAY_ENABLE_CURSOR_MODE
99

@@ -19,6 +19,7 @@ DEFINES+=ENABLE_HTTPD ENABLE_CONTENT
1919
#DEFINES+=ENABLE_SHELL UART0_ECHO LTC_READER
2020

2121
#DEFINES+=DEBUG_ARM_LTCGENERATOR
22+
#DEFINES+=DEBUG_ARM_LTCREADER
2223
#DEFINES+=DEBUG_LTCOSCSERVER
2324

2425
DEFINES+=NDEBUG

‎opi_emac_ltc_smpte/firmware/main.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ extern "C" {
140140
void h3_cpu_off(uint32_t);
141141
}
142142

143-
void static staticCallbackFunction([[maybe_unused]] const struct artnet::TimeCode *pTimeCode) {}
143+
void static staticCallbackFunction([[maybe_unused]] const struct artnet::TimeCode *pTimeCode) {
144+
145+
}
144146

145147
int main() {
146148
Hardware hw;
@@ -576,10 +578,6 @@ int main() {
576578
ntpClient.Run(); // We could check for GPS Time client running. But not really needed.
577579
}
578580

579-
if (bRunMidiSystemRealtime) {
580-
ltcMidiSystemRealtime.Run(); // UDP requests
581-
}
582-
583581
if (ltc::g_DisabledOutputs.bOled) {
584582
display.Run();
585583
}

‎opi_emac_ltc_smpte/lib/mcpbuttons.cpp

+10-12
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@
2727
#include <cstdio>
2828
#include <cassert>
2929

30-
#include "mcpbuttons.h"
3130
#include "hardware.h"
3231
#include "network.h"
3332

34-
#include "arm/synchronize.h"
33+
#include "mcpbuttons.h"
3534

3635
#include "ltcdisplayrgb.h"
3736
#include "ltcparams.h"
@@ -41,6 +40,7 @@
4140
#include "rotaryencoder.h"
4241

4342
#include "hal_i2c.h"
43+
#include "hal_gpio.h"
4444
#include "mcp23x17.h"
4545

4646
#include "displayedittimecode.h"
@@ -54,10 +54,6 @@
5454
#include "arm/ltcgenerator.h"
5555
#include "arm/systimereader.h"
5656

57-
// Interrupt
58-
#include "board/h3_opi_zero.h"
59-
#include "h3_gpio.h"
60-
6157
#include "configstore.h"
6258

6359
#include "debug.h"
@@ -177,16 +173,18 @@ void McpButtons::UpdateDisplays(const ltc::Source ltcSource) {
177173
LtcDisplayMax7219::Get()->WriteChar(nSource);
178174
return;
179175
}
180-
176+
#if !defined (CONFIG_LTC_DISABLE_WS28XX)
181177
if (!ltc::g_DisabledOutputs.bWS28xx){
182178
LtcDisplayRgb::Get()->WriteChar(nSource);
183179
return;
184180
}
185-
181+
#endif
182+
#if !defined (CONFIG_LTC_DISABLE_RGB_PANEL)
186183
if (!ltc::g_DisabledOutputs.bRgbPanel) {
187184
LtcDisplayRgb::Get()->ShowSource(ltcSource);
188185
return;
189186
}
187+
#endif
190188
}
191189

192190
bool McpButtons::Check() {
@@ -212,8 +210,8 @@ bool McpButtons::Check() {
212210

213211
UpdateDisplays(m_tLtcReaderSource);
214212

215-
h3_gpio_fsel(gpio::INTA, GPIO_FSEL_INPUT); // PA7
216-
h3_gpio_set_pud(gpio::INTA, GPIO_PULL_UP);
213+
FUNC_PREFIX(gpio_fsel(gpio::INTA, GPIO_FSEL_INPUT));
214+
FUNC_PREFIX(gpio_set_pud(gpio::INTA, GPIO_PULL_UP));
217215

218216
DEBUG_EXIT
219217
return true;
@@ -227,7 +225,7 @@ bool McpButtons::Wait(ltc::Source& ltcSource, struct ltc::TimeCode& StartTimeCod
227225
return false;
228226
}
229227

230-
if (__builtin_expect(h3_gpio_lev(gpio::INTA) == LOW, 0)) {
228+
if (__builtin_expect(FUNC_PREFIX(gpio_lev(gpio::INTA)) == 0, 0)) {
231229
m_nLedTickerMax = UINT32_MAX;
232230

233231
const auto nPortA = m_I2C.ReadRegister(mcp23x17::REG_GPIOA);
@@ -407,7 +405,7 @@ void McpButtons::Run() {
407405
return;
408406
}
409407

410-
if (__builtin_expect(h3_gpio_lev(gpio::INTA) == LOW, 0)) {
408+
if (__builtin_expect(FUNC_PREFIX(gpio_lev(gpio::INTA)) == 0, 0)) {
411409

412410
const auto nPortA = m_I2C.ReadRegister(mcp23x17::REG_GPIOA);
413411
const uint8_t nButtonsChanged = (nPortA ^ m_nPortAPrevious) & nPortA;

0 commit comments

Comments
 (0)
Please sign in to comment.