From db36383aab5674772da789960c4e9be2744e7768 Mon Sep 17 00:00:00 2001 From: Dennis Frett Date: Wed, 7 Sep 2022 21:59:46 +0200 Subject: [PATCH] Add setters for volume / muted + extend example. --- MiniDSP.cpp | 39 +++++++++++++++++++++++++++++++++--- MiniDSP.h | 21 +++++++++++++++++++ examples/MiniDSP/MiniDSP.ino | 16 +++++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/MiniDSP.cpp b/MiniDSP.cpp index 985efaf5..0a3299d6 100644 --- a/MiniDSP.cpp +++ b/MiniDSP.cpp @@ -22,13 +22,28 @@ #include "MiniDSP.h" void MiniDSP::ParseHIDData(USBHID *hid __attribute__ ((unused)), bool is_rpt_id __attribute__ ((unused)), uint8_t len, uint8_t *buf) { - constexpr uint8_t InputCommand[] = {0x05, 0xFF}; - // Only care about valid data for the MiniDSP 2x4HD. if(HIDUniversal::VID != MINIDSP_VID || HIDUniversal::PID != MINIDSP_PID || len <= 4 || buf == nullptr) return; - // Only deal with valid inputs. + // Check if this is a requested mute change. + if(buf[1] == 0x17){ + // Response is of format [ length ] [ 0x17 ] [ muted ] + muted = (bool)buf[2]; + Serial.println("Muted was set to: " + String(muted)); + } + + // Check if this is a requested volume change. + if(buf[1] == 0x42){ + // Response is of format [ length ] [ 0x42 ] [ volume ] + volume = buf[2]; + Serial.println("Volume was set to: " + String(volume)); + + } + + constexpr uint8_t InputCommand[] = {0x05, 0xFF}; + + // Only deal with status updates from now on. if(memcmp(buf + 1, InputCommand, sizeof (InputCommand)) != 0) return; @@ -161,4 +176,22 @@ void MiniDSP::RequestConfig() const { uint8_t RequestConfigCommand[] = {0x05, 0xFF, 0xD8, 0x01}; SendCommand(RequestConfigCommand, sizeof(RequestConfigCommand)); +} + +void MiniDSP::setVolumeDB(float volumeDB) const { + // Only accept values between 0dB and -127.5dB. + // Don't do error handling. + if(volume > 0 || volume < -127.5){ + return; + } + + uint8_t SetVolumeCommand[] = {0x42, (int)-2*volumedB}; + + SendCommand(SetVolumeCommand, sizeof(SetVolumeCommand)); +} + +void MiniDSP::setMuted(bool muted) const { + uint8_t SetMutedommand[] = {0x17, muted ? 0x01 : 0x00}; + + SendCommand(SetMutedommand, sizeof(SetMutedommand)); } \ No newline at end of file diff --git a/MiniDSP.h b/MiniDSP.h index fed8c501..06f2dc32 100644 --- a/MiniDSP.h +++ b/MiniDSP.h @@ -148,6 +148,27 @@ public: return inputSource; } + /** + * Set volume of the MiniDSP in dB. Values between 0 and -127.5 are + * accepted. If any values outside if this range are passed, this + * function does nothing. + * + * Calling this function will not trigger the volume change callback. + * + * @param volumeDB New volume to set. + */ + void setVolumeDB(float volumeDB) const; + + + /** + * Mute or unmute the MiniDSP. + * + * Calling this function will not trigger the mute change callback. + * + * @param muted Muted status. + */ + void setMuted(bool muted) const; + protected: /** @name HIDUniversal implementation */ /** diff --git a/examples/MiniDSP/MiniDSP.ino b/examples/MiniDSP/MiniDSP.ino index 49413939..af581836 100644 --- a/examples/MiniDSP/MiniDSP.ino +++ b/examples/MiniDSP/MiniDSP.ino @@ -39,6 +39,21 @@ void OnInputSourceChange(MiniDSP::InputSource inputSource) { Serial.println("Input source: " + inputSourceStr); } +void OnConfigChange(MiniDSP::Config config) { + String configStr = "Unknown"; + + if (config == MiniDSP::Config::Config_1) { + configStr = "Config 1"; + } else if (config == MiniDSP::Config::Config_2) { + configStr = "Config 2"; + } else if (config == MiniDSP::Config::Config_3) { + configStr = "Config 3"; + } else if (config == MiniDSP::Config::Config_4) { + configStr = "Config 4"; + } + Serial.println("Config: " + configStr); +} + void setup() { Serial.begin(115200); #if !defined(__MIPSEL__) @@ -55,6 +70,7 @@ void setup() { MiniDSP.attachOnVolumeChange(&OnVolumeChange); MiniDSP.attachOnMutedChange(&OnMutedChange); MiniDSP.attachOnInputSourceChange(&OnInputSourceChange); + MiniDSP.attachOnConfigChange(&OnConfigChange); } void loop() {