Skip to content

Commit

Permalink
More tweaks to USB hack
Browse files Browse the repository at this point in the history
  • Loading branch information
beserge committed Nov 30, 2023
1 parent 697934c commit e6e92da
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 12 deletions.
18 changes: 11 additions & 7 deletions src/hid/midi.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class MidiUartTransport
Config();
};

void Reset() {}

/** @brief Initialization of UART using config struct */
inline void Init(Config config)
{
Expand Down Expand Up @@ -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_;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions src/hid/usb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/hid/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class UsbHandle
*/
void Init(UsbPeriph dev);

void Reset();

/** Deinitializes the specified peripheral(s)
\param dev Device to deinitialize
*/
Expand Down
24 changes: 20 additions & 4 deletions src/hid/usb_midi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class MidiUsbTransport::Impl
public:
void Init(Config config);

void Reset();

void StartRx(MidiRxParseCallback callback, void* context)
{
rx_active_ = true;
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
4 changes: 3 additions & 1 deletion src/hid/usb_midi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit e6e92da

Please sign in to comment.