From e169d9fa90fdfcd75bbf27c40765f9c0c40ab5ac Mon Sep 17 00:00:00 2001 From: Lauszus Date: Sat, 6 Feb 2021 20:04:02 +0000 Subject: [PATCH] deploy: 37c7c5155ad62f6bced800fcb78555e72d781409 --- _mini_d_s_p_8cpp_source.html | 2 +- _mini_d_s_p_8h_source.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/_mini_d_s_p_8cpp_source.html b/_mini_d_s_p_8cpp_source.html index d99705fd..36d61e47 100644 --- a/_mini_d_s_p_8cpp_source.html +++ b/_mini_d_s_p_8cpp_source.html @@ -63,7 +63,7 @@ $(function() {
MiniDSP.cpp
-Go to the documentation of this file.
1 /* Copyright (C) 2020 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 }
uint8_t OnInitSuccessful()
Definition: MiniDSP.cpp:57
+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 }
uint8_t OnInitSuccessful()
Definition: MiniDSP.cpp:57
Definition: usbhid.h:143
USB * pUsb
Definition: usbhid.h:145
void ParseHIDData(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
Definition: MiniDSP.cpp:24
diff --git a/_mini_d_s_p_8h_source.html b/_mini_d_s_p_8h_source.html index 8df1981f..cadbab3a 100644 --- a/_mini_d_s_p_8h_source.html +++ b/_mini_d_s_p_8h_source.html @@ -63,7 +63,7 @@ $(function() {
MiniDSP.h
-Go to the documentation of this file.
1 /* Copyright (C) 2020 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 #pragma once
23 
24 #include "hiduniversal.h"
25 
26 #define MINIDSP_VID 0x2752 // MiniDSP
27 #define MINIDSP_PID 0x0011 // MiniDSP 2x4HD
28 
39 class MiniDSP : public HIDUniversal {
40 public:
41 
47  };
48 
53  bool connected() {
55  };
56 
62  void attachOnInit(void (*funcOnInit)(void)) {
63  pFuncOnInit = funcOnInit;
64  };
65 
72  void attachOnVolumeChange(void (*funcOnVolumeChange)(uint8_t)) {
73  pFuncOnVolumeChange = funcOnVolumeChange;
74  }
75 
82  void attachOnMutedChange(void (*funcOnMutedChange)(bool)) {
83  pFuncOnMutedChange = funcOnMutedChange;
84  }
85 
92  int getVolume() const {
93  return volume;
94  }
95 
100  float getVolumeDB() const {
101  return volume / -2.0;
102  }
103 
108  bool isMuted() const {
109  return muted;
110  }
111 
112 protected:
121  void ParseHIDData(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
122 
129  uint8_t OnInitSuccessful();
141  virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
142  return vid == MINIDSP_VID && pid == MINIDSP_PID;
143  };
146 private:
153  uint8_t Checksum(const uint8_t *data, uint8_t data_length) const;
154 
159  void
160  RequestStatus() const;
161 
169  void SendCommand(uint8_t *command, uint8_t command_length) const;
170 
171  // Callbacks
172 
173  // Pointer to function called in onInit().
174  void (*pFuncOnInit)(void) = nullptr;
175 
176  // Pointer to function called when volume changes.
177  void (*pFuncOnVolumeChange)(uint8_t) = nullptr;
178 
179  // Pointer to function called when muted status changes.
180  void (*pFuncOnMutedChange)(bool) = nullptr;
181 
182  // -----------------------------------------------------------------------------
183 
184  // MiniDSP state. Currently only volume and muted status are
185  // implemented, but others can be added easily if needed.
186 
187  // The volume is stored as an unsigned integer that represents twice the
188  // -dB value. Example: 19 represents -9.5dB.
189  uint8_t volume = 0;
190  bool muted = false;
191 };
uint8_t OnInitSuccessful()
Definition: MiniDSP.cpp:57
+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 #pragma once
23 
24 #include "hiduniversal.h"
25 
26 #define MINIDSP_VID 0x2752 // MiniDSP
27 #define MINIDSP_PID 0x0011 // MiniDSP 2x4HD
28 
39 class MiniDSP : public HIDUniversal {
40 public:
41 
47  };
48 
53  bool connected() {
55  };
56 
62  void attachOnInit(void (*funcOnInit)(void)) {
63  pFuncOnInit = funcOnInit;
64  };
65 
72  void attachOnVolumeChange(void (*funcOnVolumeChange)(uint8_t)) {
73  pFuncOnVolumeChange = funcOnVolumeChange;
74  }
75 
82  void attachOnMutedChange(void (*funcOnMutedChange)(bool)) {
83  pFuncOnMutedChange = funcOnMutedChange;
84  }
85 
92  int getVolume() const {
93  return volume;
94  }
95 
100  float getVolumeDB() const {
101  return volume / -2.0;
102  }
103 
108  bool isMuted() const {
109  return muted;
110  }
111 
112 protected:
121  void ParseHIDData(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
122 
129  uint8_t OnInitSuccessful();
141  virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
142  return vid == MINIDSP_VID && pid == MINIDSP_PID;
143  };
146 private:
153  uint8_t Checksum(const uint8_t *data, uint8_t data_length) const;
154 
159  void
160  RequestStatus() const;
161 
169  void SendCommand(uint8_t *command, uint8_t command_length) const;
170 
171  // Callbacks
172 
173  // Pointer to function called in onInit().
174  void (*pFuncOnInit)(void) = nullptr;
175 
176  // Pointer to function called when volume changes.
177  void (*pFuncOnVolumeChange)(uint8_t) = nullptr;
178 
179  // Pointer to function called when muted status changes.
180  void (*pFuncOnMutedChange)(bool) = nullptr;
181 
182  // -----------------------------------------------------------------------------
183 
184  // MiniDSP state. Currently only volume and muted status are
185  // implemented, but others can be added easily if needed.
186 
187  // The volume is stored as an unsigned integer that represents twice the
188  // -dB value. Example: 19 represents -9.5dB.
189  uint8_t volume = 0;
190  bool muted = false;
191 };
uint8_t OnInitSuccessful()
Definition: MiniDSP.cpp:57
Definition: usbhid.h:143
virtual bool VIDPIDOK(uint16_t vid, uint16_t pid)
Definition: MiniDSP.h:141
void ParseHIDData(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
Definition: MiniDSP.cpp:24