1
/* $Id: drv_BWCT.c 975 2009-01-18 11:16:20Z michael $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/drv_BWCT.c $
4
* new style driver for BWCT USB LCD displays
6
* Copyright (C) 2003 Michael Reinelt <michael@reinelt.co.at>
7
* Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
9
* This file is part of LCD4Linux.
11
* LCD4Linux is free software; you can redistribute it and/or modify
12
* it under the terms of the GNU General Public License as published by
13
* the Free Software Foundation; either version 2, or (at your option)
16
* LCD4Linux is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU General Public License for more details.
21
* You should have received a copy of the GNU General Public License
22
* along with this program; if not, write to the Free Software
23
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31
* struct DRIVER drv_BWCT
44
#include <sys/ioctl.h>
54
#include "widget_text.h"
55
#include "widget_icon.h"
56
#include "widget_bar.h"
58
#include "drv_generic_text.h"
61
#define LCD_USB_VENDOR 0x03da
62
#define LCD_USB_DEVICE 0x0002
67
#define LCD_CONTRAST 4
70
static char Name[] = "BWCT";
72
static usb_dev_handle *lcd;
75
extern int got_signal;
78
/****************************************/
79
/*** hardware dependant functions ***/
80
/****************************************/
82
static int drv_BW_open(void)
84
struct usb_bus *busses, *bus;
85
struct usb_device *dev;
89
info("%s: scanning USB for BWCT LCD...", Name);
96
busses = usb_get_busses();
98
for (bus = busses; bus; bus = bus->next) {
99
for (dev = bus->devices; dev; dev = dev->next) {
101
if (dev->descriptor.idVendor != LCD_USB_VENDOR)
103
/* Loop through all of the configurations */
104
for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
106
for (i = 0; i < dev->config[c].bNumInterfaces; i++) {
108
for (a = 0; a < dev->config[c].interface[i].num_altsetting; a++) {
109
if ((dev->descriptor.idProduct == LCD_USB_DEVICE) ||
110
((dev->config[c].interface[i].altsetting[a].bInterfaceClass == 0xff) &&
111
(dev->config[c].interface[i].altsetting[a].bInterfaceSubClass == 0x01))) {
112
info("%s: found BWCT USB LCD on bus %s device %s", Name, bus->dirname, dev->filename);
115
if (usb_claim_interface(lcd, interface) < 0) {
116
error("%s: usb_claim_interface() failed!", Name);
130
static int drv_BW_close(void)
132
usb_release_interface(lcd, interface);
139
static int drv_BW_send(int request, int value)
141
static int errors = 0;
146
if (usb_control_msg(lcd, USB_TYPE_VENDOR, request, value, interface, NULL, 0, 1000) < 0) {
147
error("%s: USB request failed!", Name);
149
error("%s: too many USB errors, aborting.", Name);
159
static void drv_BW_command(const unsigned char cmd)
161
drv_BW_send(LCD_CMD, cmd);
165
static void drv_BW_clear(void)
167
drv_BW_command(0x01); /* clear display */
168
drv_BW_command(0x03); /* return home */
172
static void drv_BW_write(const int row, const int col, const char *data, int len)
176
/* 16x4 Displays use a slightly different layout */
177
if (DCOLS == 16 && DROWS == 4) {
178
pos = (row % 2) * 64 + (row / 2) * 16 + col;
180
pos = (row % 2) * 64 + (row / 2) * 20 + col;
183
drv_BW_command(0x80 | pos);
186
drv_BW_send(LCD_DATA, *data++);
190
static void drv_BW_defchar(const int ascii, const unsigned char *matrix)
194
drv_BW_command(0x40 | 8 * ascii);
196
for (i = 0; i < 8; i++) {
197
drv_BW_send(LCD_DATA, *matrix++ & 0x1f);
202
static int drv_BW_contrast(int contrast)
209
drv_BW_send(LCD_CONTRAST, contrast);
215
static int drv_BW_start(const char *section, const int quiet)
218
int rows = -1, cols = -1;
221
s = cfg_get(section, "Size", NULL);
222
if (s == NULL || *s == '\0') {
223
error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
226
if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) {
227
error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
235
if (drv_BW_open() < 0) {
236
error("%s: could not find a BWCT USB LCD", Name);
241
drv_BW_send(LCD_RESET, 0);
243
/* initialize display */
244
drv_BW_command(0x29); /* 8 Bit mode, 1/16 duty cycle, 5x8 font */
245
drv_BW_command(0x08); /* Display off, cursor off, blink off */
246
drv_BW_command(0x0c); /* Display on, cursor off, blink off */
247
drv_BW_command(0x06); /* curser moves to right, no shift */
250
if (cfg_number(section, "Contrast", 0, 0, 255, &contrast) > 0) {
251
drv_BW_contrast(contrast);
254
drv_BW_clear(); /* clear display */
258
qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
259
if (drv_generic_text_greet(buffer, "www.bwct.de")) {
269
/****************************************/
271
/****************************************/
273
static void plugin_contrast(RESULT * result, RESULT * arg1)
277
contrast = drv_BW_contrast(R2N(arg1));
278
SetResult(&result, R_NUMBER, &contrast);
282
/****************************************/
283
/*** widget callbacks ***/
284
/****************************************/
287
/* using drv_generic_text_draw(W) */
288
/* using drv_generic_text_icon_draw(W) */
289
/* using drv_generic_text_bar_draw(W) */
292
/****************************************/
293
/*** exported functions ***/
294
/****************************************/
298
int drv_BW_list(void)
300
printf("BWCT USB to HD44780 interface");
305
/* initialize driver & display */
306
int drv_BW_init(const char *section, const int quiet)
312
info("%s: %s", Name, "$Rev: 975 $");
314
/* display preferences */
315
XRES = 5; /* pixel width of one char */
316
YRES = 8; /* pixel height of one char */
317
CHARS = 8; /* number of user-defineable characters */
318
CHAR0 = 0; /* ASCII of first user-defineable char */
319
GOTO_COST = 2; /* number of bytes a goto command requires */
321
/* real worker functions */
322
drv_generic_text_real_write = drv_BW_write;
323
drv_generic_text_real_defchar = drv_BW_defchar;
327
if ((ret = drv_BW_start(section, quiet)) != 0)
330
/* initialize generic text driver */
331
if ((ret = drv_generic_text_init(section, Name)) != 0)
334
/* initialize generic icon driver */
335
if ((ret = drv_generic_text_icon_init()) != 0)
338
/* initialize generic bar driver */
339
if ((ret = drv_generic_text_bar_init(0)) != 0)
342
/* add fixed chars to the bar driver */
343
/* most displays have a full block on ascii 255, but some have kind of */
344
/* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */
345
/* char will not be used, but rendered by the bar driver */
346
cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug);
347
drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */
349
drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */
351
/* register text widget */
353
wc.draw = drv_generic_text_draw;
354
widget_register(&wc);
356
/* register icon widget */
358
wc.draw = drv_generic_text_icon_draw;
359
widget_register(&wc);
361
/* register bar widget */
363
wc.draw = drv_generic_text_bar_draw;
364
widget_register(&wc);
366
/* register plugins */
367
AddFunction("LCD::contrast", 1, plugin_contrast);
373
/* close driver & display */
374
int drv_BW_quit(const int quiet)
377
info("%s: shutting down.", Name);
379
drv_generic_text_quit();
386
drv_generic_text_greet("goodbye!", NULL);
389
debug("closing USB connection");