added periodic message

fixed buffer length
still some characters get omitted...
master
Jonas Arnold 3 years ago
parent 1f76bebaa9
commit 4d580393f8
  1. 20
      src/main.cpp

@ -16,6 +16,8 @@
#include "HID-Project.h" #include "HID-Project.h"
#include "McuUtility.h" #include "McuUtility.h"
#define PERIODIC_MESSAGE 0
const int pinLed = 17; const int pinLed = 17;
bool ledState = true; bool ledState = true;
@ -25,7 +27,7 @@ bool ledState = true;
// If the data is not read until the host sends the next data // If the data is not read until the host sends the next data
// it will also respond with an error and the data will be lost. // it will also respond with an error and the data will be lost.
uint8_t rawhidData[255]; uint8_t rawhidData[255];
uint8_t sendMessageBuffer[60]; uint8_t sendMessageBuffer[64];
char receivedMessageBuffer[255]; char receivedMessageBuffer[255];
uint8_t counter; uint8_t counter;
@ -48,6 +50,14 @@ void setup() {
} }
void loop() { void loop() {
#if PERIODIC_MESSAGE
// send periodic message
memset(sendMessageBuffer, '\0', sizeof(sendMessageBuffer)); // clear send buffer
McuUtility_strcpy((unsigned char*)sendMessageBuffer, sizeof(sendMessageBuffer), (unsigned char*)"periodic message\n");
RawHID.write(sendMessageBuffer, sizeof(sendMessageBuffer)); // send buffer
Serial.print((char*)sendMessageBuffer); // print
#endif
// Check if there is new data from the RawHID device // Check if there is new data from the RawHID device
int bytesAvailable = RawHID.available(); int bytesAvailable = RawHID.available();
if (bytesAvailable) if (bytesAvailable)
@ -82,6 +92,7 @@ void loop() {
digitalWrite(pinLed, ledState == true ? HIGH : LOW); digitalWrite(pinLed, ledState == true ? HIGH : LOW);
// send response // send response
memset(sendMessageBuffer, '\0', sizeof(sendMessageBuffer)); // clear send buffer
McuUtility_strcpy((unsigned char*)sendMessageBuffer, sizeof(sendMessageBuffer), (unsigned char*)LED_COMMAND_RESPONSE); McuUtility_strcpy((unsigned char*)sendMessageBuffer, sizeof(sendMessageBuffer), (unsigned char*)LED_COMMAND_RESPONSE);
RawHID.write(sendMessageBuffer, sizeof(sendMessageBuffer)); RawHID.write(sendMessageBuffer, sizeof(sendMessageBuffer));
Serial.print("Sent: '"); Serial.print("Sent: '");
@ -117,6 +128,7 @@ void UsbHidSendLine(unsigned char* str)
if (str!=NULL) { if (str!=NULL) {
int32_t messageBytes = McuUtility_strlen((char*)str); int32_t messageBytes = McuUtility_strlen((char*)str);
uint8_t bufferSize = sizeof(sendMessageBuffer); uint8_t bufferSize = sizeof(sendMessageBuffer);
uint8_t numBytesPerMessage = bufferSize - 1; // last byte got omitted always, therefore it is not sent
Serial.print("Sending message with "); Serial.print("Sending message with ");
Serial.print(messageBytes); Serial.print(messageBytes);
Serial.print(" bytes and buffer size "); Serial.print(" bytes and buffer size ");
@ -124,13 +136,13 @@ void UsbHidSendLine(unsigned char* str)
Serial.print(": \n'''\n"); Serial.print(": \n'''\n");
// split message in buffer Size amount // split message in buffer Size amount
for(int byteNum = 0; messageBytes > 0; byteNum++){ for(int messageNum = 0; messageBytes > 0; messageNum++){
memset(sendMessageBuffer, '\0', sizeof(sendMessageBuffer)); // clear send buffer memset(sendMessageBuffer, '\0', sizeof(sendMessageBuffer)); // clear send buffer
// copy part of message to send buffer. move pointer by buffer size to take the next part of the string. // copy part of message to send buffer. move pointer by buffer size to take the next part of the string.
McuUtility_strcpy((unsigned char*)sendMessageBuffer, sizeof(sendMessageBuffer), (unsigned char*)(str + bufferSize*byteNum)); McuUtility_strcpy((unsigned char*)sendMessageBuffer, numBytesPerMessage, (unsigned char*)(str + (numBytesPerMessage*messageNum)));
RawHID.write(sendMessageBuffer, sizeof(sendMessageBuffer)); // write buffer RawHID.write(sendMessageBuffer, sizeof(sendMessageBuffer)); // write buffer
Serial.print((char*)sendMessageBuffer); // print Serial.print((char*)sendMessageBuffer); // print
messageBytes -= bufferSize; // subtract bufferSize from message bytes messageBytes -= numBytesPerMessage; // subtract numBytes that were sent from message bytes
} }
Serial.print("\n'''\n"); Serial.print("\n'''\n");

Loading…
Cancel
Save