From 3a36dfeccdedf02bb28ba4ed76b23a59ba33f88d Mon Sep 17 00:00:00 2001 From: Dennis Frett Date: Tue, 6 Sep 2022 18:55:44 +0200 Subject: [PATCH] Add functionality to retrieve input source from MiniDSP 2x4HD. --- MiniDSP.cpp | 33 ++++++++++++++++++++++++++++++++- MiniDSP.h | 22 ++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/MiniDSP.cpp b/MiniDSP.cpp index 359018ba..bc6102c0 100644 --- a/MiniDSP.cpp +++ b/MiniDSP.cpp @@ -24,6 +24,7 @@ void MiniDSP::ParseHIDData(USBHID *hid __attribute__ ((unused)), bool is_rpt_id __attribute__ ((unused)), uint8_t len, uint8_t *buf) { constexpr uint8_t StatusInputCommand[] = {0x05, 0xFF, 0xDA}; + constexpr uint8_t InputSourceInputCommand[] = {0x05, 0xFF, 0xD9}; // Only care about valid data for the MiniDSP 2x4HD. if(HIDUniversal::VID != MINIDSP_VID || HIDUniversal::PID != MINIDSP_PID || len <= 4 || buf == nullptr) @@ -52,6 +53,28 @@ void MiniDSP::ParseHIDData(USBHID *hid __attribute__ ((unused)), bool is_rpt_id if(pFuncOnMutedChange != nullptr && mutedChanged) pFuncOnMutedChange(muted); } + + + // Check if this is an input source update. + // First byte is the length, we ignore that for now. + if(memcmp(buf + 1, InputSourceInputCommand, sizeof (InputSourceInputCommand)) == 0) { + + // Parse data. + // Response is of format [ length ] [ 0x05 0xFF 0xD9 ] [ source ]. + const auto newInputSource = buf[sizeof (InputSourceInputCommand) + 1]; + + // Ensure we only interpret valid inputs. + if(newInputSource >= 0x00 && newInputSource <= 0x02){ + const auto inputSourceChanged = newInputSource != (char) inputSource; + + // Update values. + inputSource = (InputSource) newInputSource; + + // Call callbacks. + if(pFuncOnInputSourceChange != nullptr && inputSourceChanged) + pFuncOnInputSourceChange(inputSource); + } + } }; uint8_t MiniDSP::OnInitSuccessful() { @@ -59,8 +82,9 @@ uint8_t MiniDSP::OnInitSuccessful() { if(HIDUniversal::VID != MINIDSP_VID || HIDUniversal::PID != MINIDSP_PID) return 0; - // Request current status so we can initialize the values. + // Request current information so we can initialize the values. RequestStatus(); + RequestInputSource(); if(pFuncOnInit != nullptr) pFuncOnInit(); @@ -109,3 +133,10 @@ void MiniDSP::RequestStatus() const { SendCommand(RequestStatusOutputCommand, sizeof (RequestStatusOutputCommand)); } + + +void MiniDSP::RequestInputSource() const { + uint8_t RequestInputSourceCommand[] = {0x05, 0xFF, 0xD9, 0x01}; + + SendCommand(RequestInputSourceCommand, sizeof(RequestInputSourceCommand)); +} diff --git a/MiniDSP.h b/MiniDSP.h index 442a8f39..499631ed 100644 --- a/MiniDSP.h +++ b/MiniDSP.h @@ -39,6 +39,13 @@ class MiniDSP : public HIDUniversal { public: + enum class InputSource { + ANALOG = 0x00, + TOSLINK = 0x01, + USB = 0x02, + UNKNOWN = 0x03 + }; + /** * Constructor for the MiniDSP class. * @param p Pointer to the USB class instance. @@ -83,6 +90,14 @@ public: pFuncOnMutedChange = funcOnMutedChange; } + /** + * Used to call your own function when the input source has changed. + * @param funcOnInputSourceChange Function to call. + */ + void attachOnInputSourceChange(void (*funcOnInputSourceChange)(InputSource)) { + pFuncOnInputSourceChange = funcOnInputSourceChange; + } + /** * Retrieve the current volume of the MiniDSP. * The volume is passed as an unsigned integer that represents twice the @@ -159,6 +174,9 @@ private: void RequestStatus() const; + void + RequestInputSource() const; + /** * Send the given MiniDSP command. This function will create a buffer * with the expected header and checksum and send it to the MiniDSP. @@ -179,6 +197,9 @@ private: // Pointer to function called when muted status changes. void (*pFuncOnMutedChange)(bool) = nullptr; + // Pointer to function called when input source changes. + void (*pFuncOnInputSourceChange)(InputSource) = nullptr; + // ----------------------------------------------------------------------------- // MiniDSP state. Currently only volume and muted status are @@ -188,4 +209,5 @@ private: // -dB value. Example: 19 represents -9.5dB. uint8_t volume = 0; bool muted = false; + InputSource inputSource = InputSource::UNKNOWN; };