1
/* $Id: drv_ula200.c 1126 2010-07-13 03:25:44Z michael $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/drv_ula200.c $
4
* ULA200 driver for lcd4linux
6
* Copyright (C) 2008 Bernhard Walle <bernhard.walle@gmx.de>
8
* This file is part of LCD4Linux.
10
* LCD4Linux is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2, or (at your option)
15
* LCD4Linux is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27
* Driver for the ELV ULA200 USB device. The device can control one
28
* HD44780 display up to 4x20 characters.
30
* Implemented functions:
31
* - displaying characters :-)
32
* - controlling the backlight
38
* - Size (XxY): size of the display (e.g. '20x4')
39
* - Backlight (0/1): initial state of the backlight
42
* Bernhard Walle <bernhard.walle@gmx.de>
45
* struct DRIVER drv_ula200
65
#include "widget_text.h"
66
#include "widget_icon.h"
67
#include "widget_bar.h"
70
/* text mode display? */
71
#include "drv_generic_text.h"
73
/****************************************/
74
/*** Global variables ***/
75
/****************************************/
77
static char Name[] = "ULA200";
78
static struct ftdi_context *Ftdi = NULL;
81
/****************************************/
83
/****************************************/
86
#define ULA200_VENDOR_ID 0x0403
87
#define ULA200_PRODUCT_ID 0xf06d
89
/* connection parameters */
90
#define ULA200_BAUDRATE 19200
91
#define ULA200_DATABITS BITS_8
92
#define ULA200_STOPBITS STOP_BIT_1
93
#define ULA200_PARITY EVEN
95
/* character constants used for the communication */
96
#define ULA200_CH_STX 0x02
97
#define ULA200_CH_ETX 0x03
98
#define ULA200_CH_ENQ 0x05
99
#define ULA200_CH_ACK 0x06
100
#define ULA200_CH_NAK 0x15
101
#define ULA200_CH_DC2 0x12
102
#define ULA200_CH_DC3 0x13
104
/* commands used for the communication (names are German) */
105
#define ULA200_CMD_POSITION 'p' /* 'position' */
106
#define ULA200_CMD_STRING 's' /* 'string' */
107
#define ULA200_CMD_CLEAR 'l' /* 'loeschen' */
108
#define ULA200_CMD_BACKLIGHT 'h' /* 'hintergrund' */
109
#define ULA200_CMD_CHAR 'c' /* 'character' */
111
/* raw register access */
112
#define ULA200_RS_DATA 0x00 /* data */
113
#define ULA200_RS_INSTR 0x01 /* instruction */
114
#define ULA200_SETCHAR 0x40 /* set user-defined character */
116
/* character sizes */
117
#define ULA200_CELLWIDTH 5
118
#define ULA200_CELLHEIGHT 8
120
/* internal implementation constants */
121
#define ULA200_BUFFER_LENGTH 1024
122
#define ULA200_MAXLEN 512
123
#define ULA200_MAX_REPEATS 20
125
/* define TRUE and FALSE for better code readability if not already defined */
134
/****************************************/
136
/****************************************/
138
#define ULA200_ERROR(msg, ...) \
139
error("%s: In %s():%d: " msg, Name, \
140
__FUNCTION__, __LINE__, ##__VA_ARGS__)
142
#define ULA200_INFO(msg, ...) \
143
info("%s: " msg, Name, ##__VA_ARGS__)
145
#define ULA200_DEBUG(msg, ...) \
146
debug("%s: In %s():%d: " msg, Name, \
147
__FUNCTION__, __LINE__, ##__VA_ARGS__)
149
#define ULA200_TRACE() \
150
debug("%s: Calling %s()", Name, __FUNCTION__)
153
/****************************************/
155
/****************************************/
157
static int drv_ula200_ftdi_read_response(void);
158
static int drv_ula200_ftdi_usb_read(void);
159
static int drv_ula200_ftdi_write_command(const unsigned char *, int);
160
static int drv_ula200_backlight(int);
161
static int drv_ula200_close(void);
163
static void plugin_backlight(RESULT *, RESULT *);
165
/****************************************/
166
/*** Internal (helper) funcs ***/
167
/****************************************/
170
* Write a command to the display. Adds the STX and ETX header/trailer.
172
* @param[in] data the data bytes
173
* @param[in] length the number of bytes in data which are valid
174
* @return 0 on success, negative value on error
176
static int drv_ula200_ftdi_write_command(const unsigned char *data, int length)
179
int repeat_count = 0;
181
unsigned char buffer[ULA200_BUFFER_LENGTH];
183
/* check for the maximum length */
184
if (length > ULA200_MAXLEN) {
189
buffer[pos++] = ULA200_CH_STX;
190
for (i = 0; i < length; i++) {
191
if (data[i] == ULA200_CH_STX) {
192
buffer[pos++] = ULA200_CH_ENQ;
193
buffer[pos++] = ULA200_CH_DC2;
194
} else if (data[i] == ULA200_CH_ETX) {
195
buffer[pos++] = ULA200_CH_ENQ;
196
buffer[pos++] = ULA200_CH_DC3;
197
} else if (data[i] == ULA200_CH_ENQ) {
198
buffer[pos++] = ULA200_CH_ENQ;
199
buffer[pos++] = ULA200_CH_NAK;
201
buffer[pos++] = data[i];
204
buffer[pos++] = ULA200_CH_ETX;
207
/* ULA200_DEBUG("ftdi_write_data(%p, %d)", buffer, pos); */
208
err = ftdi_write_data(Ftdi, buffer, pos);
210
ULA200_ERROR("ftdi_write_data() failed");
214
while (!drv_ula200_ftdi_read_response() && (repeat_count++ < ULA200_MAX_REPEATS));
220
* Reads a character from USB.
222
* @return a positive value between 0 and 255 indicates the character that
223
* has been read successfully, -1 indicates an error
225
static int drv_ula200_ftdi_usb_read(void)
227
unsigned char buffer[1];
230
while ((err = ftdi_read_data(Ftdi, buffer, 1)) == 0);
231
return err >= 0 ? buffer[0] : -1;
236
* Reads the response of the display. Currently, key input is ignored
237
* and only ACK / NACK is read.
239
* @return TRUE on success (ACK), FALSE on failure (NACK)
241
static int drv_ula200_ftdi_read_response(void)
244
int answer_read = FALSE;
248
while (!answer_read) {
251
ret = drv_ula200_ftdi_usb_read();
252
/* ULA200_DEBUG("STX drv_ula200_ftdi_usb_read = %d", ret); */
253
} while ((ret != ULA200_CH_STX) && (ret > 0));
260
ch = drv_ula200_ftdi_usb_read();
261
/* ULA200_DEBUG("drv_ula200_ftdi_usb_read = %d", ch); */
265
ch = drv_ula200_ftdi_usb_read();
266
/* ULA200_DEBUG("drv_ula200_ftdi_usb_read = %d", ch); */
267
/* ignore currently */
282
ULA200_ERROR("Read invalid answer");
287
ret = drv_ula200_ftdi_usb_read();
288
/* ULA200_DEBUG("ETX drv_ula200_ftdi_usb_read = %d", ret); */
289
} while ((ret != ULA200_CH_ETX) && (ret > 0));
299
static int drv_ula200_ftdi_enable_raw_mode(void)
301
unsigned char command[3];
306
return drv_ula200_ftdi_write_command(command, 3);
311
* Writes raw data (access the HD44780 registers directly.
313
* @param[in] flags ULA200_RS_DATA or ULA200_RS_INSTR
314
* @param[in] ch the real data
315
* @return 0 on success, a negative value on error
317
static int drv_ula200_ftdi_rawdata(unsigned char flags, unsigned char ch)
319
unsigned char command[3];
323
command[1] = flags == ULA200_RS_DATA ? '2' : '0';
325
err = drv_ula200_ftdi_write_command(command, 3);
327
ULA200_ERROR("ula200_ftdi_write_command() failed");
335
* Sets the cursor position.
337
* @param[in] x the x coordinate of the position
338
* @param[in] y the y coordinate of the position
339
* @return 0 on success, a negative value on error
341
static int drv_ula200_set_position(int x, int y)
343
unsigned char command[3];
348
x += DCOLS; /* XXX: multiply by 2? */
351
command[0] = ULA200_CMD_POSITION;
354
err = drv_ula200_ftdi_write_command(command, 3);
356
ULA200_ERROR("ula200_ftdi_write_command() failed");
365
* @param[in] data the data bytes
366
* @param[in] len the number of valid bytes in @p data
367
* @return 0 on success, a negative value on error
369
static int drv_ula200_send_text(const unsigned char *data, int len)
371
unsigned char buffer[ULA200_BUFFER_LENGTH];
374
if (len > ULA200_MAXLEN) {
378
buffer[0] = ULA200_CMD_STRING;
380
memcpy(buffer + 2, data, len);
381
buffer[2 + len] = 0; /* only necessary for the debug message */
383
/* ULA200_DEBUG("Text: =%s= (%d)", buffer+2, len); */
385
err = drv_ula200_ftdi_write_command(buffer, len + 2);
387
ULA200_ERROR("ula200_ftdi_write_command() failed");
395
* Sends one character.
397
* @param[in] ch the character to send
398
* @return 0 on success, a negative value on error
400
static int drv_ula200_send_char(char ch)
402
unsigned char buffer[2];
405
buffer[0] = ULA200_CMD_CHAR;
408
err = drv_ula200_ftdi_write_command(buffer, 2);
410
ULA200_ERROR("ula200_ftdi_write_command() failed");
418
* Opens the ULA200 display. Uses libftdi to initialise the USB communication to
421
@ @return a value less then zero on failure, 0 on success
423
static int drv_ula200_open(void)
427
/* check if the device was already open */
429
ULA200_ERROR("open called although device was already open");
433
/* get memory for the device descriptor */
434
Ftdi = malloc(sizeof(struct ftdi_context));
436
ULA200_ERROR("Memory allocation failed");
440
/* open the ftdi library */
442
Ftdi->usb_write_timeout = 20;
443
Ftdi->usb_read_timeout = 20;
445
/* open the device */
446
err = ftdi_usb_open(Ftdi, ULA200_VENDOR_ID, ULA200_PRODUCT_ID);
448
ULA200_ERROR("ftdi_usb_open() failed");
454
/* set the baudrate */
455
err = ftdi_set_baudrate(Ftdi, ULA200_BAUDRATE);
457
ULA200_ERROR("ftdi_set_baudrate() failed");
458
ftdi_usb_close(Ftdi);
463
/* set communication parameters */
464
err = ftdi_set_line_property(Ftdi, ULA200_DATABITS, ULA200_STOPBITS, ULA200_PARITY);
466
ULA200_ERROR("ftdi_set_line_property() failed");
467
ftdi_usb_close(Ftdi);
477
* Closes the display.
479
* @return 0 on success, a negative value on failure
481
static int drv_ula200_close(void)
485
ftdi_usb_purge_buffers(Ftdi);
486
ftdi_usb_close(Ftdi);
496
* Clears the contents of the display.
498
* @return 0 on success, a negative value on error
500
static void drv_ula200_clear(void)
502
unsigned const char command[] = { ULA200_CMD_CLEAR };
507
err = drv_ula200_ftdi_write_command(command, 1);
509
ULA200_ERROR("ula200_ftdi_write_command() failed");
514
* Writes data to the display.
516
* @param[in] row the row where the data should be written to
517
* @param[in] col the column where the data should be written to
518
* @param[in] data the data that should actually be written
519
* @param[in] len the number of valid bytes in @p data
521
static void drv_ula200_write(const int row, const int col, const char *data, int len)
525
/* do the cursor positioning here */
526
ret = drv_ula200_set_position(col, row);
528
ULA200_ERROR("drv_ula200_set_position() failed");
532
/* send string to the display */
534
ret = drv_ula200_send_char(data[0]);
536
ret = drv_ula200_send_text((unsigned char *) data, len);
539
ULA200_ERROR("drv_ula200_send_text() failed");
544
/* text mode displays only */
545
static void drv_ula200_defchar(const int ascii, const unsigned char *matrix)
550
ULA200_ERROR("Invalid value in drv_ula200_defchar");
554
/* Tell the HD44780 we will redefine char number 'ascii' */
555
err = drv_ula200_ftdi_rawdata(ULA200_RS_INSTR, ULA200_SETCHAR | (ascii * 8));
557
ULA200_ERROR("drv_ula200_ftdi_rawdata() failed");
561
/* Send the subsequent rows */
562
for (i = 0; i < YRES; i++) {
563
err = drv_ula200_ftdi_rawdata(ULA200_RS_DATA, *matrix++ & 0x1f);
565
ULA200_ERROR("ula200_ftdi_rawdata() failed");
572
* Controls the backlight of the ULA200 display.
574
* @param[in] backlight a negative value if the backlight should be turned off,
575
* a positive value if it should be turned on
576
* @return 0 on success, any other value on failure
578
static int drv_ula200_backlight(int backlight)
580
unsigned char cmd[2] = { ULA200_CMD_BACKLIGHT };
583
if (backlight <= 0) {
590
ret = drv_ula200_ftdi_write_command(cmd, 2);
592
ULA200_ERROR("ula200_ftdi_write_command() failed");
595
return backlight == '1';
599
* Starts the display.
601
* @param[in] section the section of the configuration file
602
* @return 0 on success, a negative value on failure
604
static int drv_ula200_start(const char *section)
606
int rows = -1, cols = -1;
611
s = cfg_get(section, "Size", NULL);
612
if (s == NULL || *s == '\0') {
613
ULA200_ERROR("No '%s.Size' entry from %s", section, cfg_source());
616
if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) {
617
ULA200_ERROR("Bad %s.Size '%s' from %s", section, s, cfg_source());
625
/* open communication with the display */
626
err = drv_ula200_open();
631
cfg_number(section, "Backlight", 0, 0, 1, &backlight);
632
err = drv_ula200_backlight(backlight);
634
ULA200_ERROR("drv_ula200_backlight() failed");
641
/* enable raw mode for defining own chars */
642
drv_ula200_ftdi_enable_raw_mode();
647
/****************************************/
649
/****************************************/
654
static void plugin_backlight(RESULT * result, RESULT * arg1)
658
backlight = drv_ula200_backlight(R2N(arg1));
659
SetResult(&result, R_NUMBER, &backlight);
662
/****************************************/
663
/*** exported functions ***/
664
/****************************************/
669
* @return 0 on success, a negative value on failure
671
int drv_ula200_list(void)
678
* initialize driver & display
680
* @param[in] section the name of the section in the configuration file
681
* @param[in] quiet TRUE on quiet mode
682
* @return 0 on success, any negative error value on failure
684
/* use this function for a text display */
685
int drv_ula200_init(const char *section, const int quiet)
690
ULA200_INFO("%s", "$Rev: 1126 $");
692
/* display preferences */
693
XRES = ULA200_CELLWIDTH; /* pixel width of one char */
694
YRES = ULA200_CELLHEIGHT; /* pixel height of one char */
695
CHARS = 7; /* number of user-defineable characters */
696
CHAR0 = 1; /* ASCII of first user-defineable char */
697
GOTO_COST = 4; /* number of bytes a goto command requires */
699
/* real worker functions */
700
drv_generic_text_real_write = drv_ula200_write;
701
drv_generic_text_real_defchar = drv_ula200_defchar;
704
if ((ret = drv_ula200_start(section)) != 0) {
710
qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
711
if (drv_generic_text_greet(buffer, "ULA 200")) {
717
/* initialize generic text driver */
718
if ((ret = drv_generic_text_init(section, Name)) != 0)
721
/* initialize generic icon driver */
722
if ((ret = drv_generic_text_icon_init()) != 0)
725
/* initialize generic bar driver */
726
if ((ret = drv_generic_text_bar_init(0)) != 0)
729
/* add fixed chars to the bar driver */
730
drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */
732
/* register text widget */
734
wc.draw = drv_generic_text_draw;
735
widget_register(&wc);
737
/* register icon widget */
739
wc.draw = drv_generic_text_icon_draw;
740
widget_register(&wc);
742
/* register bar widget */
744
wc.draw = drv_generic_text_bar_draw;
745
widget_register(&wc);
747
/* register plugins */
748
AddFunction("LCD::backlight", -1, plugin_backlight);
754
* close driver & display
756
* @param[in] quiet TRUE on quiet mode
757
* @return 0 on success, any negative error value on failure
759
/* use this function for a text display */
760
int drv_ula200_quit(int quiet)
762
ULA200_INFO("shutting down.");
764
drv_generic_text_quit();
766
/* turn backlight off */
767
drv_ula200_backlight(0);
774
drv_generic_text_greet("goodbye!", NULL);
777
debug("closing connection");
783
/* use this one for a text display */
784
DRIVER drv_ula200 = {
786
.list = drv_ula200_list,
787
.init = drv_ula200_init,
788
.quit = drv_ula200_quit,
791
/* :indentSize=4:tabSize=8:noTabs=false: */