1
/* $Id: drv_LCDLinux.c 975 2009-01-18 11:16:20Z michael $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/drv_LCDLinux.c $
4
* driver for the LCD-Linux HD44780 kernel driver
5
* http://lcd-linux.sourceforge.net
7
* Copyright (C) 2005 Michael Reinelt <michael@reinelt.co.at>
8
* Copyright (C) 2005 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_LCDLinux
44
#include <sys/types.h>
46
#include <sys/ioctl.h>
53
#include "widget_text.h"
54
#include "widget_icon.h"
55
#include "widget_bar.h"
57
#include "drv_generic_text.h"
59
#define LCD_LINUX_MAIN
60
#include <linux/lcd-linux.h>
61
#include <linux/hd44780.h>
63
static char Name[] = "LCD-Linux";
64
static char Device[] = "/dev/lcd";
65
static int lcdlinux_fd = -1;
68
/****************************************/
69
/*** hardware dependant functions ***/
70
/****************************************/
72
static void drv_LL_send(const char *string, const int len)
76
for (run = 0; run < 10; run++) {
77
ret = write(lcdlinux_fd, string, len);
78
if (ret >= 0 || errno != EAGAIN)
81
info("%s: write(%s): EAGAIN #%d", Name, Device, run);
86
error("%s: write(%s) failed: %s", Name, Device, strerror(errno));
87
} else if (ret != len) {
88
error("%s: partial write(%s): len=%d ret=%d", Name, Device, len, ret);
95
static void drv_LL_clear(void)
97
/* No return value check since this ioctl cannot fail */
98
ioctl(lcdlinux_fd, LCDL_CLEAR_DISP);
102
static void drv_LL_write(const int row, const int col, const char *data, int len)
104
int pos = row * DCOLS + col;
106
if (lseek(lcdlinux_fd, pos, SEEK_SET) == (off_t) - 1) {
107
error("%s: lseek(%s) failed: %s", Name, Device, strerror(errno));
109
drv_LL_send(data, len);
113
static void drv_LL_defchar(const int ascii, const unsigned char *matrix)
119
for (i = 1; i < 9; i++) {
120
buf[i] = *matrix++ & 0x1f;
123
if (ioctl(lcdlinux_fd, LCDL_SET_CGRAM_CHAR, &buf) != 0) {
124
error("%s: ioctl(LCDL_SET_CGRAM_CHAR) failed: %s", Name, strerror(errno));
129
static int drv_LL_start(const char *section, const int quiet)
132
int rows = -1, cols = -1;
133
int use_busy = 0, bus4bits = 0, commit = 0;
134
struct lcd_parameters buf;
136
/* emit version information */
137
info("%s: Version %s", Name, LCD_LINUX_VERSION);
139
/* get size from config file */
140
s = cfg_get(section, "Size", NULL);
141
if (s != NULL || *s != '\0') {
142
if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) {
143
error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
151
lcdlinux_fd = open(Device, O_WRONLY);
152
if (lcdlinux_fd == -1) {
153
error("%s: open(%s) failed: %s", Name, Device, strerror(errno));
157
/* get display size */
158
memset(&buf, 0, sizeof(buf));
159
if (ioctl(lcdlinux_fd, LCDL_GET_PARAM, &buf) != 0) {
160
error("%s: ioctl(LCDL_GET_PARAM) failed: %s", Name, strerror(errno));
161
error("%s: Could not query display information!", Name);
164
info("%s: %dx%d display with %d controllers, flags=0x%02lx:",
165
Name, buf.cntr_cols, buf.cntr_rows * buf.num_cntr, buf.num_cntr, buf.flags);
166
info("%s: busy-flag checking %sabled", Name, (buf.flags & HD44780_CHECK_BF) ? "en" : "dis");
167
info("%s: bus width %d bits", Name, (buf.flags & HD44780_4BITS_BUS) ? 4 : 8);
168
info("%s: font size %s", Name, (buf.flags & HD44780_5X10_FONT) ? "5x10" : "5x8");
170
/* overwrite width size from lcd4linux.conf */
171
if (buf.vs_rows || buf.vs_cols) {
172
info("%s: disabling virtual screen", Name);
178
if ((rows > 0 && rows != buf.cntr_rows * buf.num_cntr) || (cols > 0 && cols != buf.cntr_cols)) {
179
info("%s: changing size to %dx%d", Name, cols, rows);
180
buf.cntr_rows = rows / buf.num_cntr;
181
buf.cntr_cols = cols;
185
DROWS = buf.cntr_rows * buf.num_cntr;
186
DCOLS = buf.cntr_cols;
188
/* overwrite busy-flag checking from lcd4linux.conf */
189
cfg_number(section, "UseBusy", 0, 0, 1, &use_busy);
190
if (use_busy && !(buf.flags & HD44780_CHECK_BF)) {
191
info("%s: activating busy-flag checking", Name);
192
buf.flags |= HD44780_CHECK_BF;
194
} else if (!use_busy && (buf.flags & HD44780_CHECK_BF)) {
195
info("%s: deactivating busy-flag checking", Name);
196
buf.flags &= ~HD44780_CHECK_BF;
200
/* overwrite bus length from lcd4linux.conf */
201
cfg_number(section, "Bus4Bit", 0, 0, 1, &bus4bits);
202
if (bus4bits && !(buf.flags & HD44780_4BITS_BUS)) {
203
info("%s: setting bus length to 4 bits", Name);
204
buf.flags |= HD44780_4BITS_BUS;
206
} else if (!bus4bits && (buf.flags & HD44780_4BITS_BUS)) {
207
info("%s: setting bus length to 8 bits", Name);
208
buf.flags &= ~HD44780_4BITS_BUS;
212
if (commit && ioctl(lcdlinux_fd, LCDL_SET_PARAM, &buf) != 0) {
213
error("%s: ioctl(LCDL_SET_PARAM) failed: %s", Name, strerror(errno));
217
/* initialize display */
218
drv_LL_clear(); /* clear display */
220
/* Disable control characters interpretation. */
221
/* No return value check since this ioctl cannot fail */
222
ioctl(lcdlinux_fd, LCDL_RAW_MODE, 1);
226
qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
227
if (drv_generic_text_greet(buffer, "lcd-linux.sf.net")) {
237
/****************************************/
239
/****************************************/
244
/****************************************/
245
/*** widget callbacks ***/
246
/****************************************/
249
/* using drv_generic_text_draw(W) */
250
/* using drv_generic_text_icon_draw(W) */
251
/* using drv_generic_text_bar_draw(W) */
254
/****************************************/
255
/*** exported functions ***/
256
/****************************************/
260
int drv_LL_list(void)
262
printf("LCD-Linux HD44780 kernel driver");
267
/* initialize driver & display */
268
int drv_LL_init(const char *section, const int quiet)
274
info("%s: %s", Name, "$Rev: 975 $");
276
/* display preferences */
277
XRES = 5; /* pixel width of one char */
278
YRES = 8; /* pixel height of one char */
279
CHARS = 8; /* number of user-defineable characters */
280
CHAR0 = 0; /* ASCII of first user-defineable char */
281
GOTO_COST = -1; /* number of bytes a goto command requires */
283
/* real worker functions */
284
drv_generic_text_real_write = drv_LL_write;
285
drv_generic_text_real_defchar = drv_LL_defchar;
289
if ((ret = drv_LL_start(section, quiet)) != 0)
292
/* initialize generic text driver */
293
if ((ret = drv_generic_text_init(section, Name)) != 0)
296
/* initialize generic icon driver */
297
if ((ret = drv_generic_text_icon_init()) != 0)
300
/* initialize generic bar driver */
301
if ((ret = drv_generic_text_bar_init(0)) != 0)
304
/* add fixed chars to the bar driver */
305
/* most displays have a full block on ascii 255, but some have kind of */
306
/* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */
307
/* char will not be used, but rendered by the bar driver */
308
cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug);
309
drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */
311
drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */
313
/* register text widget */
315
wc.draw = drv_generic_text_draw;
316
widget_register(&wc);
318
/* register icon widget */
320
wc.draw = drv_generic_text_icon_draw;
321
widget_register(&wc);
323
/* register bar widget */
325
wc.draw = drv_generic_text_bar_draw;
326
widget_register(&wc);
328
/* register plugins */
335
/* close driver & display */
336
int drv_LL_quit(const int quiet)
339
info("%s: shutting down.", Name);
341
drv_generic_text_quit();
348
drv_generic_text_greet("goodbye!", NULL);
351
/* Enable control characters interpretation. */
352
/* No return value check since this ioctl cannot fail */
353
ioctl(lcdlinux_fd, LCDL_RAW_MODE, 0);
362
DRIVER drv_LCDLinux = {