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 <posix.hpp> |
11 |
#include <truck.hpp> |
12 |
|
13 |
#include <queue> |
14 |
|
15 |
class Display |
16 |
{ |
17 |
int ucom; |
18 |
struct Packet |
19 |
{ |
20 |
enum Command |
21 |
{ |
22 |
PingCommand = 0x00, |
23 |
GetHardwareAndFirmwareVersion = 0x01, |
24 |
WriteUserFlashArea = 0x02, |
25 |
ReadUserFlashArea = 0x03, |
26 |
StoreCurrentStateAsBootState = 0x04, |
27 |
RebootResetHostOrPowerOffHost = 0x05, |
28 |
ClearLCDScreen = 0x06, |
29 |
SetLCDSpecialCharacterData = 0x09, |
30 |
Read8ByesOfLCDMemory = 0x0a, |
31 |
SetLCDCursorPosition = 0x0b, |
32 |
SetLCDCursorStyle = 0x0c, |
33 |
SetLCDContrast = 0x0d, |
34 |
SetLCDAndKeypadBacklight = 0x0e, |
35 |
SetUpFanReporting = 0x10, |
36 |
SetFanPower = 0x11, |
37 |
ReadDOWDeviceInformation = 0x12, |
38 |
SetUpTemperatureReporting = 0x13, |
39 |
ArbitraryDOWTransaction = 0x14, |
40 |
SendCommandDirectlyToTheLCDController = 0x16, |
41 |
ConfigureKeyReporting = 0x17, |
42 |
ReadKeypad = 0x18, |
43 |
SetFanPowerFailSafe = 0x19, |
44 |
SetFanTachometerGlitchFilter = 0x1a, |
45 |
QueryFanPowerAndFailSafeMask = 0x1b, |
46 |
SetATXPowerSwitchFunctionality = 0x1c, |
47 |
EnableDisableAndResetTheWatchdog = 0x1d, |
48 |
ReadReportingAndStatus = 0x1e, |
49 |
SendDataToLCD = 0x1f, |
50 |
SetBaudRate = 0x21, |
51 |
SetOrSetAndConfigureGPIOPin = 0x22, |
52 |
ReadGPIOPinLevelsAndConfigurationState = 0x23, |
53 |
Response = 0x40, |
54 |
KeyActivity = 0x80, |
55 |
FanSpeedReport = 0x81, |
56 |
TemperatureSensorReport = 0x82, |
57 |
Error = 0xc0, |
58 |
}; |
59 |
|
60 |
uint8_t command; |
61 |
uint8_t length; |
62 |
uint8_t data[22]; |
63 |
uint16_t crc; |
64 |
|
65 |
inline Packet() {} |
66 |
Packet(Command command, uint8_t length, const uint8_t *data); |
67 |
|
68 |
inline std::string GetData() { return std::string(reinterpret_cast<char *>(data), length); } |
69 |
}; |
70 |
std::queue<Packet> keyActivity; |
71 |
|
72 |
inline Packet Communicate(Packet::Command command) { return Communicate_(command, 0, reinterpret_cast<const uint8_t *>(NULL)); } |
73 |
template <typename Arg0> |
74 |
Packet Communicate(Packet::Command command, Arg0 arg0); |
75 |
template <typename Arg0, typename Arg1> |
76 |
Packet Communicate(Packet::Command command, Arg0 arg0, Arg1 arg1); |
77 |
template <typename Arg0, typename Arg1, typename Arg2> |
78 |
Packet Communicate(Packet::Command command, Arg0 arg0, Arg1 arg1, Arg2 arg2); |
79 |
Packet Communicate_(Packet::Command command, uint8_t length, const uint8_t *data); |
80 |
|
81 |
public: |
82 |
enum CursorStyle |
83 |
{ |
84 |
NoCursor = 0, |
85 |
BlinkingBlockCursor = 1, |
86 |
UnderscoreCursor = 2, |
87 |
BlinkingBlockPlusUnderscore = 3, |
88 |
InvertingBlinkingBlock = 4, |
89 |
}; |
90 |
enum Key |
91 |
{ |
92 |
Up = 0x01, |
93 |
Enter = 0x02, |
94 |
Cancel = 0x04, |
95 |
Left = 0x08, |
96 |
Right = 0x10, |
97 |
Down = 0x20, |
98 |
}; |
99 |
enum Color |
100 |
{ |
101 |
Green = 1, |
102 |
Red = 0, |
103 |
}; |
104 |
|
105 |
Display(const std::string &device); |
106 |
~Display() { Posix::Close(ucom); } |
107 |
|
108 |
bool Ping(const std::string &data = std::string()); |
109 |
std::string Version(); |
110 |
inline void Store() { Communicate(Packet::StoreCurrentStateAsBootState); } |
111 |
inline void Clear() { Communicate(Packet::ClearLCDScreen); } |
112 |
inline void SetCursorPosition(uint8_t column, uint8_t row) { Communicate(Packet::SetLCDCursorPosition, column, row); } |
113 |
inline void SetCursorStyle(CursorStyle style) { Communicate(Packet::SetLCDCursorStyle, style); } |
114 |
inline void SetContrast(uint8_t contrast) { Communicate(Packet::SetLCDContrast, contrast); } |
115 |
inline void SetBacklight(uint8_t backlight) { Communicate(Packet::SetLCDAndKeypadBacklight, backlight); } |
116 |
inline void KeyReporting(uint8_t press, uint8_t release) { Communicate(Packet::ConfigureKeyReporting, press, release); } |
117 |
void Set(uint8_t column, uint8_t row, const std::string &data); |
118 |
void Set(uint8_t led, Color color, uint8_t brightness) { Communicate(Packet::SetOrSetAndConfigureGPIOPin, 12 - (led << 1) - color, brightness); } |
119 |
}; |
120 |
|
121 |
template <typename Arg0> |
122 |
inline Display::Packet Display::Communicate(Packet::Command command, Arg0 arg0) |
123 |
{ |
124 |
uint8_t data(arg0); |
125 |
|
126 |
return Communicate_(command, 1, &data); |
127 |
} |
128 |
|
129 |
template <> |
130 |
inline Display::Packet Display::Communicate(Packet::Command command, const std::string &string) |
131 |
{ |
132 |
return Communicate_(command, string.size(), reinterpret_cast<const uint8_t *>(string.data())); |
133 |
} |
134 |
|
135 |
template <typename Arg0, typename Arg1> |
136 |
inline Display::Packet Display::Communicate(Packet::Command command, Arg0 arg0, Arg1 arg1) |
137 |
{ |
138 |
uint8_t data[] = { arg0, arg1 }; |
139 |
|
140 |
return Communicate_(command, 2, data); |
141 |
} |
142 |
|
143 |
template <typename Arg0, typename Arg1, typename Arg2> |
144 |
inline Display::Packet Display::Communicate(Packet::Command command, Arg0 arg0, Arg1 arg1, Arg2 arg2) |
145 |
{ |
146 |
uint8_t data[] = { arg0, arg1, arg2 }; |
147 |
|
148 |
return Communicate_(command, 2, data); |
149 |
} |
150 |
|
151 |
template <> |
152 |
inline Display::Packet Display::Communicate(Packet::Command command, uint8_t arg0, uint8_t arg1, const std::string &string) |
153 |
{ |
154 |
uint8_t data[] = { arg0, arg1 }; |
155 |
|
156 |
return Communicate_(command, 2 + string.size(), reinterpret_cast<const uint8_t *>((reinterpret_cast<const char *>(data) + string).data())); |
157 |
} |
158 |
|
159 |
#endif//_Display_hpp_ |