USB Host Shield 2.0
MiniDSP.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2021 Kristian Sloth Lauszus and Dennis Frett. All rights reserved.
2 
3  This software may be distributed and modified under the terms of the GNU
4  General Public License version 2 (GPL2) as published by the Free Software
5  Foundation and appearing in the file GPL2.TXT included in the packaging of
6  this file. Please note that GPL2 Section 2[b] requires that all works based
7  on this software must also be made publicly available under the terms of
8  the GPL2 ("Copyleft").
9 
10  Contact information
11  -------------------
12 
13  Kristian Sloth Lauszus
14  Web : https://lauszus.com
15  e-mail : lauszus@gmail.com
16 
17  Dennis Frett
18  GitHub : https://github.com/dennisfrett
19  e-mail : dennis.frett@live.com
20  */
21 
22 #include "MiniDSP.h"
23 
24 void MiniDSP::ParseHIDData(USBHID *hid __attribute__ ((unused)), bool is_rpt_id __attribute__ ((unused)), uint8_t len, uint8_t *buf) {
25 
26  constexpr uint8_t StatusInputCommand[] = {0x05, 0xFF, 0xDA};
27 
28  // Only care about valid data for the MiniDSP 2x4HD.
29  if(HIDUniversal::VID != MINIDSP_VID || HIDUniversal::PID != MINIDSP_PID || len <= 4 || buf == nullptr)
30  return;
31 
32  // Check if this is a status update.
33  // First byte is the length, we ignore that for now.
34  if(memcmp(buf + 1, StatusInputCommand, sizeof (StatusInputCommand)) == 0) {
35 
36  // Parse data.
37  // Response is of format [ length ] [ 0x05 0xFF 0xDA ] [ volume ] [ muted ].
38  const auto newVolume = buf[sizeof (StatusInputCommand) + 1];
39  const auto newIsMuted = (bool)buf[sizeof (StatusInputCommand) + 2];
40 
41  const auto volumeChanged = newVolume != volume;
42  const auto mutedChanged = newIsMuted != muted;
43 
44  // Update status.
45  volume = newVolume;
46  muted = newIsMuted;
47 
48  // Call callbacks.
49  if(pFuncOnVolumeChange != nullptr && volumeChanged)
50  pFuncOnVolumeChange(volume);
51 
52  if(pFuncOnMutedChange != nullptr && mutedChanged)
53  pFuncOnMutedChange(muted);
54  }
55 };
56 
58  // Verify we're actually connected to the MiniDSP 2x4HD.
60  return 0;
61 
62  // Request current status so we can initialize the values.
63  RequestStatus();
64 
65  if(pFuncOnInit != nullptr)
66  pFuncOnInit();
67 
68  return 0;
69 };
70 
71 uint8_t MiniDSP::Checksum(const uint8_t *data, uint8_t data_length) const {
72  uint16_t sum = 0;
73  for(uint8_t i = 0; i < data_length; i++)
74  sum += data[i];
75 
76  return sum & 0xFF;
77 }
78 
79 void MiniDSP::SendCommand(uint8_t *command, uint8_t command_length) const {
80  // Sanity check on command length.
81  if(command_length > 63)
82  return;
83 
84  // Message is padded to 64 bytes with 0xFF and is of format:
85  // [ length (command + checksum byte) ] [ command ] [ checksum ] [ OxFF... ]
86 
87  // MiniDSP expects 64 byte messages.
88  uint8_t buf[64];
89 
90  // Set length, including checksum byte.
91  buf[0] = command_length + 1;
92 
93  // Copy actual command.
94  memcpy(&buf[1], command, command_length);
95 
96  const auto checksumOffset = command_length + 1;
97 
98  // Set checksum byte.
99  buf[checksumOffset] = Checksum(buf, command_length + 1);
100 
101  // Pad the rest.
102  memset(&buf[checksumOffset + 1], 0xFF, sizeof (buf) - checksumOffset - 1);
103 
104  pUsb->outTransfer(bAddress, epInfo[epInterruptOutIndex].epAddr, sizeof (buf), buf);
105 }
106 
107 void MiniDSP::RequestStatus() const {
108  uint8_t RequestStatusOutputCommand[] = {0x05, 0xFF, 0xDA, 0x02};
109 
110  SendCommand(RequestStatusOutputCommand, sizeof (RequestStatusOutputCommand));
111 }
#define MINIDSP_PID
Definition: MiniDSP.h:27
#define MINIDSP_VID
Definition: MiniDSP.h:26
uint16_t PID
Definition: hidcomposite.h:71
EpInfo epInfo[totalEndpoints]
Definition: hidcomposite.h:63
uint16_t VID
Definition: hidcomposite.h:71
uint8_t OnInitSuccessful()
Definition: MiniDSP.cpp:57
void ParseHIDData(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
Definition: MiniDSP.cpp:24
Definition: usbhid.h:143
uint8_t bAddress
Definition: usbhid.h:146
USB * pUsb
Definition: usbhid.h:145
static const uint8_t epInterruptOutIndex
Definition: usbhid.h:150
uint8_t outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *data)
Definition: Usb.cpp:303