Implement virtual com port receiver
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include "init.h"
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "utils.hpp"
|
||||
#include "vcp_receiver.hpp"
|
||||
|
||||
std::array<uint16_t, 3> sampleLightSensors()
|
||||
{
|
||||
@@ -31,6 +32,8 @@ int main()
|
||||
{
|
||||
init();
|
||||
|
||||
VcpReceiver vcpReceiver;
|
||||
|
||||
HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_RESET);
|
||||
@@ -41,7 +44,13 @@ int main()
|
||||
HAL_GPIO_WritePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_SET);
|
||||
|
||||
uint8_t data = 0;
|
||||
|
||||
while(true) {
|
||||
if(vcpReceiver.rxByte(data)) {
|
||||
CDC_Transmit_FS(&data, 1);
|
||||
}
|
||||
|
||||
const auto ldrValues = sampleLightSensors();
|
||||
|
||||
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_RESET);
|
||||
@@ -51,8 +60,8 @@ int main()
|
||||
for(uint8_t i = 0; i < ldrValues.size(); ++i) {
|
||||
const auto ldrID = i + 1;
|
||||
const auto percentage = ldrValues[i] * 100 / 0xFFF;
|
||||
const auto bufLen =
|
||||
std::sprintf(reinterpret_cast<char*>(printBuffer.data()), "LDR%d: %04hu - %03d%%\r\n%s", ldrID, ldrValues[i], percentage, (i == 2) ? "\r\n" : "");
|
||||
const auto bufLen = std::sprintf(reinterpret_cast<char*>(printBuffer.data()), "LDR%d: %04hu - %03d%%\r\n%s", ldrID, ldrValues[i], percentage,
|
||||
(i == 2) ? "\r\n" : "");
|
||||
if(bufLen > 0) {
|
||||
while(CDC_Transmit_FS(printBuffer.data(), bufLen) == USBD_BUSY)
|
||||
;
|
||||
|
||||
@@ -246,6 +246,8 @@ static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length)
|
||||
/* USER CODE END 5 */
|
||||
}
|
||||
|
||||
extern void vcpReceiveCallback(uint8_t* buf, uint32_t len);
|
||||
|
||||
/**
|
||||
* @brief Data received over USB OUT endpoint are sent over CDC interface
|
||||
* through this function.
|
||||
@@ -265,6 +267,7 @@ static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
|
||||
/* USER CODE BEGIN 6 */
|
||||
USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
|
||||
USBD_CDC_ReceivePacket(&hUsbDeviceFS);
|
||||
vcpReceiveCallback(Buf, *Len);
|
||||
return (USBD_OK);
|
||||
/* USER CODE END 6 */
|
||||
}
|
||||
|
||||
10
AdaptiveBrightnessFirmware/Src/vcp_receiver.cpp
Normal file
10
AdaptiveBrightnessFirmware/Src/vcp_receiver.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include "vcp_receiver.hpp"
|
||||
|
||||
extern "C" void vcpReceiveCallback(uint8_t* buf, uint32_t len)
|
||||
{
|
||||
for(uint32_t i = 0; i < len; ++i) {
|
||||
VcpReceiver::rxHandler(buf[i]);
|
||||
}
|
||||
}
|
||||
|
||||
volatile VcpReceiver::RingBuffer<detail::VCP_RECEIVE_BUFFER_SIZE> VcpReceiver::m_receiveBuffer;
|
||||
Reference in New Issue
Block a user