| 1 | /* $Id$ |
|---|
| 2 | * $URL$ |
|---|
| 3 | * |
|---|
| 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 | ************************************************************************** |
|---|
| 12 | * |
|---|
| 13 | * The IR receiving par is compatible with IgrPlug protocol |
|---|
| 14 | * and can be used from LIRC. |
|---|
| 15 | * |
|---|
| 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> |
|---|
| 19 | * |
|---|
| 20 | * This file is part of LCD4Linux. |
|---|
| 21 | * |
|---|
| 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) |
|---|
| 25 | * any later version. |
|---|
| 26 | * |
|---|
| 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. |
|---|
| 31 | * |
|---|
| 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. |
|---|
| 35 | * |
|---|
| 36 | */ |
|---|
| 37 | |
|---|
| 38 | /* |
|---|
| 39 | * |
|---|
| 40 | * exported fuctions: |
|---|
| 41 | * |
|---|
| 42 | * struct DRIVER drv_IRLCD |
|---|
| 43 | * |
|---|
| 44 | */ |
|---|
| 45 | |
|---|
| 46 | #include "config.h" |
|---|
| 47 | |
|---|
| 48 | #include <stdlib.h> |
|---|
| 49 | #include <stdio.h> |
|---|
| 50 | #include <unistd.h> |
|---|
| 51 | #include <string.h> |
|---|
| 52 | #include <errno.h> |
|---|
| 53 | #include <termios.h> |
|---|
| 54 | #include <fcntl.h> |
|---|
| 55 | #include <ctype.h> |
|---|
| 56 | #include <sys/ioctl.h> |
|---|
| 57 | #include <sys/time.h> |
|---|
| 58 | |
|---|
| 59 | #include <usb.h> |
|---|
| 60 | |
|---|
| 61 | #include "debug.h" |
|---|
| 62 | #include "cfg.h" |
|---|
| 63 | #include "qprintf.h" |
|---|
| 64 | #include "udelay.h" |
|---|
| 65 | #include "plugin.h" |
|---|
| 66 | #include "widget.h" |
|---|
| 67 | #include "widget_text.h" |
|---|
| 68 | #include "widget_icon.h" |
|---|
| 69 | #include "widget_bar.h" |
|---|
| 70 | #include "drv.h" |
|---|
| 71 | |
|---|
| 72 | #include "drv_generic_text.h" |
|---|
| 73 | |
|---|
| 74 | /* vid/pid of IRLCD */ |
|---|
| 75 | #define LCD_USB_VENDOR 0x03EB /* for Atmel device */ |
|---|
| 76 | #define LCD_USB_DEVICE 0x0002 |
|---|
| 77 | |
|---|
| 78 | /* Protocole IR/LCD */ |
|---|
| 79 | #define LCD_INSTR 20 |
|---|
| 80 | #define LCD_DATA 21 |
|---|
| 81 | |
|---|
| 82 | static char Name[] = "IRLCD"; |
|---|
| 83 | static char *device_id = NULL, *bus_id = NULL; |
|---|
| 84 | |
|---|
| 85 | static usb_dev_handle *lcd; |
|---|
| 86 | |
|---|
| 87 | extern int got_signal; |
|---|
| 88 | |
|---|
| 89 | /****************************************/ |
|---|
| 90 | /*** hardware dependant functions ***/ |
|---|
| 91 | /****************************************/ |
|---|
| 92 | |
|---|
| 93 | static int drv_IRLCD_open(char *bus_id, char *device_id) |
|---|
| 94 | { |
|---|
| 95 | struct usb_bus *busses, *bus; |
|---|
| 96 | struct usb_device *dev; |
|---|
| 97 | |
|---|
| 98 | lcd = NULL; |
|---|
| 99 | |
|---|
| 100 | info("%s: scanning USB for IRLCD interface ...", Name); |
|---|
| 101 | |
|---|
| 102 | if (bus_id != NULL) |
|---|
| 103 | info("%s: scanning for bus id: %s", Name, bus_id); |
|---|
| 104 | |
|---|
| 105 | if (device_id != NULL) |
|---|
| 106 | info("%s: scanning for device id: %s", Name, device_id); |
|---|
| 107 | |
|---|
| 108 | usb_set_debug(0); |
|---|
| 109 | |
|---|
| 110 | usb_init(); |
|---|
| 111 | usb_find_busses(); |
|---|
| 112 | usb_find_devices(); |
|---|
| 113 | busses = usb_get_busses(); |
|---|
| 114 | |
|---|
| 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))) { |
|---|
| 118 | |
|---|
| 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))) { |
|---|
| 122 | |
|---|
| 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); |
|---|
| 125 | lcd = usb_open(dev); |
|---|
| 126 | if (usb_claim_interface(lcd, 0) < 0) { |
|---|
| 127 | error("%s: usb_claim_interface() failed!", Name); |
|---|
| 128 | return -1; |
|---|
| 129 | } |
|---|
| 130 | return 0; |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | } |
|---|
| 136 | return -1; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | static int drv_IRLCD_close(void) |
|---|
| 141 | { |
|---|
| 142 | usb_release_interface(lcd, 0); |
|---|
| 143 | usb_close(lcd); |
|---|
| 144 | |
|---|
| 145 | return 0; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | /* Send a buffer to lcd via a control message */ |
|---|
| 150 | static int drv_IRLCD_send(int request, unsigned char *buffer, int size) |
|---|
| 151 | { |
|---|
| 152 | if (usb_control_msg(lcd, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, /* bRequestType */ |
|---|
| 153 | request, /* bRequest (LCD_INSTR / LCD_DATA) */ |
|---|
| 154 | 0, /* wValue (0) */ |
|---|
| 155 | 0, /* wIndex (0) */ |
|---|
| 156 | (char *) buffer, /* pointer to destination buffer */ |
|---|
| 157 | size, /* wLength */ |
|---|
| 158 | 1000) < 0) { /* Timeout in millisectonds */ |
|---|
| 159 | error("%s: USB request failed! Trying to reconnect device.", Name); |
|---|
| 160 | |
|---|
| 161 | usb_release_interface(lcd, 0); |
|---|
| 162 | usb_close(lcd); |
|---|
| 163 | |
|---|
| 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); |
|---|
| 167 | got_signal = -1; |
|---|
| 168 | return -1; |
|---|
| 169 | } |
|---|
| 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) */ |
|---|
| 173 | 0, /* wValue (0) */ |
|---|
| 174 | 0, /* wIndex (0) */ |
|---|
| 175 | (char *) buffer, /* pointer to destination buffer */ |
|---|
| 176 | size, /* wLength */ |
|---|
| 177 | 1000) < 0) { /* Timeout in millisectonds */ |
|---|
| 178 | error("%s: retried USB request failed, aborting!", Name); |
|---|
| 179 | got_signal = -1; |
|---|
| 180 | return -1; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | info("%s: Device successfully reconnected.", Name); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | return 0; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | /* text mode displays only */ |
|---|
| 191 | static void drv_IRLCD_clear(void) |
|---|
| 192 | { |
|---|
| 193 | unsigned char cmd[1]; |
|---|
| 194 | |
|---|
| 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); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | /* text mode displays only */ |
|---|
| 203 | static void drv_IRLCD_write(const int row, const int col, const char *data, int len) |
|---|
| 204 | { |
|---|
| 205 | unsigned char cmd[1]; |
|---|
| 206 | static int pos; |
|---|
| 207 | |
|---|
| 208 | /* for 2 lines display */ |
|---|
| 209 | pos = (row % 2) * 64 + (row / 2) * 20 + col; |
|---|
| 210 | |
|---|
| 211 | /* do the cursor positioning here */ |
|---|
| 212 | cmd[0] = 0x80 | pos; |
|---|
| 213 | |
|---|
| 214 | /* do positionning */ |
|---|
| 215 | drv_IRLCD_send(LCD_INSTR, cmd, 1); |
|---|
| 216 | |
|---|
| 217 | /* send string to the display */ |
|---|
| 218 | drv_IRLCD_send(LCD_DATA, (unsigned char *) data, len); |
|---|
| 219 | |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | /* text mode displays only */ |
|---|
| 223 | static void drv_IRLCD_defchar(const int ascii, const unsigned char *matrix) |
|---|
| 224 | { |
|---|
| 225 | unsigned char cmd[10]; |
|---|
| 226 | int i; |
|---|
| 227 | |
|---|
| 228 | /* Write to CGRAM */ |
|---|
| 229 | cmd[0] = 0x40 | 8 * ascii; |
|---|
| 230 | drv_IRLCD_send(LCD_INSTR, cmd, 1); |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | /* send bitmap to the display */ |
|---|
| 234 | for (i = 0; i < 8; i++) { |
|---|
| 235 | cmd[i] = matrix[i] & 0x1f; |
|---|
| 236 | } |
|---|
| 237 | drv_IRLCD_send(LCD_DATA, cmd, 8); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | /* start text mode display */ |
|---|
| 242 | static int drv_IRLCD_start(const char *section) |
|---|
| 243 | { |
|---|
| 244 | int rows = -1, cols = -1; |
|---|
| 245 | char *s; |
|---|
| 246 | |
|---|
| 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()); |
|---|
| 250 | return -1; |
|---|
| 251 | } |
|---|
| 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()); |
|---|
| 254 | free(s); |
|---|
| 255 | return -1; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | DROWS = rows; |
|---|
| 259 | DCOLS = cols; |
|---|
| 260 | |
|---|
| 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); |
|---|
| 266 | |
|---|
| 267 | if (drv_IRLCD_open(bus_id, device_id) < 0) { |
|---|
| 268 | error("%s: could not find a IRLC USB LCD", Name); |
|---|
| 269 | return -1; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | /* reset & initialize display */ |
|---|
| 273 | drv_IRLCD_clear(); /* clear display */ |
|---|
| 274 | |
|---|
| 275 | return 0; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | /****************************************/ |
|---|
| 280 | /*** plugins ***/ |
|---|
| 281 | /****************************************/ |
|---|
| 282 | /* no plugins capabilities */ |
|---|
| 283 | |
|---|
| 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) */ |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | /****************************************/ |
|---|
| 294 | /*** exported functions ***/ |
|---|
| 295 | /****************************************/ |
|---|
| 296 | |
|---|
| 297 | /* list models */ |
|---|
| 298 | int drv_IRLCD_list(void) |
|---|
| 299 | { |
|---|
| 300 | printf("generic"); |
|---|
| 301 | return 0; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | /* initialize driver & display */ |
|---|
| 305 | int drv_IRLCD_init(const char *section, const int quiet) |
|---|
| 306 | { |
|---|
| 307 | WIDGET_CLASS wc; |
|---|
| 308 | int asc255bug; |
|---|
| 309 | int ret; |
|---|
| 310 | |
|---|
| 311 | info("%s: %s", Name, "$Rev$"); |
|---|
| 312 | |
|---|
| 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 */ |
|---|
| 319 | |
|---|
| 320 | /* real worker functions */ |
|---|
| 321 | drv_generic_text_real_write = drv_IRLCD_write; |
|---|
| 322 | drv_generic_text_real_defchar = drv_IRLCD_defchar; |
|---|
| 323 | |
|---|
| 324 | /* start display */ |
|---|
| 325 | if ((ret = drv_IRLCD_start(section)) != 0) |
|---|
| 326 | return ret; |
|---|
| 327 | |
|---|
| 328 | if (!quiet) { |
|---|
| 329 | char buffer[40]; |
|---|
| 330 | qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); |
|---|
| 331 | if (drv_generic_text_greet(buffer, " www.civade.com")) { |
|---|
| 332 | sleep(3); |
|---|
| 333 | drv_IRLCD_clear(); |
|---|
| 334 | } |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | /* initialize generic text driver */ |
|---|
| 338 | if ((ret = drv_generic_text_init(section, Name)) != 0) |
|---|
| 339 | return ret; |
|---|
| 340 | |
|---|
| 341 | /* initialize generic icon driver */ |
|---|
| 342 | if ((ret = drv_generic_text_icon_init()) != 0) |
|---|
| 343 | return ret; |
|---|
| 344 | |
|---|
| 345 | /* initialize generic bar driver */ |
|---|
| 346 | if ((ret = drv_generic_text_bar_init(0)) != 0) |
|---|
| 347 | return ret; |
|---|
| 348 | |
|---|
| 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 */ |
|---|
| 355 | if (!asc255bug) |
|---|
| 356 | drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */ |
|---|
| 357 | |
|---|
| 358 | /* register text widget */ |
|---|
| 359 | wc = Widget_Text; |
|---|
| 360 | wc.draw = drv_generic_text_draw; |
|---|
| 361 | widget_register(&wc); |
|---|
| 362 | |
|---|
| 363 | /* register icon widget */ |
|---|
| 364 | wc = Widget_Icon; |
|---|
| 365 | wc.draw = drv_generic_text_icon_draw; |
|---|
| 366 | widget_register(&wc); |
|---|
| 367 | |
|---|
| 368 | /* register bar widget */ |
|---|
| 369 | wc = Widget_Bar; |
|---|
| 370 | wc.draw = drv_generic_text_bar_draw; |
|---|
| 371 | widget_register(&wc); |
|---|
| 372 | |
|---|
| 373 | return 0; |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | /* close driver & display */ |
|---|
| 378 | /* use this function for a text display */ |
|---|
| 379 | int drv_IRLCD_quit(const int quiet) |
|---|
| 380 | { |
|---|
| 381 | |
|---|
| 382 | info("%s: shutting down.", Name); |
|---|
| 383 | |
|---|
| 384 | drv_generic_text_quit(); |
|---|
| 385 | |
|---|
| 386 | /* clear display */ |
|---|
| 387 | drv_IRLCD_clear(); |
|---|
| 388 | |
|---|
| 389 | /* say goodbye... */ |
|---|
| 390 | if (!quiet) { |
|---|
| 391 | drv_generic_text_greet("goodbye!", NULL); |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | debug("closing usb connection"); |
|---|
| 395 | drv_IRLCD_close(); |
|---|
| 396 | |
|---|
| 397 | return (0); |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | /* use this one for a text display */ |
|---|
| 401 | DRIVER drv_IRLCD = { |
|---|
| 402 | .name = Name, |
|---|
| 403 | .list = drv_IRLCD_list, |
|---|
| 404 | .init = drv_IRLCD_init, |
|---|
| 405 | .quit = drv_IRLCD_quit, |
|---|
| 406 | }; |
|---|