implemented sending prefix and fixed wrong payload length

master
Jonas Arnold 3 years ago
parent 4d580393f8
commit ffa4243ac4
  1. 19
      src/main.cpp

@ -69,14 +69,18 @@ void loop() {
counter = 0; counter = 0;
// Mirror data via Serial // Mirror data via Serial
Serial.print("Received raw integers: ");
while (bytesAvailable--) { while (bytesAvailable--) {
char currentByte = (char)RawHID.read(); int currentByte = RawHID.read();
Serial.print(currentByte);
Serial.print(" ");
// save only if it is a printable character // save only if it is a printable character
if(currentByte >= 32 && currentByte <= 126) if(currentByte >= 32 && currentByte <= 126)
{ {
receivedMessageBuffer[counter++] = currentByte; receivedMessageBuffer[counter++] = currentByte;
} }
} }
Serial.print("\n");
// print read message // print read message
Serial.print("Received: '"); Serial.print("Received: '");
@ -128,7 +132,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 uint8_t maxNumBytesPerMessage = bufferSize - 2; // 1 because last byte got omitted always (therefore it is not sent), 2 because of prefix
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 ");
@ -138,11 +142,16 @@ void UsbHidSendLine(unsigned char* str)
// split message in buffer Size amount // split message in buffer Size amount
for(int messageNum = 0; messageBytes > 0; messageNum++){ 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. // calculate length of payload
McuUtility_strcpy((unsigned char*)sendMessageBuffer, numBytesPerMessage, (unsigned char*)(str + (numBytesPerMessage*messageNum))); uint8_t payloadLength = messageBytes > maxNumBytesPerMessage ? maxNumBytesPerMessage : messageBytes;
// prefix: 0=report ID, 1=amount of bytes for this payload, rest=payload
sendMessageBuffer[0] = 0x00;
sendMessageBuffer[1] = payloadLength;
// copy part of message to send buffer. move pointer by buffer size to take the next part of the string. (offset destination pointer by 2 because of prefix)
McuUtility_strcpy(((unsigned char*)sendMessageBuffer) + 2, maxNumBytesPerMessage+1, (unsigned char*)(str + (maxNumBytesPerMessage*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 -= numBytesPerMessage; // subtract numBytes that were sent from message bytes messageBytes -= maxNumBytesPerMessage; // subtract numBytes that were sent from message bytes
} }
Serial.print("\n'''\n"); Serial.print("\n'''\n");

Loading…
Cancel
Save