root/branches/0.10.1/drv_Trefon.c

Revision 773, 9.3 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 TREFON USB LCD displays - http://www.trefon.de
5 *
6 * Copyright (C) 2005 Michael Reinelt <reinelt@eunet.at>
7 * Copyright (C) 2005 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_Trefon
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 0xfff0
62#define LCD_USB_DEVICE 0xfffe
63
64#define PKT_START     0x02
65#define PKT_BACKLIGHT 0x01
66#define PKT_DATA      0x02
67#define PKT_CTRL      0x06
68#define PKT_END       0xff
69
70static char Name[] = "TREFON";
71
72static usb_dev_handle *lcd;
73static int interface;
74
75extern int usb_debug;
76
77
78/****************************************/
79/***  hardware dependant functions    ***/
80/****************************************/
81
82static int drv_TF_open(void)
83{
84    struct usb_bus *busses, *bus;
85    struct usb_device *dev;
86
87    lcd = NULL;
88
89    info("%s: scanning USB for TREFON LCD...", Name);
90
91    usb_debug = 0;
92
93    usb_init();
94    usb_find_busses();
95    usb_find_devices();
96    busses = usb_get_busses();
97
98    for (bus = busses; bus; bus = bus->next) {
99  for (dev = bus->devices; dev; dev = dev->next) {
100      if ((dev->descriptor.idVendor == LCD_USB_VENDOR) && (dev->descriptor.idProduct == LCD_USB_DEVICE)) {
101    info("%s: found TREFON USB LCD on bus %s device %s", Name, bus->dirname, dev->filename);
102    lcd = usb_open(dev);
103    if (usb_set_configuration(lcd, 1) < 0) {
104        error("%s: usb_set_configuration() failed!", Name);
105        return -1;
106    }
107    interface = 0;
108    if (usb_claim_interface(lcd, interface) < 0) {
109        error("%s: usb_claim_interface() failed!", Name);
110        return -1;
111    }
112    return 0;
113      }
114  }
115    }
116    return -1;
117}
118
119
120static int drv_TF_close(void)
121{
122    usb_release_interface(lcd, interface);
123    usb_close(lcd);
124
125    return 0;
126}
127
128
129static void drv_TF_send(unsigned char *data, int size)
130{
131    unsigned char buffer[64];
132
133    /* the controller always wants a 64-byte packet */
134    memset(buffer, 0, 64);
135    memcpy(buffer, data, size);
136
137    /* Endpoint hardcoded to 2 */
138    usb_bulk_write(lcd, 2, (char *) buffer, 64, 2000);
139}
140
141
142static void drv_TF_command(const unsigned char cmd)
143{
144    unsigned char buffer[4] = { PKT_START, PKT_CTRL, 0, PKT_END };
145    buffer[2] = cmd;
146    drv_TF_send(buffer, 4);
147}
148
149
150static void drv_TF_clear(void)
151{
152    drv_TF_command(0x01);
153}
154
155
156static void drv_TF_write(const int row, const int col, const char *data, const int len)
157{
158    unsigned char buffer[64];
159    unsigned char *p;
160    int pos = 0;
161
162    if (DCOLS == 8 && DROWS == 1) { /* 8x1 Characters */
163  pos = row * 0x40 + col;
164    } else if (DCOLS == 16 && DROWS == 2) { /* 16x2 Characters */
165  pos = row * 0x40 + col;
166    } else if (DCOLS == 20 && DROWS == 4) { /* 20x4 Characters */
167  pos = row * 0x20 + col;
168    } else {
169  error("%s: internal error: DCOLS=%d DROWS=%d", Name, DCOLS, DROWS);
170  return;
171    }
172
173    /* combine the GOTO and the data into one packet */
174    p = buffer;
175    *p++ = PKT_START;
176    *p++ = PKT_CTRL;    /* Goto */
177    *p++ = 0x80 | pos;
178    *p++ = PKT_DATA;    /* Data */
179    *p++ = (char) len;
180    for (pos = 0; pos < len; pos++) {
181  *p++ = *data++;
182    }
183    *p++ = PKT_END;
184
185    drv_TF_send(buffer, len + 5);
186}
187
188
189static void drv_TF_defchar(const int ascii, const unsigned char *matrix)
190{
191
192    unsigned char buffer[14];
193    unsigned char *p;
194    int i;
195
196    p = buffer;
197    *p++ = PKT_START;
198    *p++ = PKT_CTRL;
199    *p++ = 0x40 | 8 * ascii;
200    *p++ = PKT_DATA;
201    *p++ = 8;     /* data length */
202    for (i = 0; i < 8; i++) {
203  *p++ = *matrix++ & 0x1f;
204    }
205    *p++ = PKT_END;
206
207    drv_TF_send(buffer, 14);
208}
209
210
211static int drv_TF_backlight(int backlight)
212{
213    unsigned char buffer[4] = { PKT_START, PKT_BACKLIGHT, 0, PKT_END };
214
215    if (backlight < 0)
216  backlight = 0;
217    if (backlight > 1)
218  backlight = 1;
219
220    buffer[2] = backlight;
221    drv_TF_send(buffer, 4);
222
223    return backlight;
224}
225
226
227/* test for existing resolutions from TREFON USB-LCDs (TEXT-Mode only) */
228int drv_TF_valid_resolution(int rows, int cols)
229{
230
231    if (rows == 1 && cols == 8) {
232  return 0;
233    } else if (rows == 2 && cols == 16) {
234  return 0;
235    } else if (rows == 4 && cols == 20) {
236  return 0;
237    }
238    return -1;
239}
240
241
242static int drv_TF_start(const char *section, const int quiet)
243{
244    int backlight;
245    int rows = -1, cols = -1;
246    char *s;
247
248    s = cfg_get(section, "Size", NULL);
249    if (s == NULL || *s == '\0') {
250  error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
251  return -1;
252    }
253    if (sscanf(s, "%dx%d", &cols, &rows) != 2 || drv_TF_valid_resolution(rows, cols) < 0) {
254  error("%s: bad %s.Size '%s' (only 8x1/16x2/20x4) from %s", Name, section, s, cfg_source());
255  free(s);
256  return -1;
257    }
258
259    DROWS = rows;
260    DCOLS = cols;
261
262    if (drv_TF_open() < 0) {
263  error("%s: could not find a TREFON USB LCD", Name);
264  return -1;
265    }
266
267    if (cfg_number(section, "Backlight", 1, 0, 1, &backlight) > 0) {
268  drv_TF_backlight(backlight);
269    }
270
271    drv_TF_clear();   /* clear display */
272
273    if (!quiet) {
274  char buffer[40];
275  qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
276  if (drv_generic_text_greet(buffer, "www.trefon.de")) {
277      sleep(3);
278      drv_TF_clear();
279  }
280    }
281
282    return 0;
283}
284
285
286/****************************************/
287/***            plugins               ***/
288/****************************************/
289
290static void plugin_backlight(RESULT * result, RESULT * arg1)
291{
292    double backlight;
293
294    backlight = drv_TF_backlight(R2N(arg1));
295    SetResult(&result, R_NUMBER, &backlight);
296}
297
298
299/****************************************/
300/***        widget callbacks          ***/
301/****************************************/
302
303
304/* using drv_generic_text_draw(W) */
305/* using drv_generic_text_icon_draw(W) */
306/* using drv_generic_text_bar_draw(W) */
307
308
309/****************************************/
310/***        exported functions        ***/
311/****************************************/
312
313
314/* list models */
315int drv_TF_list(void)
316{
317    printf("generic");
318    return 0;
319}
320
321
322/* initialize driver & display */
323int drv_TF_init(const char *section, const int quiet)
324{
325    WIDGET_CLASS wc;
326    int asc255bug;
327    int ret;
328
329    info("%s: %s", Name, "$Rev$");
330
331    /* display preferences */
332    XRES = 5;     /* pixel width of one char  */
333    YRES = 8;     /* pixel height of one char  */
334    CHARS = 8;      /* number of user-defineable characters */
335    CHAR0 = 1;      /* ASCII of first user-defineable char */
336    GOTO_COST = 64;   /* number of bytes a goto command requires */
337
338    /* real worker functions */
339    drv_generic_text_real_write = drv_TF_write;
340    drv_generic_text_real_defchar = drv_TF_defchar;
341
342
343    /* start display */
344    if ((ret = drv_TF_start(section, quiet)) != 0)
345  return ret;
346
347    /* initialize generic text driver */
348    if ((ret = drv_generic_text_init(section, Name)) != 0)
349  return ret;
350
351    /* initialize generic icon driver */
352    if ((ret = drv_generic_text_icon_init()) != 0)
353  return ret;
354
355    /* initialize generic bar driver */
356    if ((ret = drv_generic_text_bar_init(0)) != 0)
357  return ret;
358
359    /* add fixed chars to the bar driver */
360    /* most displays have a full block on ascii 255, but some have kind of  */
361    /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */
362    /* char will not be used, but rendered by the bar driver */
363    cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug);
364    drv_generic_text_bar_add_segment(0, 0, 255, 32);  /* ASCII  32 = blank */
365    if (!asc255bug)
366  drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */
367
368    /* register text widget */
369    wc = Widget_Text;
370    wc.draw = drv_generic_text_draw;
371    widget_register(&wc);
372
373    /* register icon widget */
374    wc = Widget_Icon;
375    wc.draw = drv_generic_text_icon_draw;
376    widget_register(&wc);
377
378    /* register bar widget */
379    wc = Widget_Bar;
380    wc.draw = drv_generic_text_bar_draw;
381    widget_register(&wc);
382
383    /* register plugins */
384    AddFunction("LCD::backlight", 1, plugin_backlight);
385
386    return 0;
387}
388
389
390/* close driver & display */
391int drv_TF_quit(const int quiet)
392{
393
394    info("%s: shutting down.", Name);
395
396    drv_generic_text_quit();
397
398    /* clear display */
399    drv_TF_clear();
400
401    /* say goodbye... */
402    if (!quiet) {
403  drv_generic_text_greet("goodbye!", NULL);
404    }
405
406    debug("closing USB connection");
407    drv_TF_close();
408
409    return (0);
410}
411
412
413DRIVER drv_Trefon = {
414    .name = Name,
415    .list = drv_TF_list,
416    .init = drv_TF_init,
417    .quit = drv_TF_quit,
418};
Note: See TracBrowser for help on using the browser.