aboutsummaryrefslogtreecommitdiffstats
path: root/drv_EA232graphic.c
blob: bec5fc93cbee94e58adc1f0d8900a743721fd351 (plain)
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
/* $Id: drv_EA232graphic.c 1172 2012-02-10 16:04:36Z michael $
 * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/drv_EA232graphic.c $
 *
 * Driver for Electronic Assembly serial graphic display
 *
 * Copyright (C) 2012 Robert Resch <fli4l@robert.reschpara.de>
 * Copyright (C) 2007 Stefan Gmeiner <stefangmeiner@solnet.ch>
 * Copyright (C) 2005 Michael Reinelt <michael@reinelt.co.at>
 * Copyright (C) 2005, 2006, 2007 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 *
 * 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.
 *
 */

/*
 * Electronic Assembly RS232 Graphic Displays
 *
 * This driver supports some of the Electronic Assembly serial graphic displays. Although
 * most of this display support higher level operation like text and graphic command, non of
 * these are used. Instead the lcd4linux graphic routines creates the graphic which is then
 * transferd to the display.
 * 
 * FIXME: Some display have addition features such as GPI which are not yet implemented.
 *
 * Display          Protocol   Output  Contrast
 * ============================================
 * GE120-5NV24         1         8       yes
 * GE128-6N3V24        1         8       no
 * GE128-6N9V24        2         0       yes
 * GE128-7KV24         3         8       no
 * GE240-6KV24         3         8       no
 * GE240-6KCV24        3         8       no
 * GE240-7KV24         3         8       no
 * GE240-7KLWV24       3         8       no
 * GE240-6KLWV24       3         8       no
 * KIT120-5            4         5       no
 * KIT129-6            4         8       no
 * KIT160-6            2         8       no
 * KIT160-7            2         8       no
 * KIT240-6            2         8       no
 * KIT240-7            2         8       no
 * KIT320-8            2         8       no
 *
 * Supported protocol commands:
 *
 *            Clear
 * Protocol  Display  Set Output     Set Contrast    Bitmap                    Orientation
 * =======================================================================================
 *    1      DL       Y(0..7)(0..1)  K(0..20)        U(x)(y)(w)(h)(...)        Vertical
 *    2      <ESC>DL  <ESC>YW...     <ESC>DK(0..63)  <ESC>UL(x)(y)(w)(h)(...)  Horizontal
 *    3      DL       Y(0..7)(0..1)  --              U(x)(y)(w)(h)(...)        Horizontal
 *    4      <ESC>DL  <ESC>YW...     --              <ESC>UL(x)(y)(w)(h)(...)  Vertical
 *
 * Bitmap orientation:
 *
 * Vertical:        Byte-No.
 *                 123456789....
 *           Bit 0 *********
 *           Bit 1 *********
 *           Bit 2 *********
 *           Bit 3 *********
 *           Bit 4 *********
 *           Bit 5 *********
 *           Bit 6 *********
 *           Bit 7 *********
 *
 * Horizontal:      Bit-No.
 *                 76543210
 *          Byte 0 ********
 *          Byte 1 ********
 *          Byte 3 ********
 *          ...
 *
 *
 */

/* 
 *
 * exported fuctions:
 *
 * struct DRIVER drv_EA232graphic
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <unistd.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"

/* graphic display */
#include "drv_generic_graphic.h"

/* serial port */
#include "drv_generic_serial.h"

/* GPO */
#include "drv_generic_gpio.h"



#define ESC ((char)27)
#define MSB_BYTE 0x80
#define LSB_BYTE 0x01

static char Name[] = "EA232graphic";

typedef struct {
    char *name;
    int columns;
    int rows;
    int max_contrast;
    int default_contrast;
    int gpo;
    int protocol;
} MODEL;

