diff --git a/lib-ltc/include/arm/ltcmidisystemrealtime.h b/lib-ltc/include/arm/ltcmidisystemrealtime.h index 07a7b363a..dd1c0faf8 100755 --- a/lib-ltc/include/arm/ltcmidisystemrealtime.h +++ b/lib-ltc/include/arm/ltcmidisystemrealtime.h @@ -2,7 +2,7 @@ * @file ltcmidisystemrealtime.h * */ -/* Copyright (C) 2020-2023 by Arjan van Vught mailto:info@gd32-dmx.org +/* Copyright (C) 2020-2024 by Arjan van Vught mailto:info@gd32-dmx.org * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -42,8 +42,6 @@ class LtcMidiSystemRealtime { void Start(); void Stop(); - void Run(); - void SendStart() { Send(midi::Types::START); @@ -59,6 +57,8 @@ class LtcMidiSystemRealtime { void SetBPM(uint32_t nBPM); + void Input(const uint8_t *pBuffer, uint32_t nSize, uint32_t nFromIp, uint16_t nFromPort); + static LtcMidiSystemRealtime *Get() { return s_pThis; } @@ -78,11 +78,14 @@ class LtcMidiSystemRealtime { LtcOutputs::Get()->ShowBPM(nBPM); } + void static staticCallbackFunctionInput(const uint8_t *pBuffer, uint32_t nSize, uint32_t nFromIp, uint16_t nFromPort) { + s_pThis->Input(pBuffer, nSize, nFromIp, nFromPort); + } + private: int32_t m_nHandle { -1 }; uint32_t m_nBPMPrevious { 999 }; - static inline char *s_pUdpBuffer; static inline LtcMidiSystemRealtime *s_pThis; }; diff --git a/lib-ltc/include/arm/ltcoutputs.h b/lib-ltc/include/arm/ltcoutputs.h index eb0139b2e..aa14a2fed 100755 --- a/lib-ltc/include/arm/ltcoutputs.h +++ b/lib-ltc/include/arm/ltcoutputs.h @@ -53,6 +53,7 @@ class LtcOutputs { bool m_bShowSysTime; ltc::Type m_TypePrevious { ltc::Type::INVALID }; uint32_t m_nMidiQuarterFramePiece { 0 }; + uint32_t m_nRtpMidiQuarterFramePiece { 0 }; char m_aTimeCode[ltc::timecode::CODE_MAX_LENGTH]; char m_aSystemTime[ltc::timecode::SYSTIME_MAX_LENGTH]; int32_t m_nSecondsPrevious { 60 }; diff --git a/lib-ltc/src/arm/artnetreader.cpp b/lib-ltc/src/arm/artnetreader.cpp index d54e69baa..24aa2a3b6 100755 --- a/lib-ltc/src/arm/artnetreader.cpp +++ b/lib-ltc/src/arm/artnetreader.cpp @@ -82,10 +82,6 @@ void ArtNetReader::Handler(const struct artnet::TimeCode *ArtNetTimeCode) { LtcSender::Get()->SetTimeCode(reinterpret_cast(ArtNetTimeCode)); } - if (!ltc::g_DisabledOutputs.bRtpMidi) { - RtpMidi::Get()->SendTimeCode(reinterpret_cast(ArtNetTimeCode)); - } - if (!ltc::g_DisabledOutputs.bEtc) { LtcEtc::Get()->Send(reinterpret_cast(ArtNetTimeCode)); } diff --git a/lib-ltc/src/arm/ltcetcreader.cpp b/lib-ltc/src/arm/ltcetcreader.cpp index 2f70248d3..44c55cd68 100755 --- a/lib-ltc/src/arm/ltcetcreader.cpp +++ b/lib-ltc/src/arm/ltcetcreader.cpp @@ -84,10 +84,6 @@ void LtcEtcReader::Handler(const midi::Timecode *pTimeCode) { ArtNetNode::Get()->SendTimeCode(reinterpret_cast(pTimeCode)); } - if (!ltc::g_DisabledOutputs.bRtpMidi) { - RtpMidi::Get()->SendTimeCode(pTimeCode); - } - memcpy(&m_MidiTimeCode, pTimeCode, sizeof(struct midi::Timecode)); LtcOutputs::Get()->Update(reinterpret_cast(pTimeCode)); diff --git a/lib-ltc/src/arm/ltcgenerator.cpp b/lib-ltc/src/arm/ltcgenerator.cpp index fca1ab269..83c332d19 100755 --- a/lib-ltc/src/arm/ltcgenerator.cpp +++ b/lib-ltc/src/arm/ltcgenerator.cpp @@ -748,10 +748,6 @@ void LtcGenerator::Update() { ArtNetNode::Get()->SendTimeCode(reinterpret_cast(&g_ltc_LtcTimeCode)); } - if (!ltc::g_DisabledOutputs.bRtpMidi) { - RtpMidi::Get()->SendTimeCode(reinterpret_cast(&g_ltc_LtcTimeCode)); - } - if (!ltc::g_DisabledOutputs.bEtc) { LtcEtc::Get()->Send(reinterpret_cast(&g_ltc_LtcTimeCode)); } diff --git a/lib-ltc/src/arm/ltcmidisystemrealtime.cpp b/lib-ltc/src/arm/ltcmidisystemrealtime.cpp index 56e74498c..b21fb2f49 100755 --- a/lib-ltc/src/arm/ltcmidisystemrealtime.cpp +++ b/lib-ltc/src/arm/ltcmidisystemrealtime.cpp @@ -23,6 +23,10 @@ * THE SOFTWARE. */ +#if defined (DEBUG_ARM_LTCMIDISYSTEMREALTIME) +# undef NDEBUG +#endif + #pragma GCC push_options #pragma GCC optimize ("O2") #pragma GCC optimize ("no-tree-loop-distribute-patterns") @@ -74,7 +78,7 @@ static void timer_handler() { #endif void LtcMidiSystemRealtime::Start() { - m_nHandle = Network::Get()->Begin(udp::PORT); + m_nHandle = Network::Get()->Begin(udp::PORT, staticCallbackFunctionInput); assert(m_nHandle != -1); } @@ -104,67 +108,61 @@ void LtcMidiSystemRealtime::SetBPM(uint32_t nBPM) { } } -void LtcMidiSystemRealtime::Run() { - uint32_t nIPAddressFrom; - uint16_t nForeignPort; - - auto nBytesReceived = Network::Get()->RecvFrom(m_nHandle, const_cast(reinterpret_cast(&s_pUdpBuffer)), &nIPAddressFrom, &nForeignPort); - - if (__builtin_expect((nBytesReceived < 9), 1)) { - return; - } - - if (__builtin_expect((memcmp("midi!", s_pUdpBuffer, 5) != 0), 0)) { +void LtcMidiSystemRealtime::Input(const uint8_t *pBuffer, uint32_t nSize, [[maybe_unused]] uint32_t nFromIp, [[maybe_unused]] uint16_t nFromPort) { + if (__builtin_expect((memcmp("midi!", pBuffer, 5) != 0), 0)) { return; } - if (s_pUdpBuffer[nBytesReceived - 1] == '\n') { - nBytesReceived--; + if (pBuffer[nSize - 1] == '\n') { + nSize--; } - debug_dump(s_pUdpBuffer, nBytesReceived); + debug_dump(pBuffer, nSize); - if (nBytesReceived == (5 + length::START)) { - if (memcmp(&s_pUdpBuffer[5], cmd::START, length::START) == 0) { + if (nSize == (5 + length::START)) { + if (memcmp(&pBuffer[5], cmd::START, length::START) == 0) { SendStart(); DEBUG_PUTS("Start"); return; } } - if (nBytesReceived == (5 + length::STOP)) { - if (memcmp(&s_pUdpBuffer[5], cmd::STOP, length::STOP) == 0) { + if (nSize == (5 + length::STOP)) { + if (memcmp(&pBuffer[5], cmd::STOP, length::STOP) == 0) { SendStop(); DEBUG_PUTS("Stop"); return; } } - if (nBytesReceived == (5 + length::CONTINUE)) { - if (memcmp(&s_pUdpBuffer[5], cmd::CONTINUE, length::CONTINUE) == 0) { + if (nSize == (5 + length::CONTINUE)) { + if (memcmp(&pBuffer[5], cmd::CONTINUE, length::CONTINUE) == 0) { SendContinue(); DEBUG_PUTS("Continue"); return; } } - if (nBytesReceived == (5 + length::BPM + 3)) { - if (memcmp(&s_pUdpBuffer[5], cmd::BPM, length::BPM) == 0) { + if (nSize == (5 + length::BPM + 3)) { + if (memcmp(&pBuffer[5], cmd::BPM, length::BPM) == 0) { uint32_t nOfffset = 5 + length::BPM; uint32_t nBPM; - if (isdigit(s_pUdpBuffer[nOfffset])) { - nBPM = 100U * static_cast(s_pUdpBuffer[nOfffset++] - '0'); - if (isdigit(s_pUdpBuffer[nOfffset])) { - nBPM += 10U * static_cast(s_pUdpBuffer[nOfffset++] - '0'); - if (isdigit(s_pUdpBuffer[nOfffset])) { - nBPM += static_cast(s_pUdpBuffer[nOfffset++] - '0'); + if (isdigit(pBuffer[nOfffset])) { + nBPM = 100U * static_cast(pBuffer[nOfffset++] - '0'); + + if (isdigit(pBuffer[nOfffset])) { + nBPM += 10U * static_cast(pBuffer[nOfffset++] - '0'); + + if (isdigit(pBuffer[nOfffset])) { + nBPM += static_cast(pBuffer[nOfffset++] - '0'); SetBPM(nBPM); ShowBPM(nBPM); DEBUG_PRINTF("BPM: %u", nBPM); } } } + return; } } diff --git a/lib-ltc/src/arm/ltcoutputs.cpp b/lib-ltc/src/arm/ltcoutputs.cpp index b324a00a9..5d9dd472b 100755 --- a/lib-ltc/src/arm/ltcoutputs.cpp +++ b/lib-ltc/src/arm/ltcoutputs.cpp @@ -103,7 +103,7 @@ LtcOutputs::LtcOutputs(const ltc::Source source, const bool bShowSysTime): m_bSh } void LtcOutputs::Init() { - if (!ltc::g_DisabledOutputs.bMidi) { + if ((!ltc::g_DisabledOutputs.bMidi) || (!ltc::g_DisabledOutputs.bRtpMidi)) { #if defined (H3) irq_timer_set(IRQ_TIMER_1, static_cast(irq_timer1_midi_handler)); #elif defined (GD32) @@ -119,13 +119,13 @@ void LtcOutputs::Init() { void LtcOutputs::Update(const struct ltc::TimeCode *ptLtcTimeCode) { assert(ptLtcTimeCode != nullptr); - if (!ltc::g_DisabledOutputs.bNtp) { - NtpServer::Get()->SetTimeCode(ptLtcTimeCode); - } - if (ptLtcTimeCode->nType != static_cast(m_TypePrevious)) { m_TypePrevious = static_cast(ptLtcTimeCode->nType); + if (!ltc::g_DisabledOutputs.bRtpMidi) { + RtpMidi::Get()->SendTimeCode(reinterpret_cast(ptLtcTimeCode)); + } + if (!ltc::g_DisabledOutputs.bMidi) { Midi::Get()->SendTimeCode(reinterpret_cast(ptLtcTimeCode)); } @@ -140,6 +140,7 @@ void LtcOutputs::Update(const struct ltc::TimeCode *ptLtcTimeCode) { #endif m_nMidiQuarterFramePiece = 0; + m_nRtpMidiQuarterFramePiece = 0; if (!ltc::g_DisabledOutputs.bOled) { Display::Get()->TextLine(2, ltc::get_type(static_cast(ptLtcTimeCode->nType)), ltc::timecode::TYPE_MAX_LENGTH); @@ -154,6 +155,10 @@ void LtcOutputs::Update(const struct ltc::TimeCode *ptLtcTimeCode) { m_aTimeCode[ltc::timecode::index::COLON_3] = (ptLtcTimeCode->nType != static_cast(ltc::Type::DF) ? ':' : ';'); } + if (!ltc::g_DisabledOutputs.bNtp) { + NtpServer::Get()->SetTimeCode(ptLtcTimeCode); + } + ltc::itoa_base10(ptLtcTimeCode, m_aTimeCode); if (!ltc::g_DisabledOutputs.bOled) { @@ -176,7 +181,14 @@ void LtcOutputs::UpdateMidiQuarterFrameMessage(const struct ltc::TimeCode *pltcT if (__builtin_expect((sv_isMidiQuarterFrameMessage), 0)) { sv_isMidiQuarterFrameMessage = false; - Midi::Get()->SendQf(reinterpret_cast(pltcTimeCode), m_nMidiQuarterFramePiece); + + if (!ltc::g_DisabledOutputs.bRtpMidi) { + RtpMidi::Get()->SendQf(reinterpret_cast(pltcTimeCode), m_nRtpMidiQuarterFramePiece); + } + + if (!ltc::g_DisabledOutputs.bMidi) { + Midi::Get()->SendQf(reinterpret_cast(pltcTimeCode), m_nMidiQuarterFramePiece); + } } } diff --git a/lib-ltc/src/arm/ltcreader.cpp b/lib-ltc/src/arm/ltcreader.cpp index 0bae3c848..8798a6678 100755 --- a/lib-ltc/src/arm/ltcreader.cpp +++ b/lib-ltc/src/arm/ltcreader.cpp @@ -292,10 +292,6 @@ void LtcReader::Run() { ArtNetNode::Get()->SendTimeCode(reinterpret_cast(<cTimeCode)); } - if (!ltc::g_DisabledOutputs.bRtpMidi) { - RtpMidi::Get()->SendTimeCode(reinterpret_cast(const_cast(&s_midiTimeCode))); - } - if (!ltc::g_DisabledOutputs.bEtc) { LtcEtc::Get()->Send(reinterpret_cast(const_cast(&s_midiTimeCode))); } @@ -303,8 +299,6 @@ void LtcReader::Run() { if (m_nTypePrevious != TimeCodeType) { m_nTypePrevious = TimeCodeType; - Midi::Get()->SendTimeCode(reinterpret_cast(const_cast(&s_midiTimeCode))); - #if defined (H3) H3_TIMER->TMR1_INTV = TimeCodeConst::TMR_INTV[static_cast(TimeCodeType)] / 4; H3_TIMER->TMR1_CTRL |= (TIMER_CTRL_EN_START | TIMER_CTRL_RELOAD); @@ -314,7 +308,7 @@ void LtcReader::Run() { #endif } - LtcOutputs::Get()->Update(reinterpret_cast(<cTimeCode)); + LtcOutputs::Get()->Update(reinterpret_cast(<cTimeCode)); } __DMB(); diff --git a/lib-ltc/src/arm/midireader.cpp b/lib-ltc/src/arm/midireader.cpp index 494664051..a52600702 100755 --- a/lib-ltc/src/arm/midireader.cpp +++ b/lib-ltc/src/arm/midireader.cpp @@ -153,10 +153,6 @@ void MidiReader::Update() { ArtNetNode::Get()->SendTimeCode(reinterpret_cast(&m_MidiTimeCode)); } - if (!ltc::g_DisabledOutputs.bRtpMidi) { - RtpMidi::Get()->SendTimeCode(&m_MidiTimeCode); - } - if (!ltc::g_DisabledOutputs.bEtc) { LtcEtc::Get()->Send(&m_MidiTimeCode); } diff --git a/lib-ltc/src/arm/systimereader.cpp b/lib-ltc/src/arm/systimereader.cpp index 5994debe5..95ed4e742 100755 --- a/lib-ltc/src/arm/systimereader.cpp +++ b/lib-ltc/src/arm/systimereader.cpp @@ -175,10 +175,6 @@ void SystimeReader::ActionSetRate(const char *pTimeCodeRate) { ArtNetNode::Get()->SendTimeCode(reinterpret_cast(&m_MidiTimeCode)); } - if (!ltc::g_DisabledOutputs.bRtpMidi) { - RtpMidi::Get()->SendTimeCode(reinterpret_cast(&m_MidiTimeCode)); - } - if (!ltc::g_DisabledOutputs.bEtc) { LtcEtc::Get()->Send(&m_MidiTimeCode); } @@ -285,10 +281,6 @@ void SystimeReader::Run() { ArtNetNode::Get()->SendTimeCode(reinterpret_cast(&m_MidiTimeCode)); } - if (!ltc::g_DisabledOutputs.bRtpMidi) { - RtpMidi::Get()->SendTimeCode(reinterpret_cast(&m_MidiTimeCode)); - } - if (!ltc::g_DisabledOutputs.bEtc) { LtcEtc::Get()->Send(&m_MidiTimeCode); } diff --git a/lib-ltc/src/arm/tcnetreader.cpp b/lib-ltc/src/arm/tcnetreader.cpp index b4a2123d0..1cb47a662 100755 --- a/lib-ltc/src/arm/tcnetreader.cpp +++ b/lib-ltc/src/arm/tcnetreader.cpp @@ -119,10 +119,6 @@ void TCNetReader::Handler(const struct tcnet::TimeCode *pTimeCode) { ArtNetNode::Get()->SendTimeCode(reinterpret_cast(pTimeCode)); } - if (!ltc::g_DisabledOutputs.bRtpMidi) { - RtpMidi::Get()->SendTimeCode(reinterpret_cast(pTimeCode)); - } - if (!ltc::g_DisabledOutputs.bEtc) { LtcEtc::Get()->Send(&m_MidiTimeCode); } diff --git a/lib-midi/.cproject b/lib-midi/.cproject index 299d62016..385f3ccfa 100644 --- a/lib-midi/.cproject +++ b/lib-midi/.cproject @@ -207,6 +207,7 @@ + @@ -223,6 +224,7 @@ + @@ -242,6 +244,7 @@ + diff --git a/lib-midi/.settings/language.settings.xml b/lib-midi/.settings/language.settings.xml index 6437b5761..e22071c65 100644 --- a/lib-midi/.settings/language.settings.xml +++ b/lib-midi/.settings/language.settings.xml @@ -5,7 +5,7 @@ - + @@ -14,7 +14,7 @@ - + diff --git a/lib-midi/include/applemidi.h b/lib-midi/include/applemidi.h index 1e6eaa855..3b758b0ea 100644 --- a/lib-midi/include/applemidi.h +++ b/lib-midi/include/applemidi.h @@ -44,8 +44,6 @@ #include "debug.h" namespace applemidi { -static constexpr auto UPD_PORT_CONTROL_DEFAULT = 5004U; -static constexpr auto UPD_PORT_MIDI_DEFAULT = UPD_PORT_CONTROL_DEFAULT + 1U; static constexpr auto SESSION_NAME_LENGTH_MAX = 24; static constexpr auto VERSION = 2; @@ -73,6 +71,9 @@ static constexpr auto EXCHANGE_PACKET_MIN_LENGTH = sizeof(struct applemidi::Exch } // namespace applemidi class AppleMidi { + static constexpr uint16_t UPD_PORT_CONTROL_DEFAULT = 5004; + static constexpr uint16_t UPD_PORT_MIDI_DEFAULT = UPD_PORT_CONTROL_DEFAULT + 1; + static constexpr uint16_t SIGNATURE = 0xffff; public: AppleMidi(); @@ -82,8 +83,6 @@ class AppleMidi { void Start() { DEBUG_ENTRY -// assert(MDNS::Get() != nullptr); -// MDNS::Get()->ServiceRecordAdd(nullptr, mdns::Services::MIDI, nullptr, m_nPort); mdns_service_record_add(nullptr, mdns::Services::MIDI, nullptr, m_nPort); m_nHandleControl = Network::Get()->Begin(m_nPort); @@ -108,9 +107,33 @@ class AppleMidi { DEBUG_EXIT } - void Run(); + void Run() { + m_nBytesReceived = Network::Get()->RecvFrom(m_nHandleMidi, const_cast(reinterpret_cast(&m_pBuffer)), &m_nRemoteIp, &m_nRemotePort); - void SetPort(uint16_t nPort) { + if (__builtin_expect((m_nBytesReceived >= 12), 0)) { + if (m_SessionStatus.nRemoteIp == m_nRemoteIp) { + HandleMidiMessage(); + } + } + + m_nBytesReceived = Network::Get()->RecvFrom(m_nHandleControl, const_cast(reinterpret_cast(&m_pBuffer)), &m_nRemoteIp, &m_nRemotePort); + + if (__builtin_expect((m_nBytesReceived >= applemidi::EXCHANGE_PACKET_MIN_LENGTH), 0)) { + if (*reinterpret_cast(m_pBuffer) == SIGNATURE) { + HandleControlMessage(); + } + } + + if (m_SessionStatus.sessionState == applemidi::SessionState::ESTABLISHED) { + if (__builtin_expect((Hardware::Get()->Millis() - m_SessionStatus.nSynchronizationTimestamp > (90 * 1000)), 0)) { + m_SessionStatus.sessionState = applemidi::SessionState::WAITING_IN_CONTROL; + m_SessionStatus.nRemoteIp = 0; + DEBUG_PUTS("End Session {time-out}"); + } + } + } + + void SetPort(const uint16_t nPort) { assert(nPort > 1024); m_nPort = nPort; } @@ -165,7 +188,7 @@ class AppleMidi { uint32_t m_nRemoteIp { 0 }; uint32_t m_nBytesReceived { 0 }; uint16_t m_nExchangePacketReplySize; - uint16_t m_nPort { applemidi::UPD_PORT_CONTROL_DEFAULT }; + uint16_t m_nPort { UPD_PORT_CONTROL_DEFAULT }; uint16_t m_nRemotePort { 0 }; applemidi::ExchangePacket m_ExchangePacketReply; applemidi::SessionStatus m_SessionStatus; diff --git a/lib-midi/include/rtpmidi.h b/lib-midi/include/rtpmidi.h index fe9362f8d..25fe8f864 100644 --- a/lib-midi/include/rtpmidi.h +++ b/lib-midi/include/rtpmidi.h @@ -2,7 +2,7 @@ * @file rtpmidi.h * */ -/* Copyright (C) 2019-2023 by Arjan van Vught mailto:info@gd32-dmx.org +/* Copyright (C) 2019-2024 by Arjan van Vught mailto:info@gd32-dmx.org * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -86,14 +86,14 @@ class RtpMidi final: public AppleMidi { AppleMidi::Run(); } - void SendRaw(uint8_t nByte) { + void SendRaw(const uint8_t nByte) { auto *data = &m_pSendBuffer[rtpmidi::COMMAND_OFFSET + 1]; data[0] = nByte; Send(1); } - void SendRaw(midi::Types tType) { - SendRaw(static_cast(tType)); + void SendRaw(const midi::Types type) { + SendRaw(static_cast(type)); } void SendTimeCode(const midi::Timecode *tTimeCode) { @@ -113,6 +113,52 @@ class RtpMidi final: public AppleMidi { Send(10); } + void SendQf(const uint8_t nValue) { + auto *data = &m_pSendBuffer[rtpmidi::COMMAND_OFFSET + 1]; + + data[0] = 0xF1; + data[1] = nValue; + + Send(2); + } + + void SendQf(const struct midi::Timecode *timeCode, uint32_t& nMidiQuarterFramePiece) { + auto data = static_cast(nMidiQuarterFramePiece << 4); + + switch (nMidiQuarterFramePiece) { + case 0: + data = data | (timeCode->nFrames & 0x0F); + break; + case 1: + data = data | static_cast((timeCode->nFrames & 0x10) >> 4); + break; + case 2: + data = data | (timeCode->nSeconds & 0x0F); + break; + case 3: + data = data | static_cast((timeCode->nSeconds & 0x30) >> 4); + break; + case 4: + data = data | (timeCode->nMinutes & 0x0F); + break; + case 5: + data = data | static_cast((timeCode->nMinutes & 0x30) >> 4); + break; + case 6: + data = data | (timeCode->nHours & 0x0F); + break; + case 7: + data = static_cast(data | (timeCode->nType << 1) | ((timeCode->nHours & 0x10) >> 4)); + break; + default: + break; + } + + SendQf(data); + + nMidiQuarterFramePiece = (nMidiQuarterFramePiece + 1) & 0x07; + } + void SetHandler(RtpMidiHandler *pRtpMidiHandler) { m_pRtpMidiHandler = pRtpMidiHandler; } @@ -121,7 +167,7 @@ class RtpMidi final: public AppleMidi { AppleMidi::Print(); } - static RtpMidi* Get() { + static RtpMidi *Get() { return s_pThis; } @@ -131,7 +177,7 @@ class RtpMidi final: public AppleMidi { int32_t DecodeTime(uint32_t nCommandLength, uint32_t nOffset); int32_t DecodeMidi(uint32_t nCommandLength, uint32_t nOffset); - midi::Types GetTypeFromStatusByte(uint8_t nStatusByte) { + midi::Types GetTypeFromStatusByte(const uint8_t nStatusByte) { if ((nStatusByte < 0x80) || (nStatusByte == 0xf4) || (nStatusByte == 0xf5) || (nStatusByte == 0xf9) || (nStatusByte == 0xfD)) { return midi::Types::INVALIDE_TYPE; } @@ -143,7 +189,7 @@ class RtpMidi final: public AppleMidi { return static_cast(nStatusByte); } - uint8_t GetChannelFromStatusByte(uint8_t nStatusByte) { + uint8_t GetChannelFromStatusByte(const uint8_t nStatusByte) { return static_cast((nStatusByte & 0x0F) + 1); } @@ -165,7 +211,7 @@ class RtpMidi final: public AppleMidi { uint8_t *m_pSendBuffer { nullptr }; uint16_t m_nSequenceNumber { 0 }; - static RtpMidi *s_pThis; + static inline RtpMidi *s_pThis; }; #endif /* RTPMIDI_H_ */ diff --git a/lib-midi/src/net/applemidi.cpp b/lib-midi/src/net/applemidi.cpp index e11f2afb4..b7029b5c8 100644 --- a/lib-midi/src/net/applemidi.cpp +++ b/lib-midi/src/net/applemidi.cpp @@ -46,7 +46,7 @@ #include "debug.h" namespace applemidi { -static constexpr uint16_t SIGNATURE = 0xffff; + } // namespace applemidi enum TAppleMidiCommand { @@ -71,7 +71,7 @@ struct TTimestampSynchronization { AppleMidi::AppleMidi() : m_nSSRC(Network::Get()->GetIp()), m_nExchangePacketReplySize(applemidi::EXCHANGE_PACKET_MIN_LENGTH) { DEBUG_ENTRY - m_ExchangePacketReply.nSignature = applemidi::SIGNATURE; + m_ExchangePacketReply.nSignature = SIGNATURE; m_ExchangePacketReply.nProtocolVersion = __builtin_bswap32(applemidi::VERSION); m_ExchangePacketReply.nSSRC = m_nSSRC; @@ -148,14 +148,14 @@ void AppleMidi::HandleMidiMessage() { debug_dump(m_pBuffer, m_nBytesReceived); - if (*reinterpret_cast(m_pBuffer) == 0x6180) { + if (*reinterpret_cast(m_pBuffer) == 0x6180) { HandleRtpMidi(m_pBuffer); return; } if (m_nBytesReceived >= applemidi::EXCHANGE_PACKET_MIN_LENGTH) { - if (*reinterpret_cast(m_pBuffer) == applemidi::SIGNATURE) { + if (*reinterpret_cast(m_pBuffer) == SIGNATURE) { if (m_SessionStatus.sessionState == applemidi::SessionState::WAITING_IN_MIDI) { DEBUG_PUTS("SESSION_STATE_WAITING_IN_MIDI"); @@ -219,29 +219,3 @@ void AppleMidi::HandleMidiMessage() { DEBUG_EXIT } - -void AppleMidi::Run() { - m_nBytesReceived = Network::Get()->RecvFrom(m_nHandleMidi, const_cast(reinterpret_cast(&m_pBuffer)), &m_nRemoteIp, &m_nRemotePort); - - if (__builtin_expect((m_nBytesReceived >= 12), 0)) { - if (m_SessionStatus.nRemoteIp == m_nRemoteIp) { - HandleMidiMessage(); - } - } - - m_nBytesReceived = Network::Get()->RecvFrom(m_nHandleControl, const_cast(reinterpret_cast(&m_pBuffer)), &m_nRemoteIp, &m_nRemotePort); - - if (__builtin_expect((m_nBytesReceived >= applemidi::EXCHANGE_PACKET_MIN_LENGTH), 0)) { - if (*reinterpret_cast(m_pBuffer) == applemidi::SIGNATURE) { - HandleControlMessage(); - } - } - - if (m_SessionStatus.sessionState == applemidi::SessionState::ESTABLISHED) { - if (__builtin_expect((Hardware::Get()->Millis() - m_SessionStatus.nSynchronizationTimestamp > (90 * 1000)), 0)) { - m_SessionStatus.sessionState = applemidi::SessionState::WAITING_IN_CONTROL; - m_SessionStatus.nRemoteIp = 0; - DEBUG_PUTS("End Session {time-out}"); - } - } -} diff --git a/lib-midi/src/net/rtpmidi.cpp b/lib-midi/src/net/rtpmidi.cpp index 5161a3bb1..12c481984 100644 --- a/lib-midi/src/net/rtpmidi.cpp +++ b/lib-midi/src/net/rtpmidi.cpp @@ -2,7 +2,7 @@ * @file rtpmidi.cpp * */ -/* Copyright (C) 2019-2023 by Arjan van Vught mailto:info@gd32-dmx.org +/* Copyright (C) 2019-2024 by Arjan van Vught mailto:info@gd32-dmx.org * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -47,8 +47,6 @@ #define RTP_MIDI_CS_MASK_SHORTLEN 0x0f #define RTP_MIDI_CS_MASK_LONGLEN 0x0fff -RtpMidi *RtpMidi::s_pThis = nullptr; - int32_t RtpMidi::DecodeTime([[maybe_unused]] uint32_t nCommandLength, uint32_t nOffset) { DEBUG_ENTRY diff --git a/opi_emac_ltc_smpte/.settings/language.settings.xml b/opi_emac_ltc_smpte/.settings/language.settings.xml index 581680857..782291eee 100644 --- a/opi_emac_ltc_smpte/.settings/language.settings.xml +++ b/opi_emac_ltc_smpte/.settings/language.settings.xml @@ -5,7 +5,7 @@ - + diff --git a/opi_emac_ltc_smpte/Makefile.H3 b/opi_emac_ltc_smpte/Makefile.H3 index 591f32f2a..fcc1d68fa 100644 --- a/opi_emac_ltc_smpte/Makefile.H3 +++ b/opi_emac_ltc_smpte/Makefile.H3 @@ -3,7 +3,7 @@ PLATFORM=ORANGE_PI DEFINES =NODE_LTC_SMPTE ARM_ALLOW_MULTI_CORE DEFINES+=CONFIG_LTC_USE_DAC -DEFINES+=CONFIG_NTP_CLIENT_POLL_POWER=3 +#DEFINES+=CONFIG_NTP_CLIENT_POLL_POWER=3 DEFINES+=CONFIG_DISPLAY_ENABLE_SSD1311 CONFIG_DISPLAY_ENABLE_HD44780 CONFIG_DISPLAY_ENABLE_CURSOR_MODE @@ -19,6 +19,7 @@ DEFINES+=ENABLE_HTTPD ENABLE_CONTENT #DEFINES+=ENABLE_SHELL UART0_ECHO LTC_READER #DEFINES+=DEBUG_ARM_LTCGENERATOR +#DEFINES+=DEBUG_ARM_LTCREADER #DEFINES+=DEBUG_LTCOSCSERVER DEFINES+=NDEBUG diff --git a/opi_emac_ltc_smpte/firmware/main.cpp b/opi_emac_ltc_smpte/firmware/main.cpp index b18f76601..df5287f98 100644 --- a/opi_emac_ltc_smpte/firmware/main.cpp +++ b/opi_emac_ltc_smpte/firmware/main.cpp @@ -140,7 +140,9 @@ extern "C" { void h3_cpu_off(uint32_t); } -void static staticCallbackFunction([[maybe_unused]] const struct artnet::TimeCode *pTimeCode) {} +void static staticCallbackFunction([[maybe_unused]] const struct artnet::TimeCode *pTimeCode) { + +} int main() { Hardware hw; @@ -576,10 +578,6 @@ int main() { ntpClient.Run(); // We could check for GPS Time client running. But not really needed. } - if (bRunMidiSystemRealtime) { - ltcMidiSystemRealtime.Run(); // UDP requests - } - if (ltc::g_DisabledOutputs.bOled) { display.Run(); } diff --git a/opi_emac_ltc_smpte/lib/mcpbuttons.cpp b/opi_emac_ltc_smpte/lib/mcpbuttons.cpp index 56b93fcd3..bdbb9ac16 100644 --- a/opi_emac_ltc_smpte/lib/mcpbuttons.cpp +++ b/opi_emac_ltc_smpte/lib/mcpbuttons.cpp @@ -27,11 +27,10 @@ #include #include -#include "mcpbuttons.h" #include "hardware.h" #include "network.h" -#include "arm/synchronize.h" +#include "mcpbuttons.h" #include "ltcdisplayrgb.h" #include "ltcparams.h" @@ -41,6 +40,7 @@ #include "rotaryencoder.h" #include "hal_i2c.h" +#include "hal_gpio.h" #include "mcp23x17.h" #include "displayedittimecode.h" @@ -54,10 +54,6 @@ #include "arm/ltcgenerator.h" #include "arm/systimereader.h" -// Interrupt -#include "board/h3_opi_zero.h" -#include "h3_gpio.h" - #include "configstore.h" #include "debug.h" @@ -177,16 +173,18 @@ void McpButtons::UpdateDisplays(const ltc::Source ltcSource) { LtcDisplayMax7219::Get()->WriteChar(nSource); return; } - +#if !defined (CONFIG_LTC_DISABLE_WS28XX) if (!ltc::g_DisabledOutputs.bWS28xx){ LtcDisplayRgb::Get()->WriteChar(nSource); return; } - +#endif +#if !defined (CONFIG_LTC_DISABLE_RGB_PANEL) if (!ltc::g_DisabledOutputs.bRgbPanel) { LtcDisplayRgb::Get()->ShowSource(ltcSource); return; } +#endif } bool McpButtons::Check() { @@ -212,8 +210,8 @@ bool McpButtons::Check() { UpdateDisplays(m_tLtcReaderSource); - h3_gpio_fsel(gpio::INTA, GPIO_FSEL_INPUT); // PA7 - h3_gpio_set_pud(gpio::INTA, GPIO_PULL_UP); + FUNC_PREFIX(gpio_fsel(gpio::INTA, GPIO_FSEL_INPUT)); + FUNC_PREFIX(gpio_set_pud(gpio::INTA, GPIO_PULL_UP)); DEBUG_EXIT return true; @@ -227,7 +225,7 @@ bool McpButtons::Wait(ltc::Source& ltcSource, struct ltc::TimeCode& StartTimeCod return false; } - if (__builtin_expect(h3_gpio_lev(gpio::INTA) == LOW, 0)) { + if (__builtin_expect(FUNC_PREFIX(gpio_lev(gpio::INTA)) == 0, 0)) { m_nLedTickerMax = UINT32_MAX; const auto nPortA = m_I2C.ReadRegister(mcp23x17::REG_GPIOA); @@ -407,7 +405,7 @@ void McpButtons::Run() { return; } - if (__builtin_expect(h3_gpio_lev(gpio::INTA) == LOW, 0)) { + if (__builtin_expect(FUNC_PREFIX(gpio_lev(gpio::INTA)) == 0, 0)) { const auto nPortA = m_I2C.ReadRegister(mcp23x17::REG_GPIOA); const uint8_t nButtonsChanged = (nPortA ^ m_nPortAPrevious) & nPortA;