4 |
|
// |
5 |
|
// $Id$ |
6 |
|
|
7 |
+ |
#include <foreach.hpp> |
8 |
+ |
|
9 |
|
#include <iostream> |
10 |
|
|
11 |
|
#include <fcntl.h> |
13 |
|
|
14 |
|
#include "Display.hpp" |
15 |
|
|
16 |
+ |
extern bool debug; |
17 |
+ |
|
18 |
|
static uint16_t GetCRC(uint8_t *buffer, size_t length) |
19 |
|
{ |
20 |
|
static const uint16_t table[256] = { |
59 |
|
return ~crc; |
60 |
|
} |
61 |
|
|
62 |
< |
Display::Packet::Packet(Display::Packet::Command command, const std::string &data) : command(command), length(data.size()) |
62 |
> |
Display::Packet::Packet(Display::Packet::Command command, uint8_t length, const uint8_t *data) : command(command), length(length) |
63 |
|
{ |
64 |
|
if (length > sizeof (this->data)) |
65 |
|
throw; |
66 |
|
|
67 |
< |
::memcpy(this->data, data.data(), length); |
67 |
> |
::memcpy(this->data, data, length); |
68 |
|
|
69 |
|
crc = GetCRC(reinterpret_cast<uint8_t *>(this), length + 2); |
70 |
|
} |
83 |
|
Posix::Read(fd, packet.data, packet.length); |
84 |
|
Posix::Read(fd, &packet.crc, 2); |
85 |
|
|
86 |
< |
std::printf("0x%02x\n%i\n", packet.command, packet.length); |
86 |
> |
if (debug) |
87 |
> |
{ |
88 |
> |
char *data; |
89 |
> |
|
90 |
> |
::asprintf(&data, "0x%02x %i ", packet.command, packet.length); |
91 |
|
|
92 |
< |
std::cout << '[' << std::string((char*)(packet.data), size_t(packet.length)) << ']' << std::endl; |
92 |
> |
std::cerr << data << '[' << packet.GetData() << ']' << std::endl; |
93 |
> |
|
94 |
> |
std::free(data); |
95 |
> |
} |
96 |
|
} |
97 |
|
|
98 |
|
Display::Display(const std::string &device) : ucom(Posix::Open(device, O_RDWR | O_NOCTTY)) |
114 |
|
Posix::CheckError(::tcsetattr(ucom, TCSANOW, &state)); |
115 |
|
} |
116 |
|
|
117 |
< |
bool Display::Ping(const std::string &data) |
117 |
> |
Display::Packet Display::Communicate(Display::Packet::Command command, uint8_t length, const uint8_t *data) |
118 |
|
{ |
119 |
< |
Packet packet(Packet::PingCommand, data), response; |
119 |
> |
Packet packet(command, length, data), response; |
120 |
|
|
121 |
|
ucom << packet; |
122 |
|
|
123 |
< |
do |
123 |
> |
_forever |
124 |
|
{ |
125 |
|
ucom >> response; |
126 |
+ |
|
127 |
+ |
if (packet.command | Packet::Response == response.command) |
128 |
+ |
return response; |
129 |
+ |
|
130 |
+ |
switch (response.command) |
131 |
+ |
{ |
132 |
+ |
case Packet::KeyActivity: |
133 |
+ |
keyActivity.push(response); |
134 |
+ |
|
135 |
+ |
break; |
136 |
+ |
case Packet::FanSpeedReport: |
137 |
+ |
case Packet::TemperatureSensorReport: |
138 |
+ |
break; |
139 |
+ |
} |
140 |
|
} |
141 |
< |
while (packet.command | Packet::Response != response.command); |
141 |
> |
} |
142 |
> |
|
143 |
> |
bool Display::Ping(const std::string &data) |
144 |
> |
{ |
145 |
> |
return data == Communicate(Packet::PingCommand, data).GetData(); |
146 |
> |
} |
147 |
> |
|
148 |
> |
std::string Display::Version() |
149 |
> |
{ |
150 |
> |
return Communicate(Packet::GetHardwareAndFirmwareVersion).GetData(); |
151 |
> |
} |
152 |
> |
|
153 |
> |
void Display::SetCursorPosition(uint8_t column, uint8_t row) |
154 |
> |
{ |
155 |
> |
const uint8_t data[] = { column, row }; |
156 |
> |
|
157 |
> |
Communicate(Packet::SetLCDCursorPosition, 2, data); |
158 |
> |
} |
159 |
> |
|
160 |
> |
void Display::SetCursorStyle(Display::CursorStyle style) |
161 |
> |
{ |
162 |
> |
uint8_t data(style); |
163 |
> |
|
164 |
> |
Communicate(Packet::SetLCDCursorStyle, 1, &data); |
165 |
> |
} |
166 |
> |
|
167 |
> |
void Display::KeyReporting(uint8_t press, uint8_t release) |
168 |
> |
{ |
169 |
> |
const uint8_t data[] = { press, release }; |
170 |
|
|
171 |
< |
return true; |
171 |
> |
Communicate(Packet::ConfigureKeyReporting, 2, data); |
172 |
|
} |