USB_Host_Shield_2.0/MiniDSP.cpp

140 lines
4.7 KiB
C++
Raw Normal View History

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) {
2022-09-07 20:23:23 +02:00
constexpr uint8_t InputCommand[] = {0x05, 0xFF};
2021-02-05 23:30:13 +01:00
// 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 20:23:23 +02:00
// Only deal with valid inputs.
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);
}
// Check if this is an input source update.
2022-09-07 20:23:23 +02:00
if(buf[3] == 0xA9 || buf[3] == 0xD9){
// 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];
// 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);
}
}
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;
// Request current information so we can initialize the values.
2021-02-05 23:30:13 +01:00
RequestStatus();
RequestInputSource();
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));
}
void MiniDSP::RequestInputSource() const {
uint8_t RequestInputSourceCommand[] = {0x05, 0xFF, 0xD9, 0x01};
SendCommand(RequestInputSourceCommand, sizeof(RequestInputSourceCommand));
}