1
/* $Id: drv_IRLCD.c 984 2009-02-28 06:07:10Z michael $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/drv_IRLCD.c $
4
**************************************************************************
5
* Driver for IRLCD : simple USB1.1 LCD + IR Receiver based on ATtiny2313 *
6
**************************************************************************
7
* Hardware from http://www.xs4all.nl/~dicks/avr/usbtiny/index.html : *
8
* [USBtiny LIRC compatible IR receiver and LCD controller] *
9
* Ths driver is based on LCD2USB software by Till Harbaum, adapted to *
10
* USBTiny protocol by Jean-Philippe Civade *
11
**************************************************************************
13
* The IR receiving par is compatible with IgrPlug protocol
14
* and can be used from LIRC.
16
* Copyright (C) 2008 Jean-Philippe Civade <jp@civade.com> (for porting to IRLCD)
17
* Copyright (C) 2005 Till Harbaum <till@harbaum.org> (for LCD2USB friver)
18
* Copyright (C) 2005, 2006, 2007, 2008 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
20
* This file is part of LCD4Linux.
22
* LCD4Linux is free software; you can redistribute it and/or modify
23
* it under the terms of the GNU General Public License as published by
24
* the Free Software Foundation; either version 2, or (at your option)
27
* LCD4Linux is distributed in the hope that it will be useful,
28
* but WITHOUT ANY WARRANTY; without even the implied warranty of
29
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30
* GNU General Public License for more details.
32
* You should have received a copy of the GNU General Public License
33
* along with this program; if not, write to the Free Software
34
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42
* struct DRIVER drv_IRLCD
56
#include <sys/ioctl.h>
67
#include "widget_text.h"
68
#include "widget_icon.h"
69
#include "widget_bar.h"
72
#include "drv_generic_text.h"
74
/* vid/pid of IRLCD */
75
#define LCD_USB_VENDOR 0x03EB /* for Atmel device */
76
#define LCD_USB_DEVICE 0x0002
78
/* Protocole IR/LCD */
82
static char Name[] = "IRLCD";
83
static char *device_id = NULL, *bus_id = NULL;
85
static usb_dev_handle *lcd;
87
extern int got_signal;
89
/****************************************/
90
/*** hardware dependant functions ***/
91
/****************************************/
93
static int drv_IRLCD_open(char *bus_id, char *device_id)
95
struct usb_bus *busses, *bus;
96
struct usb_device *dev;
100
info("%s: scanning USB for IRLCD interface ...", Name);
103
info("%s: scanning for bus id: %s", Name, bus_id);
105
if (device_id != NULL)
106
info("%s: scanning for device id: %s", Name, device_id);
113
busses = usb_get_busses();
115
for (bus = busses; bus; bus = bus->next) {
116
/* search this bus if no bus id was given or if this is the given bus id */
117
if (!bus_id || (bus_id && !strcasecmp(bus->dirname, bus_id))) {
119
for (dev = bus->devices; dev; dev = dev->next) {
120
/* search this device if no device id was given or if this is the given device id */
121
if (!device_id || (device_id && !strcasecmp(dev->filename, device_id))) {
123
if ((dev->descriptor.idVendor == LCD_USB_VENDOR) && (dev->descriptor.idProduct == LCD_USB_DEVICE)) {
124
info("%s: found IRLCD interface on bus %s device %s", Name, bus->dirname, dev->filename);
126
if (usb_claim_interface(lcd, 0) < 0) {
127
info("%s: WRNING! usb_claim_interface() failed!", Name);
128
/* try to proceed anyway... */
140
static int drv_IRLCD_close(void)
142
usb_release_interface(lcd, 0);
149
/* Send a buffer to lcd via a control message */
150
static int drv_IRLCD_send(int request, unsigned char *buffer, int size)
152
if (usb_control_msg(lcd, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, /* bRequestType */
153
request, /* bRequest (LCD_INSTR / LCD_DATA) */
156
(char *) buffer, /* pointer to destination buffer */
158
1000) < 0) { /* Timeout in millisectonds */
159
error("%s: USB request failed! Trying to reconnect device.", Name);
161
usb_release_interface(lcd, 0);
164
/* try to close and reopen connection */
165
if (drv_IRLCD_open(bus_id, device_id) < 0) {
166
error("%s: could not re-detect IRLCD USB LCD", Name);
170
/* and try to re-send command */
171
if (usb_control_msg(lcd, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, /* bRequestType */
172
request, /* bRequest (LCD_INSTR / LCD_DATA) */
175
(char *) buffer, /* pointer to destination buffer */
177
1000) < 0) { /* Timeout in millisectonds */
178
error("%s: retried USB request failed, aborting!", Name);
183
info("%s: Device successfully reconnected.", Name);
190
/* text mode displays only */
191
static void drv_IRLCD_clear(void)
193
unsigned char cmd[1];
195
cmd[0] = 0x01; /* clear */
196
drv_IRLCD_send(LCD_INSTR, cmd, 1);
197
cmd[0] = 0x03; /* home */
198
drv_IRLCD_send(LCD_INSTR, cmd, 1);
202
/* text mode displays only */
203
static void drv_IRLCD_write(const int row, const int col, const char *data, int len)
205
unsigned char cmd[1];
208
/* for 2 lines display */
209
pos = (row % 2) * 64 + (row / 2) * 20 + col;
211
/* do the cursor positioning here */
214
/* do positionning */
215
drv_IRLCD_send(LCD_INSTR, cmd, 1);
217
/* send string to the display */
218
drv_IRLCD_send(LCD_DATA, (unsigned char *) data, len);
222
/* text mode displays only */
223
static void drv_IRLCD_defchar(const int ascii, const unsigned char *matrix)
225
unsigned char cmd[10];
229
cmd[0] = 0x40 | 8 * ascii;
230
drv_IRLCD_send(LCD_INSTR, cmd, 1);
233
/* send bitmap to the display */
234
for (i = 0; i < 8; i++) {
235
cmd[i] = matrix[i] & 0x1f;
237
drv_IRLCD_send(LCD_DATA, cmd, 8);
241
/* start text mode display */
242
static int drv_IRLCD_start(const char *section)
244
int rows = -1, cols = -1;
247
s = cfg_get(section, "Size", NULL);
248
if (s == NULL || *s == '\0') {
249
error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
252
if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) {
253
error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
261
/* bus id and device id are strings and not just intergers, since */
262
/* the windows port of libusb treats them as strings. And this way */
263
/* we keep windows compatibility ... just in case ... */
264
bus_id = cfg_get(section, "Bus", NULL);
265
device_id = cfg_get(section, "Device", NULL);
267
if (drv_IRLCD_open(bus_id, device_id) < 0) {
268
error("%s: could not find a IRLC USB LCD", Name);
272
/* reset & initialize display */
273
drv_IRLCD_clear(); /* clear display */
279
/****************************************/
281
/****************************************/
282
/* no plugins capabilities */
284
/****************************************/
285
/*** widget callbacks ***/
286
/****************************************/
287
/* using drv_generic_text_draw(W) */
288
/* using drv_generic_text_icon_draw(W) */
289
/* using drv_generic_text_bar_draw(W) */
290
/* using drv_generic_gpio_draw(W) */
293
/****************************************/
294
/*** exported functions ***/
295
/****************************************/
298
int drv_IRLCD_list(void)
300
printf("USBtiny LCD controller");
304
/* initialize driver & display */
305
int drv_IRLCD_init(const char *section, const int quiet)
311
info("%s: %s", Name, "$Rev: 984 $");
313
/* display preferences */
314
XRES = 5; /* pixel width of one char */
315
YRES = 8; /* pixel height of one char */
316
CHARS = 8; /* number of user-defineable characters */
317
CHAR0 = 0; /* ASCII of first user-defineable char */
318
GOTO_COST = 2; /* number of bytes a goto command requires */
320
/* real worker functions */
321
drv_generic_text_real_write = drv_IRLCD_write;
322
drv_generic_text_real_defchar = drv_IRLCD_defchar;
325
if ((ret = drv_IRLCD_start(section)) != 0)
330
qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
331
if (drv_generic_text_greet(buffer, " www.civade.com")) {
337
/* initialize generic text driver */
338
if ((ret = drv_generic_text_init(section, Name)) != 0)
341
/* initialize generic icon driver */
342
if ((ret = drv_generic_text_icon_init()) != 0)
345
/* initialize generic bar driver */
346
if ((ret = drv_generic_text_bar_init(0)) != 0)
349
/* add fixed chars to the bar driver */
350
/* most displays have a full block on ascii 255, but some have kind of */
351
/* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */
352
/* char will not be used, but rendered by the bar driver */
353
cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug);
354
drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */
356
drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */
358
/* register text widget */
360
wc.draw = drv_generic_text_draw;
361
widget_register(&wc);
363
/* register icon widget */
365
wc.draw = drv_generic_text_icon_draw;
366
widget_register(&wc);
368
/* register bar widget */
370
wc.draw = drv_generic_text_bar_draw;
371
widget_register(&wc);
377
/* close driver & display */
378
/* use this function for a text display */
379
int drv_IRLCD_quit(const int quiet)
382
info("%s: shutting down.", Name);
384
drv_generic_text_quit();
391
drv_generic_text_greet("goodbye!", NULL);
394
debug("closing usb connection");
400
/* use this one for a text display */
403
.list = drv_IRLCD_list,
404
.init = drv_IRLCD_init,
405
.quit = drv_IRLCD_quit,