File: sed1520.c

package info (click to toggle)
lcdproc 0.5.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,064 kB
  • sloc: ansic: 59,645; sh: 1,740; perl: 681; makefile: 417
file content (576 lines) | stat: -rw-r--r-- 15,859 bytes parent folder | download
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
/** \file server/drivers/sed1520.c
 * LCDd \c sed1520 driver for graphic displays based on the SED1520.
 *
 * This is a driver for 122x32 pixel graphic displays based on the SED1520
 * controller connected to the parallel port. This Controller has no
 * built in character generator. Therefore all fonts and pixels are generated
 * by this driver.
 */

/*-
 * Copyright (C) 2001 Robin Adams <robin@adams-online.de>
 *		 2011 Markus Dolze <bsdfan@nurfuerspam.de>
 *
 * This file is released under the GNU General Public License. Refer to the
 * COPYING file distributed with this package.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "lcd.h"
#include "sed1520.h"
#include "glcd_font5x8.h"
#include "sed1520fm.h"
#include "shared/report.h"
#include "port.h"
#include "timing.h"
#include "lpt-port.h"

#define uPause timing_uPause

#ifndef DEFAULT_PORT
# define DEFAULT_PORT	0x378
#endif

#define CELLWIDTH	6
#define CELLHEIGHT	8

#define PIXELWIDTH	122
#define PIXELHEIGHT	32

#define WIDTH		((int) (PIXELWIDTH / CELLWIDTH))	/* 20 */
#define HEIGHT		((int) (PIXELHEIGHT / CELLHEIGHT))	/*  4 */

#define A0	nSEL		/* pin 17 */
#define CS1	nLF		/* pin 14, EN1 for 68-style connection */
#define CS2	INIT		/* pin 16, EN2 for 68-style connection */
#define WR	nSTRB		/* pin 1 */

/** private data for the \c sed1520 driver */
typedef struct sed1520_private_data {
    unsigned short port;
    int interface;
    int delayMult;
    int haveInverter;
    unsigned char colStartAdd;

    unsigned char *framebuf;
} PrivateData;


/* Vars for the server core */
MODULE_EXPORT char *api_version = API_VERSION;
MODULE_EXPORT int stay_in_foreground = 0;
MODULE_EXPORT int supports_multiple = 0;
MODULE_EXPORT char *symbol_prefix = "sed1520_";

/**
 * Writes command value to one or both SED1520 selected by chip.
 * \param p      Pointer to private data structure
 * \param value  Command byte to write
 * \param chip   Bitmap of controllers to send value to
 *
 * \note It is a little code duplication having writecommand and writedata
 *       both, but I leave it that way.
 */
static void
writecommand(PrivateData *p, int value, int chip)
{
    if (p->interface == 68) {
	port_out(p->port + 2, 0 ^ OUTMASK);
	port_out(p->port, value);
	/* cycle E */
	port_out(p->port + 2, ((chip & CS1) + (chip & CS2)) ^ OUTMASK);
	if (p->delayMult)
	    uPause(p->delayMult);
	port_out(p->port + 2, 0 ^ OUTMASK);
    }
    else {
	port_out(p->port, value);
	if (p->haveInverter) {
	    /*
	     * lower WR, rise A0 and CS1 and/or CS2 taking bit inversion of
	     * parallel port into account. External inverter required!
	     */
	    port_out(p->port + 2, WR + CS1 - (chip & CS1) + (chip & CS2));
	    /* rise WR */
	    port_out(p->port + 2, CS1 - (chip & CS1) + (chip & CS2));
	    if (p->delayMult)
		uPause(p->delayMult);
	    /* lower WR again */
	    port_out(p->port + 2, WR + CS1 - (chip & CS1) + (chip & CS2));
	    if (p->delayMult)
		uPause(p->delayMult);
	}
	else {			/* No inverter connected */
	    /* Note: CS?-(chip&CS?) drive the pin low if controller is set */
	    port_out(p->port + 2, (WR + CS1 - (chip & CS1) + CS2 - (chip & CS2)) ^ OUTMASK);
	    port_out(p->port + 2, (CS1 - (chip & CS1) + CS2 - (chip & CS2)) ^ OUTMASK);
	    if (p->delayMult)
		uPause(p->delayMult);
	    port_out(p->port + 2, (WR + CS1 - (chip & CS1) + CS2 - (chip & CS2)) ^ OUTMASK);
	    if (p->delayMult)
		uPause(p->delayMult);
	}
    }
}

