1
/* $Id: drv_PICGraphic.c 1141 2011-01-23 17:01:08Z mzuther $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/drv_PICGraphic.c $
4
* PICGraphic lcd4linux driver
6
* Copyright (C) 2009 Peter Bailey <peter.eldridge.bailey@gmail.com>
7
* Copyright (C) 2005 Michael Reinelt <michael@reinelt.co.at>
8
* Copyright (C) 2005, 2006, 2007 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
10
* This file is part of LCD4Linux.
12
* LCD4Linux is free software; you can redistribute it and/or modify
13
* it under the terms of the GNU General Public License as published by
14
* the Free Software Foundation; either version 2, or (at your option)
17
* LCD4Linux is distributed in the hope that it will be useful,
18
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
* GNU General Public License for more details.
22
* You should have received a copy of the GNU General Public License
23
* along with this program; if not, write to the Free Software
24
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32
* struct DRIVER drv_PICGraphic
51
#include "widget_text.h"
52
#include "widget_icon.h"
53
#include "widget_bar.h"
57
#include "drv_generic_graphic.h"
58
#include "drv_generic_gpio.h"
59
#include "drv_generic_serial.h"
61
#define min(a,b) (a < b ? a : b)
63
static char Name[] = "PICGraphic";
65
//#define partialFrame
73
# these could be automatic
93
static char *fbPG = 0, delayDone = 0;
95
void drv_PICGraphic_delay(void *arg)
103
/****************************************/
104
/*** hardware-dependent functions ***/
105
/****************************************/
106
static int drv_PICGraphic_open(const char *section)
108
/* open serial port */
109
if (drv_generic_serial_open(section, Name, 0) < 0)
116
static int drv_PICGraphic_close(void)
118
/* close opened serial port */
119
return drv_generic_serial_close();
122
static void convert2ASCII(char input, char *output)
124
unsigned char temp = input >> 4;
126
output[0] = temp + '0';
128
output[0] = temp - 10 + 'a';
131
output[1] = input + '0';
133
output[1] = input - 10 + 'a';
136
static void drv_PICGraphic_send(const char *data, const unsigned int len)
141
drv_generic_serial_write(data, len);
142
info("sending %d bytes: ", len);
143
for (i = 0; i < min(10, len); i++) { // min(10, len)
144
convert2ASCII(data[i], hexDigits);
145
debug("0x%s (%c)", hexDigits, data[i]);
149
static int drv_PICGraphic_recv(char *dest, const unsigned int len, const char *expect)
151
unsigned int bytes = 0;
153
while (bytes < len) {
154
status = drv_generic_serial_read((char *) dest + bytes, 1);
156
if (dest[bytes] != 0xa && dest[bytes] != 0xd) {
157
if (dest[bytes] != '@') {
162
info("error receiving response: %d", status);
167
status = strncmp((const char *) dest, expect, len);
176
static void drv_PICGraphic_blit(const int row, const int col, const int height, const int width)
178
/* update a rectangular portion of the display */
179
int r, c, index, status;
180
unsigned char cmd[5];
182
debug("blit from (%d,%d) to (%d,%d) out of (%d,%d)", row, col, row + height, col + width, DROWS, DCOLS);
185
for (c = min(col, DCOLS - 1); c < min(col + width, DCOLS); c++) {
186
for (r = min(row, DROWS - 1); r < min(row + height, DROWS); r++) {
187
index = DCOLS * (r / 8) + c;
188
if (index < 0 || index >= DCOLS * DROWS / 8) {
189
error("index too large: %d, r: %d, c: %d", index, r, c);
192
if (drv_generic_graphic_black(r, c)) {
193
fbPG[index] |= (1 << (r % 8));
195
fbPG[index] &= ~(1 << (r % 8));
200
// send rectangular portion with height divisible by 8
205
row8 = 8 * (row / 8);
206
height8 = 8 * (height / 8) + !!(height % 8);
207
info("sending blit");
213
drv_PICGraphic_send(cmd, 5);
214
for (r = min(row8, DROWS - 1); r < min(row8 + height8, DROWS); r += 8) {
215
drv_PICGraphic_send(fbPG + DCOLS * (r / 8) + col, width);
222
info("sending frame");
224
drv_PICGraphic_send((char *) cmd, 1);
225
drv_PICGraphic_send(fbPG, DROWS * DCOLS / 8);
227
// wait for reception of confirmation code
228
status = drv_PICGraphic_recv((char *) cmd, 2, "ff");
230
info("received ff from device");
232
info("did not receive ff from device");
239
static int drv_PICGraphic_GPO(const int num, const int val)
244
cmd[1] = val ? 's' : 'c';
248
// drv_PICGraphic_send(cmd, 3);
253
static int drv_PICGraphic_GPI(const int num)
263
// drv_PICGraphic_send(cmd, 3);
264
// ret = drv_generic_serial_read(cmd, 1);
272
/* example function used in a plugin */
273
static int drv_PICGraphic_contrast(int contrast)
277
/* adjust limits according to the display */
283
/* call a 'contrast' function */
287
// drv_PICGraphic_send(cmd, 2);
292
/* start graphic display */
293
static int drv_PICGraphic_start2(const char *section)
297
int contrast, status, tick, tack;
299
/* read display size from config */
300
s = cfg_get(section, "Size", NULL);
301
if (s == NULL || *s == '\0') {
302
error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
308
if (sscanf(s, "%dx%d", &DCOLS, &DROWS) != 2 || DCOLS < 1 || DROWS < 1) {
309
error("%s: bad Size '%s' from %s", Name, s, cfg_source());
313
s = cfg_get(section, "Font", "6x8");
314
if (s == NULL || *s == '\0') {
315
error("%s: no '%s.Font' entry from %s", Name, section, cfg_source());
321
if (sscanf(s, "%dx%d", &XRES, &YRES) != 2 || XRES < 1 || YRES < 1) {
322
error("%s: bad Font '%s' from %s", Name, s, cfg_source());
326
if (XRES != 6 && YRES != 8) {
327
error("%s: bad Font '%s' from %s (only 6x8 at the moment)", Name, s, cfg_source());
331
/* you surely want to allocate a framebuffer or something... */
332
fbPG = calloc(DCOLS * DROWS / 8, 1);
334
error("failed to allocate framebuffer");
338
info("allocated framebuffer with size %d", DCOLS * DROWS / 8);
339
if (cfg_number("Variables", "tick", 500, 100, 0, &tick) > 0 &&
340
cfg_number("Variables", "tack", 500, 100, 0, &tack) > 0) {
341
info("tick & tack read from config");
342
timer_add(drv_PICGraphic_delay, 0, min(tick, tack), 0); //
344
info("tick & tack not read from config");
345
timer_add(drv_PICGraphic_delay, 0, 80, 0); //
348
/* open communication with the display */
349
if (drv_PICGraphic_open(section) < 0) {
353
/* reset & initialize display */
355
drv_PICGraphic_send(cmd, 1);
357
// wait for reception of confirmation code
358
status = drv_PICGraphic_recv(cmd, 2, "fi");
361
info("received fi from device");
363
info("did not receive fi from device");
366
if (cfg_number(section, "Contrast", 8, 0, 15, &contrast) > 0) {
367
drv_PICGraphic_contrast(contrast);
374
/****************************************/
376
/****************************************/
378
static void plugin_contrast(RESULT * result, RESULT * arg1)
382
contrast = drv_PICGraphic_contrast(R2N(arg1));
383
SetResult(&result, R_NUMBER, &contrast);
387
/****************************************/
388
/*** widget callbacks ***/
389
/****************************************/
392
/* using drv_generic_text_draw(W) */
393
/* using drv_generic_text_icon_draw(W) */
394
/* using drv_generic_text_bar_draw(W) */
395
/* using drv_generic_gpio_draw(W) */
398
/****************************************/
399
/*** exported functions ***/
400
/****************************************/
404
int drv_PICGraphic_list(void)
406
printf("PICGraphic serial-to-graphic by Peter Bailey");
410
/* initialize driver & display */
411
int drv_PICGraphic_init2(const char *section, const int quiet)
415
/* real worker functions */
416
drv_generic_graphic_real_blit = drv_PICGraphic_blit;
417
drv_generic_gpio_real_set = drv_PICGraphic_GPO;
418
drv_generic_gpio_real_get = drv_PICGraphic_GPI;
421
if ((ret = drv_PICGraphic_start2(section)) != 0)
424
/* initialize generic graphic driver */
425
if ((ret = drv_generic_graphic_init(section, Name)) != 0)
430
qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
431
if (drv_generic_graphic_greet(buffer, NULL)) {
433
drv_generic_graphic_clear(); // also clears main framebuffer
437
/* register plugins */
438
AddFunction("LCD::contrast", 1, plugin_contrast);
443
/* close driver & display */
444
/* use this function for a graphic display */
445
int drv_PICGraphic_quit2(const int quiet)
448
info("%s: shutting down.", Name);
451
drv_generic_graphic_clear();
453
drv_generic_gpio_quit();
458
drv_generic_graphic_greet("goodbye!", NULL);
461
info("freeing framebuffer");
464
drv_generic_graphic_quit();
466
debug("closing connection");
467
drv_PICGraphic_close();
472
/* use this one for a graphic display */
473
DRIVER drv_PICGraphic = {
475
.list = drv_PICGraphic_list,
476
.init = drv_PICGraphic_init2,
477
.quit = drv_PICGraphic_quit2,