root/branches/volker_dev/drv_USBLCD.c

Revision 771, 11.2 kB (checked in by michael, 23 months ago)

lots of compiler warnings removed, C++-style comments removed, changed struc initialisation from 'field:value' to '.field=value'

  • Property svn:keywords set to Id URL Rev
Line 
1/* $Id$
2 * $URL$
3 *
4 * new style driver for USBLCD 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 * based on the old-style USBLCD driver which is
10 * Copyright (C) 2002 Robin Adams, Adams IT Services <info@usblcd.de>
11 *
12 * This file is part of LCD4Linux.
13 *
14 * LCD4Linux is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 * LCD4Linux is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 *
28 */
29
30/*
31 *
32 * exported fuctions:
33 *
34 * struct DRIVER drv_USBLCD
35 *
36 */
37
38#include "config.h"
39
40#include <stdlib.h>
41#include <stdio.h>
42#include <string.h>
43#include <errno.h>
44#include <unistd.h>
45#include <termios.h>
46#include <fcntl.h>
47#include <sys/ioctl.h>
48#include <sys/time.h>
49
50#ifdef HAVE_USB_H
51#include <usb.h>
52#endif
53
54#include "debug.h"
55#include "cfg.h"
56#include "qprintf.h"
57#include "udelay.h"
58#include "plugin.h"
59#include "widget.h"
60#include "widget_text.h"
61#include "widget_icon.h"
62#include "widget_bar.h"
63#include "drv.h"
64#include "drv_generic_text.h"
65
66
67#define USBLCD_VENDOR  0x10D2
68#define USBLCD_VENDOR2 0x1212
69#define USBLCD_DEVICE  0x0001
70
71#define IOC_GET_HARD_VERSION 1
72#define IOC_GET_DRV_VERSION  2
73
74
75static char Name[] = "USBLCD";
76
77static char *Port = NULL;
78static int use_libusb = 0;
79static int usblcd_file;
80static char *Buffer;
81static char *BufPtr;
82
83
84#ifdef HAVE_USB_H
85
86static usb_dev_handle *lcd;
87static int interface;
88
89extern int usb_debug;
90
91#endif
92
93
94
95/****************************************/
96/***  hardware dependant functions    ***/
97/****************************************/
98
99#ifdef HAVE_USB_H
100
101static int drv_UL_open(void)
102{
103    struct usb_bus *busses, *bus;
104    struct usb_device *dev;
105
106    lcd = NULL;
107
108    info("%s: scanning for USBLCD...", Name);
109
110    usb_debug = 0;
111
112    usb_init();
113    usb_find_busses();
114    usb_find_devices();
115    busses = usb_get_busses();
116
117    for (bus = busses; bus; bus = bus->next) {
118  for (dev = bus->devices; dev; dev = dev->next) {
119      if (((dev->descriptor.idVendor == USBLCD_VENDOR) ||
120     (dev->descriptor.idVendor == USBLCD_VENDOR2)) && (dev->descriptor.idProduct == USBLCD_DEVICE)) {
121
122    unsigned int v = dev->descriptor.bcdDevice;
123
124    info("%s: found USBLCD V%1d%1d.%1d%1d on bus %s device %s", Name,
125         (v & 0xF000) >> 12, (v & 0xF00) >> 8, (v & 0xF0) >> 4, (v & 0xF), bus->dirname, dev->filename);
126
127    interface = 0;
128    lcd = usb_open(dev);
129    if (usb_claim_interface(lcd, interface) < 0) {
130        error("%s: usb_claim_interface() failed!", Name);
131        error("%s: maybe you have the usblcd module loaded?", Name);
132        return -1;
133    }
134    return 0;
135      }
136  }
137    }
138    error("%s: could not find a USBLCD", Name);
139    return -1;
140}
141
142
143static int drv_UL_close(void)
144{
145    usb_release_interface(lcd, interface);
146    usb_close(lcd);
147
148    return 0;
149}
150
151#endif
152
153
154static void drv_UL_send(void)
155{
156
157#if 0
158    struct timeval now, end;
159    gettimeofday(&now, NULL);
160#endif
161
162    if (use_libusb) {
163#ifdef HAVE_USB_H
164  /* Fixme: Endpoint hardcoded to 1 ??? */
165  usb_bulk_write(lcd, 1, Buffer, BufPtr - Buffer, 1000);
166#endif
167    } else {
168  write(usblcd_file, Buffer, BufPtr - Buffer);
169    }
170
171
172#if 0
173    gettimeofday(&end, NULL);
174    debug("send %d bytes in %d usec (%d usec/byte)", BufPtr - Buffer,
175    (1000000 * (end.tv_sec - now.tv_sec) + end.tv_usec - now.tv_usec),
176    (1000000 * (end.tv_sec - now.tv_sec) + end.tv_usec - now.tv_usec) / (BufPtr - Buffer));
177#endif
178
179    BufPtr = Buffer;
180}
181
182
183static void drv_UL_command(const unsigned char cmd)
184{
185    *BufPtr++ = '\0';
186    *BufPtr++ = cmd;
187}
188
189
190static void drv_UL_clear(void)
191{
192    drv_UL_command(0x01); /* clear display */
193    drv_UL_command(0x03); /* return home */
194    drv_UL_send();    /* flush buffer */
195}
196
197
198static void drv_UL_write(const int row, const int col, const char *data, int len)
199{
200    int pos;
201
202    /* 16x4 Displays use a slightly different layout */
203    if (DCOLS == 16 && DROWS == 4) {
204  pos = (row % 2) * 64 + (row / 2) * 16 + col;
205    } else {
206  pos = (row % 2) * 64 + (row / 2) * 20 + col;
207    }
208
209    drv_UL_command(0x80 | pos);
210
211    while (len--) {
212  if (*data == 0)
213      *BufPtr++ = 0;
214  *BufPtr++ = *data++;
215    }
216
217    drv_UL_send();
218}
219
220static void drv_UL_defchar(const int ascii, const unsigned char *matrix)
221{
222    int i;
223
224    drv_UL_command(0x40 | 8 * ascii);
225
226    for (i = 0; i < 8; i++) {
227  if ((*matrix & 0x1f) == 0)
228      *BufPtr++ = 0;
229  *BufPtr++ = *matrix++ & 0x1f;
230    }
231
232    drv_UL_send();
233}
234
235
236static int drv_UL_start(const char *section, const int quiet)
237{
238    int rows = -1, cols = -1;
239    int major, minor;
240    char *port, *s;
241    char buf[128];
242
243    if (Port) {
244  free(Port);
245  Port = NULL;
246    }
247
248    if ((port = cfg_get(section, "Port", NULL)) == NULL || *port == '\0') {
249  error("%s: no '%s.Port' entry from %s", Name, section, cfg_source());
250  return -1;
251    }
252
253    if (strcasecmp(port, "libusb") == 0) {
254#ifdef HAVE_USB_H
255  use_libusb = 1;
256  debug("using libusb");
257#else
258  error("%s: cannot use 'libusb' port.", Name);
259  error("%s: lcd4linux was compiled without libusb support!", Name);
260  return -1;
261#endif
262    } else {
263  if (port[0] == '/') {
264      Port = strdup(port);
265  } else {
266      int len = 5 + strlen(port) + 1;
267      Port = malloc(len);
268      qprintf(Port, len, "/dev/%s", port);
269  }
270  debug("using device %s ", Port);
271    }
272
273    s = cfg_get(section, "Size", NULL);
274    if (s == NULL || *s == '\0') {
275  error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
276  return -1;
277    }
278    if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) {
279  error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
280  free(s);
281  return -1;
282    }
283
284    DROWS = rows;
285    DCOLS = cols;
286
287    if (use_libusb) {
288
289#ifdef HAVE_USB_H
290  if (drv_UL_open() < 0) {
291      return -1;
292  }
293#endif
294
295    } else {
296
297  /* open port */
298  usblcd_file = open(Port, O_WRONLY);
299  if (usblcd_file == -1) {
300      error("%s: open(%s) failed: %s", Name, Port, strerror(errno));
301      return -1;
302  }
303
304  /* get driver version */
305  memset(buf, 0, sizeof(buf));
306  if (ioctl(usblcd_file, IOC_GET_DRV_VERSION, buf) != 0) {
307      error("%s: ioctl() failed, could not get Driver Version!", Name);
308      return -1;
309  }
310  info("%s: Driver Version: %s", Name, buf);
311
312  if (sscanf(buf, "USBLCD Driver Version %d.%d", &major, &minor) != 2) {
313      error("%s: could not read Driver Version!", Name);
314      return -1;
315  }
316  if (major != 1) {
317      error("%s: Driver Version %d not supported!", Name, major);
318      return -1;
319  }
320
321  memset(buf, 0, sizeof(buf));
322  if (ioctl(usblcd_file, IOC_GET_HARD_VERSION, buf) != 0) {
323      error("%s: ioctl() failed, could not get Hardware Version!", Name);
324      return -1;
325  }
326  info("%s: Hardware Version: %s", Name, buf);
327
328  if (sscanf(buf, "%d.%d", &major, &minor) != 2) {
329      error("%s: could not read Hardware Version!", Name);
330      return -1;
331  }
332
333  if (major != 1) {
334      error("%s: Hardware Version %d not supported!", Name, major);
335      return -1;
336  }
337    }
338
339    /* Init the command buffer */
340    Buffer = (char *) malloc(1024);
341    if (Buffer == NULL) {
342  error("%s: coommand buffer could not be allocated: malloc() failed", Name);
343  return -1;
344    }
345    BufPtr = Buffer;
346
347    /* initialize display */
348    drv_UL_command(0x29); /* 8 Bit mode, 1/16 duty cycle, 5x8 font */
349    drv_UL_command(0x08); /* Display off, cursor off, blink off */
350    drv_UL_command(0x0c); /* Display on, cursor off, blink off */
351    drv_UL_command(0x06); /* curser moves to right, no shift */
352
353    drv_UL_clear();   /* clear display */
354
355    if (!quiet) {
356  char buffer[40];
357  qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
358  if (drv_generic_text_greet(buffer, "http://www.usblcd.de")) {
359      sleep(3);
360      drv_UL_clear();
361  }
362    }
363
364    return 0;
365}
366
367
368/****************************************/
369/***            plugins               ***/
370/****************************************/
371
372/* none at the moment... */
373
374
375/****************************************/
376/***        widget callbacks          ***/
377/****************************************/
378
379
380/* using drv_generic_text_draw(W) */
381/* using drv_generic_text_icon_draw(W) */
382/* using drv_generic_text_bar_draw(W) */
383
384
385/****************************************/
386/***        exported functions        ***/
387/****************************************/
388
389
390/* list models */
391int drv_UL_list(void)
392{
393    printf("generic");
394    return 0;
395}
396
397
398/* initialize driver & display */
399int drv_UL_init(const char *section, const int quiet)
400{
401    WIDGET_CLASS wc;
402    int asc255bug;
403    int ret;
404
405    info("%s: %s", Name, "$Rev$");
406
407    /* display preferences */
408    XRES = 5;     /* pixel width of one char  */
409    YRES = 8;     /* pixel height of one char  */
410    CHARS = 8;      /* number of user-defineable characters */
411    CHAR0 = 0;      /* ASCII of first user-defineable char */
412    GOTO_COST = 2;    /* number of bytes a goto command requires */
413
414    /* real worker functions */
415    drv_generic_text_real_write = drv_UL_write;
416    drv_generic_text_real_defchar = drv_UL_defchar;
417
418
419    /* start display */
420    if ((ret = drv_UL_start(section, quiet)) != 0)
421  return ret;
422
423    /* initialize generic text driver */
424    if ((ret = drv_generic_text_init(section, Name)) != 0)
425  return ret;
426
427    /* initialize generic icon driver */
428    if ((ret = drv_generic_text_icon_init()) != 0)
429  return ret;
430
431    /* initialize generic bar driver */
432    if ((ret = drv_generic_text_bar_init(0)) != 0)
433  return ret;
434
435    /* add fixed chars to the bar driver */
436    /* most displays have a full block on ascii 255, but some have kind of  */
437    /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */
438    /* char will not be used, but rendered by the bar driver */
439    cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug);
440    drv_generic_text_bar_add_segment(0, 0, 255, 32);  /* ASCII  32 = blank */
441    if (!asc255bug)
442  drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */
443
444    /* register text widget */
445    wc = Widget_Text;
446    wc.draw = drv_generic_text_draw;
447    widget_register(&wc);
448
449    /* register icon widget */
450    wc = Widget_Icon;
451    wc.draw = drv_generic_text_icon_draw;
452    widget_register(&wc);
453
454    /* register bar widget */
455    wc = Widget_Bar;
456    wc.draw = drv_generic_text_bar_draw;
457    widget_register(&wc);
458
459    /* register plugins */
460    /* none at the moment... */
461
462
463    return 0;
464}
465
466
467/* close driver & display */
468int drv_UL_quit(const int quiet)
469{
470
471    info("%s: shutting down.", Name);
472
473    /* flush buffer */
474    drv_UL_send();
475
476    drv_generic_text_quit();
477
478    /* clear display */
479    drv_UL_clear();
480
481    /* say goodbye... */
482    if (!quiet) {
483  drv_generic_text_greet("goodbye!", NULL);
484    }
485
486    if (use_libusb) {
487#ifdef HAVE_USB_H
488  drv_UL_close();
489#endif
490    } else {
491  debug("closing port %s", Port);
492  close(usblcd_file);
493    }
494
495    if (Buffer) {
496  free(Buffer);
497  Buffer = NULL;
498  BufPtr = Buffer;
499    }
500
501    return (0);
502}
503
504
505DRIVER drv_USBLCD = {
506    .name = Name,
507    .list = drv_UL_list,
508    .init = drv_UL_init,
509    .quit = drv_UL_quit,
510};
Note: See TracBrowser for help on using the browser.