/**
 * Writes data value to one or both SED1520 selected by chip.
 * \param p      Pointer to private data structure
 * \param value  Data byte to write
 * \param chip   Bitmap of controllers to send value to
 */
static void
writedata(PrivateData *p, int value, int chip)
{
    if (p->interface == 68) {
	port_out(p->port + 2, (A0) ^ OUTMASK);
	port_out(p->port, value);
	/* cycle E */
	port_out(p->port + 2, (A0 + (chip & CS1) + (chip & CS2)) ^ OUTMASK);
	if (p->delayMult)
	    uPause(p->delayMult);
	port_out(p->port + 2, (A0) ^ OUTMASK);
    }
    else {
	port_out(p->port, value);
	if (p->haveInverter) {
	    /* lower WR and A0, rise CS1 and/or CS2. See also writecommand. */
	    port_out(p->port + 2, A0 + WR + CS1 - (chip & CS1) + (chip & CS2));
	    port_out(p->port + 2, A0 + CS1 - (chip & CS1) + (chip & CS2));
	    if (p->delayMult)
		uPause(p->delayMult);
	    port_out(p->port + 2, A0 + WR + CS1 - (chip & CS1) + (chip & CS2));
	    if (p->delayMult)
		uPause(p->delayMult);
	}
	else {
	    port_out(p->port + 2, (A0 + WR + CS1 - (chip & CS1) + CS2 - (chip & CS2)) ^ OUTMASK);
	    port_out(p->port + 2, (A0 + CS1 - (chip & CS1) + CS2 - (chip & CS2)) ^ OUTMASK);
	    if (p->delayMult)
		uPause(p->delayMult);
	    port_out(p->port + 2, (A0 + WR + CS1 - (chip & CS1) + CS2 - (chip & CS2)) ^ OUTMASK);
	    if (p->delayMult)
		uPause(p->delayMult);
	}
    }
}

/**
 * Selects a page (=row) on both SED1520.
 * \param p     Pointer to private data structure
 * \param page  Page (=row) number (0-3)
 */
static void
selectpage(PrivateData *p, int page)
{
    writecommand(p, PAGE_ADR + (page & 0x03), CS1 + CS2);
}

/**
 * Selects a column on the SED1520 specified by chip.
 * \param p       Pointer to private data structure
 * \param column  Select column (segment) in controller (0-60)
 * \param chip    Bitmap of controllers to send value to
 */
static void
selectcolumn(PrivateData *p, int column, int chip)
{
    writecommand(p, COLUMN_ADR + (column & 0x7F), chip);
}

/*-
 * Display memory layout information:
 *
 * The SED1520 does not use a linear display data memory but a paged memory
 * with four pages (0-3) corresponding to rows 0-7, 8-15, 16-23, and 24-31.
 * Within each page, one bytes represents 8 pixels of one column (from 0-60)
 * with bit 0 (LSB) being the top and bit 7 (MSB) the bottom pixel within
 * that page.
 *
 * To write data to display memory one selects the page and column and then
 * writes bytes of pixel data to the display. The column counter is increased
 * by each write so the display fills from left to right (normal mode) or
 * right to left (inverted mapping).
 */

/**
 * Draws character z from fontmap to the framebuffer at position x,y.
 * The fontmap is stored in rows while the framebuffer is stored in columns,
 * so we need a little conversion.
 *
 * \param framebuf  Pointer to framebuffer
 * \param x         Character column (zero-based)
 * \param y         Line (zero-based)
 * \param z         Character index in fontmap
 */
