diff --git a/src/hid/midi.h b/src/hid/midi.h index 121d15233..3294d2518 100644 --- a/src/hid/midi.h +++ b/src/hid/midi.h @@ -58,6 +58,8 @@ class MidiUartTransport Config(); }; + void Reset() {} + /** @brief Initialization of UART using config struct */ inline void Init(Config config) { @@ -103,7 +105,7 @@ class MidiUartTransport inline void FlushRx() {} /** @brief sends the buffer of bytes out of the UART peripheral */ - inline void Tx(uint8_t* buff, size_t size) { uart_.PollTx(buff, size); } + inline bool Tx(uint8_t* buff, size_t size) { uart_.PollTx(buff, size); return false; } private: UartHandler uart_; @@ -170,6 +172,8 @@ class MidiHandler parser_.Init(); } + inline void ResetTransport() { transport_.Reset(); } + /** Starts listening on the selected input mode(s). * MidiEvent Queue will begin to fill, and can be checked with HasEvents() */ void StartReceive() @@ -210,22 +214,22 @@ class MidiHandler transport_.Tx(bytes, size); } - void SendNoteOn(uint8_t channel, uint8_t note, uint8_t velocity) + bool SendNoteOn(uint8_t channel, uint8_t note, uint8_t velocity) { uint8_t bytes[3] = {(uint8_t)(0x90 | channel), note, velocity}; - transport_.Tx(bytes, 3); + return transport_.Tx(bytes, 3); } - void SendNoteOff(uint8_t channel, uint8_t note, uint8_t velocity) + bool SendNoteOff(uint8_t channel, uint8_t note, uint8_t velocity) { uint8_t bytes[3] = {(uint8_t)(0x80 | channel), note, velocity}; - transport_.Tx(bytes, 3); + return transport_.Tx(bytes, 3); } - void SendCC(uint8_t channel, uint8_t control_number, uint8_t value) + bool SendCC(uint8_t channel, uint8_t control_number, uint8_t value) { uint8_t bytes[3] = {(uint8_t)(0xB0 | channel), control_number, value}; - transport_.Tx(bytes, 3); + return transport_.Tx(bytes, 3); } /** Feed in bytes to state machine from a queue. diff --git a/src/hid/usb.cpp b/src/hid/usb.cpp index a8e32c94e..1b6b54ff2 100644 --- a/src/hid/usb.cpp +++ b/src/hid/usb.cpp @@ -90,6 +90,29 @@ static void DeinitHS() } +void UsbHandle::Reset() +{ + // HS as FS + if(USBD_Init(&hUsbDeviceHS, &HS_Desc, DEVICE_HS) != USBD_OK) + { + UsbErrorHandler(); + } + if(USBD_RegisterClass(&hUsbDeviceHS, &USBD_CDC) != USBD_OK) + { + UsbErrorHandler(); + } + if(USBD_CDC_RegisterInterface(&hUsbDeviceHS, &USBD_Interface_fops_HS) + != USBD_OK) + { + UsbErrorHandler(); + } + if(USBD_Start(&hUsbDeviceHS) != USBD_OK) + { + UsbErrorHandler(); + } +} + + void UsbHandle::Init(UsbPeriph dev) { switch(dev) diff --git a/src/hid/usb.h b/src/hid/usb.h index a6f39f62e..49c3f7b77 100644 --- a/src/hid/usb.h +++ b/src/hid/usb.h @@ -46,6 +46,8 @@ class UsbHandle */ void Init(UsbPeriph dev); + void Reset(); + /** Deinitializes the specified peripheral(s) \param dev Device to deinitialize */ diff --git a/src/hid/usb_midi.cpp b/src/hid/usb_midi.cpp index 811b1c404..e93959bc2 100644 --- a/src/hid/usb_midi.cpp +++ b/src/hid/usb_midi.cpp @@ -10,6 +10,8 @@ class MidiUsbTransport::Impl public: void Init(Config config); + void Reset(); + void StartRx(MidiRxParseCallback callback, void* context) { rx_active_ = true; @@ -19,7 +21,7 @@ class MidiUsbTransport::Impl bool RxActive() { return rx_active_; } void FlushRx() { rx_buffer_.Flush(); } - void Tx(uint8_t* buffer, size_t size); + bool Tx(uint8_t* buffer, size_t size); void UsbToMidi(uint8_t* buffer, uint8_t length); void MidiToUsb(uint8_t* buffer, size_t length); @@ -80,6 +82,12 @@ void ReceiveCallback(uint8_t* buffer, uint32_t* length) } } +void MidiUsbTransport::Impl::Reset() +{ + // TODO: implement + usb_handle_.Reset(); +} + void MidiUsbTransport::Impl::Init(Config config) { // Borrowed from logger @@ -103,7 +111,7 @@ void MidiUsbTransport::Impl::Init(Config config) usb_handle_.SetReceiveCallback(ReceiveCallback, periph); } -void MidiUsbTransport::Impl::Tx(uint8_t* buffer, size_t size) +bool MidiUsbTransport::Impl::Tx(uint8_t* buffer, size_t size) { UsbHandle::Result result; int attempt_count = config_.tx_retry_count; @@ -124,6 +132,8 @@ void MidiUsbTransport::Impl::Tx(uint8_t* buffer, size_t size) } while(should_retry); tx_ptr_ = 0; + + return result != UsbHandle::Result::ERR; } void MidiUsbTransport::Impl::UsbToMidi(uint8_t* buffer, uint8_t length) @@ -307,6 +317,12 @@ void MidiUsbTransport::Init(MidiUsbTransport::Config config) pimpl_->Init(config); } +void MidiUsbTransport::Reset() +{ + pimpl_ = &midi_usb_handle; + pimpl_->Reset(); +} + void MidiUsbTransport::StartRx(MidiRxParseCallback callback, void* context) { pimpl_->StartRx(callback, context); @@ -322,7 +338,7 @@ void MidiUsbTransport::FlushRx() pimpl_->FlushRx(); } -void MidiUsbTransport::Tx(uint8_t* buffer, size_t size) +bool MidiUsbTransport::Tx(uint8_t* buffer, size_t size) { - pimpl_->Tx(buffer, size); + return pimpl_->Tx(buffer, size); } diff --git a/src/hid/usb_midi.h b/src/hid/usb_midi.h index 78e5cd983..1545d65be 100644 --- a/src/hid/usb_midi.h +++ b/src/hid/usb_midi.h @@ -45,10 +45,12 @@ class MidiUsbTransport void Init(Config config); + void Reset(); + void StartRx(MidiRxParseCallback callback, void* context); bool RxActive(); void FlushRx(); - void Tx(uint8_t* buffer, size_t size); + bool Tx(uint8_t* buffer, size_t size); class Impl;