-
Notifications
You must be signed in to change notification settings - Fork 0
/
Serial.cpp
97 lines (82 loc) · 1.93 KB
/
Serial.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
#include "Serial.h"
SerialLine::SerialLine()
{
_transmitBufferCount = 0;
_transmitBufferBit = 7;
_transmitBufferByte = 0;
_receiveBufferCount = 0;
_receiveBufferBit = 7;
_receiveBufferByte = 0;
}
void SerialLine::OnReceiveComplete(unsigned char* buffer, int count)
{
ResetReceiveBuffer();
}
void SerialLine::ClearTransmitBuffer()
{
_transmitBufferCount = 0;
_transmitBufferBit = 7;
_transmitBufferByte = 0;
}
bool SerialLine::Transmit(unsigned char* buffer, int count)
{
if (!TransmitPartial(buffer, count))
return false;
OnTransmitBegin();
return true;
}
bool SerialLine::TransmitPartial(unsigned char* buffer, int count)
{
if (count < 0 || count >= (SERIAL_BUFFER_SIZE - _transmitBufferCount))
return false;
for (int i = 0; i < count; i++)
_transmitBuffer[_transmitBufferCount + i] = buffer[i];
_transmitBufferCount += count;
_transmitBufferBit = 7;
_transmitBufferByte = 0;
return true;
}
int SerialLine::PopTransmitBit()
{
if (_transmitBufferByte == _transmitBufferCount)
return 0;
int bit = (_transmitBuffer[_transmitBufferByte] >> _transmitBufferBit) & 1;
if( _transmitBufferBit-- == 0 )
{
_transmitBufferBit = 7;
_transmitBufferByte++;
}
return bit;
}
int SerialLine::RemainingTransmitBytes()
{
return _transmitBufferCount - _transmitBufferByte;
}
int SerialLine::TransmitSize()
{
return _transmitBufferCount;
}
void SerialLine::PushReceiveBit(int bit)
{
_receiveBuffer[_receiveBufferByte] &= ~(1 << _receiveBufferBit);
_receiveBuffer[_receiveBufferByte] |= bit << _receiveBufferBit;
if (_receiveBufferBit-- == 0)
{
_receiveBufferBit = 7;
_receiveBufferByte++;
}
}
int SerialLine::ReceivedBytes()
{
return _receiveBufferByte;
}
void SerialLine::ResetReceiveBuffer()
{
_receiveBufferBit = 7;
_receiveBufferByte = 0;
}
void SerialLine::ResetTransmitBuffer()
{
_transmitBufferBit = 7;
_transmitBufferByte = 0;
}