1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/*
* GraphLCD driver library
*
* avrctl.c - AVR controlled LCD driver class
*
* This file is released under the GNU General Public License. Refer
* to the COPYING file distributed with this package.
*
* (c) 2005 Andreas Regel <andreas.regel AT powarman.de>
*/
#include <stdint.h>
#include <syslog.h>
#include "common.h"
#include "config.h"
#include "port.h"
#include "avrctl.h"
namespace GLCD
{
/* command header:
** 8 bits sync byte (0xAA for sent commands, 0x55 for received commands)
** 8 bits command id
** 16 bits command length (excluding header)
*/
const unsigned char CMD_HDR_SYNC = 0;
const unsigned char CMD_HDR_COMMAND = 1;
const unsigned char CMD_HDR_LENGTH = 2;
const unsigned char CMD_DATA_START = 4;
const unsigned char CMD_SYNC_SEND = 0xAA;
const unsigned char CMD_SYNC_RECV = 0x55;
const unsigned char CMD_SYS_SYNC = 0x00;
const unsigned char CMD_SYS_ACK = 0x01;
const unsigned char CMD_DISP_CLEAR_SCREEN = 0x10;
const unsigned char CMD_DISP_SWITCH_SCREEN = 0x11;
const unsigned char CMD_DISP_SET_BRIGHTNESS = 0x12;
const unsigned char CMD_DISP_SET_COL_DATA = 0x13;
const unsigned char CMD_DISP_SET_ROW_DATA = 0x14;
const unsigned char CMD_DISP_UPDATE = 0x15;
const int kBufferWidth = 256;
const int kBufferHeight = 128;
cDriverAvrCtl::cDriverAvrCtl(cDriverConfig * config)
: config(config)
{
oldConfig = new cDriverConfig(*config);
port = new cSerialPort();
//width = config->width;
//height = config->height;
refreshCounter = 0;
}
cDriverAvrCtl::~cDriverAvrCtl()
{
delete port;
delete oldConfig;
}
int cDriverAvrCtl::Init()
{
int x;
width = config->width;
if (width <= 0)
width = 256;
height = config->height;
if (height <= 0)
height = 128;
for (unsigned int i = 0; i < config->options.size(); i++)
{
if (config->options[i].name == "")
{
}
}
// setup lcd array (wanted state)
newLCD = new unsigned char*[kBufferWidth];
if (newLCD)
{
for (x = 0; x < kBufferWidth; x++)
{
newLCD[x] = new unsigned char[(kBufferHeight + 7) / 8];
memset(newLCD[x], 0, (kBufferHeight + 7) / 8);
}
}
// setup lcd array (current state)
oldLCD = new unsigned char*[kBufferWidth];
if (oldLCD)
{
for (x = 0; x < kBufferWidth; x++)
{
oldLCD[x] = new unsigned char[(kBufferHeight + 7) / 8];
memset(oldLCD[x], 0, (kBufferHeight + 7) / 8);
}
}
if (config->device == "")
{
return -1;
}
if (port->Open(config->device.c_str()) != 0)
return -1;
*oldConfig = *config;
// clear display
Clear();
syslog(LOG_INFO, "%s: AvrCtl initialized.\n", config->name.c_str());
return 0;
}
int cDriverAvrCtl::DeInit()
{
int x;
// free lcd array (wanted state)
if (newLCD)
{
for (x = 0; x < kBufferWidth; x++)
{
delete[] newLCD[x];
}
delete[] newLCD;
}
// free lcd array (current state)
if (oldLCD)
{
for (x = 0; x < kBufferWidth; x++)
{
delete[] oldLCD[x];
}
delete[] oldLCD;
}
if (port->Close() != 0)
return -1;
return 0;
}
int cDriverAvrCtl::CheckSetup()
{
if (config->device != oldConfig->device ||
config->width != oldConfig->width ||
config->height != oldConfig->height)
{
DeInit();
Init();
return 0;
}
if (config->upsideDown != oldConfig->upsideDown ||
config->invert != oldConfig->invert)
{
oldConfig->upsideDown = config->upsideDown;
oldConfig->invert = config->invert;
return 1;
}
return 0;
}
void cDriverAvrCtl::Clear()
{
for (int x = 0; x < kBufferWidth; x++)
memset(newLCD[x], 0, (kBufferHeight + 7) / 8);
}
void cDriverAvrCtl::Set8Pixels(int x, int y, unsigned char data)
{
if (x >= width || y >= height)
return;
if (!config->upsideDown)
{
int offset = 7 - (y % 8);
for (int i = 0; i < 8; i++)
{
newLCD[x + i][y / 8] |= ((data >> (7 - i)) << offset) & (1 << offset);
}
}
else
{
x = width - 1 - x;
y = height - 1 - y;
int offset = 7 - (y % 8);
for (int i = 0; i < 8; i++)
{
newLCD[x - i][y / 8] |= ((data >> (7 - i)) << offset) & (1 << offset);
}
}
}
void cDriverAvrCtl::Refresh(bool refreshAll)
{
int x;
int y;
int i;
int num = kBufferWidth / 2;
unsigned char data[16*num];
if (CheckSetup() == 1)
refreshAll = true;
if (config->refreshDisplay > 0)
{
refreshCounter = (refreshCounter + 1) % config->refreshDisplay;
if (!refreshAll && !refreshCounter)
refreshAll = true;
}
refreshAll = true;
if (refreshAll)
{
for (x = 0; x < kBufferWidth; x += num)
{
for (i = 0; i < num; i++)
{
for (y = 0; y < (kBufferHeight + 7) / 8; y++)
{
data[i * ((kBufferHeight + 7) / 8) + y] = (newLCD[x + i][y]) ^ (config->invert ? 0xff : 0x00);
}
memcpy(oldLCD[x + i], newLCD[x + i], (kBufferHeight + 7) / 8);
}
CmdDispSetColData(x, 0, 16 * num, data);
}
CmdDispUpdate();
CmdDispSwitchScreen();
// and reset RefreshCounter
refreshCounter = 0;
}
else
{
// draw only the changed bytes
}
}
void cDriverAvrCtl::SetBrightness(unsigned int percent)
{
CmdDispSetBrightness(percent);
}
int cDriverAvrCtl::WaitForAck(void)
{
uint8_t cmd[4];
int len;
int timeout = 10000;
len = 0;
while (len < 4 && timeout > 0)
{
len += port->ReadData(&cmd[len]);
timeout--;
}
if (timeout == 0)
return 0;
return 1;
}
void cDriverAvrCtl::CmdSysSync(void)
{
uint8_t cmd[4];
cmd[CMD_HDR_SYNC] = CMD_SYNC_SEND;
cmd[CMD_HDR_COMMAND] = CMD_SYS_SYNC;
cmd[CMD_HDR_LENGTH] = 0;
cmd[CMD_HDR_LENGTH+1] = 0;
port->WriteData(cmd, 4);
WaitForAck();
}
void cDriverAvrCtl::CmdDispClearScreen(void)
{
uint8_t cmd[4];
cmd[CMD_HDR_SYNC] = CMD_SYNC_SEND;
cmd[CMD_HDR_COMMAND] = CMD_DISP_CLEAR_SCREEN;
cmd[CMD_HDR_LENGTH] = 0;
cmd[CMD_HDR_LENGTH+1] = 0;
port->WriteData(cmd, 4);
WaitForAck();
}
void cDriverAvrCtl::CmdDispSwitchScreen(void)
{
uint8_t cmd[4];
cmd[CMD_HDR_SYNC] = CMD_SYNC_SEND;
cmd[CMD_HDR_COMMAND] = CMD_DISP_SWITCH_SCREEN;
cmd[CMD_HDR_LENGTH] = 0;
cmd[CMD_HDR_LENGTH+1] = 0;
port->WriteData(cmd, 4);
WaitForAck();
}
void cDriverAvrCtl::CmdDispSetBrightness(uint8_t percent)
{
uint8_t cmd[5];
cmd[CMD_HDR_SYNC] = CMD_SYNC_SEND;
cmd[CMD_HDR_COMMAND] = CMD_DISP_SET_BRIGHTNESS;
cmd[CMD_HDR_LENGTH] = 0;
cmd[CMD_HDR_LENGTH+1] = 1;
cmd[CMD_DATA_START] = percent;
port->WriteData(cmd, 5);
WaitForAck();
}
void cDriverAvrCtl::CmdDispSetColData(uint16_t column, uint16_t offset, uint16_t length, uint8_t * data)
{
uint8_t cmd[2560];
cmd[CMD_HDR_SYNC] = CMD_SYNC_SEND;
cmd[CMD_HDR_COMMAND] = CMD_DISP_SET_COL_DATA;
cmd[CMD_HDR_LENGTH] = (length + 6) >> 8;
cmd[CMD_HDR_LENGTH+1] = (length + 6);
cmd[CMD_DATA_START] = column >> 8;
cmd[CMD_DATA_START+1] = column;
cmd[CMD_DATA_START+2] = offset >> 8;
cmd[CMD_DATA_START+3] = offset;
cmd[CMD_DATA_START+4] = length >> 8;
cmd[CMD_DATA_START+5] = length;
memcpy(&cmd[CMD_DATA_START+6], data, length);
port->WriteData(cmd, length+10);
WaitForAck();
}
void cDriverAvrCtl::CmdDispUpdate(void)
{
uint8_t cmd[4];
cmd[CMD_HDR_SYNC] = CMD_SYNC_SEND;
cmd[CMD_HDR_COMMAND] = CMD_DISP_UPDATE;
cmd[CMD_HDR_LENGTH] = 0;
cmd[CMD_HDR_LENGTH+1] = 0;
port->WriteData(cmd, 4);
WaitForAck();
}
}
|