-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dashboard.cpp
391 lines (357 loc) · 10.8 KB
/
Dashboard.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#include "Dashboard.h"
#include "Synchronized.h"
#include "Utility.h"
#include "WPIStatus.h"
#include <strLib.h>
UINT8 Dashboard::m_updateNumber = 0;
/**
* Dashboard contructor.
*
* This is only called once when the DriverStation constructor is called.
*/
Dashboard::Dashboard(SEM_ID statusDataSem)
: m_userStatusData (NULL)
, m_userStatusDataSize (0)
, m_localBuffer (NULL)
, m_localPrintBuffer (NULL)
, m_packPtr (NULL)
, m_printSemaphore (0)
, m_statusDataSemaphore (statusDataSem)
{
m_userStatusData = new char[kMaxDashboardDataSize];
m_localBuffer = new char[kMaxDashboardDataSize];
m_localPrintBuffer = new char[kMaxDashboardDataSize * 2];
m_localPrintBuffer[0] = 0;
m_packPtr = m_localBuffer;
m_printSemaphore = semMCreate(SEM_Q_PRIORITY | SEM_DELETE_SAFE | SEM_INVERSION_SAFE);
}
/**
* Dashboard destructor.
*
* Called only when the DriverStation class is destroyed.
*/
Dashboard::~Dashboard()
{
semDelete(m_printSemaphore);
m_packPtr = NULL;
delete [] m_localPrintBuffer;
m_localPrintBuffer = NULL;
delete [] m_localBuffer;
m_localBuffer = NULL;
delete [] m_userStatusData;
m_userStatusData = NULL;
}
/**
* Pack a signed 8-bit int into the dashboard data structure.
* @param value Data to be packed into the structure.
*/
void Dashboard::AddI8(INT8 value)
{
if (!ValidateAdd(sizeof(INT8))) return;
memcpy(m_packPtr, (char*)&value, sizeof(value));
m_packPtr += sizeof(value);
AddedElement(kI8);
}
/**
* Pack a signed 16-bit int into the dashboard data structure.
* @param value Data to be packed into the structure.
*/
void Dashboard::AddI16(INT16 value)
{
if (!ValidateAdd(sizeof(INT16))) return;
memcpy(m_packPtr, (char*)&value, sizeof(value));
m_packPtr += sizeof(value);
AddedElement(kI16);
}
/**
* Pack a signed 32-bit int into the dashboard data structure.
* @param value Data to be packed into the structure.
*/
void Dashboard::AddI32(INT32 value)
{
if (!ValidateAdd(sizeof(INT32))) return;
memcpy(m_packPtr, (char*)&value, sizeof(value));
m_packPtr += sizeof(value);
AddedElement(kI32);
}
/**
* Pack an unsigned 8-bit int into the dashboard data structure.
* @param value Data to be packed into the structure.
*/
void Dashboard::AddU8(UINT8 value)
{
if (!ValidateAdd(sizeof(UINT8))) return;
memcpy(m_packPtr, (char*)&value, sizeof(value));
m_packPtr += sizeof(value);
AddedElement(kU8);
}
/**
* Pack an unsigned 16-bit int into the dashboard data structure.
* @param value Data to be packed into the structure.
*/
void Dashboard::AddU16(UINT16 value)
{
if (!ValidateAdd(sizeof(UINT16))) return;
memcpy(m_packPtr, (char*)&value, sizeof(value));
m_packPtr += sizeof(value);
AddedElement(kU16);
}
/**
* Pack an unsigned 32-bit int into the dashboard data structure.
* @param value Data to be packed into the structure.
*/
void Dashboard::AddU32(UINT32 value)
{
if (!ValidateAdd(sizeof(UINT32))) return;
memcpy(m_packPtr, (char*)&value, sizeof(value));
m_packPtr += sizeof(value);
AddedElement(kU32);
}
/**
* Pack a 32-bit floating point number into the dashboard data structure.
* @param value Data to be packed into the structure.
*/
void Dashboard::AddFloat(float value)
{
if (!ValidateAdd(sizeof(float))) return;
memcpy(m_packPtr, (char*)&value, sizeof(value));
m_packPtr += sizeof(value);
AddedElement(kFloat);
}
/**
* Pack a 64-bit floating point number into the dashboard data structure.
* @param value Data to be packed into the structure.
*/
void Dashboard::AddDouble(double value)
{
if (!ValidateAdd(sizeof(double))) return;
memcpy(m_packPtr, (char*)&value, sizeof(value));
m_packPtr += sizeof(value);
AddedElement(kDouble);
}
/**
* Pack a boolean into the dashboard data structure.
* @param value Data to be packed into the structure.
*/
void Dashboard::AddBoolean(bool value)
{
if (!ValidateAdd(sizeof(char))) return;
*m_packPtr = value ? 1 : 0;
m_packPtr += sizeof(char);
AddedElement(kBoolean);
}
/**
* Pack a NULL-terminated string of 8-bit characters into the dashboard data structure.
* @param value Data to be packed into the structure.
*/
void Dashboard::AddString(char* value)
{
AddString(value, strlen(value));
}
/**
* Pack a string of 8-bit characters of specified length into the dashboard data structure.
* @param value Data to be packed into the structure.
* @param length The number of bytes in the string to pack.
*/
void Dashboard::AddString(char* value, INT32 length)
{
if (!ValidateAdd(length + sizeof(length))) return;
memcpy(m_packPtr, (char*)&length, sizeof(length));
m_packPtr += sizeof(length);
memcpy(m_packPtr, (char*)&value, length);
m_packPtr += length;
AddedElement(kString);
}
/**
* Start an array in the packed dashboard data structure.
*
* After calling AddArray(), call the appropriate Add method for each element of the array.
* Make sure you call the same add each time. An array must contain elements of the same type.
* You can use clusters inside of arrays to make each element of the array contain a structure of values.
* You can also nest arrays inside of other arrays.
* Every call to AddArray() must have a matching call to FinalizeArray().
*/
void Dashboard::AddArray(void)
{
if (!ValidateAdd(sizeof(INT32))) return;
m_complexTypeStack.push(kArray);
m_arrayElementCount.push_back(0);
m_arraySizePtr.push_back((INT32*)m_packPtr);
m_packPtr += sizeof(INT32);
}
/**
* Indicate the end of an array packed into the dashboard data structure.
*
* After packing data into the array, call FinalizeArray().
* Every call to AddArray() must have a matching call to FinalizeArray().
*/
void Dashboard::FinalizeArray(void)
{
if (m_complexTypeStack.top() != kArray)
{
wpi_fatal(MismatchedComplexTypeClose);
return;
}
m_complexTypeStack.pop();
*(m_arraySizePtr.back()) = m_arrayElementCount.back();
m_arraySizePtr.pop_back();
if (m_arrayElementCount.back() != 0)
{
m_expectedArrayElementType.pop_back();
}
m_arrayElementCount.pop_back();
AddedElement(kOther);
}
/**
* Start a cluster in the packed dashboard data structure.
*
* After calling AddCluster(), call the appropriate Add method for each element of the cluster.
* You can use clusters inside of arrays to make each element of the array contain a structure of values.
* Every call to AddCluster() must have a matching call to FinalizeCluster().
*/
void Dashboard::AddCluster(void)
{
m_complexTypeStack.push(kCluster);
}
/**
* Indicate the end of a cluster packed into the dashboard data structure.
*
* After packing data into the cluster, call FinalizeCluster().
* Every call to AddCluster() must have a matching call to FinalizeCluster().
*/
void Dashboard::FinalizeCluster(void)
{
if (m_complexTypeStack.top() != kCluster)
{
wpi_fatal(MismatchedComplexTypeClose);
return;
}
m_complexTypeStack.pop();
AddedElement(kOther);
}
/**
* Print a string to the UserData text on the Dashboard.
*
* This will add text to the buffer to send to the dashboard.
* You must call Finalize() periodically to actually send the buffer to the dashboard if you are not using the packed dashboard data.
*/
void Dashboard::Printf(const char *writeFmt, ...)
{
va_list args;
INT32 size;
// Check if the buffer has already been used for packing.
if (m_packPtr != m_localBuffer)
{
wpi_fatal(DashboardDataCollision);
return;
}
va_start (args, writeFmt);
{
Synchronized sync(m_printSemaphore);
vsprintf(m_localPrintBuffer + strlen(m_localPrintBuffer), writeFmt, args);
size = strlen(m_localPrintBuffer);
}
if (size > kMaxDashboardDataSize)
{
wpi_fatal(DashboardDataOverflow);
}
va_end (args);
}
/**
* Indicate that the packing is complete and commit the buffer to the DriverStation.
*
* The packing of the dashboard packet is complete.
* If you are not using the packed dashboard data, you can call Finalize() to commit the Printf() buffer and the error string buffer.
* In effect, you are packing an empty structure.
* Prepares a packet to go to the dashboard...
* @return The total size of the data packed into the userData field of the status packet.
*/
INT32 Dashboard::Finalize(void)
{
if (!m_complexTypeStack.empty())
{
wpi_fatal(MismatchedComplexTypeClose);
return 0;
}
Synchronized sync(m_statusDataSemaphore);
// Sequence number
m_updateNumber++;
// Packed Dashboard Data
m_userStatusDataSize = m_packPtr - m_localBuffer;
memcpy(m_userStatusData, m_localBuffer, m_userStatusDataSize);
m_packPtr = m_localBuffer;
return m_userStatusDataSize;
}
/**
* Called by the DriverStation class to retrieve buffers, sizes, etc. for writing
* to the NetworkCommunication task.
* This function is called while holding the m_statusDataSemaphore.
*/
void Dashboard::GetStatusBuffer(char **userStatusData, INT32* userStatusDataSize)
{
// User printed strings
if (m_localPrintBuffer[0] != 0)
{
// Sequence number
m_updateNumber++;
INT32 printSize;
Synchronized syncPrint(m_printSemaphore);
printSize = strlen(m_localPrintBuffer);
m_userStatusDataSize = printSize;
memcpy(m_userStatusData, m_localPrintBuffer, m_userStatusDataSize);
m_localPrintBuffer[0] = 0;
}
*userStatusData = m_userStatusData;
*userStatusDataSize = m_userStatusDataSize;
}
/**
* Validate that the data being packed will fit in the buffer.
*/
bool Dashboard::ValidateAdd(INT32 size)
{
if ((m_packPtr - m_localBuffer) + size > kMaxDashboardDataSize)
{
wpi_fatal(DashboardDataOverflow);
return false;
}
// Make sure printf is not being used at the same time.
if (m_localPrintBuffer[0] != 0)
{
wpi_fatal(DashboardDataCollision);
return false;
}
return true;
}
/**
* Check for consistent types when adding elements to an array and keep track of the number of elements in the array.
*/
void Dashboard::AddedElement(Type type)
{
if(IsArrayRoot())
{
if (m_arrayElementCount.back() == 0)
{
m_expectedArrayElementType.push_back(type);
}
else
{
if (type != m_expectedArrayElementType.back())
{
wpi_fatal(InconsistentArrayValueAdded);
}
}
m_arrayElementCount.back() = m_arrayElementCount.back() + 1;
}
}
/**
* If the top of the type stack an array?
*/
bool Dashboard::IsArrayRoot(void)
{
return !m_complexTypeStack.empty() && m_complexTypeStack.top() == kArray;
}