Now you can download a copy of these docs so you can use them offline! Download now
DriverStationLCD.cpp
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
5 /*----------------------------------------------------------------------------*/
6 
7 #include "DriverStationLCD.h"
8 #include "NetworkCommunication/FRCComm.h"
9 #include "NetworkCommunication/UsageReporting.h"
10 #include "Synchronized.h"
11 #include "WPIErrors.h"
12 #include <strLib.h>
13 
14 const uint32_t DriverStationLCD::kSyncTimeout_ms;
15 const uint16_t DriverStationLCD::kFullDisplayTextCommand;
16 const int32_t DriverStationLCD::kLineLength;
17 const int32_t DriverStationLCD::kNumLines;
18 DriverStationLCD* DriverStationLCD::m_instance = NULL;
19 
26  : m_textBuffer (NULL)
27  , m_textBufferSemaphore (NULL)
28 {
29  m_textBuffer = new char[USER_DS_LCD_DATA_SIZE];
30  memset(m_textBuffer, ' ', USER_DS_LCD_DATA_SIZE);
31 
32  *((uint16_t *)m_textBuffer) = kFullDisplayTextCommand;
33 
34  m_textBufferSemaphore = semMCreate(SEM_DELETE_SAFE);
35 
36  nUsageReporting::report(nUsageReporting::kResourceType_DriverStationLCD, 0);
37 
39 }
40 
41 DriverStationLCD::~DriverStationLCD()
42 {
43  semDelete(m_textBufferSemaphore);
44  delete [] m_textBuffer;
45  m_instance = NULL;
46 }
47 
52 {
53  if (m_instance == NULL)
54  {
55  m_instance = new DriverStationLCD();
56  }
57  return m_instance;
58 }
59 
64 {
65  Synchronized sync(m_textBufferSemaphore);
66  setUserDsLcdData(m_textBuffer, USER_DS_LCD_DATA_SIZE, kSyncTimeout_ms);
67 }
68 
78 void DriverStationLCD::Printf(Line line, int32_t startingColumn, const char *writeFmt, ...)
79 {
80  va_list args;
81  va_start (args, writeFmt);
82  VPrintf(line, startingColumn, writeFmt, args);
83  va_end (args);
84 }
85 
86 void DriverStationLCD::VPrintf(Line line, int32_t startingColumn, const char *writeFmt, va_list args)
87 {
88  uint32_t start = startingColumn - 1;
89  int32_t maxLength = kLineLength - start;
90  char lineBuffer[kLineLength + 1];
91 
92  if (startingColumn < 1 || startingColumn > kLineLength)
93  {
94  wpi_setWPIErrorWithContext(ParameterOutOfRange, "startingColumn");
95  return;
96  }
97 
98  if (line < kMain_Line6 || line > kUser_Line6)
99  {
100  wpi_setWPIErrorWithContext(ParameterOutOfRange, "line");
101  return;
102  }
103 
104  {
105  Synchronized sync(m_textBufferSemaphore);
106  // snprintf appends NULL to its output. Therefore we can't write directly to the buffer.
107  int32_t length = vsnprintf(lineBuffer, kLineLength + 1, writeFmt, args);
108  if (length < 0) length = kLineLength;
109 
110  memcpy(m_textBuffer + start + line * kLineLength + sizeof(uint16_t), lineBuffer, std::min(maxLength,length));
111  }
112 }
113 
123 void DriverStationLCD::PrintfLine(Line line, const char *writeFmt, ...)
124 {
125  va_list args;
126  va_start (args, writeFmt);
127  VPrintfLine(line, writeFmt, args);
128  va_end (args);
129 }
130 
131 void DriverStationLCD::VPrintfLine(Line line, const char *writeFmt, va_list args)
132 {
133  char lineBuffer[kLineLength + 1];
134 
135  if (line < kMain_Line6 || line > kUser_Line6)
136  {
137  wpi_setWPIErrorWithContext(ParameterOutOfRange, "line");
138  return;
139  }
140 
141  {
142  Synchronized sync(m_textBufferSemaphore);
143  // snprintf appends NULL to its output. Therefore we can't write directly to the buffer.
144  int32_t length = std::min(vsnprintf(lineBuffer, kLineLength + 1, writeFmt, args), (int)kLineLength);
145  if (length < 0) length = kLineLength;
146 
147  // Fill the rest of the buffer
148  if (length < kLineLength)
149  {
150  memset(lineBuffer + length, ' ', kLineLength - length);
151  }
152 
153  memcpy(m_textBuffer + line * kLineLength + sizeof(uint16_t), lineBuffer, kLineLength);
154  }
155 }
156 
161 {
162  Synchronized sync(m_textBufferSemaphore);
163  memset(m_textBuffer + sizeof(uint16_t), ' ', kLineLength*kNumLines);
164 }
165 
static DriverStationLCD * GetInstance()
void AddToSingletonList()
Definition: SensorBase.cpp:47
void Printf(Line line, int32_t startingColumn, const char *writeFmt,...)
void PrintfLine(Line line, const char *writeFmt,...)

Generated on Sat Apr 26 2014 12:26:45 for WPILibC++ by doxygen 1.8.6