root/tags/0.10.0/drv_USBLCD.c

Revision 547, 13.9 kB (checked in by reinelt, 4 years ago)

[lcd4linux @ 2005-05-08 04:32:43 by reinelt]
CodingStyle? added and applied

Line 
1/* $Id: drv_USBLCD.c,v 1.23 2005/05/08 04:32:44 reinelt Exp $
2 *
3 * new style driver for USBLCD displays
4 *
5 * Copyright (C) 2003 Michael Reinelt <reinelt@eunet.at>
6 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
7 *
8 * based on the old-style USBLCD driver which is
9 * Copyright (C) 2002 Robin Adams, Adams IT Services <info@usblcd.de>
10 *
11 * This file is part of LCD4Linux.
12 *
13 * LCD4Linux is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
17 *
18 * LCD4Linux is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 *
27 *
28 * $Log: drv_USBLCD.c,v $
29 * Revision 1.23  2005/05/08 04:32:44  reinelt
30 * CodingStyle added and applied
31 *
32 * Revision 1.22  2005/04/24 04:33:46  reinelt
33 * driver for TREFON USB LCD's added
34 *
35 * Revision 1.21  2005/04/02 05:28:58  reinelt
36 * fixed gcc4 warnings about signed/unsigned mismatches
37 *
38 * Revision 1.20  2005/01/30 06:43:22  reinelt
39 * driver for LCD-Linux finished
40 *
41 * Revision 1.19  2005/01/18 06:30:23  reinelt
42 * added (C) to all copyright statements
43 *
44 * Revision 1.18  2004/12/22 20:24:02  reinelt
45 * T6963 fix for displays > 8 rows
46 *
47 * Revision 1.17  2004/10/02 09:31:58  reinelt
48 * USBLCD driver modified to use libusb
49 *
50 * Revision 1.16  2004/09/24 21:41:00  reinelt
51 * new driver for the BWCT USB LCD interface board.
52 *
53 * Revision 1.15  2004/06/26 12:04:59  reinelt
54 *
55 * uh-oh... the last CVS log message messed up things a lot...
56 *
57 * Revision 1.14  2004/06/26 09:27:21  reinelt
58 *
59 * added '-W' to CFLAGS
60 * changed all C++ comments to C ones
61 * cleaned up a lot of signed/unsigned mistakes
62 *
63 * Revision 1.13  2004/06/26 06:12:15  reinelt
64 *
65 * support for Beckmann+Egle Compact Terminals
66 * some mostly cosmetic changes in the MatrixOrbital and USBLCD driver
67 * added debugging to the generic serial driver
68 * fixed a bug in the generic text driver where icons could be drawn outside
69 * the display bounds
70 *
71 * Revision 1.12  2004/06/20 10:09:54  reinelt
72 *
73 * 'const'ified the whole source
74 *
75 * Revision 1.11  2004/06/19 08:20:19  reinelt
76 *
77 * compiler warning in image driver fixed
78 * bar bug in USBLCD driver fixed
79 *
80 * Revision 1.10  2004/06/06 06:51:59  reinelt
81 *
82 * do not display end splash screen if quiet=1
83 *
84 * Revision 1.9  2004/06/05 14:56:48  reinelt
85 *
86 * Cwlinux splash screen fixed
87 * USBLCD splash screen fixed
88 * plugin_i2c qprintf("%f") replaced with snprintf()
89 *
90 * Revision 1.8  2004/06/05 06:41:40  reinelt
91 *
92 * chancged splash screen again
93 *
94 * Revision 1.7  2004/06/05 06:13:12  reinelt
95 *
96 * splash screen for all text-based display drivers
97 *
98 * Revision 1.6  2004/06/02 09:41:19  reinelt
99 *
100 * prepared support for startup splash screen
101 *
102 * Revision 1.5  2004/05/31 05:38:02  reinelt
103 *
104 * fixed possible bugs with user-defined chars (clear high bits)
105 * thanks to Andy Baxter for debugging the MilfordInstruments driver!
106 *
107 * Revision 1.4  2004/05/26 11:37:36  reinelt
108 *
109 * Curses driver ported.
110 *
111 * Revision 1.3  2004/05/23 08:58:30  reinelt
112 *
113 * icon bug with USBLCD fixed
114 *
115 * Revision 1.2  2004/03/19 09:17:46  reinelt
116 *
117 * removed the extra 'goto' function, row and col are additional parameters
118 * of the write() function now.
119 *
120 * Revision 1.1  2004/02/15 08:22:47  reinelt
121 * ported USBLCD driver to NextGeneration
122 * added drv_M50530.c (I forgot yesterday, sorry)
123 * removed old drivers M50530.c and USBLCD.c
124 *
125 */
126
127/*
128 *
129 * exported fuctions:
130 *
131 * struct DRIVER drv_USBLCD
132 *
133 */
134
135#include "config.h"
136
137#include <stdlib.h>
138#include <stdio.h>
139#include <string.h>
140#include <errno.h>
141#include <unistd.h>
142#include <termios.h>
143#include <fcntl.h>
144#include <sys/ioctl.h>
145#include <sys/time.h>
146
147#ifdef HAVE_USB_H
148#include <usb.h>
149#endif
150
151#include "debug.h"
152#include "cfg.h"
153#include "qprintf.h"
154#include "udelay.h"
155#include "plugin.h"
156#include "widget.h"
157#include "widget_text.h"
158#include "widget_icon.h"
159#include "widget_bar.h"
160#include "drv.h"
161#include "drv_generic_text.h"
162
163
164#define USBLCD_VENDOR  0x10D2
165#define USBLCD_VENDOR2 0x1212
166#define USBLCD_DEVICE  0x0001
167
168#define IOC_GET_HARD_VERSION 1
169#define IOC_GET_DRV_VERSION  2
170
171
172static char Name[] = "USBLCD";
173
174static char *Port = NULL;
175static int use_libusb = 0;
176static int usblcd_file;
177static char *Buffer;
178static char *BufPtr;
179
180
181#ifdef HAVE_USB_H
182
183static usb_dev_handle *lcd;
184static int interface;
185
186extern int usb_debug;
187
188#endif
189
190extern int got_signal;
191
192
193
194/****************************************/
195/***  hardware dependant functions    ***/
196/****************************************/
197
198#ifdef HAVE_USB_H
199
200static int drv_UL_open(void)
201{
202    struct usb_bus *busses, *bus;
203    struct usb_device *dev;
204
205    lcd = NULL;
206
207    info("%s: scanning for USBLCD...", Name);
208
209    usb_debug = 0;
210
211    usb_init();
212    usb_find_busses();
213    usb_find_devices();
214    busses = usb_get_busses();
215
216    for (bus = busses; bus; bus = bus->next) {
217  for (dev = bus->devices; dev; dev = dev->next) {
218      if (((dev->descriptor.idVendor == USBLCD_VENDOR) ||
219     (dev->descriptor.idVendor == USBLCD_VENDOR2)) && (dev->descriptor.idProduct == USBLCD_DEVICE)) {
220
221    unsigned int v = dev->descriptor.bcdDevice;
222
223    info("%s: found USBLCD V%1d%1d.%1d%1d on bus %s device %s", Name,
224         (v & 0xF000) >> 12, (v & 0xF00) >> 8, (v & 0xF0) >> 4, (v & 0xF), bus->dirname, dev->filename);
225
226    interface = 0;
227    lcd = usb_open(dev);
228    if (usb_claim_interface(lcd, interface) < 0) {
229        error("%s: usb_claim_interface() failed!", Name);
230        error("%s: maybe you have the usblcd module loaded?", Name);
231        return -1;
232    }
233    return 0;
234      }
235  }
236    }
237    error("%s: could not find a USBLCD", Name);
238    return -1;
239}
240
241
242static int drv_UL_close(void)
243{
244    usb_release_interface(lcd, interface);
245    usb_close(lcd);
246
247    return 0;
248}
249
250#endif
251
252
253static void drv_UL_send(void)
254{
255
256#if 0
257    struct timeval now, end;
258    gettimeofday(&now, NULL);
259#endif
260
261    if (use_libusb) {
262#ifdef HAVE_USB_H
263  // Fixme: Endpoint hardcoded to 1 ???
264  usb_bulk_write(lcd, 1, Buffer, BufPtr - Buffer, 1000);
265#endif
266    } else {
267  write(usblcd_file, Buffer, BufPtr - Buffer);
268    }
269
270
271#if 0
272    gettimeofday(&end, NULL);
273    debug("send %d bytes in %d usec (%d usec/byte)", BufPtr - Buffer,
274    (1000000 * (end.tv_sec - now.tv_sec) + end.tv_usec - now.tv_usec),
275    (1000000 * (end.tv_sec - now.tv_sec) + end.tv_usec - now.tv_usec) / (BufPtr - Buffer));
276#endif
277
278    BufPtr = Buffer;
279}
280
281
282static void drv_UL_command(const unsigned char cmd)
283{
284    *BufPtr++ = '\0';
285    *BufPtr++ = cmd;
286}
287
288
289static void drv_UL_clear(void)
290{
291    drv_UL_command(0x01); /* clear display */
292    drv_UL_command(0x03); /* return home */
293    drv_UL_send();    /* flush buffer */
294}
295
296
297static void drv_UL_write(const int row, const int col, const char *data, int len)
298{
299    int pos;
300
301    /* 16x4 Displays use a slightly different layout */
302    if (DCOLS == 16 && DROWS == 4) {
303  pos = (row % 2) * 64 + (row / 2) * 16 + col;
304    } else {
305  pos = (row % 2) * 64 + (row / 2) * 20 + col;
306    }
307
308    drv_UL_command(0x80 | pos);
309
310    while (len--) {
311  if (*data == 0)
312      *BufPtr++ = 0;
313  *BufPtr++ = *data++;
314    }
315
316    drv_UL_send();
317}
318
319static void drv_UL_defchar(const int ascii, const unsigned char *matrix)
320{
321    int i;
322
323    drv_UL_command(0x40 | 8 * ascii);
324
325    for (i = 0; i < 8; i++) {
326  if ((*matrix & 0x1f) == 0)
327      *BufPtr++ = 0;
328  *BufPtr++ = *matrix++ & 0x1f;
329    }
330
331    drv_UL_send();
332}
333
334
335static int drv_UL_start(const char *section, const int quiet)
336{
337    int rows = -1, cols = -1;
338    int major, minor;
339    char *port, *s;
340    char buf[128];
341
342    if (Port) {
343  free(Port);
344  Port = NULL;
345    }
346
347    if ((port = cfg_get(section, "Port", NULL)) == NULL || *port == '\0') {
348  error("%s: no '%s.Port' entry from %s", Name, section, cfg_source());
349  return -1;
350    }
351
352    if (strcasecmp(port, "libusb") == 0) {
353#ifdef HAVE_USB_H
354  use_libusb = 1;
355  debug("using libusb");
356#else
357  error("%s: cannot use 'libusb' port.", Name);
358  error("%s: lcd4linux was compiled without libusb support!", Name);
359  return -1;
360#endif
361    } else {
362  if (port[0] == '/') {
363      Port = strdup(port);
364  } else {
365      int len = 5 + strlen(port) + 1;
366      Port = malloc(len);
367      qprintf(Port, len, "/dev/%s", port);
368  }
369  debug("using device %s ", Port);
370    }
371
372    s = cfg_get(section, "Size", NULL);
373    if (s == NULL || *s == '\0') {
374  error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
375  return -1;
376    }
377    if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) {
378  error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
379  free(s);
380  return -1;
381    }
382
383    DROWS = rows;
384    DCOLS = cols;
385
386    if (use_libusb) {
387
388#ifdef HAVE_USB_H
389  if (drv_UL_open() < 0) {
390      return -1;
391  }
392#endif
393
394    } else {
395
396  /* open port */
397  usblcd_file = open(Port, O_WRONLY);
398  if (usblcd_file == -1) {
399      error("%s: open(%s) failed: %s", Name, Port, strerror(errno));
400      return -1;
401  }
402
403  /* get driver version */
404  memset(buf, 0, sizeof(buf));
405  if (ioctl(usblcd_file, IOC_GET_DRV_VERSION, buf) != 0) {
406      error("%s: ioctl() failed, could not get Driver Version!", Name);
407      return -1;
408  }
409  info("%s: Driver Version: %s", Name, buf);
410
411  if (sscanf(buf, "USBLCD Driver Version %d.%d", &major, &minor) != 2) {
412      error("%s: could not read Driver Version!", Name);
413      return -1;
414  }
415  if (major != 1) {
416      error("%s: Driver Version %d not supported!", Name, major);
417      return -1;
418  }
419
420  memset(buf, 0, sizeof(buf));
421  if (ioctl(usblcd_file, IOC_GET_HARD_VERSION, buf) != 0) {
422      error("%s: ioctl() failed, could not get Hardware Version!", Name);
423      return -1;
424  }
425  info("%s: Hardware Version: %s", Name, buf);
426
427  if (sscanf(buf, "%d.%d", &major, &minor) != 2) {
428      error("%s: could not read Hardware Version!", Name);
429      return -1;
430  }
431
432  if (major != 1) {
433      error("%s: Hardware Version %d not supported!", Name, major);
434      return -1;
435  }
436    }
437
438    /* Init the command buffer */
439    Buffer = (char *) malloc(1024);
440    if (Buffer == NULL) {
441  error("%s: coommand buffer could not be allocated: malloc() failed", Name);
442  return -1;
443    }
444    BufPtr = Buffer;
445
446    /* initialize display */
447    drv_UL_command(0x29); /* 8 Bit mode, 1/16 duty cycle, 5x8 font */
448    drv_UL_command(0x08); /* Display off, cursor off, blink off */
449    drv_UL_command(0x0c); /* Display on, cursor off, blink off */
450    drv_UL_command(0x06); /* curser moves to right, no shift */
451
452    drv_UL_clear();   /* clear display */
453
454    if (!quiet) {
455  char buffer[40];
456  qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
457  if (drv_generic_text_greet(buffer, "http://www.usblcd.de")) {
458      sleep(3);
459      drv_UL_clear();
460  }
461    }
462
463    return 0;
464}
465
466
467/****************************************/
468/***            plugins               ***/
469/****************************************/
470
471/* none at the moment... */
472
473
474/****************************************/
475/***        widget callbacks          ***/
476/****************************************/
477
478
479/* using drv_generic_text_draw(W) */
480/* using drv_generic_text_icon_draw(W) */
481/* using drv_generic_text_bar_draw(W) */
482
483
484/****************************************/
485/***        exported functions        ***/
486/****************************************/
487
488
489/* list models */
490int drv_UL_list(void)
491{
492    printf("generic");
493    return 0;
494}
495
496
497/* initialize driver & display */
498int drv_UL_init(const char *section, const int quiet)
499{
500    WIDGET_CLASS wc;
501    int asc255bug;
502    int ret;
503
504    /* display preferences */
505    XRES = 5;     /* pixel width of one char  */
506    YRES = 8;     /* pixel height of one char  */
507    CHARS = 8;      /* number of user-defineable characters */
508    CHAR0 = 0;      /* ASCII of first user-defineable char */
509    GOTO_COST = 2;    /* number of bytes a goto command requires */
510
511    /* real worker functions */
512    drv_generic_text_real_write = drv_UL_write;
513    drv_generic_text_real_defchar = drv_UL_defchar;
514
515
516    /* start display */
517    if ((ret = drv_UL_start(section, quiet)) != 0)
518  return ret;
519
520    /* initialize generic text driver */
521    if ((ret = drv_generic_text_init(section, Name)) != 0)
522  return ret;
523
524    /* initialize generic icon driver */
525    if ((ret = drv_generic_text_icon_init()) != 0)
526  return ret;
527
528    /* initialize generic bar driver */
529    if ((ret = drv_generic_text_bar_init(0)) != 0)
530  return ret;
531
532    /* add fixed chars to the bar driver */
533    /* most displays have a full block on ascii 255, but some have kind of  */
534    /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */
535    /* char will not be used, but rendered by the bar driver */
536    cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug);
537    drv_generic_text_bar_add_segment(0, 0, 255, 32);  /* ASCII  32 = blank */
538    if (!asc255bug)
539  drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */
540
541    /* register text widget */
542    wc = Widget_Text;
543    wc.draw = drv_generic_text_draw;
544    widget_register(&wc);
545
546    /* register icon widget */
547    wc = Widget_Icon;
548    wc.draw = drv_generic_text_icon_draw;
549    widget_register(&wc);
550
551    /* register bar widget */
552    wc = Widget_Bar;
553    wc.draw = drv_generic_text_bar_draw;
554    widget_register(&wc);
555
556    /* register plugins */
557    /* none at the moment... */
558
559
560    return 0;
561}
562
563
564/* close driver & display */
565int drv_UL_quit(const int quiet)
566{
567
568    info("%s: shutting down.", Name);
569
570    /* flush buffer */
571    drv_UL_send();
572
573    drv_generic_text_quit();
574
575    /* clear display */
576    drv_UL_clear();
577
578    /* say goodbye... */
579    if (!quiet) {
580  drv_generic_text_greet("goodbye!", NULL);
581    }
582
583    if (use_libusb) {
584#ifdef HAVE_USB_H
585  drv_UL_close();
586#endif
587    } else {
588  debug("closing port %s", Port);
589  close(usblcd_file);
590    }
591
592    if (Buffer) {
593  free(Buffer);
594  Buffer = NULL;
595  BufPtr = Buffer;
596    }
597
598    return (0);
599}
600
601
602DRIVER drv_USBLCD = {
603  name:Name,
604  list:drv_UL_list,
605  init:drv_UL_init,
606  quit:drv_UL_quit,
607};
Note: See TracBrowser for help on using the browser.