-
Notifications
You must be signed in to change notification settings - Fork 0
/
DriverStationLCD.cpp
151 lines (128 loc) · 4.14 KB
/
DriverStationLCD.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
/*----------------------------------------------------------------------------*/
/* 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 "DriverStationLCD.h"
#include "NetworkCommunication/FRCComm.h"
#include "Synchronized.h"
#include "WPIStatus.h"
#include "Utility.h"
#include <strLib.h>
DriverStationLCD* DriverStationLCD::m_instance = NULL;
/**
* DriverStationLCD contructor.
*
* This is only called once the first time GetInstance() is called
*/
DriverStationLCD::DriverStationLCD()
: m_textBuffer (NULL)
, m_textBufferSemaphore (NULL)
{
m_textBuffer = new char[USER_DS_LCD_DATA_SIZE];
memset(m_textBuffer, ' ', USER_DS_LCD_DATA_SIZE);
*((UINT16 *)m_textBuffer) = kFullDisplayTextCommand;
m_textBufferSemaphore = semMCreate(SEM_DELETE_SAFE | SEM_INVERSION_SAFE);
AddToSingletonList();
}
DriverStationLCD::~DriverStationLCD()
{
semDelete(m_textBufferSemaphore);
delete [] m_textBuffer;
m_instance = NULL;
}
/**
* Return a pointer to the singleton DriverStationLCD.
*/
DriverStationLCD* DriverStationLCD::GetInstance()
{
if (m_instance == NULL)
{
m_instance = new DriverStationLCD();
}
return m_instance;
}
/**
* Send the text data to the Driver Station.
*/
void DriverStationLCD::UpdateLCD()
{
Synchronized sync(m_textBufferSemaphore);
setUserDsLcdData(m_textBuffer, USER_DS_LCD_DATA_SIZE, kSyncTimeout_ms);
}
/**
* Print formatted text to the Driver Station LCD text bufer.
*
* Use UpdateLCD() periodically to actually send the test to the Driver Station.
*
* @param line The line on the LCD to print to.
* @param startingColumn The column to start printing to. This is a 1-based number.
* @param writeFmt The printf format string describing how to print.
*/
void DriverStationLCD::Printf(Line line, INT32 startingColumn, const char *writeFmt, ...)
{
va_list args;
UINT32 start = startingColumn - 1;
INT32 maxLength = kLineLength - start;
char lineBuffer[kLineLength + 1];
if (startingColumn < 1 || startingColumn > kLineLength)
{
wpi_fatal(ParameterOutOfRange);
return;
}
if (line < kMain_Line6 || line > kUser_Line6)
{
wpi_fatal(ParameterOutOfRange);
return;
}
va_start (args, writeFmt);
{
Synchronized sync(m_textBufferSemaphore);
// snprintf appends NULL to its output. Therefore we can't write directly to the buffer.
INT32 length = vsnprintf(lineBuffer, kLineLength + 1, writeFmt, args);
if (length < 0) length = kLineLength;
memcpy(m_textBuffer + start + line * kLineLength + sizeof(UINT16), lineBuffer, std::min(maxLength,length));
}
va_end (args);
}
/**
* Print formatted text to the Driver Station LCD text bufer. This function
* pads the line with empty spaces.
*
* Use UpdateLCD() periodically to actually send the test to the Driver Station.
*
* @param line The line on the LCD to print to.
* @param writeFmt The printf format string describing how to print.
*/
void DriverStationLCD::PrintfLine(Line line, const char *writeFmt, ...)
{
va_list args;
char lineBuffer[kLineLength + 1];
if (line < kMain_Line6 || line > kUser_Line6)
{
wpi_fatal(ParameterOutOfRange);
return;
}
va_start (args, writeFmt);
{
Synchronized sync(m_textBufferSemaphore);
// snprintf appends NULL to its output. Therefore we can't write directly to the buffer.
INT32 length = std::min(vsnprintf(lineBuffer, kLineLength + 1, writeFmt, args), kLineLength);
if (length < 0) length = kLineLength;
// Fill the rest of the buffer
if (length < kLineLength)
{
memset(lineBuffer + length, ' ', kLineLength - length);
}
memcpy(m_textBuffer + line * kLineLength + sizeof(UINT16), lineBuffer, kLineLength);
}
va_end (args);
}
/**
* Clear all lines on the LCD.
*/
void DriverStationLCD::Clear()
{
Synchronized sync(m_textBufferSemaphore);
memset(m_textBuffer + sizeof(UINT16), ' ', kLineLength*kNumLines);
}