static void
drawchar2fb(unsigned char *framebuf, int x, int y, unsigned char z)
{
    int i, j;

    if ((x < 0) || (x >= WIDTH) || (y < 0) || (y >= HEIGHT))
	return;

    /* for each raster column */
    for (i = CELLWIDTH; i > 0; i--) {
	int k = 0;

	/* gather the bits from the fontmap for each raster row */
	for (j = 0; j < CELLHEIGHT; j++)
	    k |= ((glcd_iso8859_1[(int)z][j] >> (i - 1)) & 0x01) << j;

	/* And store it in framebuffer pixel column */
	framebuf[(y * PIXELWIDTH) + (x * CELLWIDTH) + (CELLWIDTH - i)] = k;
    }
}

/**
 * API: Initialize the driver.
 */
MODULE_EXPORT int
sed1520_init(Driver * drvthis)
{
    PrivateData *p;
    char inverted;

    /* Allocate and store private data */
    p = (PrivateData *) calloc(1, sizeof(PrivateData));
    if (p == NULL)
	return -1;
    if (drvthis->store_private_ptr(drvthis, p))
	return -1;

    /* What port to use */
    p->port = drvthis->config_get_int(drvthis->name, "Port", 0, DEFAULT_PORT);

    /* Initialize timing */
    if (timing_init() == -1) {
	report(RPT_ERR, "%s: timing_init() failed (%s)", drvthis->name, strerror(errno));
	return -1;
    }
    p->delayMult = drvthis->config_get_int(drvthis->name, "delaymult", 0, 1);
    if (p->delayMult < 0 || p->delayMult > 1000) {
	report(RPT_WARNING, "%s: DelayMult value invalid, using default (1)", drvthis->name);
	p->delayMult = 1;
    }
    if (p->delayMult == 0)
	report(RPT_INFO, "%s: Delay is disabled", drvthis->name);

    /* Allocate our framebuffer */
    p->framebuf = (unsigned char *) calloc(PIXELWIDTH * HEIGHT, sizeof(unsigned char));
    if (p->framebuf == NULL) {
	report(RPT_ERR, "%s: unable to allocate framebuffer", drvthis->name);
	return -1;
    }

    /* Clear screen */
    memset(p->framebuf, '\0', PIXELWIDTH * HEIGHT);

    /* Open port */
    if (port_access_multiple(p->port, 3)) {
	report(RPT_ERR, "%s: unable to access port 0x%03X", drvthis->name, p->port);
	return -1;
    }

    /* Get interface style (68-family or 80-family) */
    p->interface = drvthis->config_get_int(drvthis->name, "InterfaceType", 0, 80);
    if (p->interface != 68 && p->interface != 80) {
	report(RPT_WARNING, "%s: Invalid interface configured, using type 80", drvthis->name);
	p->interface = 80;
    }

    /*
     * The original wiring used an inverter to drive the control lines. As
     * someone may still be using this, the following setting in ON by default.
     */
    p->haveInverter = drvthis->config_get_bool(drvthis->name, "HaveInverter", 0, 1);

    /*
     * Inverted Mapping: Segments are addressed from right to left and start
     * at column 19 (address 0x13) up to column 79 (address 0x4F).
     */
    inverted = drvthis->config_get_bool(drvthis->name, "InvertedMapping", 0, 0);
    if (inverted)
	p->colStartAdd = 0x13;
    else
	p->colStartAdd = 0;

    /* Initialize display */
    if (drvthis->config_get_bool(drvthis->name, "UseHardReset", 0, 0) == 1) {
	writedata(p, 0xFF, CS1 + CS2);
	writedata(p, 0xFF, CS1 + CS2);
	writedata(p, 0xFF, CS1 + CS2);
    }
    writecommand(p, SOFT_RESET, CS1 + CS2);
    writecommand(p, (inverted) ? ADC_INV : ADC_NORM, CS1 + CS2);
    writecommand(p, DISP_ON, CS1 + CS2);
    writecommand(p, DISP_START_LINE, CS1 + CS2);
    selectpage(p, 3);

    report(RPT_DEBUG, "%s: init() done", drvthis->name);
    return 0;
}

