mirror of
https://github.com/felis/USB_Host_Shield_2.0.git
synced 2024-03-22 11:31:26 +01:00
Add support for MiniDSP 2x4HD.
This commit is contained in:
parent
8758b2fc25
commit
715fef691f
3 changed files with 322 additions and 0 deletions
117
MiniDSP.cpp
Normal file
117
MiniDSP.cpp
Normal file
|
@ -0,0 +1,117 @@
|
|||
/* Copyright (C) 2014 Kristian Lauszus, TKJ Electronics. All rights reserved.
|
||||
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 Lauszus, TKJ Electronics
|
||||
Web : http://www.tkjelectronics.com
|
||||
e-mail : kristianl@tkjelectronics.com
|
||||
*/
|
||||
|
||||
#include "MiniDSP.h"
|
||||
|
||||
namespace {
|
||||
uint8_t RequestStatusOutputCommand[] = {0x05, 0xFF, 0xDA, 0x02};
|
||||
uint8_t StatusInputCommand[]{0x05, 0xFF, 0xDA};
|
||||
|
||||
// Returns first byte of the sum of given bytes.
|
||||
uint8_t Checksum(const uint8_t *data, uint16_t nbytes) {
|
||||
int sum = 0;
|
||||
for (int i = 0; i < nbytes; i++) {
|
||||
sum += data[i];
|
||||
}
|
||||
|
||||
return sum & 0xFF;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
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 <= 2 || buf == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if this is a status update.
|
||||
// First byte is the length, we ignore that for now.
|
||||
if (memcmp(buf + 1, StatusInputCommand, sizeof(StatusInputCommand)) == 0) {
|
||||
|
||||
// Parse data.
|
||||
// Response is of format [ length ] [ 0x05 0xFF 0xDA ] [ volume ] [ muted ].
|
||||
const auto newVolume = buf[sizeof(StatusInputCommand) + 1];
|
||||
const auto newIsMuted = (bool)buf[sizeof(StatusInputCommand) + 2];
|
||||
|
||||
const auto volumeUpdated = newVolume != volume;
|
||||
const auto isMutedUpdated = newIsMuted != isMuted;
|
||||
|
||||
// Update status.
|
||||
volume = newVolume;
|
||||
isMuted = newIsMuted;
|
||||
|
||||
// Call callbacks.
|
||||
if (volumeChangeCallback != nullptr && volumeUpdated) {
|
||||
volumeChangeCallback(volume);
|
||||
}
|
||||
|
||||
if (mutedChangeCallback != nullptr && isMutedUpdated) {
|
||||
mutedChangeCallback(isMuted);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
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 status so we can initialize the values.
|
||||
RequestStatus();
|
||||
|
||||
if (onInitCallback != nullptr) {
|
||||
onInitCallback();
|
||||
}
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
||||
void MiniDSP::SendCommand(uint8_t *command, uint16_t command_length) const {
|
||||
// Only send command if we're actually connected to the MiniDSP 2x4HD.
|
||||
if (HIDUniversal::VID != MINIDSP_VID || HIDUniversal::PID != MINIDSP_PID) {
|
||||
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 {
|
||||
SendCommand(RequestStatusOutputCommand, sizeof(RequestStatusOutputCommand));
|
||||
}
|
157
MiniDSP.h
Normal file
157
MiniDSP.h
Normal file
|
@ -0,0 +1,157 @@
|
|||
/* Copyright (C) 2014 Kristian Lauszus, TKJ Electronics. All rights reserved.
|
||||
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 Lauszus, TKJ Electronics
|
||||
Web : http://www.tkjelectronics.com
|
||||
e-mail : kristianl@tkjelectronics.com
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "controllerEnums.h"
|
||||
#include "hiduniversal.h"
|
||||
|
||||
#define MINIDSP_VID 0x2752 // MiniDSP
|
||||
#define MINIDSP_PID 0x0011 // MiniDSP 2x4HD
|
||||
|
||||
/**
|
||||
* Arduino MiniDSP 2x4HD USB Host Driver by Dennis Frett.
|
||||
*
|
||||
* This class implements support for the MiniDSP 2x4HD via USB.
|
||||
* Based on NodeJS implementation by Mathieu Rene:
|
||||
* https://github.com/mrene/node-minidsp and the Python implementation by Mark
|
||||
* Kubiak: https://github.com/markubiak/python3-minidsp.
|
||||
*
|
||||
* It uses the HIDUniversal class for all the USB communication.
|
||||
*/
|
||||
class MiniDSP : public HIDUniversal {
|
||||
public:
|
||||
/**
|
||||
* Constructor for the MiniDSP class.
|
||||
* @param p Pointer to the USB class instance.
|
||||
*/
|
||||
MiniDSP(USB *p) : HIDUniversal(p){};
|
||||
|
||||
/**
|
||||
* Used to check if a MiniDSP 2x4HD is connected.
|
||||
* @return Returns true if it is connected.
|
||||
*/
|
||||
bool connected() {
|
||||
return HIDUniversal::isReady() && HIDUniversal::VID == MINIDSP_VID &&
|
||||
HIDUniversal::PID == MINIDSP_PID;
|
||||
};
|
||||
|
||||
/**
|
||||
* Used to call your own function when the device is successfully initialized.
|
||||
* @param func Function to call.
|
||||
*/
|
||||
void SetOnInitCallback(void (*func)(void)) { onInitCallback = func; };
|
||||
|
||||
/**
|
||||
* Used to call your own function when the volume has changed.
|
||||
* The volume is passed as an unsigned integer that represents twice the -dB
|
||||
* value. Example: 19 represents -9.5dB.
|
||||
* @param func Function to call.
|
||||
*/
|
||||
void SetVolumeChangeCallback(void (*func)(uint8_t)) {
|
||||
volumeChangeCallback = func;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to call your own function when the muted status has changed.
|
||||
* The muted status is passed as a boolean. True means muted, false means
|
||||
* unmuted.
|
||||
* @param func Function to call.
|
||||
*/
|
||||
void SetMutedChangeCallback(void (*func)(bool)) {
|
||||
mutedChangeCallback = func;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the current volume of the MiniDSP.
|
||||
* The volume is passed as an unsigned integer that represents twice the -dB
|
||||
* value. Example: 19 represents -9.5dB.
|
||||
* @return Current volume.
|
||||
*/
|
||||
int GetVolume() const { return volume; }
|
||||
|
||||
/**
|
||||
* Retrieve the current muted status of the MiniDSP
|
||||
* @return `true` if the device is muted, `false` otherwise.
|
||||
*/
|
||||
bool IsMuted() const { return isMuted; }
|
||||
|
||||
protected:
|
||||
/** @name HIDUniversal implementation */
|
||||
/**
|
||||
* Used to parse USB HID data.
|
||||
* @param hid Pointer to the HID class.
|
||||
* @param is_rpt_id Only used for Hubs.
|
||||
* @param len The length of the incoming data.
|
||||
* @param buf Pointer to the data buffer.
|
||||
*/
|
||||
void ParseHIDData(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
|
||||
|
||||
/**
|
||||
* Called when a device is successfully initialized.
|
||||
* Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
|
||||
* This is useful for instance if you want to set the LEDs in a specific way.
|
||||
*/
|
||||
uint8_t OnInitSuccessful();
|
||||
/**@}*/
|
||||
|
||||
/** @name USBDeviceConfig implementation */
|
||||
/**
|
||||
* Used by the USB core to check what this driver support.
|
||||
* @param vid The device's VID.
|
||||
* @param pid The device's PID.
|
||||
* @return Returns true if the device's VID and PID matches this driver.
|
||||
*/
|
||||
virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
|
||||
return vid == MINIDSP_VID && pid == MINIDSP_PID;
|
||||
};
|
||||
/**@}*/
|
||||
|
||||
private:
|
||||
/**
|
||||
* Send the "Request status" command to the MiniDSP. The response includes the
|
||||
* current volume and the muted status.
|
||||
*/
|
||||
void RequestStatus() const;
|
||||
|
||||
/**
|
||||
* Send the given MiniDSP command. This function will create a buffer with the
|
||||
* expected header and checksum and send it to the MiniDSP. Responses will
|
||||
* come in throug `ParseHIDData`.
|
||||
* @param command Buffer of the command to send.
|
||||
* @param command_length Length of the buffer.
|
||||
*/
|
||||
void SendCommand(uint8_t *command, uint16_t command_length) const;
|
||||
|
||||
// Callbacks
|
||||
|
||||
// Pointer to function called in onInit().
|
||||
void (*onInitCallback)(void) = nullptr;
|
||||
|
||||
// Pointer to function called when volume changes.
|
||||
void (*volumeChangeCallback)(uint8_t) = nullptr;
|
||||
|
||||
// Pointer to function called when muted status changes.
|
||||
void (*mutedChangeCallback)(bool) = nullptr;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// MiniDSP state. Currently only volume and muted status are implemented, but
|
||||
// others can be added easily if needed.
|
||||
|
||||
// The volume is stored as an unsigned integer that represents twice the
|
||||
// -dB value. Example: 19 represents -9.5dB.
|
||||
uint8_t volume = 0;
|
||||
bool isMuted = false;
|
||||
};
|
48
examples/MiniDSP/MiniDSP.ino
Normal file
48
examples/MiniDSP/MiniDSP.ino
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
Example sketch for the Playstation Buzz library - developed by Kristian Lauszus
|
||||
For more information visit my blog: http://blog.tkjelectronics.dk/ or
|
||||
send me an e-mail: kristianl@tkjelectronics.com
|
||||
*/
|
||||
|
||||
#include <MiniDSP.h>
|
||||
|
||||
// Satisfy the IDE, which needs to see the include statment in the ino too.
|
||||
#ifdef dobogusinclude
|
||||
#include <spi4teensy3.h>
|
||||
#endif
|
||||
#include <SPI.h>
|
||||
|
||||
USB Usb;
|
||||
MiniDSP MiniDSP(&Usb);
|
||||
|
||||
void OnMiniDSPConnected() { Serial.println("MiniDSP connected"); }
|
||||
|
||||
void OnVolumeChange(uint8_t volume) {
|
||||
Serial.println("Volume is: " + String(volume));
|
||||
}
|
||||
|
||||
void OnMutedChange(bool isMuted) {
|
||||
Serial.println("Muted status: " + String(isMuted ? "muted" : "unmuted"));
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
#if !defined(__MIPSEL__)
|
||||
while (!Serial)
|
||||
; // Wait for serial port to connect - used on Leonardo, Teensy and other
|
||||
// boards with built-in USB CDC serial connection
|
||||
#endif
|
||||
if (Usb.Init() == -1) {
|
||||
Serial.print(F("\r\nOSC did not start"));
|
||||
while (1)
|
||||
; // Halt
|
||||
}
|
||||
Serial.println(F("\r\nMiniDSP 2x4HD Library Started"));
|
||||
|
||||
// Register callbacks.
|
||||
MiniDSP.SetOnInitCallback(&OnMiniDSPConnected);
|
||||
MiniDSP.SetVolumeChangeCallback(&OnVolumeChange);
|
||||
MiniDSP.SetMutedChangeCallback(&OnMutedChange);
|
||||
}
|
||||
|
||||
void loop() { Usb.Task(); }
|
Loading…
Reference in a new issue