1 |
// Display (Crystalfontz CFA-635 USB LCD) |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#ifndef _Display_hpp_ |
8 |
#define _Display_hpp_ |
9 |
|
10 |
#include <threads.hpp> |
11 |
#include <truck.hpp> |
12 |
|
13 |
#include <queue> |
14 |
|
15 |
class Display |
16 |
{ |
17 |
int ucom; |
18 |
Pthreads::ThreadMutex ucomLock; |
19 |
Pthreads::ThreadCondition ucomCondition; |
20 |
volatile bool running; |
21 |
|
22 |
struct Packet |
23 |
{ |
24 |
enum Command |
25 |
{ |
26 |
PingCommand = 0x00, |
27 |
GetHardwareAndFirmwareVersion = 0x01, |
28 |
WriteUserFlashArea = 0x02, |
29 |
ReadUserFlashArea = 0x03, |
30 |
StoreCurrentStateAsBootState = 0x04, |
31 |
RebootResetHostOrPowerOffHost = 0x05, |
32 |
ClearLCDScreen = 0x06, |
33 |
SetLCDSpecialCharacterData = 0x09, |
34 |
Read8ByesOfLCDMemory = 0x0a, |
35 |
SetLCDCursorPosition = 0x0b, |
36 |
SetLCDCursorStyle = 0x0c, |
37 |
SetLCDContrast = 0x0d, |
38 |
SetLCDAndKeypadBacklight = 0x0e, |
39 |
SetUpFanReporting = 0x10, |
40 |
SetFanPower = 0x11, |
41 |
ReadDOWDeviceInformation = 0x12, |
42 |
SetUpTemperatureReporting = 0x13, |
43 |
ArbitraryDOWTransaction = 0x14, |
44 |
SendCommandDirectlyToTheLCDController = 0x16, |
45 |
ConfigureKeyReporting = 0x17, |
46 |
ReadKeypad = 0x18, |
47 |
SetFanPowerFailSafe = 0x19, |
48 |
SetFanTachometerGlitchFilter = 0x1a, |
49 |
QueryFanPowerAndFailSafeMask = 0x1b, |
50 |
SetATXPowerSwitchFunctionality = 0x1c, |
51 |
EnableDisableAndResetTheWatchdog = 0x1d, |
52 |
ReadReportingAndStatus = 0x1e, |
53 |
SendDataToLCD = 0x1f, |
54 |
SetBaudRate = 0x21, |
55 |
SetOrSetAndConfigureGPIOPin = 0x22, |
56 |
ReadGPIOPinLevelsAndConfigurationState = 0x23, |
57 |
Response = 0x40, |
58 |
Report = 0x80, |
59 |
KeyActivity = 0x80, |
60 |
FanSpeedReport = 0x81, |
61 |
TemperatureSensorReport = 0x82, |
62 |
Error = 0xc0, |
63 |
}; |
64 |
|
65 |
uint8_t command; |
66 |
uint8_t length; |
67 |
uint8_t data[22]; |
68 |
uint16_t crc; |
69 |
|
70 |
inline Packet() {} |
71 |
Packet(Command command, uint8_t length, const uint8_t *data); |
72 |
|
73 |
inline std::string GetData() const |
74 |
{ |
75 |
return std::string(reinterpret_cast<const char *>(data), length); |
76 |
} |
77 |
}; |
78 |
|
79 |
std::queue<Packet> responses; |
80 |
Pthreads::ThreadMutex responsesLock; |
81 |
Pthreads::ThreadCondition responsesCondition; |
82 |
std::queue<Packet> keyActivity; |
83 |
Pthreads::ThreadMutex keyActivityLock; |
84 |
Pthreads::ThreadCondition keyActivityCondition; |
85 |
|
86 |
inline Packet Communicate(Packet::Command command) |
87 |
{ |
88 |
return Communicate_(command, 0, reinterpret_cast<const uint8_t *>(NULL)); |
89 |
} |
90 |
|
91 |
template <typename Arg0> |
92 |
Packet Communicate(Packet::Command command, Arg0 arg0); |
93 |
template <typename Arg0, typename Arg1> |
94 |
Packet Communicate(Packet::Command command, Arg0 arg0, Arg1 arg1); |
95 |
template <typename Arg0, typename Arg1, typename Arg2> |
96 |
Packet Communicate(Packet::Command command, Arg0 arg0, Arg1 arg1, Arg2 arg2); |
97 |
void Communicate_(); |
98 |
Packet Communicate_(Packet::Command command, uint8_t length, const uint8_t *data); |
99 |
void Activity(); |
100 |
|
101 |
public: |
102 |
enum KeyActivity |
103 |
{ |
104 |
None = 0, |
105 |
UpPress = 1, |
106 |
DownPress = 2, |
107 |
LeftPress = 3, |
108 |
RightPress = 4, |
109 |
EnterPress = 5, |
110 |
ExitPress = 6, |
111 |
UpRelease = 7, |
112 |
DownRelease = 8, |
113 |
LeftRelease = 9, |
114 |
RightRelease = 10, |
115 |
EnterRelease = 11, |
116 |
ExitRelease = 12, |
117 |
}; |
118 |
|
119 |
typedef void (*Callback)(KeyActivity, void *); |
120 |
|
121 |
private: |
122 |
Callback callback; |
123 |
void *data; |
124 |
Pthreads::Thread communicate, activity; |
125 |
|
126 |
public: |
127 |
enum CursorStyle |
128 |
{ |
129 |
NoCursor = 0, |
130 |
BlinkingBlockCursor = 1, |
131 |
UnderscoreCursor = 2, |
132 |
BlinkingBlockPlusUnderscore = 3, |
133 |
InvertingBlinkingBlock = 4, |
134 |
}; |
135 |
|
136 |
enum Key |
137 |
{ |
138 |
Up = 0x01, |
139 |
Enter = 0x02, |
140 |
Cancel = 0x04, |
141 |
Left = 0x08, |
142 |
Right = 0x10, |
143 |
Down = 0x20, |
144 |
}; |
145 |
|
146 |
enum Color |
147 |
{ |
148 |
Green = 1, |
149 |
Red = 0, |
150 |
}; |
151 |
|
152 |
Display(const std::string &device, Callback callback = NULL, void *data = NULL); |
153 |
~Display(); |
154 |
|
155 |
bool Ping(const std::string &data = std::string()); |
156 |
std::string Version(); |
157 |
|
158 |
inline void Store() |
159 |
{ |
160 |
Communicate(Packet::StoreCurrentStateAsBootState); |
161 |
} |
162 |
|
163 |
inline void Clear() |
164 |
{ |
165 |
Communicate(Packet::ClearLCDScreen); |
166 |
} |
167 |
|
168 |
inline void SetCursorPosition(uint8_t column, uint8_t row) |
169 |
{ |
170 |
Communicate(Packet::SetLCDCursorPosition, column, row); |
171 |
} |
172 |
|
173 |
inline void SetCursorStyle(CursorStyle style) |
174 |
{ |
175 |
Communicate(Packet::SetLCDCursorStyle, style); |
176 |
} |
177 |
|
178 |
inline void SetContrast(uint8_t contrast) |
179 |
{ |
180 |
Communicate(Packet::SetLCDContrast, contrast); |
181 |
} |
182 |
|
183 |
inline void SetBacklight(uint8_t backlight) |
184 |
{ |
185 |
Communicate(Packet::SetLCDAndKeypadBacklight, backlight); |
186 |
} |
187 |
|
188 |
inline void KeyReporting(uint8_t press, uint8_t release) |
189 |
{ |
190 |
Communicate(Packet::ConfigureKeyReporting, press, release); |
191 |
} |
192 |
|
193 |
void Set(uint8_t column, uint8_t row, const std::string &data); |
194 |
|
195 |
inline void Set(uint8_t column, uint8_t row, char data) |
196 |
{ |
197 |
Set(column, row, std::string(1, data)); |
198 |
} |
199 |
|
200 |
inline void Set(uint8_t led, Color color, uint8_t brightness) |
201 |
{ |
202 |
Communicate(Packet::SetOrSetAndConfigureGPIOPin, 12 - (led << 1) - color, brightness); |
203 |
} |
204 |
|
205 |
friend std::ostream &operator<<(std::ostream &output, const Packet &packet); |
206 |
friend void operator<<(int fd, const Display::Packet &packet); |
207 |
friend void operator>>(int fd, Display::Packet &packet); |
208 |
friend void Communicate(Display *display); |
209 |
friend void Activity(Display *display); |
210 |
}; |
211 |
|
212 |
template <typename Arg0> |
213 |
inline Display::Packet Display::Communicate(Packet::Command command, Arg0 arg0) |
214 |
{ |
215 |
uint8_t data(arg0); |
216 |
|
217 |
return Communicate_(command, 1, &data); |
218 |
} |
219 |
|
220 |
template <> |
221 |
inline Display::Packet Display::Communicate(Packet::Command command, const std::string &string) |
222 |
{ |
223 |
return Communicate_(command, string.size(), reinterpret_cast<const uint8_t *>(string.data())); |
224 |
} |
225 |
|
226 |
template <typename Arg0, typename Arg1> |
227 |
inline Display::Packet Display::Communicate(Packet::Command command, Arg0 arg0, Arg1 arg1) |
228 |
{ |
229 |
uint8_t data[] = { arg0, arg1 }; |
230 |
|
231 |
return Communicate_(command, 2, data); |
232 |
} |
233 |
|
234 |
template <typename Arg0, typename Arg1, typename Arg2> |
235 |
inline Display::Packet Display::Communicate(Packet::Command command, Arg0 arg0, Arg1 arg1, Arg2 arg2) |
236 |
{ |
237 |
uint8_t data[] = { arg0, arg1, arg2 }; |
238 |
|
239 |
return Communicate_(command, 2, data); |
240 |
} |
241 |
|
242 |
template <> |
243 |
inline Display::Packet Display::Communicate(Packet::Command command, uint8_t arg0, uint8_t arg1, std::string string) |
244 |
{ |
245 |
string.insert(0, 1, arg0); |
246 |
string.insert(1, 1, arg1); |
247 |
|
248 |
return Communicate<const std::string &>(command, string); |
249 |
} |
250 |
|
251 |
#endif//_Display_hpp_ |