static MODEL Models[] = {
    /* Protocol 1 models */
    {"GE120-5NV24", 120, 32, 20, 8, 8, 1},
    {"GE128-6N3V24", 128, 64, 0, 0, 8, 1},

    /* Protocol 2 models */
    {"GE128-6N9V24", 128, 64, 63, 40, 0, 2},
    {"KIT160-6", 160, 80, 0, 0, 8, 2},
    {"KIT160-7", 160, 128, 0, 0, 8, 2},
    {"KIT240-6", 240, 64, 0, 0, 8, 2},
    {"KIT240-7", 240, 128, 0, 0, 8, 2},
    {"KIT320-8", 320, 240, 0, 0, 8, 2},

    /* Protocol 3 models */
    {"GE128-7KV24", 128, 128, 0, 0, 8, 3},
    {"GE240-6KV24", 240, 64, 0, 0, 8, 3},
    {"GE240-6KCV24", 240, 64, 0, 0, 8, 3},
    {"GE240-7KV24", 240, 128, 0, 0, 8, 3},
    {"GE240-7KLWV24", 240, 128, 0, 0, 8, 3},
    {"GE240-6KLWV24", 240, 64, 0, 0, 8, 3},

    /* Protocol 4 models */
    {"KIT120-5", 120, 32, 0, 0, 5, 4},
    {"KIT129-6", 128, 64, 0, 0, 8, 4},

    {NULL, 0, 0, 0, 0, 0, 0}
};

static MODEL *Model;


/****************************************/
/***  hardware dependant functions    ***/
/****************************************/


static int drv_EA232graphic_open(const char *section)
{
    /* open serial port */
    /* don't mind about device, speed and stuff, this function will take care of */
    if (drv_generic_serial_open_handshake(section, Name, 0) < 0)
	return -1;

    return 0;
}


static int drv_EA232graphic_close(void)
{
    drv_generic_serial_close();

    return 0;
}


/* write data to the display */
static void drv_EA232graphic_send(const char *data, const int len)
{
    drv_generic_serial_write_rts(data, len);
}


/* delete Display */
static void drv_EA232graphic_clear_display()
{
    char cmd[4];

    switch (Model->protocol) {
    case 1:
    case 3:
	/* Clear Display */
	cmd[0] = 'D';
	cmd[1] = 'L';
	drv_EA232graphic_send(cmd, 2);
	break;
    case 2:
	/* Clear Display */
	cmd[0] = ESC;
	cmd[1] = 'D';
	cmd[2] = 'L';
	drv_EA232graphic_send(cmd, 3);
	usleep(500000);
	/* Disable Cursor */
	cmd[0] = ESC;
	cmd[1] = 'Q';
	cmd[2] = 'C';
	cmd[3] = '0';
	drv_EA232graphic_send(cmd, 4);
	break;
    case 4:
	/* Set all GPIO to Output */
	cmd[0] = ESC;
	cmd[1] = 'Y';
	cmd[2] = 'M';
	cmd[3] = 8;
	drv_EA232graphic_send(cmd, 4);
	/* Clear Display */
	cmd[0] = ESC;
	cmd[1] = 'D';
	cmd[2] = 'L';
	drv_EA232graphic_send(cmd, 3);
	usleep(500000);
	/* Disable Cursor */
	cmd[0] = ESC;
	cmd[1] = 'Q';
	cmd[2] = 'C';
	cmd[3] = '0';
	drv_EA232graphic_send(cmd, 4);
	break;
    default:
	error("%s: undefined protocol type", Name);
	return;
    }
}