/**
 * API: Frees the framebuffer and exits the driver.
 */
MODULE_EXPORT void
sed1520_close(Driver * drvthis)
{
    PrivateData *p = drvthis->private_data;

    if (p != NULL) {
	if (p->framebuf != NULL)
	    free(p->framebuf);

	free(p);
    }
    drvthis->store_private_ptr(drvthis, NULL);
}

/**
 * API: Returns the display width
 */
MODULE_EXPORT int
sed1520_width(Driver * drvthis)
{
    return WIDTH;
}

/**
 * API: Returns the display height
 */
MODULE_EXPORT int
sed1520_height(Driver * drvthis)
{
    return HEIGHT;
}

/**
 * API: Return the width of a character in pixels.
 */
MODULE_EXPORT int
sed1520_cellwidth(Driver * drvthis)
{
    return CELLWIDTH;
}

/**
 * API: Return the height of a character in pixels.
 */
MODULE_EXPORT int
sed1520_cellheight(Driver * drvthis)
{
    return CELLHEIGHT;
}

/**
 * API: Clears the LCD screen
 */
MODULE_EXPORT void
sed1520_clear(Driver * drvthis)
{
    PrivateData *p = drvthis->private_data;

    memset(p->framebuf, '\0', PIXELWIDTH * HEIGHT);
}

/**
 * API: Flushes all output to the lcd. No conversion needed here as
 * framebuffer is prepared by \c drawchar2fb.
 */
MODULE_EXPORT void
sed1520_flush(Driver * drvthis)
{
    PrivateData *p = drvthis->private_data;
    int i, j;

    for (i = 0; i < HEIGHT; i++) {
	/* select line/page */
	selectpage(p, i);

	/* update left half of display */
	selectcolumn(p, p->colStartAdd, CS1);
	for (j = 0; j < PIXELWIDTH / 2; j++)
	    writedata(p, p->framebuf[j + (i * PIXELWIDTH)], CS1);

	/* update right half of display */
	selectcolumn(p, p->colStartAdd, CS2);
	for (j = PIXELWIDTH / 2; j < PIXELWIDTH; j++)
	    writedata(p, p->framebuf[j + (i * PIXELWIDTH)], CS2);
    }
}

/**
 * API: Prints a string on the lc display, at position (x,y). The
 * upper-left is (1,1), and the lower right should be (20,4).
 */
MODULE_EXPORT void
sed1520_string(Driver * drvthis, int x, int y, const char string[])
{
    PrivateData *p = drvthis->private_data;
    int i;

    x--;			/* Convert 1-based coords to 0-based */
    y--;

    for (i = 0; string[i] != '\0'; i++)
	drawchar2fb(p->framebuf, x + i, y, string[i]);
}

/**
 * API: Writes  char c at position x,y into the framebuffer.
 * x and y are 1-based textmode coordinates.
 */
MODULE_EXPORT void
sed1520_chr(Driver * drvthis, int x, int y, char c)
{
    PrivateData *p = drvthis->private_data;

    y--;
    x--;
    drawchar2fb(p->framebuf, x, y, c);
}

/**
 * API: This function draws a number num into the last 3 rows of the
 * framebuffer at 1-based position x. It should draw a 4-row font, but me
 * thinks this would look a little stretched. When num=10 a colon is drawn.
 */
MODULE_EXPORT void
sed1520_num(Driver * drvthis, int x, int num)
{
    PrivateData *p = drvthis->private_data;
    int z, c;

    x--;

    /* return on illegal char or illegal position */
    if ((x >= WIDTH) || (num < 0) || (num > 10))
	return;

    /*
     * For each page (= row of 8 dots) starting in page 1 (page 0 is left
     * empty - thus the numbers are aligned at the bottom of the display),
     * copy <width> number of bytes into the framebuffer.
     */
    for (z = 0; z < 3; z++) {
	for (c = 0; c < widtbl_NUM[num]; c++) {
	    if ((x * CELLWIDTH + c >= 0) && (x * CELLWIDTH + c < PIXELWIDTH)) {
		p->framebuf[((z + 1) * PIXELWIDTH) + (x * CELLWIDTH) + c] =
		    chrtbl_NUM[num][c * 3 + z];
	    }
	}
    }
}

