root/branches/0.10.1/drv_BWCT.c

Revision 773, 9.0 kB (checked in by michael, 23 months ago)

backport 768:771 from trunk, version changed to 0.10.1-RC2

  • Property svn:keywords set to Id URL Rev
Line 
1/* $Id$
2 * $URL$
3 *
4 * new style driver for BWCT USB LCD displays
5 *
6 * Copyright (C) 2003 Michael Reinelt <reinelt@eunet.at>
7 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
8 *
9 * This file is part of LCD4Linux.
10 *
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)
14 * any later version.
15 *
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.
20 *
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.
24 *
25 */
26
27/*
28 *
29 * exported fuctions:
30 *
31 * struct DRIVER drv_BWCT
32 *
33 */
34
35#include "config.h"
36
37#include <stdlib.h>
38#include <stdio.h>
39#include <string.h>
40#include <errno.h>
41#include <unistd.h>
42#include <termios.h>
43#include <fcntl.h>
44#include <sys/ioctl.h>
45#include <sys/time.h>
46
47#include <usb.h>
48
49#include "debug.h"
50#include "cfg.h"
51#include "qprintf.h"
52#include "plugin.h"
53#include "widget.h"
54#include "widget_text.h"
55#include "widget_icon.h"
56#include "widget_bar.h"
57#include "drv.h"
58#include "drv_generic_text.h"
59
60
61#define LCD_USB_VENDOR 0x03da
62#define LCD_USB_DEVICE 0x0002
63
64#define LCD_RESET    1
65#define LCD_CMD      2
66#define LCD_DATA     3
67#define LCD_CONTRAST 4
68
69
70static char Name[] = "BWCT";
71
72static usb_dev_handle *lcd;
73static int interface;
74
75extern int usb_debug;
76extern int got_signal;
77
78
79/****************************************/
80/***  hardware dependant functions    ***/
81/****************************************/
82
83static int drv_BW_open(void)
84{
85    struct usb_bus *busses, *bus;
86    struct usb_device *dev;
87
88    lcd = NULL;
89
90    info("%s: scanning USB for BWCT LCD...", Name);
91
92    usb_debug = 0;
93
94    usb_init();
95    usb_find_busses();
96    usb_find_devices();
97    busses = usb_get_busses();
98
99    for (bus = busses; bus; bus = bus->next) {
100  for (dev = bus->devices; dev; dev = dev->next) {
101      int c;
102      if (dev->descriptor.idVendor != LCD_USB_VENDOR)
103    continue;
104      /* Loop through all of the configurations */
105      for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
106    int i;
107    for (i = 0; i < dev->config[c].bNumInterfaces; i++) {
108        int a;
109        for (a = 0; a < dev->config[c].interface[i].num_altsetting; a++) {
110      if ((dev->descriptor.idProduct == LCD_USB_DEVICE) ||
111          ((dev->config[c].interface[i].altsetting[a].bInterfaceClass == 0xff) &&
112           (dev->config[c].interface[i].altsetting[a].bInterfaceSubClass == 0x01))) {
113          info("%s: found BWCT USB LCD on bus %s device %s", Name, bus->dirname, dev->filename);
114          interface = i;
115          lcd = usb_open(dev);
116          if (usb_claim_interface(lcd, interface) < 0) {
117        error("%s: usb_claim_interface() failed!", Name);
118        return -1;
119          }
120          return 0;
121      }
122        }
123    }
124      }
125  }
126    }
127    return -1;
128}
129
130
131static int drv_BW_close(void)
132{
133    usb_release_interface(lcd, interface);
134    usb_close(lcd);
135
136    return 0;
137}
138
139
140static int drv_BW_send(int request, int value)
141{
142    static int errors = 0;
143
144    if (errors > 20)
145  return -1;
146
147    if (usb_control_msg(lcd, USB_TYPE_VENDOR, request, value, interface, NULL, 0, 1000) < 0) {
148  error("%s: USB request failed!", Name);
149  if (++errors > 20) {
150      error("%s: too many USB errors, aborting.", Name);
151      got_signal = -1;
152  }
153  return -1;
154    }
155    errors = 0;
156    return 0;
157}
158
159
160static void drv_BW_command(const unsigned char cmd)
161{
162    drv_BW_send(LCD_CMD, cmd);
163}
164
165
166static void drv_BW_clear(void)
167{
168    drv_BW_command(0x01); /* clear display */
169    drv_BW_command(0x03); /* return home */
170}
171
172
173static void drv_BW_write(const int row, const int col, const char *data, int len)
174{
175    int pos;
176
177    /* 16x4 Displays use a slightly different layout */
178    if (DCOLS == 16 && DROWS == 4) {
179  pos = (row % 2) * 64 + (row / 2) * 16 + col;
180    } else {
181  pos = (row % 2) * 64 + (row / 2) * 20 + col;
182    }
183
184    drv_BW_command(0x80 | pos);
185
186    while (len--) {
187  drv_BW_send(LCD_DATA, *data++);
188    }
189}
190
191static void drv_BW_defchar(const int ascii, const unsigned char *matrix)
192{
193    int i;
194
195    drv_BW_command(0x40 | 8 * ascii);
196
197    for (i = 0; i < 8; i++) {
198  drv_BW_send(LCD_DATA, *matrix++ & 0x1f);
199    }
200}
201
202
203static int drv_BW_contrast(int contrast)
204{
205    if (contrast < 0)
206  contrast = 0;
207    if (contrast > 255)
208  contrast = 255;
209
210    drv_BW_send(LCD_CONTRAST, contrast);
211
212    return contrast;
213}
214
215
216static int drv_BW_start(const char *section, const int quiet)
217{
218    int contrast;
219    int rows = -1, cols = -1;
220    char *s;
221
222    s = cfg_get(section, "Size", NULL);
223    if (s == NULL || *s == '\0') {
224  error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
225  return -1;
226    }
227    if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) {
228  error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
229  free(s);
230  return -1;
231    }
232
233    DROWS = rows;
234    DCOLS = cols;
235
236    if (drv_BW_open() < 0) {
237  error("%s: could not find a BWCT USB LCD", Name);
238  return -1;
239    }
240
241    /* reset */
242    drv_BW_send(LCD_RESET, 0);
243
244    /* initialize display */
245    drv_BW_command(0x29); /* 8 Bit mode, 1/16 duty cycle, 5x8 font */
246    drv_BW_command(0x08); /* Display off, cursor off, blink off */
247    drv_BW_command(0x0c); /* Display on, cursor off, blink off */
248    drv_BW_command(0x06); /* curser moves to right, no shift */
249
250
251    if (cfg_number(section, "Contrast", 0, 0, 255, &contrast) > 0) {
252  drv_BW_contrast(contrast);
253    }
254
255    drv_BW_clear();   /* clear display */
256
257    if (!quiet) {
258  char buffer[40];
259  qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
260  if (drv_generic_text_greet(buffer, "www.bwct.de")) {
261      sleep(3);
262      drv_BW_clear();
263  }
264    }
265
266    return 0;
267}
268
269
270/****************************************/
271/***            plugins               ***/
272/****************************************/
273
274static void plugin_contrast(RESULT * result, RESULT * arg1)
275{
276    double contrast;
277
278    contrast = drv_BW_contrast(R2N(arg1));
279    SetResult(&result, R_NUMBER, &contrast);
280}
281
282
283/****************************************/
284/***        widget callbacks          ***/
285/****************************************/
286
287
288/* using drv_generic_text_draw(W) */
289/* using drv_generic_text_icon_draw(W) */
290/* using drv_generic_text_bar_draw(W) */
291
292
293/****************************************/
294/***        exported functions        ***/
295/****************************************/
296
297
298/* list models */
299int drv_BW_list(void)
300{
301    printf("generic");
302    return 0;
303}
304
305
306/* initialize driver & display */
307int drv_BW_init(const char *section, const int quiet)
308{
309    WIDGET_CLASS wc;
310    int asc255bug;
311    int ret;
312
313    info("%s: %s", Name, "$Rev$");
314
315    /* display preferences */
316    XRES = 5;     /* pixel width of one char  */
317    YRES = 8;     /* pixel height of one char  */
318    CHARS = 8;      /* number of user-defineable characters */
319    CHAR0 = 0;      /* ASCII of first user-defineable char */
320    GOTO_COST = 2;    /* number of bytes a goto command requires */
321
322    /* real worker functions */
323    drv_generic_text_real_write = drv_BW_write;
324    drv_generic_text_real_defchar = drv_BW_defchar;
325
326
327    /* start display */
328    if ((ret = drv_BW_start(section, quiet)) != 0)
329  return ret;
330
331    /* initialize generic text driver */
332    if ((ret = drv_generic_text_init(section, Name)) != 0)
333  return ret;
334
335    /* initialize generic icon driver */
336    if ((ret = drv_generic_text_icon_init()) != 0)
337  return ret;
338
339    /* initialize generic bar driver */
340    if ((ret = drv_generic_text_bar_init(0)) != 0)
341  return ret;
342
343    /* add fixed chars to the bar driver */
344    /* most displays have a full block on ascii 255, but some have kind of  */
345    /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */
346    /* char will not be used, but rendered by the bar driver */
347    cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug);
348    drv_generic_text_bar_add_segment(0, 0, 255, 32);  /* ASCII  32 = blank */
349    if (!asc255bug)
350  drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */
351
352    /* register text widget */
353    wc = Widget_Text;
354    wc.draw = drv_generic_text_draw;
355    widget_register(&wc);
356
357    /* register icon widget */
358    wc = Widget_Icon;
359    wc.draw = drv_generic_text_icon_draw;
360    widget_register(&wc);
361
362    /* register bar widget */
363    wc = Widget_Bar;
364    wc.draw = drv_generic_text_bar_draw;
365    widget_register(&wc);
366
367    /* register plugins */
368    AddFunction("LCD::contrast", 1, plugin_contrast);
369
370    return 0;
371}
372
373
374/* close driver & display */
375int drv_BW_quit(const int quiet)
376{
377
378    info("%s: shutting down.", Name);
379
380    drv_generic_text_quit();
381
382    /* clear display */
383    drv_BW_clear();
384
385    /* say goodbye... */
386    if (!quiet) {
387  drv_generic_text_greet("goodbye!", NULL);
388    }
389
390    debug("closing USB connection");
391    drv_BW_close();
392
393    return (0);
394}
395
396
397DRIVER drv_BWCT = {
398    .name = Name,
399    .list = drv_BW_list,
400    .init = drv_BW_init,
401    .quit = drv_BW_quit,
402};
Note: See TracBrowser for help on using the browser.