root/branches/0.10.1/drv_LCD2USB.c

Revision 773, 16.8 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 * driver for USB2LCD display interface
5 * see http://www.harbaum.org/till/lcd2usb for schematics
6 *
7 * Copyright 2005 Till Harbaum <till@harbaum.org>
8 * Copyright 2005 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
9 *
10 * This file is part of LCD4Linux.
11 *
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)
15 * any later version.
16 *
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.
21 *
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.
25 *
26 */
27
28/*
29 *
30 * exported fuctions:
31 *
32 * struct DRIVER drv_LCD2USB
33 *
34 */
35
36#include "config.h"
37
38#include <stdlib.h>
39#include <stdio.h>
40#include <string.h>
41#include <errno.h>
42#include <unistd.h>
43#include <termios.h>
44#include <fcntl.h>
45#include <ctype.h>
46#include <sys/ioctl.h>
47#include <sys/time.h>
48
49#include <usb.h>
50
51#include "debug.h"
52#include "cfg.h"
53#include "timer.h"
54#include "qprintf.h"
55#include "plugin.h"
56#include "widget.h"
57#include "widget_text.h"
58#include "widget_icon.h"
59#include "widget_bar.h"
60#include "widget_keypad.h"
61#include "drv.h"
62#include "drv_generic_text.h"
63#include "drv_generic_keypad.h"
64
65/* vid/pid donated by FTDI */
66#define LCD_USB_VENDOR 0x0403
67#define LCD_USB_DEVICE 0xC630
68
69/* number of buttons on display */
70#define L2U_BUTTONS        (2)
71
72#define LCD_ECHO           (0<<5)
73#define LCD_CMD            (1<<5)
74#define LCD_DATA           (2<<5)
75#define LCD_SET            (3<<5)
76#define LCD_GET            (4<<5)
77
78/* target is a bit map for CMD/DATA */
79#define LCD_CTRL_0              (1<<3)
80#define LCD_CTRL_1              (1<<4)
81#define LCD_BOTH           (LCD_CTRL_0 | LCD_CTRL_1)
82
83/* target is value to set */
84#define LCD_SET_CONTRAST   (LCD_SET | (0<<3))
85#define LCD_SET_BRIGHTNESS (LCD_SET | (1<<3))
86#define LCD_SET_RESERVED0  (LCD_SET | (2<<3))
87#define LCD_SET_RESERVED1  (LCD_SET | (3<<3))
88
89/* target is value to get */
90#define LCD_GET_FWVER      (LCD_GET | (0<<3))
91#define LCD_GET_BUTTONS    (LCD_GET | (1<<3))
92#define LCD_GET_CTRL       (LCD_GET | (2<<3))
93#define LCD_GET_RESERVED1  (LCD_GET | (3<<3))
94
95static char Name[] = "LCD2USB";
96static char *device_id = NULL, *bus_id = NULL;
97
98static usb_dev_handle *lcd;
99static int controllers = 0;
100
101extern int usb_debug;
102extern int got_signal;
103
104/****************************************/
105/***  hardware dependant functions    ***/
106/****************************************/
107
108static int drv_L2U_open(char *bus_id, char *device_id)
109{
110    struct usb_bus *busses, *bus;
111    struct usb_device *dev;
112
113    lcd = NULL;
114
115    info("%s: scanning USB for LCD2USB interface ...", Name);
116
117    if (bus_id != NULL)
118  info("%s: scanning for bus id: %s", Name, bus_id);
119
120    if (device_id != NULL)
121  info("%s: scanning for device id: %s", Name, device_id);
122
123    usb_debug = 0;
124
125    usb_init();
126    usb_find_busses();
127    usb_find_devices();
128    busses = usb_get_busses();
129
130    for (bus = busses; bus; bus = bus->next) {
131  /* search this bus if no bus id was given or if this is the given bus id */
132  if (!bus_id || (bus_id && !strcasecmp(bus->dirname, bus_id))) {
133
134      for (dev = bus->devices; dev; dev = dev->next) {
135    /* search this device if no device id was given or if this is the given device id */
136    if (!device_id || (device_id && !strcasecmp(dev->filename, device_id))) {
137
138        if ((dev->descriptor.idVendor == LCD_USB_VENDOR) && (dev->descriptor.idProduct == LCD_USB_DEVICE)) {
139      info("%s: found LCD2USB interface on bus %s device %s", Name, bus->dirname, dev->filename);
140      lcd = usb_open(dev);
141      if (usb_claim_interface(lcd, 0) < 0) {
142          error("%s: usb_claim_interface() failed!", Name);
143          return -1;
144      }
145      return 0;
146        }
147    }
148      }
149  }
150    }
151    return -1;
152}
153
154
155static int drv_L2U_close(void)
156{
157    usb_release_interface(lcd, 0);
158    usb_close(lcd);
159
160    return 0;
161}
162
163
164static int drv_L2U_send(int request, int value, int index)
165{
166    if (usb_control_msg(lcd, USB_TYPE_VENDOR, request, value, index, NULL, 0, 1000) < 0) {
167  error("%s: USB request failed! Trying to reconnect device.", Name);
168
169  usb_release_interface(lcd, 0);
170  usb_close(lcd);
171
172  /* try to close and reopen connection */
173  if (drv_L2U_open(bus_id, device_id) < 0) {
174      error("%s: could not re-detect LCD2USB USB LCD", Name);
175      got_signal = -1;
176      return -1;
177  }
178  /* and try to re-send command */
179  if (usb_control_msg(lcd, USB_TYPE_VENDOR, request, value, index, NULL, 0, 1000) < 0) {
180      error("%s: retried USB request failed, aborting!", Name);
181      got_signal = -1;
182      return -1;
183  }
184
185  info("%s: Device successfully reconnected.", Name);
186    }
187
188    return 0;
189}
190
191/* send a number of 16 bit words to the lcd2usb interface */
192/* and verify that they are correctly returned by the echo */
193/* command. This may be used to check the reliability of */
194/* the usb interfacing */
195#define ECHO_NUM 100
196
197int drv_L2U_echo(void)
198{
199    int i, nBytes, errors = 0;
200    unsigned short val, ret;
201
202    for (i = 0; i < ECHO_NUM; i++) {
203  val = rand() & 0xffff;
204
205  nBytes = usb_control_msg(lcd,
206         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
207         LCD_ECHO, val, 0, (char *) &ret, sizeof(ret), 1000);
208
209  if (nBytes < 0) {
210      error("%s: USB request failed!", Name);
211      return -1;
212  }
213
214  if (val != ret)
215      errors++;
216    }
217
218    if (errors) {
219  error("%s: ERROR, %d out of %d echo transfers failed!", Name, errors, ECHO_NUM);
220  return -1;
221    } else
222  info("%s: echo test successful", Name);
223
224    return 0;
225}
226
227/* get a value from the lcd2usb interface */
228static int drv_L2U_get(unsigned char cmd)
229{
230    unsigned char buffer[2];
231    int nBytes;
232
233    /* send control request and accept return value */
234    nBytes = usb_control_msg(lcd,
235           USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
236           cmd, 0, 0, (char *) buffer, sizeof(buffer), 1000);
237
238    if (nBytes < 0) {
239  error("%s: USB request failed!", Name);
240  return -1;
241    }
242
243    return buffer[0] + 256 * buffer[1];
244}
245
246/* get lcd2usb interface firmware version */
247static void drv_L2U_get_version(void)
248{
249    int ver = drv_L2U_get(LCD_GET_FWVER);
250
251    if (ver != -1)
252  info("%s: firmware version %d.%02d", Name, ver & 0xff, ver >> 8);
253    else
254  error("%s: unable to read firmware version", Name);
255}
256
257/* get the bit mask of installed LCD controllers (0 = no */
258/* lcd found, 1 = single controller display, 3 = dual */
259/* controller display */
260static void drv_L2U_get_controllers(void)
261{
262    controllers = drv_L2U_get(LCD_GET_CTRL);
263
264    if (controllers != -1) {
265  if (controllers)
266      info("%s: installed controllers: %s%s", Name,
267     (controllers & 1) ? "CTRL0" : "", (controllers & 2) ? " CTRL1" : "");
268  else
269      error("%s: no controllers found", Name);
270    } else {
271  error("%s: unable to read installed controllers", Name);
272  controllers = 0;  /* don't access any controllers */
273    }
274
275    /* convert into controller map matching our protocol */
276    controllers = ((controllers & 1) ? LCD_CTRL_0 : 0) | ((controllers & 2) ? LCD_CTRL_1 : 0);
277}
278
279/* get state of the two optional buttons */
280static unsigned long drv_L2U_get_buttons(void)
281{
282    int buttons = drv_L2U_get(LCD_GET_BUTTONS);
283
284    if (buttons == -1) {
285  error("%s: unable to read button state", Name);
286  buttons = 0;
287    }
288
289    return buttons;
290}
291
292
293/* to increase performance, a little buffer is being used to */
294/* collect command bytes of the same type before transmitting them */
295#define BUFFER_MAX_CMD 4  /* current protocol supports up to 4 bytes */
296int buffer_current_type = -1; /* nothing in buffer yet */
297int buffer_current_fill = 0;  /* -"- */
298unsigned char buffer[BUFFER_MAX_CMD];
299
300/* command format:
301 * 7 6 5 4 3 2 1 0
302 * C C C T T R L L
303 *
304 * TT = target bit map
305 * R = reserved for future use, set to 0
306 * LL = number of bytes in transfer - 1
307 */
308
309/* flush command queue due to buffer overflow / content */
310/* change or due to explicit request */
311static void drv_L2U_flush(void)
312{
313    int request, value, index;
314
315    /* anything to flush? ignore request if not */
316    if (buffer_current_type == -1)
317  return;
318
319    /* build request byte */
320    request = buffer_current_type | (buffer_current_fill - 1);
321
322    /* fill value and index with buffer contents. endianess should IMHO not */
323    /* be a problem, since usb_control_msg() will handle this. */
324    value = buffer[0] | (buffer[1] << 8);
325    index = buffer[2] | (buffer[3] << 8);
326
327    if (controllers) {
328  /* send current buffer contents */
329  drv_L2U_send(request, value, index);
330    }
331
332    /* buffer is now free again */
333    buffer_current_type = -1;
334    buffer_current_fill = 0;
335}
336
337/* enqueue a command into the buffer */
338static void drv_L2U_enqueue(int command_type, int value)
339{
340    if ((buffer_current_type >= 0) && (buffer_current_type != command_type))
341  drv_L2U_flush();
342
343    /* add new item to buffer */
344    buffer_current_type = command_type;
345    buffer[buffer_current_fill++] = value;
346
347    /* flush buffer if it's full */
348    if (buffer_current_fill == BUFFER_MAX_CMD)
349  drv_L2U_flush();
350}
351
352static void drv_L2U_command(const unsigned char ctrl, const unsigned char cmd)
353{
354    drv_L2U_enqueue(LCD_CMD | (ctrl & controllers), cmd);
355}
356
357
358static void drv_L2U_clear(void)
359{
360    drv_L2U_command(LCD_BOTH, 0x01);  /* clear display */
361    drv_L2U_command(LCD_BOTH, 0x03);  /* return home */
362}
363
364static void drv_L2U_write(int row, const int col, const char *data, int len)
365{
366    int pos, ctrl = LCD_CTRL_0;
367
368    /* displays with more two rows and 20 columns have a logical width */
369    /* of 40 chars and use more than one controller */
370    if ((DROWS > 2) && (DCOLS > 20) && (row > 1)) {
371  /* use second controller */
372  row -= 2;
373  ctrl = LCD_CTRL_1;
374    }
375
376    /* 16x4 Displays use a slightly different layout */
377    if (DCOLS == 16 && DROWS == 4) {
378  pos = (row % 2) * 64 + (row / 2) * 16 + col;
379    } else {
380  pos = (row % 2) * 64 + (row / 2) * 20 + col;
381    }
382
383    drv_L2U_command(ctrl, 0x80 | pos);
384
385    while (len--) {
386  drv_L2U_enqueue(LCD_DATA | (ctrl & controllers), *data++);
387    }
388
389    drv_L2U_flush();
390}
391
392static void drv_L2U_defchar(const int ascii, const unsigned char *matrix)
393{
394    int i;
395
396    drv_L2U_command(LCD_BOTH, 0x40 | 8 * ascii);
397
398    for (i = 0; i < 8; i++) {
399  drv_L2U_enqueue(LCD_DATA | (LCD_BOTH & controllers), *matrix++ & 0x1f);
400    }
401
402    drv_L2U_flush();
403}
404
405static int drv_L2U_contrast(int contrast)
406{
407    if (contrast < 0)
408  contrast = 0;
409    if (contrast > 255)
410  contrast = 255;
411
412    drv_L2U_send(LCD_SET_CONTRAST, contrast, 0);
413
414    return contrast;
415}
416
417static int drv_L2U_brightness(int brightness)
418{
419    if (brightness < 0)
420  brightness = 0;
421    if (brightness > 255)
422  brightness = 255;
423
424    drv_L2U_send(LCD_SET_BRIGHTNESS, brightness, 0);
425
426    return brightness;
427}
428
429static void drv_L2U_timer(void __attribute__ ((unused)) * notused)
430{
431    static unsigned long last_but = 0;
432    unsigned long new_but = drv_L2U_get_buttons();
433    int i;
434
435    /* check if button state changed */
436    if (new_but ^ last_but) {
437
438  /* send single keypad events for all changed buttons */
439  for (i = 0; i < L2U_BUTTONS; i++)
440      if ((new_but & (1 << i)) ^ (last_but & (1 << i)))
441    drv_generic_keypad_press(((new_but & (1 << i)) ? 0x80 : 0) | i);
442    }
443
444    last_but = new_but;
445}
446
447static int drv_L2U_keypad(const int num)
448{
449    int val = 0;
450
451    /* check for key press event */
452    if (num & 0x80)
453  val = WIDGET_KEY_PRESSED;
454    else
455  val = WIDGET_KEY_RELEASED;
456
457    if ((num & 0x7f) == 0)
458  val += WIDGET_KEY_UP;
459
460    if ((num & 0x7f) == 1)
461  val += WIDGET_KEY_DOWN;
462
463    return val;
464}
465
466static int drv_L2U_start(const char *section, const int quiet)
467{
468    int contrast, brightness;
469    int rows = -1, cols = -1;
470    char *s;
471
472    s = cfg_get(section, "Size", NULL);
473    if (s == NULL || *s == '\0') {
474  error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
475  return -1;
476    }
477    if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) {
478  error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
479  free(s);
480  return -1;
481    }
482
483    DROWS = rows;
484    DCOLS = cols;
485
486    /* bus id and device id are strings and not just intergers, since */
487    /* the windows port of libusb treats them as strings. And this way */
488    /* we keep windows compatibility ... just in case ... */
489    bus_id = cfg_get(section, "Bus", NULL);
490    device_id = cfg_get(section, "Device", NULL);
491
492    if (drv_L2U_open(bus_id, device_id) < 0) {
493  error("%s: could not find a LCD2USB USB LCD", Name);
494  return -1;
495    }
496
497    /* test interface reliability */
498    drv_L2U_echo();
499
500    /* get some infos from the interface */
501    drv_L2U_get_version();
502    drv_L2U_get_controllers();
503    if (!controllers)
504  return -1;
505
506    /* regularly request key state */
507    /* Fixme: make 100msec configurable */
508    timer_add(drv_L2U_timer, NULL, 100, 0);
509
510    if (cfg_number(section, "Contrast", 0, 0, 255, &contrast) > 0) {
511  drv_L2U_contrast(contrast);
512    }
513
514    if (cfg_number(section, "Brightness", 0, 0, 255, &brightness) > 0) {
515  drv_L2U_brightness(brightness);
516    }
517
518    drv_L2U_clear();    /* clear display */
519
520    if (!quiet) {
521  char buffer[40];
522  qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
523  if (drv_generic_text_greet(buffer, "www.harbaum.org/till")) {
524      sleep(3);
525      drv_L2U_clear();
526  }
527    }
528
529    return 0;
530}
531
532
533/****************************************/
534/***            plugins               ***/
535/****************************************/
536
537static void plugin_contrast(RESULT * result, RESULT * arg1)
538{
539    double contrast;
540
541    contrast = drv_L2U_contrast(R2N(arg1));
542    SetResult(&result, R_NUMBER, &contrast);
543}
544
545
546static void plugin_brightness(RESULT * result, RESULT * arg1)
547{
548    double brightness;
549
550    brightness = drv_L2U_brightness(R2N(arg1));
551    SetResult(&result, R_NUMBER, &brightness);
552}
553
554
555/****************************************/
556/***        widget callbacks          ***/
557/****************************************/
558
559
560/* using drv_generic_text_draw(W) */
561/* using drv_generic_text_icon_draw(W) */
562/* using drv_generic_text_bar_draw(W) */
563
564
565/****************************************/
566/***        exported functions        ***/
567/****************************************/
568
569
570/* list models */
571int drv_L2U_list(void)
572{
573    printf("generic");
574    return 0;
575}
576
577
578/* initialize driver & display */
579int drv_L2U_init(const char *section, const int quiet)
580{
581    WIDGET_CLASS wc;
582    int asc255bug;
583    int ret;
584
585    info("%s: %s", Name, "$Rev$");
586
587    /* display preferences */
588    XRES = 5;     /* pixel width of one char  */
589    YRES = 8;     /* pixel height of one char  */
590    CHARS = 8;      /* number of user-defineable characters */
591    CHAR0 = 0;      /* ASCII of first user-defineable char */
592    GOTO_COST = 2;    /* number of bytes a goto command requires */
593
594    /* real worker functions */
595    drv_generic_text_real_write = drv_L2U_write;
596    drv_generic_text_real_defchar = drv_L2U_defchar;
597    drv_generic_keypad_real_press = drv_L2U_keypad;
598
599    /* start display */
600    if ((ret = drv_L2U_start(section, quiet)) != 0)
601  return ret;
602
603    /* initialize generic text driver */
604    if ((ret = drv_generic_text_init(section, Name)) != 0)
605  return ret;
606
607    /* initialize generic icon driver */
608    if ((ret = drv_generic_text_icon_init()) != 0)
609  return ret;
610
611    /* initialize generic bar driver */
612    if ((ret = drv_generic_text_bar_init(0)) != 0)
613  return ret;
614
615    /* add fixed chars to the bar driver */
616    /* most displays have a full block on ascii 255, but some have kind of  */
617    /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */
618    /* char will not be used, but rendered by the bar driver */
619    cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug);
620    drv_generic_text_bar_add_segment(0, 0, 255, 32);  /* ASCII  32 = blank */
621    if (!asc255bug)
622  drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */
623
624    /* initialize generic key pad driver */
625    if ((ret = drv_generic_keypad_init(section, Name)) != 0)
626  return ret;
627
628    /* register text widget */
629    wc = Widget_Text;
630    wc.draw = drv_generic_text_draw;
631    widget_register(&wc);
632
633    /* register icon widget */
634    wc = Widget_Icon;
635    wc.draw = drv_generic_text_icon_draw;
636    widget_register(&wc);
637
638    /* register bar widget */
639    wc = Widget_Bar;
640    wc.draw = drv_generic_text_bar_draw;
641    widget_register(&wc);
642
643    /* register plugins */
644    AddFunction("LCD::contrast", 1, plugin_contrast);
645    AddFunction("LCD::brightness", 1, plugin_brightness);
646
647    return 0;
648}
649
650
651/* close driver & display */
652int drv_L2U_quit(const int quiet)
653{
654
655    info("%s: shutting down.", Name);
656
657    drv_generic_text_quit();
658    drv_generic_keypad_quit();
659
660    /* clear display */
661    drv_L2U_clear();
662
663    /* say goodbye... */
664    if (!quiet) {
665  drv_generic_text_greet("That's all, folks!", NULL);
666    }
667
668    debug("closing USB connection");
669    drv_L2U_close();
670
671    return (0);
672}
673
674
675DRIVER drv_LCD2USB = {
676    .name = Name,
677    .list = drv_L2U_list,
678    .init = drv_L2U_init,
679    .quit = drv_L2U_quit,
680};
Note: See TracBrowser for help on using the browser.