/* copy framebuffer to display */
static void drv_EA232graphic_blit(const int row, const int col, const int height, const int width)
{

    int r, c, l, p, d;
    char *cmd;

    /* calculate length of command */
    l = 0;
    switch (Model->protocol) {
    case 1:
    case 4:
	l = ((height + 7) / 8) * width;
	break;
    case 2:
    case 3:
	l = ((width + 7) / 8) * height;
	break;
    default:
	error("%s: undefined protocol type", Name);
	return;
    }

    /* add maximum length of command header */
    cmd = (char *) malloc(l + 10);
    if (cmd == NULL) {
	error("%s: allocation of buffer failed: %s", Name, strerror(errno));
	return;
    }
    p = 0;

    /* write command header */
    switch (Model->protocol) {
    case 1:
    case 3:
	cmd[p++] = 'U';
	cmd[p++] = col;
	cmd[p++] = row;
	cmd[p++] = width;
	cmd[p++] = height;
	break;
    case 2:
    case 4:
	cmd[p++] = ESC;
	cmd[p++] = 'U';
	cmd[p++] = 'L';
	cmd[p++] = col;
	cmd[p++] = row;
	cmd[p++] = width;
	cmd[p++] = height;
	break;
    default:
	error("%s: undefined protocol type", Name);
	free(cmd);
	return;
    }

    /* clear all pixels */
    memset(cmd + p, 0, l);

    /* set pixels */
    switch (Model->protocol) {
    case 1:
    case 4:
	for (r = 0; r < height; r++) {
	    for (c = 0; c < width; c++) {
		if (drv_generic_graphic_black(r + row, c + col)) {
		    cmd[(r / 8) * width + c + p] |= (LSB_BYTE << (r % 8));
		}
	    }
	}
	break;
    case 2:
	d = ((width + 7) / 8);
	for (r = 0; r < height; r++) {
	    for (c = 0; c < width; c++) {
		if (drv_generic_graphic_black(r + row, c + col)) {
		    cmd[(c / 8) + (d * r) + p] |= (MSB_BYTE >> (c % 8));
		}
	    }
	}
	break;
    case 3:
	for (c = 0; c < width; c++) {
	    for (r = 0; r < height; r++) {
		if (drv_generic_graphic_black(r + row, c + col)) {
		    cmd[(c / 8) * height + r + p] |= (MSB_BYTE >> (c % 8));
		}
	    }
	}
	break;
    default:
	error("%s: undefined protocol type", Name);
	free(cmd);
	return;
    }

    drv_EA232graphic_send(cmd, p + l);

    free(cmd);
}


static int drv_EA232graphic_GPO(const int num, const int val)
{
    char cmd[5];

    if (Model->gpo == 0) {
	error("%s: GPO not supported on this model.", Name);
	return -1;
    }


    if (Model->gpo < num) {
	error("%s: GPO %d is not available.", Name, num);
	return -1;
    }

    switch (Model->protocol) {
    case 1:
    case 3:
	cmd[0] = 'Y';
	cmd[1] = num;
	cmd[2] = (val > 0) ? 1 : 0;
	drv_EA232graphic_send(cmd, 3);
	break;
    case 2:
    case 4:
	cmd[0] = ESC;
	cmd[1] = 'Y';
	cmd[2] = 'W';
	cmd[3] = num + 1;
	cmd[4] = (val > 0) ? 1 : 0;
	drv_EA232graphic_send(cmd, 5);
	break;
    }

    return 0;
}


/* adjust contast */
static int drv_EA232graphic_contrast(int contrast)
{
    char cmd[4];

    if (Model->max_contrast == 0) {
	error("%s: contrast setting not support by model", Name);
	return -1;
    }

    /* adjust limits according to the display */
    if (contrast < 0)
	contrast = 0;
    if (contrast > Model->max_contrast)
	contrast = Model->max_contrast;


    switch (Model->protocol) {
    case 1:
	cmd[0] = 'K';
	cmd[1] = contrast;
	drv_EA232graphic_send(cmd, 2);
	break;
    case 2:
	cmd[0] = ESC;
	cmd[1] = 'D';
	cmd[2] = 'K';
	cmd[3] = contrast;
	drv_EA232graphic_send(cmd, 4);
	break;
    default:
	error("%s: undefined protocol type", Name);
	return -1;
    }

    return contrast;
}


