| 1 | /* $Id$ |
|---|
| 2 | * $URL$ |
|---|
| 3 | * |
|---|
| 4 | * new style driver for BWCT USB LCD displays |
|---|
| 5 | * |
|---|
| 6 | * Copyright (C) 2003 Michael Reinelt <michael@reinelt.co.at> |
|---|
| 7 | * Copyright (C) 2004 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_BWCT |
|---|
| 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 0x03da |
|---|
| 62 | #define LCD_USB_DEVICE 0x0002 |
|---|
| 63 | |
|---|
| 64 | #define LCD_RESET 1 |
|---|
| 65 | #define LCD_CMD 2 |
|---|
| 66 | #define LCD_DATA 3 |
|---|
| 67 | #define LCD_CONTRAST 4 |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | static char Name[] = "BWCT"; |
|---|
| 71 | |
|---|
| 72 | static usb_dev_handle *lcd; |
|---|
| 73 | static int interface; |
|---|
| 74 | |
|---|
| 75 | extern int got_signal; |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | /****************************************/ |
|---|
| 79 | /*** hardware dependant functions ***/ |
|---|
| 80 | /****************************************/ |
|---|
| 81 | |
|---|
| 82 | static int drv_BW_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 BWCT LCD...", Name); |
|---|
| 90 | |
|---|
| 91 | usb_set_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 | int c; |
|---|
| 101 | if (dev->descriptor.idVendor != LCD_USB_VENDOR) |
|---|
| 102 | continue; |
|---|
| 103 | /* Loop through all of the configurations */ |
|---|
| 104 | for (c = 0; c < dev->descriptor.bNumConfigurations; c++) { |
|---|
| 105 | int i; |
|---|
| 106 | for (i = 0; i < dev->config[c].bNumInterfaces; i++) { |
|---|
| 107 | int a; |
|---|
| 108 | for (a = 0; a < dev->config[c].interface[i].num_altsetting; a++) { |
|---|
| 109 | if ((dev->descriptor.idProduct == LCD_USB_DEVICE) || |
|---|
| 110 | ((dev->config[c].interface[i].altsetting[a].bInterfaceClass == 0xff) && |
|---|
| 111 | (dev->config[c].interface[i].altsetting[a].bInterfaceSubClass == 0x01))) { |
|---|
| 112 | info("%s: found BWCT USB LCD on bus %s device %s", Name, bus->dirname, dev->filename); |
|---|
| 113 | interface = i; |
|---|
| 114 | lcd = usb_open(dev); |
|---|
| 115 | if (usb_claim_interface(lcd, interface) < 0) { |
|---|
| 116 | error("%s: usb_claim_interface() failed!", Name); |
|---|
| 117 | return -1; |
|---|
| 118 | } |
|---|
| 119 | return 0; |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | return -1; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | static int drv_BW_close(void) |
|---|
| 131 | { |
|---|
| 132 | usb_release_interface(lcd, interface); |
|---|
| 133 | usb_close(lcd); |
|---|
| 134 | |
|---|
| 135 | return 0; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | static int drv_BW_send(int request, int value) |
|---|
| 140 | { |
|---|
| 141 | static int errors = 0; |
|---|
| 142 | |
|---|
| 143 | if (errors > 20) |
|---|
| 144 | return -1; |
|---|
| 145 | |
|---|
| 146 | if (usb_control_msg(lcd, USB_TYPE_VENDOR, request, value, interface, NULL, 0, 1000) < 0) { |
|---|
| 147 | error("%s: USB request failed!", Name); |
|---|
| 148 | if (++errors > 20) { |
|---|
| 149 | error("%s: too many USB errors, aborting.", Name); |
|---|
| 150 | got_signal = -1; |
|---|
| 151 | } |
|---|
| 152 | return -1; |
|---|
| 153 | } |
|---|
| 154 | errors = 0; |
|---|
| 155 | return 0; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | static void drv_BW_command(const unsigned char cmd) |
|---|
| 160 | { |
|---|
| 161 | drv_BW_send(LCD_CMD, cmd); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | static void drv_BW_clear(void) |
|---|
| 166 | { |
|---|
| 167 | drv_BW_command(0x01); /* clear display */ |
|---|
| 168 | drv_BW_command(0x03); /* return home */ |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | static void drv_BW_write(const int row, const int col, const char *data, int len) |
|---|
| 173 | { |
|---|
| 174 | int pos; |
|---|
| 175 | |
|---|
| 176 | /* 16x4 Displays use a slightly different layout */ |
|---|
| 177 | if (DCOLS == 16 && DROWS == 4) { |
|---|
| 178 | pos = (row % 2) * 64 + (row / 2) * 16 + col; |
|---|
| 179 | } else { |
|---|
| 180 | pos = (row % 2) * 64 + (row / 2) * 20 + col; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | drv_BW_command(0x80 | pos); |
|---|
| 184 | |
|---|
| 185 | while (len--) { |
|---|
| 186 | drv_BW_send(LCD_DATA, *data++); |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | static void drv_BW_defchar(const int ascii, const unsigned char *matrix) |
|---|
| 191 | { |
|---|
| 192 | int i; |
|---|
| 193 | |
|---|
| 194 | drv_BW_command(0x40 | 8 * ascii); |
|---|
| 195 | |
|---|
| 196 | for (i = 0; i < 8; i++) { |
|---|
| 197 | drv_BW_send(LCD_DATA, *matrix++ & 0x1f); |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | static int drv_BW_contrast(int contrast) |
|---|
| 203 | { |
|---|
| 204 | if (contrast < 0) |
|---|
| 205 | contrast = 0; |
|---|
| 206 | if (contrast > 255) |
|---|
| 207 | contrast = 255; |
|---|
| 208 | |
|---|
| 209 | drv_BW_send(LCD_CONTRAST, contrast); |
|---|
| 210 | |
|---|
| 211 | return contrast; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | static int drv_BW_start(const char *section, const int quiet) |
|---|
| 216 | { |
|---|
| 217 | int contrast; |
|---|
| 218 | int rows = -1, cols = -1; |
|---|
| 219 | char *s; |
|---|
| 220 | |
|---|
| 221 | s = cfg_get(section, "Size", NULL); |
|---|
| 222 | if (s == NULL || *s == '\0') { |
|---|
| 223 | error("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); |
|---|
| 224 | return -1; |
|---|
| 225 | } |
|---|
| 226 | if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) { |
|---|
| 227 | error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); |
|---|
| 228 | free(s); |
|---|
| 229 | return -1; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | DROWS = rows; |
|---|
| 233 | DCOLS = cols; |
|---|
| 234 | |
|---|
| 235 | if (drv_BW_open() < 0) { |
|---|
| 236 | error("%s: could not find a BWCT USB LCD", Name); |
|---|
| 237 | return -1; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | /* reset */ |
|---|
| 241 | drv_BW_send(LCD_RESET, 0); |
|---|
| 242 | |
|---|
| 243 | /* initialize display */ |
|---|
| 244 | drv_BW_command(0x29); /* 8 Bit mode, 1/16 duty cycle, 5x8 font */ |
|---|
| 245 | drv_BW_command(0x08); /* Display off, cursor off, blink off */ |
|---|
| 246 | drv_BW_command(0x0c); /* Display on, cursor off, blink off */ |
|---|
| 247 | drv_BW_command(0x06); /* curser moves to right, no shift */ |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | if (cfg_number(section, "Contrast", 0, 0, 255, &contrast) > 0) { |
|---|
| 251 | drv_BW_contrast(contrast); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | drv_BW_clear(); /* clear display */ |
|---|
| 255 | |
|---|
| 256 | if (!quiet) { |
|---|
| 257 | char buffer[40]; |
|---|
| 258 | qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); |
|---|
| 259 | if (drv_generic_text_greet(buffer, "www.bwct.de")) { |
|---|
| 260 | sleep(3); |
|---|
| 261 | drv_BW_clear(); |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | return 0; |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | /****************************************/ |
|---|
| 270 | /*** plugins ***/ |
|---|
| 271 | /****************************************/ |
|---|
| 272 | |
|---|
| 273 | static void plugin_contrast(RESULT * result, RESULT * arg1) |
|---|
| 274 | { |
|---|
| 275 | double contrast; |
|---|
| 276 | |
|---|
| 277 | contrast = drv_BW_contrast(R2N(arg1)); |
|---|
| 278 | SetResult(&result, R_NUMBER, &contrast); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | /****************************************/ |
|---|
| 283 | /*** widget callbacks ***/ |
|---|
| 284 | /****************************************/ |
|---|
| 285 | |
|---|
| 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 | |
|---|
| 291 | |
|---|
| 292 | /****************************************/ |
|---|
| 293 | /*** exported functions ***/ |
|---|
| 294 | /****************************************/ |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | /* list models */ |
|---|
| 298 | int drv_BW_list(void) |
|---|
| 299 | { |
|---|
| 300 | printf("generic"); |
|---|
| 301 | return 0; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | /* initialize driver & display */ |
|---|
| 306 | int drv_BW_init(const char *section, const int quiet) |
|---|
| 307 | { |
|---|
| 308 | WIDGET_CLASS wc; |
|---|
| 309 | int asc255bug; |
|---|
| 310 | int ret; |
|---|
| 311 | |
|---|
| 312 | info("%s: %s", Name, "$Rev$"); |
|---|
| 313 | |
|---|
| 314 | /* display preferences */ |
|---|
| 315 | XRES = 5; /* pixel width of one char */ |
|---|
| 316 | YRES = 8; /* pixel height of one char */ |
|---|
| 317 | CHARS = 8; /* number of user-defineable characters */ |
|---|
| 318 | CHAR0 = 0; /* ASCII of first user-defineable char */ |
|---|
| 319 | GOTO_COST = 2; /* number of bytes a goto command requires */ |
|---|
| 320 | |
|---|
| 321 | /* real worker functions */ |
|---|
| 322 | drv_generic_text_real_write = drv_BW_write; |
|---|
| 323 | drv_generic_text_real_defchar = drv_BW_defchar; |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | /* start display */ |
|---|
| 327 | if ((ret = drv_BW_start(section, quiet)) != 0) |
|---|
| 328 | return ret; |
|---|
| 329 | |
|---|
| 330 | /* initialize generic text driver */ |
|---|
| 331 | if ((ret = drv_generic_text_init(section, Name)) != 0) |
|---|
| 332 | return ret; |
|---|
| 333 | |
|---|
| 334 | /* initialize generic icon driver */ |
|---|
| 335 | if ((ret = drv_generic_text_icon_init()) != 0) |
|---|
| 336 | return ret; |
|---|
| 337 | |
|---|
| 338 | /* initialize generic bar driver */ |
|---|
| 339 | if ((ret = drv_generic_text_bar_init(0)) != 0) |
|---|
| 340 | return ret; |
|---|
| 341 | |
|---|
| 342 | /* add fixed chars to the bar driver */ |
|---|
| 343 | /* most displays have a full block on ascii 255, but some have kind of */ |
|---|
| 344 | /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */ |
|---|
| 345 | /* char will not be used, but rendered by the bar driver */ |
|---|
| 346 | cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug); |
|---|
| 347 | drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */ |
|---|
| 348 | if (!asc255bug) |
|---|
| 349 | drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */ |
|---|
| 350 | |
|---|
| 351 | /* register text widget */ |
|---|
| 352 | wc = Widget_Text; |
|---|
| 353 | wc.draw = drv_generic_text_draw; |
|---|
| 354 | widget_register(&wc); |
|---|
| 355 | |
|---|
| 356 | /* register icon widget */ |
|---|
| 357 | wc = Widget_Icon; |
|---|
| 358 | wc.draw = drv_generic_text_icon_draw; |
|---|
| 359 | widget_register(&wc); |
|---|
| 360 | |
|---|
| 361 | /* register bar widget */ |
|---|
| 362 | wc = Widget_Bar; |
|---|
| 363 | wc.draw = drv_generic_text_bar_draw; |
|---|
| 364 | widget_register(&wc); |
|---|
| 365 | |
|---|
| 366 | /* register plugins */ |
|---|
| 367 | AddFunction("LCD::contrast", 1, plugin_contrast); |
|---|
| 368 | |
|---|
| 369 | return 0; |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | /* close driver & display */ |
|---|
| 374 | int drv_BW_quit(const int quiet) |
|---|
| 375 | { |
|---|
| 376 | |
|---|
| 377 | info("%s: shutting down.", Name); |
|---|
| 378 | |
|---|
| 379 | drv_generic_text_quit(); |
|---|
| 380 | |
|---|
| 381 | /* clear display */ |
|---|
| 382 | drv_BW_clear(); |
|---|
| 383 | |
|---|
| 384 | /* say goodbye... */ |
|---|
| 385 | if (!quiet) { |
|---|
| 386 | drv_generic_text_greet("goodbye!", NULL); |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | debug("closing USB connection"); |
|---|
| 390 | drv_BW_close(); |
|---|
| 391 | |
|---|
| 392 | return (0); |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | |
|---|
| 396 | DRIVER drv_BWCT = { |
|---|
| 397 | .name = Name, |
|---|
| 398 | .list = drv_BW_list, |
|---|
| 399 | .init = drv_BW_init, |
|---|
| 400 | .quit = drv_BW_quit, |
|---|
| 401 | }; |
|---|