1
/* $Id: drv_picoLCD.c 1143 2011-02-12 22:46:19Z mzuther $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/drv_picoLCD.c $
4
* driver for picoLCD displays from mini-box.com
6
* Copyright (C) 2005 Michael Reinelt <michael@reinelt.co.at>
7
* Copyright (C) 2005, 2006, 2007 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
9
* Copyright (C) 2007 Nicu Pavel, Mini-Box.com <npavel@mini-box.com>
11
* This file is part of LCD4Linux.
13
* LCD4Linux is free software; you can redistribute it and/or modify
14
* it under the terms of the GNU General Public License as published by
15
* the Free Software Foundation; either version 2, or (at your option)
18
* LCD4Linux is distributed in the hope that it will be useful,
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
* GNU General Public License for more details.
23
* You should have received a copy of the GNU General Public License
24
* along with this program; if not, write to the Free Software
25
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33
* struct DRIVER drv_picoLCD
46
#include <sys/ioctl.h>
57
#include "widget_text.h"
58
#include "widget_icon.h"
59
#include "widget_bar.h"
61
#include "drv_generic_text.h"
62
#include "drv_generic_gpio.h"
63
#include "drv_generic_keypad.h"
67
#define picoLCD_VENDOR 0x04d8
68
#define picoLCD_DEVICE 0x0002
70
static char Name[] = "picoLCD";
72
static unsigned int gpo = 0;
77
static usb_dev_handle *lcd;
81
/****************************************/
82
/*** hardware dependant functions ***/
83
/****************************************/
85
static int drv_pL_open(void)
87
struct usb_bus *busses, *bus;
88
struct usb_device *dev;
91
char manufacturer[1024];
92
char serialnumber[1024];
97
info("%s: scanning for picoLCD...", Name);
104
busses = usb_get_busses();
106
for (bus = busses; bus; bus = bus->next) {
107
for (dev = bus->devices; dev; dev = dev->next) {
108
if ((dev->descriptor.idVendor == picoLCD_VENDOR) && (dev->descriptor.idProduct == picoLCD_DEVICE)) {
110
info("%s: found picoLCD on bus %s device %s", Name, bus->dirname, dev->filename);
114
ret = usb_get_driver_np(lcd, 0, driver, sizeof(driver));
117
info("%s: interface 0 already claimed by '%s'", Name, driver);
118
info("%s: attempting to detach driver...", Name);
119
if (usb_detach_kernel_driver_np(lcd, 0) < 0) {
120
error("%s: usb_detach_kernel_driver_np() failed!", Name);
125
usb_set_configuration(lcd, 1);
128
if (usb_claim_interface(lcd, 0) < 0) {
129
error("%s: usb_claim_interface() failed!", Name);
133
usb_set_altinterface(lcd, 0);
135
usb_get_string_simple(lcd, dev->descriptor.iProduct, product, sizeof(product));
136
usb_get_string_simple(lcd, dev->descriptor.iManufacturer, manufacturer, sizeof(manufacturer));
137
usb_get_string_simple(lcd, dev->descriptor.iSerialNumber, serialnumber, sizeof(serialnumber));
139
info("%s: Manufacturer='%s' Product='%s' SerialNumber='%s'", Name, manufacturer, product, serialnumber);
145
error("%s: could not find a picoLCD", Name);
150
static int drv_pL_close(void)
152
usb_release_interface(lcd, 0);
159
static void drv_pL_send(unsigned char *data, int size)
161
usb_interrupt_write(lcd, USB_ENDPOINT_OUT + 1, (char *) data, size, 1000);
164
static int drv_pL_read(unsigned char *data, int size)
166
return usb_interrupt_read(lcd, USB_ENDPOINT_OUT + 1, (char *) data, size, 1000);
171
static void drv_pL_clear(void)
173
unsigned char cmd[1] = { 0x94 }; /* clear display */
177
static int drv_pL_contrast(int contrast)
179
unsigned char cmd[2] = { 0x92 }; /* set contrast */
193
static int drv_pL_backlight(int backlight)
195
unsigned char cmd[2] = { 0x91 }; /* set backlight */
208
#define _USBLCD_MAX_DATA_LEN 24
209
#define IN_REPORT_KEY_STATE 0x11
210
static int drv_pL_gpi( __attribute__ ((unused))
214
unsigned char read_packet[_USBLCD_MAX_DATA_LEN];
215
ret = drv_pL_read(read_packet, _USBLCD_MAX_DATA_LEN);
216
if ((ret > 0) && (read_packet[0] == IN_REPORT_KEY_STATE)) {
217
debug("picoLCD: pressed key= 0x%02x\n", read_packet[1]);
218
return read_packet[1];
223
static int drv_pL_gpo(int num, int val)
225
unsigned char cmd[2] = { 0x81 }; /* set GPO */
237
/* set led bit to 1 or 0 */
250
static void drv_pL_write(const int row, const int col, const char *data, int len)
252
unsigned char cmd[64];
255
cmd[0] = 0x98; /* goto/write */
268
static void drv_pL_defchar(const int ascii, const unsigned char *matrix)
270
unsigned char cmd[10] = { 0x9c }; /* define character */
274
for (i = 0; i < 8; i++) {
275
cmd[i + 2] = *matrix++ & 0x1f;
278
drv_pL_send(cmd, 10);
282
static int drv_pL_start(const char *section, const int quiet)
284
int rows = -1, cols = -1;
288
s = cfg_get(section, "Size", NULL);
289
if (s == NULL || *s == '\0') {
290
error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
293
if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) {
294
error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
302
if (drv_pL_open() < 0) {
306
/* Init the command buffer */
307
Buffer = (char *) malloc(1024);
308
if (Buffer == NULL) {
309
error("%s: command buffer could not be allocated: malloc() failed", Name);
314
if (cfg_number(section, "Contrast", 0, 0, 255, &value) > 0) {
315
info("Setting contrast to %d", value);
316
drv_pL_contrast(value);
319
if (cfg_number(section, "Backlight", 0, 0, 1, &value) > 0) {
320
info("Setting backlight to %d", value);
321
drv_pL_backlight(value);
324
drv_pL_clear(); /* clear display */
328
qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
329
if (drv_generic_text_greet(buffer, "http://www.picolcd.com")) {
339
/****************************************/
341
/****************************************/
343
static void plugin_contrast(RESULT * result, RESULT * arg1)
347
contrast = drv_pL_contrast(R2N(arg1));
348
SetResult(&result, R_NUMBER, &contrast);
351
static void plugin_backlight(RESULT * result, RESULT * arg1)
355
backlight = drv_pL_backlight(R2N(arg1));
356
SetResult(&result, R_NUMBER, &backlight);
359
static void plugin_gpo(RESULT * result, RESULT * argv[])
362
gpo = drv_pL_gpo(R2N(argv[0]), R2N(argv[1]));
363
SetResult(&result, R_NUMBER, &gpo);
366
/****************************************/
367
/*** widget callbacks ***/
368
/****************************************/
371
/* using drv_generic_text_draw(W) */
372
/* using drv_generic_text_icon_draw(W) */
373
/* using drv_generic_text_bar_draw(W) */
376
/****************************************/
377
/*** exported functions ***/
378
/****************************************/
382
int drv_pL_list(void)
384
printf("picoLCD 20x2 Text LCD");
389
/* initialize driver & display */
390
int drv_pL_init(const char *section, const int quiet)
395
info("%s: %s", Name, "$Rev: 1143 $");
397
/* display preferences */
398
XRES = 5; /* pixel width of one char */
399
YRES = 8; /* pixel height of one char */
400
CHARS = 8; /* number of user-defineable characters */
401
CHAR0 = 0; /* ASCII of first user-defineable char */
405
GOTO_COST = 2; /* number of bytes a goto command requires */
407
/* real worker functions */
408
drv_generic_text_real_write = drv_pL_write;
409
drv_generic_text_real_defchar = drv_pL_defchar;
410
drv_generic_gpio_real_set = drv_pL_gpo;
411
drv_generic_gpio_real_get = drv_pL_gpi;
414
if ((ret = drv_pL_start(section, quiet)) != 0)
417
/* initialize generic text driver */
418
if ((ret = drv_generic_text_init(section, Name)) != 0)
421
/* initialize generic icon driver */
422
if ((ret = drv_generic_text_icon_init()) != 0)
425
/* initialize generic bar driver */
426
if ((ret = drv_generic_text_bar_init(0)) != 0)
429
drv_generic_text_bar_add_segment(0, 0, 255, 32);
432
if ((ret = drv_generic_gpio_init(section, Name)) != 0)
435
/* register text widget */
437
wc.draw = drv_generic_text_draw;
438
widget_register(&wc);
440
/* register icon widget */
442
wc.draw = drv_generic_text_icon_draw;
443
widget_register(&wc);
445
/* register bar widget */
447
wc.draw = drv_generic_text_bar_draw;
448
widget_register(&wc);
450
/* register plugins */
451
AddFunction("LCD::contrast", -1, plugin_contrast);
452
AddFunction("LCD::backlight", -1, plugin_backlight);
453
AddFunction("LCD::gpo", -1, plugin_gpo);
459
/* close driver & display */
460
int drv_pL_quit(const int quiet)
463
info("%s: shutting down.", Name);
465
drv_generic_text_quit();
472
drv_generic_text_greet("goodbye!", NULL);
487
DRIVER drv_picoLCD = {