forked from collin80/ESP32RET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commbuffer.cpp
95 lines (86 loc) · 3.14 KB
/
commbuffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "commbuffer.h"
#include "Logger.h"
CommBuffer::CommBuffer()
{
transmitBufferLength = 0;
}
size_t CommBuffer::numAvailableBytes()
{
return transmitBufferLength;
}
void CommBuffer::clearBufferedBytes()
{
transmitBufferLength = 0;
}
uint8_t* CommBuffer::getBufferedBytes()
{
return transmitBuffer;
}
//a bit faster version that blasts through the copy more efficiently.
void CommBuffer::sendBytesToBuffer(uint8_t *bytes, size_t length)
{
memcpy(&transmitBuffer[transmitBufferLength], bytes, length);
transmitBufferLength += length;
}
void CommBuffer::sendByteToBuffer(uint8_t byt)
{
transmitBuffer[transmitBufferLength++] = byt;
}
void CommBuffer::sendString(String str)
{
char buff[300];
str.toCharArray(buff, 300);
sendCharString(buff);
}
void CommBuffer::sendCharString(char *str)
{
char *p = str;
int i = 0;
while (*p)
{
sendByteToBuffer(*p++);
i++;
}
Logger::debug("Queued %i bytes", i);
}
void CommBuffer::sendFrameToBuffer(CAN_FRAME &frame, int whichBus)
{
uint8_t temp;
size_t writtenBytes;
if (settings.useBinarySerialComm) {
if (frame.extended) frame.id |= 1 << 31;
transmitBuffer[transmitBufferLength++] = 0xF1;
transmitBuffer[transmitBufferLength++] = 0; //0 = canbus frame sending
uint32_t now = micros();
transmitBuffer[transmitBufferLength++] = (uint8_t)(now & 0xFF);
transmitBuffer[transmitBufferLength++] = (uint8_t)(now >> 8);
transmitBuffer[transmitBufferLength++] = (uint8_t)(now >> 16);
transmitBuffer[transmitBufferLength++] = (uint8_t)(now >> 24);
transmitBuffer[transmitBufferLength++] = (uint8_t)(frame.id & 0xFF);
transmitBuffer[transmitBufferLength++] = (uint8_t)(frame.id >> 8);
transmitBuffer[transmitBufferLength++] = (uint8_t)(frame.id >> 16);
transmitBuffer[transmitBufferLength++] = (uint8_t)(frame.id >> 24);
transmitBuffer[transmitBufferLength++] = frame.length + (uint8_t)(whichBus << 4);
for (int c = 0; c < frame.length; c++) {
transmitBuffer[transmitBufferLength++] = frame.data.uint8[c];
}
//temp = checksumCalc(buff, 11 + frame.length);
temp = 0;
transmitBuffer[transmitBufferLength++] = temp;
//Serial.write(buff, 12 + frame.length);
} else {
writtenBytes = sprintf((char *)&transmitBuffer[transmitBufferLength], "%d - %x", micros(), frame.id);
transmitBufferLength += writtenBytes;
if (frame.extended) sprintf((char *)&transmitBuffer[transmitBufferLength], " X ");
else sprintf((char *)&transmitBuffer[transmitBufferLength], " S ");
transmitBufferLength += 3;
writtenBytes = sprintf((char *)&transmitBuffer[transmitBufferLength], "%i %i", whichBus, frame.length);
transmitBufferLength += writtenBytes;
for (int c = 0; c < frame.length; c++) {
writtenBytes = sprintf((char *)&transmitBuffer[transmitBufferLength], " %x", frame.data.uint8[c]);
transmitBufferLength += writtenBytes;
}
sprintf((char *)&transmitBuffer[transmitBufferLength], "\r\n");
transmitBufferLength += 2;
}
}