forked from jmccrohan/lcd4linux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
drv_ula200.c
791 lines (661 loc) · 18.3 KB
/
drv_ula200.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/* $Id$
* $URL$
*
* ULA200 driver for lcd4linux
*
* Copyright (C) 2008 Bernhard Walle <[email protected]>
*
* This file is part of LCD4Linux.
*
* LCD4Linux is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* LCD4Linux is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*
* Driver for the ELV ULA200 USB device. The device can control one
* HD44780 display up to 4x20 characters.
*
* Implemented functions:
* - displaying characters :-)
* - controlling the backlight
*
* Todo:
* - input buttons
*
* Configuration:
* - Size (XxY): size of the display (e.g. '20x4')
* - Backlight (0/1): initial state of the backlight
*
* Author:
* Bernhard Walle <[email protected]>
*
* exported fuctions:
* struct DRIVER drv_ula200
*
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <ftdi.h>
#include "debug.h"
#include "cfg.h"
#include "qprintf.h"
#include "udelay.h"
#include "plugin.h"
#include "widget.h"
#include "widget_text.h"
#include "widget_icon.h"
#include "widget_bar.h"
#include "drv.h"
/* text mode display? */
#include "drv_generic_text.h"
/****************************************/
/*** Global variables ***/
/****************************************/
static char Name[] = "ULA200";
static struct ftdi_context *Ftdi = NULL;
/****************************************/
/*** Constants ***/
/****************************************/
/* USB connection */
#define ULA200_VENDOR_ID 0x0403
#define ULA200_PRODUCT_ID 0xf06d
/* connection parameters */
#define ULA200_BAUDRATE 19200
#define ULA200_DATABITS BITS_8
#define ULA200_STOPBITS STOP_BIT_1
#define ULA200_PARITY EVEN
/* character constants used for the communication */
#define ULA200_CH_STX 0x02
#define ULA200_CH_ETX 0x03
#define ULA200_CH_ENQ 0x05
#define ULA200_CH_ACK 0x06
#define ULA200_CH_NAK 0x15
#define ULA200_CH_DC2 0x12
#define ULA200_CH_DC3 0x13
/* commands used for the communication (names are German) */
#define ULA200_CMD_POSITION 'p' /* 'position' */
#define ULA200_CMD_STRING 's' /* 'string' */
#define ULA200_CMD_CLEAR 'l' /* 'loeschen' */
#define ULA200_CMD_BACKLIGHT 'h' /* 'hintergrund' */
#define ULA200_CMD_CHAR 'c' /* 'character' */
/* raw register access */
#define ULA200_RS_DATA 0x00 /* data */
#define ULA200_RS_INSTR 0x01 /* instruction */
#define ULA200_SETCHAR 0x40 /* set user-defined character */
/* character sizes */
#define ULA200_CELLWIDTH 5
#define ULA200_CELLHEIGHT 8
/* internal implementation constants */
#define ULA200_BUFFER_LENGTH 1024
#define ULA200_MAXLEN 512
#define ULA200_MAX_REPEATS 20
/* define TRUE and FALSE for better code readability if not already defined */
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/****************************************/
/*** Macros ***/
/****************************************/
#define ULA200_ERROR(msg, ...) \
error("%s: In %s():%d: " msg, Name, \
__FUNCTION__, __LINE__, ##__VA_ARGS__)
#define ULA200_INFO(msg, ...) \
info("%s: " msg, Name, ##__VA_ARGS__)
#define ULA200_DEBUG(msg, ...) \
debug("%s: In %s():%d: " msg, Name, \
__FUNCTION__, __LINE__, ##__VA_ARGS__)
#define ULA200_TRACE() \
debug("%s: Calling %s()", Name, __FUNCTION__)
/****************************************/
/*** Prototypes ***/
/****************************************/
static int drv_ula200_ftdi_read_response(void);
static int drv_ula200_ftdi_usb_read(void);
static int drv_ula200_ftdi_write_command(const unsigned char *, int);
static int drv_ula200_backlight(int);
static int drv_ula200_close(void);
static void plugin_backlight(RESULT *, RESULT *);
/****************************************/
/*** Internal (helper) funcs ***/
/****************************************/
/**
* Write a command to the display. Adds the STX and ETX header/trailer.
*
* @param[in] data the data bytes
* @param[in] length the number of bytes in data which are valid
* @return 0 on success, negative value on error
*/
static int drv_ula200_ftdi_write_command(const unsigned char *data, int length)
{
int i, err;
int repeat_count = 0;
int pos = 0;
unsigned char buffer[ULA200_BUFFER_LENGTH];
/* check for the maximum length */
if (length > ULA200_MAXLEN) {
return -EINVAL;
}
/* fill the array */
buffer[pos++] = ULA200_CH_STX;
for (i = 0; i < length; i++) {
if (data[i] == ULA200_CH_STX) {
buffer[pos++] = ULA200_CH_ENQ;
buffer[pos++] = ULA200_CH_DC2;
} else if (data[i] == ULA200_CH_ETX) {
buffer[pos++] = ULA200_CH_ENQ;
buffer[pos++] = ULA200_CH_DC3;
} else if (data[i] == ULA200_CH_ENQ) {
buffer[pos++] = ULA200_CH_ENQ;
buffer[pos++] = ULA200_CH_NAK;
} else {
buffer[pos++] = data[i];
}
}
buffer[pos++] = ULA200_CH_ETX;
do {
/* ULA200_DEBUG("ftdi_write_data(%p, %d)", buffer, pos); */
err = ftdi_write_data(Ftdi, buffer, pos);
if (err < 0) {
ULA200_ERROR("ftdi_write_data() failed");
return -1;
}
}
while (!drv_ula200_ftdi_read_response() && (repeat_count++ < ULA200_MAX_REPEATS));
return 0;
}
/**
* Reads a character from USB.
*
* @return a positive value between 0 and 255 indicates the character that
* has been read successfully, -1 indicates an error
*/
static int drv_ula200_ftdi_usb_read(void)
{
unsigned char buffer[1];
int err;
while ((err = ftdi_read_data(Ftdi, buffer, 1)) == 0);
return err >= 0 ? buffer[0] : -1;
}
/**
* Reads the response of the display. Currently, key input is ignored
* and only ACK / NACK is read.
*
* @return TRUE on success (ACK), FALSE on failure (NACK)
*/
static int drv_ula200_ftdi_read_response(void)
{
int result = FALSE;
int answer_read = FALSE;
int ret;
int ch;
while (!answer_read) {
/* wait until STX */
do {
ret = drv_ula200_ftdi_usb_read();
/* ULA200_DEBUG("STX drv_ula200_ftdi_usb_read = %d", ret); */
} while ((ret != ULA200_CH_STX) && (ret > 0));
if (ret < 0) {
return FALSE;
}
/* read next char */
ch = drv_ula200_ftdi_usb_read();
/* ULA200_DEBUG("drv_ula200_ftdi_usb_read = %d", ch); */
switch (ch) {
case 't':
ch = drv_ula200_ftdi_usb_read();
/* ULA200_DEBUG("drv_ula200_ftdi_usb_read = %d", ch); */
/* ignore currently */
break;
case ULA200_CH_ACK:
answer_read = TRUE;
result = TRUE;
break;
case ULA200_CH_NAK:
answer_read = TRUE;
result = FALSE;
break;
default:
answer_read = TRUE;
ULA200_ERROR("Read invalid answer");
}
/* wait until ETX */
do {
ret = drv_ula200_ftdi_usb_read();
/* ULA200_DEBUG("ETX drv_ula200_ftdi_usb_read = %d", ret); */
} while ((ret != ULA200_CH_ETX) && (ret > 0));
if (ret < 0) {
return FALSE;
}
}
return result;
}
static int drv_ula200_ftdi_enable_raw_mode(void)
{
unsigned char command[3];
command[0] = 'R';
command[1] = 'E';
command[2] = '1';
return drv_ula200_ftdi_write_command(command, 3);
}
/**
* Writes raw data (access the HD44780 registers directly.
*
* @param[in] flags ULA200_RS_DATA or ULA200_RS_INSTR
* @param[in] ch the real data
* @return 0 on success, a negative value on error
*/
static int drv_ula200_ftdi_rawdata(unsigned char flags, unsigned char ch)
{
unsigned char command[3];
int err;
command[0] = 'R';
command[1] = flags == ULA200_RS_DATA ? '2' : '0';
command[2] = ch;
err = drv_ula200_ftdi_write_command(command, 3);
if (err < 0) {
ULA200_ERROR("ula200_ftdi_write_command() failed");
return -1;
}
return 0;
}
/**
* Sets the cursor position.
*
* @param[in] x the x coordinate of the position
* @param[in] y the y coordinate of the position
* @return 0 on success, a negative value on error
*/
static int drv_ula200_set_position(int x, int y)
{
unsigned char command[3];
int err;
if (y >= 2) {
y -= 2;
x += DCOLS; /* XXX: multiply by 2? */
}
command[0] = ULA200_CMD_POSITION;
command[1] = x;
command[2] = y;
err = drv_ula200_ftdi_write_command(command, 3);
if (err < 0) {
ULA200_ERROR("ula200_ftdi_write_command() failed");
}
return err;
}
/**
* Sends the text
*
* @param[in] data the data bytes
* @param[in] len the number of valid bytes in @p data
* @return 0 on success, a negative value on error
*/
static int drv_ula200_send_text(const unsigned char *data, int len)
{
unsigned char buffer[ULA200_BUFFER_LENGTH];
int err;
if (len > ULA200_MAXLEN) {
return -EINVAL;
}
buffer[0] = ULA200_CMD_STRING;
buffer[1] = len;
memcpy(buffer + 2, data, len);
buffer[2 + len] = 0; /* only necessary for the debug message */
/* ULA200_DEBUG("Text: =%s= (%d)", buffer+2, len); */
err = drv_ula200_ftdi_write_command(buffer, len + 2);
if (err < 0) {
ULA200_ERROR("ula200_ftdi_write_command() failed");
return -1;
}
return 0;
}
/**
* Sends one character.
*
* @param[in] ch the character to send
* @return 0 on success, a negative value on error
*/
static int drv_ula200_send_char(char ch)
{
unsigned char buffer[2];
int err;
buffer[0] = ULA200_CMD_CHAR;
buffer[1] = ch;
err = drv_ula200_ftdi_write_command(buffer, 2);
if (err < 0) {
ULA200_ERROR("ula200_ftdi_write_command() failed");
return -1;
}
return 0;
}
/**
* Opens the ULA200 display. Uses libftdi to initialise the USB communication to
* the display.
*
@ @return a value less then zero on failure, 0 on success
*/
static int drv_ula200_open(void)
{
int err;
/* check if the device was already open */
if (Ftdi != NULL) {
ULA200_ERROR("open called although device was already open");
drv_ula200_close();
}
/* get memory for the device descriptor */
Ftdi = malloc(sizeof(struct ftdi_context));
if (Ftdi == NULL) {
ULA200_ERROR("Memory allocation failed");
return -1;
}
/* open the ftdi library */
ftdi_init(Ftdi);
Ftdi->usb_write_timeout = 20;
Ftdi->usb_read_timeout = 20;
/* open the device */
err = ftdi_usb_open(Ftdi, ULA200_VENDOR_ID, ULA200_PRODUCT_ID);
if (err < 0) {
ULA200_ERROR("ftdi_usb_open() failed");
free(Ftdi);
Ftdi = NULL;
return -1;
}
/* set the baudrate */
err = ftdi_set_baudrate(Ftdi, ULA200_BAUDRATE);
if (err < 0) {
ULA200_ERROR("ftdi_set_baudrate() failed");
ftdi_usb_close(Ftdi);
free(Ftdi);
Ftdi = NULL;
return -1;
}
/* set communication parameters */
err = ftdi_set_line_property(Ftdi, ULA200_DATABITS, ULA200_STOPBITS, ULA200_PARITY);
if (err < 0) {
ULA200_ERROR("ftdi_set_line_property() failed");
ftdi_usb_close(Ftdi);
free(Ftdi);
Ftdi = NULL;
return -1;
}
return 0;
}
/**
* Closes the display.
*
* @return 0 on success, a negative value on failure
*/
static int drv_ula200_close(void)
{
ULA200_TRACE();
ftdi_usb_purge_buffers(Ftdi);
ftdi_usb_close(Ftdi);
ftdi_deinit(Ftdi);
free(Ftdi);
Ftdi = NULL;
return 0;
}
/**
* Clears the contents of the display.
*
* @return 0 on success, a negative value on error
*/
static void drv_ula200_clear(void)
{
unsigned const char command[] = { ULA200_CMD_CLEAR };
int err;
ULA200_TRACE();
err = drv_ula200_ftdi_write_command(command, 1);
if (err < 0) {
ULA200_ERROR("ula200_ftdi_write_command() failed");
}
}
/**
* Writes data to the display.
*
* @param[in] row the row where the data should be written to
* @param[in] col the column where the data should be written to
* @param[in] data the data that should actually be written
* @param[in] len the number of valid bytes in @p data
*/
static void drv_ula200_write(const int row, const int col, const char *data, int len)
{
int ret;
/* do the cursor positioning here */
ret = drv_ula200_set_position(col, row);
if (ret < 0) {
ULA200_ERROR("drv_ula200_set_position() failed");
return;
}
/* send string to the display */
if (len == 1) {
ret = drv_ula200_send_char(data[0]);
} else {
ret = drv_ula200_send_text((unsigned char *) data, len);
}
if (ret < 0) {
ULA200_ERROR("drv_ula200_send_text() failed");
return;
}
}
/* text mode displays only */
static void drv_ula200_defchar(const int ascii, const unsigned char *matrix)
{
int err, i;
if (ascii >= 8) {
ULA200_ERROR("Invalid value in drv_ula200_defchar");
return;
}
/* Tell the HD44780 we will redefine char number 'ascii' */
err = drv_ula200_ftdi_rawdata(ULA200_RS_INSTR, ULA200_SETCHAR | (ascii * 8));
if (err < 0) {
ULA200_ERROR("drv_ula200_ftdi_rawdata() failed");
return;
}
/* Send the subsequent rows */
for (i = 0; i < YRES; i++) {
err = drv_ula200_ftdi_rawdata(ULA200_RS_DATA, *matrix++ & 0x1f);
if (err < 0) {
ULA200_ERROR("ula200_ftdi_rawdata() failed");
return;
}
}
}
/**
* Controls the backlight of the ULA200 display.
*
* @param[in] backlight a negative value if the backlight should be turned off,
* a positive value if it should be turned on
* @return 0 on success, any other value on failure
*/
static int drv_ula200_backlight(int backlight)
{
unsigned char cmd[2] = { ULA200_CMD_BACKLIGHT };
int ret;
if (backlight <= 0) {
backlight = '0';
} else {
backlight = '1';
}
cmd[1] = backlight;
ret = drv_ula200_ftdi_write_command(cmd, 2);
if (ret < 0) {
ULA200_ERROR("ula200_ftdi_write_command() failed");
}
return backlight == '1';
}
/**
* Starts the display.
*
* @param[in] section the section of the configuration file
* @return 0 on success, a negative value on failure
*/
static int drv_ula200_start(const char *section)
{
int rows = -1, cols = -1;
char *s;
int backlight = 0;
int err;
s = cfg_get(section, "Size", NULL);
if (s == NULL || *s == '\0') {
ULA200_ERROR("No '%s.Size' entry from %s", section, cfg_source());
return -1;
}
if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) {
ULA200_ERROR("Bad %s.Size '%s' from %s", section, s, cfg_source());
free(s);
return -1;
}
DROWS = rows;
DCOLS = cols;
/* open communication with the display */
err = drv_ula200_open();
if (err < 0) {
return -1;
}
cfg_number(section, "Backlight", 0, 0, 1, &backlight);
err = drv_ula200_backlight(backlight);
if (err < 0) {
ULA200_ERROR("drv_ula200_backlight() failed");
return -1;
}
/* clear display */
drv_ula200_clear();
/* enable raw mode for defining own chars */
drv_ula200_ftdi_enable_raw_mode();
return 0;
}
/****************************************/
/*** plugins ***/
/****************************************/
/**
* Backlight plugin
*/
static void plugin_backlight(RESULT * result, RESULT * arg1)
{
double backlight;
backlight = drv_ula200_backlight(R2N(arg1));
SetResult(&result, R_NUMBER, &backlight);
}
/****************************************/
/*** exported functions ***/
/****************************************/
/**
* list models
*
* @return 0 on success, a negative value on failure
*/
int drv_ula200_list(void)
{
printf("ULA200");
return 0;
}
/**
* initialize driver & display
*
* @param[in] section the name of the section in the configuration file
* @param[in] quiet TRUE on quiet mode
* @return 0 on success, any negative error value on failure
*/
/* use this function for a text display */
int drv_ula200_init(const char *section, const int quiet)
{
WIDGET_CLASS wc;
int ret;
ULA200_INFO("%s", "$Rev$");
/* display preferences */
XRES = ULA200_CELLWIDTH; /* pixel width of one char */
YRES = ULA200_CELLHEIGHT; /* pixel height of one char */
CHARS = 7; /* number of user-defineable characters */
CHAR0 = 1; /* ASCII of first user-defineable char */
GOTO_COST = 4; /* number of bytes a goto command requires */
/* real worker functions */
drv_generic_text_real_write = drv_ula200_write;
drv_generic_text_real_defchar = drv_ula200_defchar;
/* start display */
if ((ret = drv_ula200_start(section)) != 0) {
return ret;
}
if (!quiet) {
char buffer[40];
qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
if (drv_generic_text_greet(buffer, "ULA 200")) {
sleep(3);
drv_ula200_clear();
}
}
/* initialize generic text driver */
if ((ret = drv_generic_text_init(section, Name)) != 0)
return ret;
/* initialize generic icon driver */
if ((ret = drv_generic_text_icon_init()) != 0)
return ret;
/* initialize generic bar driver */
if ((ret = drv_generic_text_bar_init(0)) != 0)
return ret;
/* add fixed chars to the bar driver */
drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */
/* register text widget */
wc = Widget_Text;
wc.draw = drv_generic_text_draw;
widget_register(&wc);
/* register icon widget */
wc = Widget_Icon;
wc.draw = drv_generic_text_icon_draw;
widget_register(&wc);
/* register bar widget */
wc = Widget_Bar;
wc.draw = drv_generic_text_bar_draw;
widget_register(&wc);
/* register plugins */
AddFunction("LCD::backlight", -1, plugin_backlight);
return 0;
}
/**
* close driver & display
*
* @param[in] quiet TRUE on quiet mode
* @return 0 on success, any negative error value on failure
*/
/* use this function for a text display */
int drv_ula200_quit(int quiet)
{
ULA200_INFO("shutting down.");
drv_generic_text_quit();
/* turn backlight off */
drv_ula200_backlight(0);
/* clear display */
drv_ula200_clear();
/* say goodbye... */
if (!quiet) {
drv_generic_text_greet("goodbye!", NULL);
}
debug("closing connection");
drv_ula200_close();
return 0;
}
/* use this one for a text display */
DRIVER drv_ula200 = {
.name = Name,
.list = drv_ula200_list,
.init = drv_ula200_init,
.quit = drv_ula200_quit,
};
/* :indentSize=4:tabSize=8:noTabs=false: */