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 |
struct Packet |
22 |
{ |
23 |
enum Command |
24 |
{ |
25 |
PingCommand = 0x00, |
26 |
GetHardwareAndFirmwareVersion = 0x01, |
27 |
WriteUserFlashArea = 0x02, |
28 |
ReadUserFlashArea = 0x03, |
29 |
StoreCurrentStateAsBootState = 0x04, |
30 |
RebootResetHostOrPowerOffHost = 0x05, |
31 |
ClearLCDScreen = 0x06, |
32 |
SetLCDSpecialCharacterData = 0x09, |
33 |
Read8ByesOfLCDMemory = 0x0a, |
34 |
SetLCDCursorPosition = 0x0b, |
35 |
SetLCDCursorStyle = 0x0c, |
36 |
SetLCDContrast = 0x0d, |
37 |
SetLCDAndKeypadBacklight = 0x0e, |
38 |
SetUpFanReporting = 0x10, |
39 |
SetFanPower = 0x11, |
40 |
ReadDOWDeviceInformation = 0x12, |
41 |
SetUpTemperatureReporting = 0x13, |
42 |
ArbitraryDOWTransaction = 0x14, |
43 |
SendCommandDirectlyToTheLCDController = 0x16, |
44 |
ConfigureKeyReporting = 0x17, |
45 |
ReadKeypad = 0x18, |
46 |
SetFanPowerFailSafe = 0x19, |
47 |
SetFanTachometerGlitchFilter = 0x1a, |
48 |
QueryFanPowerAndFailSafeMask = 0x1b, |
49 |
SetATXPowerSwitchFunctionality = 0x1c, |
50 |
EnableDisableAndResetTheWatchdog = 0x1d, |
51 |
ReadReportingAndStatus = 0x1e, |
52 |
SendDataToLCD = 0x1f, |
53 |
SetBaudRate = 0x21, |
54 |
SetOrSetAndConfigureGPIOPin = 0x22, |
55 |
ReadGPIOPinLevelsAndConfigurationState = 0x23, |
56 |
Response = 0x40, |
57 |
Report = 0x80, |
58 |
KeyActivity = 0x80, |
59 |
FanSpeedReport = 0x81, |
60 |
TemperatureSensorReport = 0x82, |
61 |
Error = 0xc0, |
62 |
}; |
63 |
|
64 |
uint8_t command; |
65 |
uint8_t length; |
66 |
uint8_t data[22]; |
67 |
uint16_t crc; |
68 |
|
69 |
inline Packet() {} |
70 |
Packet(Command command, uint8_t length, const uint8_t *data); |
71 |
|
72 |
inline std::string GetData() const { return std::string(reinterpret_cast<const char *>(data), length); } |
73 |
}; |
74 |
std::queue<Packet> responses; |
75 |
Pthreads::ThreadMutex responsesLock; |
76 |
Pthreads::ThreadCondition responsesCondition; |
77 |
std::queue<Packet> keyActivity; |
78 |
Pthreads::ThreadMutex keyActivityLock; |
79 |
Pthreads::Thread thread; |
80 |
|
81 |
inline Packet Communicate(Packet::Command command) { return Communicate_(command, 0, reinterpret_cast<const uint8_t *>(NULL)); } |
82 |
template <typename Arg0> |
83 |
Packet Communicate(Packet::Command command, Arg0 arg0); |
84 |
template <typename Arg0, typename Arg1> |
85 |
Packet Communicate(Packet::Command command, Arg0 arg0, Arg1 arg1); |
86 |
template <typename Arg0, typename Arg1, typename Arg2> |
87 |
Packet Communicate(Packet::Command command, Arg0 arg0, Arg1 arg1, Arg2 arg2); |
88 |
void Communicate_(); |
89 |
Packet Communicate_(Packet::Command command, uint8_t length, const uint8_t *data); |
90 |
|
91 |
public: |
92 |
enum CursorStyle |
93 |
{ |
94 |
NoCursor = 0, |
95 |
BlinkingBlockCursor = 1, |
96 |
UnderscoreCursor = 2, |
97 |
BlinkingBlockPlusUnderscore = 3, |
98 |
InvertingBlinkingBlock = 4, |
99 |
}; |
100 |
enum Key |
101 |
{ |
102 |
Up = 0x01, |
103 |
Enter = 0x02, |
104 |
Cancel = 0x04, |
105 |
Left = 0x08, |
106 |
Right = 0x10, |
107 |
Down = 0x20, |
108 |
}; |
109 |
enum Color |
110 |
{ |
111 |
Green = 1, |
112 |
Red = 0, |
113 |
}; |
114 |
enum KeyActivity |
115 |
{ |
116 |
None = 0, |
117 |
UpPress = 1, |
118 |
DownPress = 2, |
119 |
LeftPress = 3, |
120 |
RightPress = 4, |
121 |
EnterPress = 5, |
122 |
ExitPress = 6, |
123 |
UpRelease = 7, |
124 |
DownRelease = 8, |
125 |
LeftRelease = 9, |
126 |
RightRelease = 10, |
127 |
EnterRelease = 11, |
128 |
ExitRelease = 12, |
129 |
}; |
130 |
|
131 |
Display(const std::string &device); |
132 |
~Display(); |
133 |
|
134 |
bool Ping(const std::string &data = std::string()); |
135 |
std::string Version(); |
136 |
inline void Store() { Communicate(Packet::StoreCurrentStateAsBootState); } |
137 |
inline void Clear() { Communicate(Packet::ClearLCDScreen); } |
138 |
inline void SetCursorPosition(uint8_t column, uint8_t row) { Communicate(Packet::SetLCDCursorPosition, column, row); } |
139 |
inline void SetCursorStyle(CursorStyle style) { Communicate(Packet::SetLCDCursorStyle, style); } |
140 |
inline void SetContrast(uint8_t contrast) { Communicate(Packet::SetLCDContrast, contrast); } |
141 |
inline void SetBacklight(uint8_t backlight) { Communicate(Packet::SetLCDAndKeypadBacklight, backlight); } |
142 |
inline void KeyReporting(uint8_t press, uint8_t release) { Communicate(Packet::ConfigureKeyReporting, press, release); } |
143 |
void Set(uint8_t column, uint8_t row, const std::string &data); |
144 |
void Set(uint8_t led, Color color, uint8_t brightness) { Communicate(Packet::SetOrSetAndConfigureGPIOPin, 12 - (led << 1) - color, brightness); } |
145 |
KeyActivity GetKeyActivity(); |
146 |
|
147 |
friend std::ostream &operator<<(std::ostream &output, const Packet &packet); |
148 |
friend void operator<<(int fd, const Display::Packet &packet); |
149 |
friend void operator>>(int fd, Display::Packet &packet); |
150 |
friend void Communicate(Display *display); |
151 |
}; |
152 |
|
153 |
template <typename Arg0> |
154 |
inline Display::Packet Display::Communicate(Packet::Command command, Arg0 arg0) |
155 |
{ |
156 |
uint8_t data(arg0); |
157 |
|
158 |
return Communicate_(command, 1, &data); |
159 |
} |
160 |
|
161 |
template <> |
162 |
inline Display::Packet Display::Communicate(Packet::Command command, const std::string &string) |
163 |
{ |
164 |
return Communicate_(command, string.size(), reinterpret_cast<const uint8_t *>(string.data())); |
165 |
} |
166 |
|
167 |
template <typename Arg0, typename Arg1> |
168 |
inline Display::Packet Display::Communicate(Packet::Command command, Arg0 arg0, Arg1 arg1) |
169 |
{ |
170 |
uint8_t data[] = { arg0, arg1 }; |
171 |
|
172 |
return Communicate_(command, 2, data); |
173 |
} |
174 |
|
175 |
template <typename Arg0, typename Arg1, typename Arg2> |
176 |
inline Display::Packet Display::Communicate(Packet::Command command, Arg0 arg0, Arg1 arg1, Arg2 arg2) |
177 |
{ |
178 |
uint8_t data[] = { arg0, arg1, arg2 }; |
179 |
|
180 |
return Communicate_(command, 2, data); |
181 |
} |
182 |
|
183 |
template <> |
184 |
inline Display::Packet Display::Communicate(Packet::Command command, uint8_t arg0, uint8_t arg1, std::string string) |
185 |
{ |
186 |
string.insert(0, 1, arg0); |
187 |
string.insert(1, 1, arg1); |
188 |
|
189 |
return Communicate<const std::string &>(command, string); |
190 |
} |
191 |
|
192 |
#endif//_Display_hpp_ |