/* start graphic display */
static int drv_EA232graphic_start(const char *section)
{
    char *s;
    int contrast;
    int i;

    /* read model from configuration */
    s = cfg_get(section, "Model", NULL);
    if (s == NULL || *s == '\0') {
	error("%s: no '%s.Model' entry from %s", Name, section, cfg_source());
	return -1;
    }

    for (i = 0; Models[i].name != NULL; i++) {
	if (strcasecmp(Models[i].name, s) == 0)
	    break;
    }
    if (!Models[i].name) {
	error("%s: %s.Model '%s' is unknown from %s", Name, section, s, cfg_source());
	free(s);
	return -1;
    }
    Model = &Models[i];
    info("%s: using model '%s'", Name, Model->name);
    free(s);

    /* read display size from model configuration */
    DROWS = Model->rows;
    DCOLS = Model->columns;

    /* set number of GPO's from model */
    GPOS = Model->gpo;

    /* read font size from configuration */
    s = cfg_get(section, "Font", "6x8");
    if (s == NULL || *s == '\0') {
	error("%s: no '%s.Font' entry from %s", Name, section, cfg_source());
	return -1;
    }

    XRES = -1;
    YRES = -1;
    if (sscanf(s, "%dx%d", &XRES, &YRES) != 2 || XRES < 1 || YRES < 1) {
	error("%s: bad Font '%s' from %s", Name, s, cfg_source());
	free(s);
	return -1;
    }
    free(s);

    /* open communication with the display */
    if (drv_EA232graphic_open(section) < 0) {
	return -1;
    }

    /* reset & initialize display */
    drv_EA232graphic_clear_display();

    if (cfg_number(section, "Contrast", Model->default_contrast, 0, Model->max_contrast, &contrast) > 0) {
	drv_EA232graphic_contrast(contrast);
    }

    return 0;
}


/****************************************/
/***            plugins               ***/
/****************************************/

static void plugin_contrast(RESULT * result, RESULT * arg1)
{
    double contrast;

    contrast = drv_EA232graphic_contrast(R2N(arg1));
    SetResult(&result, R_NUMBER, &contrast);
}


/****************************************/
/***        widget callbacks          ***/
/****************************************/


/* using drv_generic_text_draw(W) */
/* using drv_generic_text_icon_draw(W) */
/* using drv_generic_text_bar_draw(W) */
/* using drv_generic_gpio_draw(W) */


/****************************************/
/***        exported functions        ***/
/****************************************/


/* list models */
int drv_EA232graphic_list(void)
{
    int i;

    for (i = 0; Models[i].name; i++) {
	printf("%s ", Models[i].name);
    }

    return 0;
}


/* initialize driver & display */
int drv_EA232graphic_init(const char *section, const int quiet)
{
    int ret;

    /* real worker functions */
    drv_generic_graphic_real_blit = drv_EA232graphic_blit;
    drv_generic_gpio_real_set = drv_EA232graphic_GPO;

    /* start display */
    if ((ret = drv_EA232graphic_start(section)) != 0)
	return ret;

    /* initialize generic graphic driver */
    if ((ret = drv_generic_graphic_init(section, Name)) != 0)
	return ret;

    /* initialize GPO */
    if (Model->gpo > 0 && (ret = drv_generic_gpio_init(section, Name)) != 0)
	return ret;

    if (!quiet) {
	char buffer[40];
	qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
	if (drv_generic_graphic_greet(buffer, NULL)) {
	    sleep(3);
	    drv_generic_graphic_clear();
	}
    }

    /* register plugins */
    AddFunction("LCD::contrast", 1, plugin_contrast);

    return 0;
}

/* close driver & display */
int drv_EA232graphic_quit(const int quiet)
{

    info("%s: shutting down.", Name);

    /* clear display */
    drv_generic_graphic_clear();

    /* say goodbye... */
    if (!quiet) {
	drv_generic_graphic_greet("goodbye!", NULL);
    }

    drv_generic_graphic_quit();

    if (Model->gpo > 0)
	drv_generic_gpio_quit();

    debug("%s: closing connection", Name);
    drv_EA232graphic_close();

    return (0);
}

/* use this one for a graphic display */
DRIVER drv_EA232graphic = {
    .name = Name,
    .list = drv_EA232graphic_list,
    .init = drv_EA232graphic_init,
    .quit = drv_EA232graphic_quit,
};