-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrasmitSerial.ino
More file actions
125 lines (100 loc) · 3.2 KB
/
TrasmitSerial.ino
File metadata and controls
125 lines (100 loc) · 3.2 KB
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
/*
This code contains all of the serial sending functions
the most important are:
prepStreamingData() - prepares data for streaming.
outputSerialData() - actually sends the data.
data sent format:
begin msg / device ID / packet number / message type + data packets / packet size / packet timer / end msg
*/
void prepStreamingData()
{
//write functions here to buffer data
bufferSensors(); //example
ackFlag=0; //no acknowledgement needed
outputSerialData(); //this function trasmits buffered data to wireless device after all data has been buffered
/*
Polling means the device will send data continuously at the <pollInterval> ms rate.
If a message from the host device is not received after a delay of <pollEnableInterval> ms then polling is disabled.
This protects the host device from a flood of serial data when the host is not ready to receive it.
Sometimes flooding the host device will cause your PC to freeze. . .
*/
if(millis()>(pollEnableTimer+pollEnableInterval)){
//bufferSensors();
pollEnable=0;
}
}
/*
An example of buffering data
*/
void bufferSensors()
{
bufferByte(sensors); //buffer message type first
//buffer bytes
for (int i=0;i<2;i++) {
bufferByte(i);
}
//buffer an int
//for(int i=3;i<10;i++) bufferInt(outIMU[i]);
}
void sendErrorMessage(byte error){
ackFlag=0;
bufferByte(errorMsg);
bufferByte(error);
outputSerialData();
}
void sendAckMessage(byte packetNumber){
bufferByte(acknowledgement);
bufferByte(packetNumber);
ackFlag=0;
outputSerialData();
}
/*
functions for writing data to the serial output buffer <serialOutputBuffer[]>
*/
void bufferByte(byte input)
{
serialOutputBuffer[bufferIndex]=input;
bufferIndex+=1;
}
void bufferInt(int output)
{
serialOutputBuffer[bufferIndex]=((byte)(output >> 8)); //top byte
bufferIndex=bufferIndex+1;
serialOutputBuffer[bufferIndex]=((byte)(output & 0xFF)); //bottom byte
bufferIndex=bufferIndex+1;
}
/***************************************************************************
outputSerialData is the function that actually transmits the serial data.
#TODO device ID, msgType and pktNumber are not SLIP encoded
****************************************************************************/
void outputSerialData()
{
Serial.write(beginMsg); //SLIP start message
slipOut(deviceID); //device ID
//setup byte 2 to include both ackFlag and a pktnumber between 0-127
byte byte2 = (ackFlag<<7) | pktNumber; //2nd byte - acknowledge flag and number
pktNumber=(pktNumber+1)%128;
slipOut(byte2);
for(int i=0;i<bufferIndex;i++) { //message type + data. . .
slipOut(serialOutputBuffer[i]);
}
slipOut(bufferIndex);
bufferIndex=0; //reset buffer to have 0 elements
//set time elapsed since last packet was sent. Seems redundant here. . . .
pktTime= (millis()-streamTimer);
slipOut(pktTime);
streamTimer=millis();
Serial.write(endMsg); //SLIP end message
}
/*
escapes special characters
*/
void slipOut(byte input){
//check to see if input is a special character
for(int i=0;i<(sizeof(specialChar));i++) {
if(input==specialChar[i]) { //if it is, escape it
Serial.write(escByte);
}
}
Serial.write(input); //write data byte
}