/**
 * API: Changes the font of character n to a pattern given by *dat.
 * HD44780 controllers only posses 8 programmable chars. But we store the
 * fontmap completely in RAM, so every character can be altered.
 *
 * Important: Characters have to be redrawn by drawchar2fb() to show their
 * new shape. Because we use a non-standard 6x8 font a *dat not calculated
 * from width and height will fail.
 */
MODULE_EXPORT void
sed1520_set_char(Driver * drvthis, int n, char *dat)
{
    int row;
    unsigned char mask = (1 << CELLWIDTH) - 1;

    if (n < 0 || n > 255)
	return;
    if (!dat)
	return;

    for (row = 0; row < CELLHEIGHT; row++)
	glcd_iso8859_1[n][row] = dat[row] & mask;
}

/**
 * API: Draws a vertical from the bottom up at 1-based position x.
 */
MODULE_EXPORT void
sed1520_vbar(Driver *drvthis, int x, int y, int len, int promille, int options)
{
    PrivateData *p = drvthis->private_data;
    int i, j, k;
    int pixels;

    x--;

    if (x < 0 || y < 1 || x >= WIDTH || y > HEIGHT || len > HEIGHT)
	return;

    pixels = len * CELLHEIGHT * promille / 1000;

    for (j = 0; j < 3; j++) {
	k = 0;
	/* Set height of block. Bottom is leftmost bit */
	for (i = 0; i < CELLHEIGHT; i++) {
	    if (pixels > i)
		k |= (1 << (CELLHEIGHT - 1 - i));
	}

	/* Draw directly into framebuffer starting from bottom */
	p->framebuf[((3 - j) * PIXELWIDTH) + (x * CELLWIDTH) + 0] = 0;
	p->framebuf[((3 - j) * PIXELWIDTH) + (x * CELLWIDTH) + 1] = k;
	p->framebuf[((3 - j) * PIXELWIDTH) + (x * CELLWIDTH) + 2] = k;
	p->framebuf[((3 - j) * PIXELWIDTH) + (x * CELLWIDTH) + 3] = k;
	p->framebuf[((3 - j) * PIXELWIDTH) + (x * CELLWIDTH) + 4] = k;
	p->framebuf[((3 - j) * PIXELWIDTH) + (x * CELLWIDTH) + 5] = 0;

	pixels -= CELLHEIGHT;
    }
}

/**
 * API: Draws a horizontal bar from left to right at 1-based position x,y into
 * the framebuffer.
 */
MODULE_EXPORT void
sed1520_hbar(Driver *drvthis, int x, int y, int len, int promille, int options)
{
    PrivateData *p = drvthis->private_data;
    int i;
    int pixels;

    x--;
    y--;

    if ((y < 0) || (y >= HEIGHT) || (x < 0) || (len < 0) || (x + len > WIDTH))
	return;

    pixels = len * CELLWIDTH * promille / 1000;

    /*
     * Draw directly into framebuffer. Drawing is easy, as one byte is one
     * column in a page. Use 5 dots in the middle (0x3E).
     */
    for (i = 0; i < pixels; i++)
	p->framebuf[(y * PIXELWIDTH) + (x * CELLWIDTH) + i] = 0x7C;
}

/**
 * API: Place an icon on the screen.
 */
MODULE_EXPORT int
sed1520_icon(Driver * drvthis, int x, int y, int icon)
{
    int icon_char;

    if ((icon_char = glcd_icon5x8(icon)) != -1) {
        sed1520_chr(drvthis, x, y, icon_char);
        return 0;
    }
    return -1;
}