diff --git a/MiniDSP.cpp b/MiniDSP.cpp index b5836a39..985efaf5 100644 --- a/MiniDSP.cpp +++ b/MiniDSP.cpp @@ -72,6 +72,25 @@ void MiniDSP::ParseHIDData(USBHID *hid __attribute__ ((unused)), bool is_rpt_id pFuncOnInputSourceChange(inputSource); } } + + // Check if this is an Config update. + if(buf[3] == 0xD8){ + // Parse data. + // Response is of format [ length ] [ 0x05 0xFF 0xD8 ] [ config ]. + const auto newConfig = buf[4]; + + // Ensure we only interpret valid inputs. + if(newConfig >= 0x00 && newConfig <= 0x03){ + const auto configChanged = newConfig != (char) config; + + // Update values. + config = (Config) newConfig; + + // Call callbacks. + if(pFuncOnConfigChange != nullptr && configChanged) + pFuncOnConfigChange(config); + } + } }; uint8_t MiniDSP::OnInitSuccessful() { @@ -82,6 +101,7 @@ uint8_t MiniDSP::OnInitSuccessful() { // Request current information so we can initialize the values. RequestStatus(); RequestInputSource(); + RequestConfig(); if(pFuncOnInit != nullptr) pFuncOnInit(); @@ -131,9 +151,14 @@ void MiniDSP::RequestStatus() const { SendCommand(RequestStatusOutputCommand, sizeof (RequestStatusOutputCommand)); } - void MiniDSP::RequestInputSource() const { - uint8_t RequestInputSourceCommand[] = {0x05, 0xFF, 0xD9, 0x01}; + uint8_t RequestInputSourceCommand[] = {0x05, 0xFF, 0xD9, 0x01}; - SendCommand(RequestInputSourceCommand, sizeof(RequestInputSourceCommand)); + SendCommand(RequestInputSourceCommand, sizeof(RequestInputSourceCommand)); } + +void MiniDSP::RequestConfig() const { + uint8_t RequestConfigCommand[] = {0x05, 0xFF, 0xD8, 0x01}; + + SendCommand(RequestConfigCommand, sizeof(RequestConfigCommand)); +} \ No newline at end of file diff --git a/MiniDSP.h b/MiniDSP.h index 55ac367b..fed8c501 100644 --- a/MiniDSP.h +++ b/MiniDSP.h @@ -46,6 +46,14 @@ public: Unknown = 0x03 }; + enum class Config : uint8_t { + Config_1 = 0x00, + Config_2 = 0x01, + Config_3 = 0x02, + Config_4 = 0x03, + Unknown = 0x04 + }; + /** * Constructor for the MiniDSP class. * @param p Pointer to the USB class instance. @@ -98,6 +106,14 @@ public: pFuncOnInputSourceChange = funcOnInputSourceChange; } + /** + * Used to call your own function when the config has changed. + * @param funcOnConfigChange Function to call. + */ + void attachOnConfigChange(void (*funcOnConfigChange)(Config)) { + pFuncOnConfigChange = funcOnConfigChange; + } + /** * Retrieve the current volume of the MiniDSP. * The volume is passed as an unsigned integer that represents twice the @@ -188,6 +204,12 @@ private: void RequestInputSource() const; + /** + * Send the "Request config" command to the MiniDSP. + */ + void + RequestConfig() const; + /** * Send the given MiniDSP command. This function will create a buffer * with the expected header and checksum and send it to the MiniDSP. @@ -211,6 +233,9 @@ private: // Pointer to function called when input source changes. void (*pFuncOnInputSourceChange)(InputSource) = nullptr; + // Pointer to function called when config changes. + void (*pFuncOnConfigChange)(Config) = nullptr; + // ----------------------------------------------------------------------------- // MiniDSP state. Currently only volume and muted status are @@ -221,4 +246,5 @@ private: uint8_t volume = 0; bool muted = false; InputSource inputSource = InputSource::Unknown; + Config config = Config::Unknown; };