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); |
120 |
|
121 |
private: |
122 |
Callback callback; |
123 |
Pthreads::Thread communicate, activity; |
124 |
|
125 |
public: |
126 |
enum CursorStyle |
127 |
{ |
128 |
NoCursor = 0, |
129 |
BlinkingBlockCursor = 1, |
130 |
UnderscoreCursor = 2, |
131 |
BlinkingBlockPlusUnderscore = 3, |
132 |
InvertingBlinkingBlock = 4, |
133 |
}; |
134 |
|
135 |
enum Key |
136 |
{ |
137 |
Up = 0x01, |
138 |
Enter = 0x02, |
139 |
Cancel = 0x04, |
140 |
Left = 0x08, |
141 |
Right = 0x10, |
142 |
Down = 0x20, |
143 |
}; |
144 |
|
145 |
enum Color |
146 |
{ |
147 |
Green = 1, |
148 |
Red = 0, |
149 |
}; |
150 |
|
151 |
Display(const std::string &device, Callback callback = NULL); |
152 |
~Display(); |
153 |
|
154 |
bool Ping(const std::string &data = std::string()); |
155 |
std::string Version(); |
156 |
|
157 |
inline void Store() |
158 |
{ |
159 |
Communicate(Packet::StoreCurrentStateAsBootState); |
160 |
} |
161 |
|
162 |
inline void Clear() |
163 |
{ |
164 |
Communicate(Packet::ClearLCDScreen); |
165 |
} |
166 |
|
167 |
inline void SetCursorPosition(uint8_t column, uint8_t row) |
168 |
{ |
169 |
Communicate(Packet::SetLCDCursorPosition, column, row); |
170 |
} |
171 |
|
172 |
inline void SetCursorStyle(CursorStyle style) |
173 |
{ |
174 |
Communicate(Packet::SetLCDCursorStyle, style); |
175 |
} |
176 |
|
177 |
inline void SetContrast(uint8_t contrast) |
178 |
{ |
179 |
Communicate(Packet::SetLCDContrast, contrast); |
180 |
} |
181 |
|
182 |
inline void SetBacklight(uint8_t backlight) |
183 |
{ |
184 |
Communicate(Packet::SetLCDAndKeypadBacklight, backlight); |
185 |
} |
186 |
|
187 |
inline void KeyReporting(uint8_t press, uint8_t release) |
188 |
{ |
189 |
Communicate(Packet::ConfigureKeyReporting, press, release); |
190 |
} |
191 |
|
192 |
void Set(uint8_t column, uint8_t row, const std::string &data); |
193 |
|
194 |
inline void Set(uint8_t led, Color color, uint8_t brightness) |
195 |
{ |
196 |
Communicate(Packet::SetOrSetAndConfigureGPIOPin, 12 - (led << 1) - color, brightness); |
197 |
} |
198 |
|
199 |
friend std::ostream &operator<<(std::ostream &output, const Packet &packet); |
200 |
friend void operator<<(int fd, const Display::Packet &packet); |
201 |
friend void operator>>(int fd, Display::Packet &packet); |
202 |
friend void Communicate(Display *display); |
203 |
friend void Activity(Display *display); |
204 |
}; |
205 |
|
206 |
template <typename Arg0> |
207 |
inline Display::Packet Display::Communicate(Packet::Command command, Arg0 arg0) |
208 |
{ |
209 |
uint8_t data(arg0); |
210 |
|
211 |
return Communicate_(command, 1, &data); |
212 |
} |
213 |
|
214 |
template <> |
215 |
inline Display::Packet Display::Communicate(Packet::Command command, const std::string &string) |
216 |
{ |
217 |
return Communicate_(command, string.size(), reinterpret_cast<const uint8_t *>(string.data())); |
218 |
} |
219 |
|
220 |
template <typename Arg0, typename Arg1> |
221 |
inline Display::Packet Display::Communicate(Packet::Command command, Arg0 arg0, Arg1 arg1) |
222 |
{ |
223 |
uint8_t data[] = { arg0, arg1 }; |
224 |
|
225 |
return Communicate_(command, 2, data); |
226 |
} |
227 |
|
228 |
template <typename Arg0, typename Arg1, typename Arg2> |
229 |
inline Display::Packet Display::Communicate(Packet::Command command, Arg0 arg0, Arg1 arg1, Arg2 arg2) |
230 |
{ |
231 |
uint8_t data[] = { arg0, arg1, arg2 }; |
232 |
|
233 |
return Communicate_(command, 2, data); |
234 |
} |
235 |
|
236 |
template <> |
237 |
inline Display::Packet Display::Communicate(Packet::Command command, uint8_t arg0, uint8_t arg1, std::string string) |
238 |
{ |
239 |
string.insert(0, 1, arg0); |
240 |
string.insert(1, 1, arg1); |
241 |
|
242 |
return Communicate<const std::string &>(command, string); |
243 |
} |
244 |
|
245 |
#endif//_Display_hpp_ |