-
Notifications
You must be signed in to change notification settings - Fork 56
/
commbuffer.cpp
140 lines (129 loc) · 5.37 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "commbuffer.h"
#include "Logger.h"
#include "gvret_comm.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;
}
}
void CommBuffer::sendFrameToBuffer(CAN_FRAME_FD &frame, int whichBus)
{
uint8_t temp;
size_t writtenBytes;
if (settings.useBinarySerialComm) {
if (frame.extended) frame.id |= 1 << 31;
transmitBuffer[transmitBufferLength++] = 0xF1;
transmitBuffer[transmitBufferLength++] = PROTO_BUILD_FD_FRAME;
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;
transmitBuffer[transmitBufferLength++] = (uint8_t)(whichBus);
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;
}
}