thx @arghgra. defined variable != declared variable
2 * GraphLCD driver library
4 * picctl.c - PIC controlled LCD driver class
6 * This file is released under the GNU General Public License. Refer
7 * to the COPYING file distributed with this package.
9 * Codebase by Andreas Regel <andreas.regel AT powarman.de>
10 * (c) 2007 Carsten Presser
28 ** 8 bits sync byte (0xAA for sent commands, 0x55 for received commands)
30 ** 16 bits command length (excluding header)
32 const unsigned char CMD_HDR_SYNC = 0;
33 const unsigned char CMD_HDR_COMMAND = 1;
34 const unsigned char CMD_HDR_LENGTH = 2;
35 const unsigned char CMD_DATA_START = 4;
37 const unsigned char CMD_SYNC_SEND = 0xAA;
38 const unsigned char CMD_SYNC_RECV = 0x55;
39 const unsigned char CMD_ESCAPE_BYTE = 0x42;
42 const unsigned char CMD_SYS_SYNC = 0x00;
43 const unsigned char CMD_SYS_ACK = 0x01;
44 const unsigned char CMD_SYS_NACK = 0x02;
45 const unsigned char CMD_SYS_NIMP = 0xFF;
46 const unsigned char CMD_SYS_IR = 0x10;
48 const unsigned char CMD_DISP_CLEAR_SCREEN = 0x10;
49 const unsigned char CMD_DISP_SET_ROW_DATA = 0x14;
50 const unsigned char CMD_DISP_SET_ADDRESS = 0x16;
52 const unsigned char CMD_READ_CLOCK = 0x40;
53 const unsigned char CMD_WRITE_CLOCK = 0x41;
54 const unsigned char CMD_SET_PWM1 = 0x45;
55 const unsigned char CMD_SET_PWM2 = 0x46;
57 const unsigned char CMD_SET_MODE_MANAGED = 0x70;
58 const unsigned char CMD_SET_MODE_UNMANAGED = 0x71;
60 const unsigned char CMD_BOOT = 0x80;
64 uint8_t cDriverPICCtl::buf[255];
65 uint8_t cDriverPICCtl::buf_pos;
66 uint8_t cDriverPICCtl::buf_cmd_start;
67 bool cDriverPICCtl::buf_flag_escape;
70 cDriverPICCtl* cDriverPICCtl::instance = 0;
72 cDriverPICCtl* cDriverPICCtl::getInstance(cDriverConfig * config)
75 instance = new cDriverPICCtl(config);
78 // end singleton-pattern
82 cDriverPICCtl::cDriverPICCtl(cDriverConfig * config)
85 oldConfig = new cDriverConfig(*config);
87 port = new cSerialPort();
89 //width = config->width;
90 //height = config->height;
96 buf_flag_escape = false;
99 cDriverPICCtl::~cDriverPICCtl()
105 void cDriverPICCtl::SignalHandler(int signal)
107 // read all available data
108 while (instance->port->ReadData(&buf[buf_pos]))
110 // search for SYNC byte
111 if ((buf[buf_pos] == CMD_SYNC_RECV) && (!buf_flag_escape))
113 if (buf_cmd_start != 255)
115 // bytes = buf_cmd_start - buf_pos
116 syslog(LOG_INFO, "PICCtl received a malformed packet. Scrapped xxx bytes\n");
119 buf[buf_pos] = CMD_SYNC_RECV;
121 buf_cmd_start = buf_pos;
125 if ((buf[buf_pos] == CMD_ESCAPE_BYTE) && (!buf_flag_escape))
127 buf_flag_escape = true;
128 buf_pos--; // should do!!! better would be something like next();
134 buf_flag_escape = false;
137 // we need to check for a valid packet...
138 if (buf_pos >=CMD_DATA_START)
140 // if we recieved a valid packet: start decoding
141 if (buf[CMD_HDR_LENGTH+1] == (buf_pos - CMD_DATA_START))
143 instance->DecodeCmd(&buf[buf_cmd_start],(buf_pos-1));
155 void cDriverPICCtl::DecodeCmd(uint8_t * cmd, uint8_t len)
157 switch (cmd[CMD_HDR_COMMAND])
159 case CMD_SYS_ACK: instance->ack_flag = true;
162 case CMD_SYS_IR: printf(" Incoming Key-Event 0x%02x (Source 0x%02x)\n",cmd[CMD_DATA_START+1],cmd[CMD_DATA_START]);
165 default: printf(" recieved Message 0x%x \n",cmd[CMD_HDR_COMMAND]);
170 int cDriverPICCtl::Init()
174 // fixed values so far...
178 for (unsigned int i = 0; i < config->options.size(); i++)
180 if (config->options[i].name == "")
185 // setup lcd array (wanted state)
186 newLCD = new unsigned char*[height];
189 for (y = 0; y < height; y++)
191 newLCD[y] = new unsigned char[width/8];
192 memset(newLCD[y], 0, (width/8));
195 // setup lcd array (current state)
196 oldLCD = new unsigned char*[height];
199 for (y = 0; y < height; y++)
201 oldLCD[y] = new unsigned char[width/8];
202 memset(oldLCD[y], 0, (width/8));
206 if (config->device == "")
210 if (port->Open(config->device.c_str(), SignalHandler) != 0)
213 *oldConfig = *config;
217 CmdDispClearScreen();
220 syslog(LOG_INFO, "%s: PICCtl initialized.\n", config->name.c_str());
224 int cDriverPICCtl::DeInit()
227 // free lcd array (wanted state)
230 for (y = 0; y < height; y++)
236 // free lcd array (current state)
239 for (y = 0; y < height; y++)
246 CmdDispSetUnManaged();
251 if (port->Close() != 0)
256 int cDriverPICCtl::CheckSetup()
258 if (config->device != oldConfig->device ||
259 config->width != oldConfig->width ||
260 config->height != oldConfig->height)
267 if (config->upsideDown != oldConfig->upsideDown ||
268 config->invert != oldConfig->invert)
270 oldConfig->upsideDown = config->upsideDown;
271 oldConfig->invert = config->invert;
277 void cDriverPICCtl::Clear()
279 for (int y = 0; y < height; y++)
280 memset(newLCD[y], 0, (width/8));
283 void cDriverPICCtl::Set8Pixels(int x, int y, unsigned char data)
285 if (x >= width || y >= height)
288 if (!config->upsideDown)
290 newLCD[y][x/8] = data;
296 newLCD[y][x / 8] |= ReverseBits(data);
300 void cDriverPICCtl::Refresh(bool refreshAll)
304 if (CheckSetup() == 1)
307 if (config->refreshDisplay > 0)
309 refreshCounter = (refreshCounter + 1) % config->refreshDisplay;
310 if (!refreshAll && !refreshCounter)
318 // always draw a complete row! (30 bytes!)
319 for (y = 0; y < height; y++)
321 CmdDispSetColData(y, 0, (width/8), newLCD[y]);
322 memcpy(oldLCD[y], newLCD[y], (width/8));
324 // and reset RefreshCounter
330 for (y = 0; y< height; y++)
332 // did anysthing change? -> draw new line
333 if (memcmp(newLCD[y],oldLCD[y],(width/8)) != 0)
335 CmdDispSetColData(y, 0, (width/8), newLCD[y]);
336 memcpy(oldLCD[y], newLCD[y], (width/8));
342 void cDriverPICCtl::SetBrightness(unsigned int percent)
344 CmdDispSetBrightness(percent);
347 void cDriverPICCtl::SetClock(unsigned int percent)
349 CmdDispSetBrightness(percent);
352 int cDriverPICCtl::WaitForAck(void)
356 for (timeout=0; timeout<10000; timeout++)
359 if (ack_flag) return 1;
361 syslog(LOG_INFO, "PICCtl: missed a ACK packet!.\n");
365 void cDriverPICCtl::CmdDispClearScreen(void)
369 cmd[CMD_HDR_SYNC] = CMD_SYNC_SEND;
370 cmd[CMD_HDR_COMMAND] = CMD_DISP_CLEAR_SCREEN;
371 cmd[CMD_HDR_LENGTH] = 0;
372 cmd[CMD_HDR_LENGTH+1] = 0;
374 port->WriteData(cmd, 4);
378 void cDriverPICCtl::CmdDispSetBrightness(uint8_t percent)
382 cmd[CMD_HDR_SYNC] = CMD_SYNC_SEND;
383 cmd[CMD_HDR_COMMAND] = CMD_SET_PWM1;
384 cmd[CMD_HDR_LENGTH] = 0;
385 cmd[CMD_HDR_LENGTH+1] = 1;
386 cmd[CMD_DATA_START] = (uint8_t) round(percent * 2.55);
388 port->WriteData(cmd, 5);
392 void cDriverPICCtl::CmdDispSetColData(uint8_t yoffset, uint8_t xoffset, uint8_t length, uint8_t * data)
397 address = 0x1E * yoffset + xoffset;
399 cmd[CMD_HDR_SYNC] = CMD_SYNC_SEND;
400 cmd[CMD_HDR_COMMAND] = CMD_DISP_SET_ROW_DATA;
401 cmd[CMD_HDR_LENGTH] = 0;
402 cmd[CMD_HDR_LENGTH+1] = (length + 2);
403 cmd[CMD_DATA_START] = (uint8_t)(address % 256); // lower address
404 cmd[CMD_DATA_START+1] = (uint8_t)(address / 256); // upper address
405 memcpy(&cmd[CMD_DATA_START+2], data, length);
407 port->WriteData(cmd, length + 6);
410 // for (address = 0; address < length+6; address ++) printf("0x%02x ",cmd[address]);
411 // printf("address=0x%x upper=0x%x lower=0x%x \n", address, (uint8_t)(address / 256), (uint8_t)(address % 256));
415 void cDriverPICCtl::CmdDispSetManaged(void)
419 cmd[CMD_HDR_SYNC] = CMD_SYNC_SEND;
420 cmd[CMD_HDR_COMMAND] = CMD_SET_MODE_MANAGED;
421 cmd[CMD_HDR_LENGTH] = 0;
422 cmd[CMD_HDR_LENGTH+1] = 0;
424 port->WriteData(cmd,4);
428 void cDriverPICCtl::CmdDispSetUnManaged(void)
432 cmd[CMD_HDR_SYNC] = CMD_SYNC_SEND;
433 cmd[CMD_HDR_COMMAND] = CMD_SET_MODE_UNMANAGED;
434 cmd[CMD_HDR_LENGTH] = 0;
435 cmd[CMD_HDR_LENGTH+1] = 0;
437 port->WriteData(cmd,4);