2021-02-06 20:41:32 +01:00
|
|
|
/* Copyright (C) 2021 Kristian Sloth Lauszus and Dennis Frett. All rights reserved.
|
2021-02-05 23:30:13 +01:00
|
|
|
|
|
|
|
This software may be distributed and modified under the terms of the GNU
|
|
|
|
General Public License version 2 (GPL2) as published by the Free Software
|
|
|
|
Foundation and appearing in the file GPL2.TXT included in the packaging of
|
|
|
|
this file. Please note that GPL2 Section 2[b] requires that all works based
|
|
|
|
on this software must also be made publicly available under the terms of
|
|
|
|
the GPL2 ("Copyleft").
|
|
|
|
|
|
|
|
Contact information
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
Kristian Sloth Lauszus
|
|
|
|
Web : https://lauszus.com
|
|
|
|
e-mail : lauszus@gmail.com
|
|
|
|
|
|
|
|
Dennis Frett
|
2021-02-06 20:41:32 +01:00
|
|
|
GitHub : https://github.com/dennisfrett
|
2021-02-05 23:30:13 +01:00
|
|
|
e-mail : dennis.frett@live.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "MiniDSP.h"
|
|
|
|
|
|
|
|
void MiniDSP::ParseHIDData(USBHID *hid __attribute__ ((unused)), bool is_rpt_id __attribute__ ((unused)), uint8_t len, uint8_t *buf) {
|
|
|
|
// Only care about valid data for the MiniDSP 2x4HD.
|
|
|
|
if(HIDUniversal::VID != MINIDSP_VID || HIDUniversal::PID != MINIDSP_PID || len <= 4 || buf == nullptr)
|
|
|
|
return;
|
|
|
|
|
2022-09-07 21:59:46 +02:00
|
|
|
// 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.
|
2022-09-07 20:23:23 +02:00
|
|
|
if(memcmp(buf + 1, InputCommand, sizeof (InputCommand)) != 0)
|
|
|
|
return;
|
2021-02-05 23:30:13 +01:00
|
|
|
|
2022-09-07 20:23:23 +02:00
|
|
|
if(buf[3] == 0xDA){
|
2021-02-05 23:30:13 +01:00
|
|
|
// Parse data.
|
|
|
|
// Response is of format [ length ] [ 0x05 0xFF 0xDA ] [ volume ] [ muted ].
|
2022-09-07 20:23:23 +02:00
|
|
|
const auto newVolume = buf[4];
|
|
|
|
const auto newIsMuted = (bool)buf[5];
|
2021-02-05 23:30:13 +01:00
|
|
|
|
|
|
|
const auto volumeChanged = newVolume != volume;
|
|
|
|
const auto mutedChanged = newIsMuted != muted;
|
|
|
|
|
|
|
|
// Update status.
|
|
|
|
volume = newVolume;
|
|
|
|
muted = newIsMuted;
|
|
|
|
|
|
|
|
// Call callbacks.
|
|
|
|
if(pFuncOnVolumeChange != nullptr && volumeChanged)
|
|
|
|
pFuncOnVolumeChange(volume);
|
|
|
|
|
|
|
|
if(pFuncOnMutedChange != nullptr && mutedChanged)
|
|
|
|
pFuncOnMutedChange(muted);
|
|
|
|
}
|
2022-09-06 18:55:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Check if this is an input source update.
|
2022-09-07 20:23:23 +02:00
|
|
|
if(buf[3] == 0xA9 || buf[3] == 0xD9){
|
2022-09-06 18:55:44 +02:00
|
|
|
// Parse data.
|
2022-09-07 20:23:23 +02:00
|
|
|
// Response is of format [ length ] [ 0x05 0xFF 0xA9/0xD9 ] [ source ].
|
|
|
|
const auto newInputSource = buf[4];
|
2022-09-06 18:55:44 +02:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
2022-09-07 20:34:47 +02:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 23:30:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
uint8_t MiniDSP::OnInitSuccessful() {
|
|
|
|
// Verify we're actually connected to the MiniDSP 2x4HD.
|
|
|
|
if(HIDUniversal::VID != MINIDSP_VID || HIDUniversal::PID != MINIDSP_PID)
|
|
|
|
return 0;
|
|
|
|
|
2022-09-06 18:55:44 +02:00
|
|
|
// Request current information so we can initialize the values.
|
2021-02-05 23:30:13 +01:00
|
|
|
RequestStatus();
|
2022-09-06 18:55:44 +02:00
|
|
|
RequestInputSource();
|
2022-09-07 20:34:47 +02:00
|
|
|
RequestConfig();
|
2021-02-05 23:30:13 +01:00
|
|
|
|
|
|
|
if(pFuncOnInit != nullptr)
|
|
|
|
pFuncOnInit();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
uint8_t MiniDSP::Checksum(const uint8_t *data, uint8_t data_length) const {
|
|
|
|
uint16_t sum = 0;
|
|
|
|
for(uint8_t i = 0; i < data_length; i++)
|
|
|
|
sum += data[i];
|
|
|
|
|
|
|
|
return sum & 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MiniDSP::SendCommand(uint8_t *command, uint8_t command_length) const {
|
|
|
|
// Sanity check on command length.
|
|
|
|
if(command_length > 63)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Message is padded to 64 bytes with 0xFF and is of format:
|
|
|
|
// [ length (command + checksum byte) ] [ command ] [ checksum ] [ OxFF... ]
|
|
|
|
|
|
|
|
// MiniDSP expects 64 byte messages.
|
|
|
|
uint8_t buf[64];
|
|
|
|
|
|
|
|
// Set length, including checksum byte.
|
|
|
|
buf[0] = command_length + 1;
|
|
|
|
|
|
|
|
// Copy actual command.
|
|
|
|
memcpy(&buf[1], command, command_length);
|
|
|
|
|
|
|
|
const auto checksumOffset = command_length + 1;
|
|
|
|
|
|
|
|
// Set checksum byte.
|
|
|
|
buf[checksumOffset] = Checksum(buf, command_length + 1);
|
|
|
|
|
|
|
|
// Pad the rest.
|
|
|
|
memset(&buf[checksumOffset + 1], 0xFF, sizeof (buf) - checksumOffset - 1);
|
|
|
|
|
|
|
|
pUsb->outTransfer(bAddress, epInfo[epInterruptOutIndex].epAddr, sizeof (buf), buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MiniDSP::RequestStatus() const {
|
|
|
|
uint8_t RequestStatusOutputCommand[] = {0x05, 0xFF, 0xDA, 0x02};
|
|
|
|
|
|
|
|
SendCommand(RequestStatusOutputCommand, sizeof (RequestStatusOutputCommand));
|
|
|
|
}
|
2022-09-06 18:55:44 +02:00
|
|
|
|
|
|
|
void MiniDSP::RequestInputSource() const {
|
2022-09-07 20:34:47 +02:00
|
|
|
uint8_t RequestInputSourceCommand[] = {0x05, 0xFF, 0xD9, 0x01};
|
2022-09-06 18:55:44 +02:00
|
|
|
|
2022-09-07 20:34:47 +02:00
|
|
|
SendCommand(RequestInputSourceCommand, sizeof(RequestInputSourceCommand));
|
2022-09-06 18:55:44 +02:00
|
|
|
}
|
2022-09-07 20:34:47 +02:00
|
|
|
|
|
|
|
void MiniDSP::RequestConfig() const {
|
|
|
|
uint8_t RequestConfigCommand[] = {0x05, 0xFF, 0xD8, 0x01};
|
|
|
|
|
|
|
|
SendCommand(RequestConfigCommand, sizeof(RequestConfigCommand));
|
2022-09-07 21:59:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2022-09-07 20:34:47 +02:00
|
|
|
}
|