root/tags/0.10.0/drv_BeckmannEgle.c

Revision 547, 17.4 kB (checked in by reinelt, 4 years ago)

[lcd4linux @ 2005-05-08 04:32:43 by reinelt]
CodingStyle? added and applied

Line 
1/* $Id: drv_BeckmannEgle.c,v 1.15 2005/05/08 04:32:44 reinelt Exp $
2 *
3 * driver for Beckmann+Egle "Mini Terminals" and "Compact Terminals"
4 * Copyright (C) 2000 Michael Reinelt <reinelt@eunet.at>
5 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
6 *
7 * This file is part of LCD4Linux.
8 *
9 * LCD4Linux is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
13 *
14 * LCD4Linux is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *
24 * $Log: drv_BeckmannEgle.c,v $
25 * Revision 1.15  2005/05/08 04:32:44  reinelt
26 * CodingStyle added and applied
27 *
28 * Revision 1.14  2005/01/18 06:30:22  reinelt
29 * added (C) to all copyright statements
30 *
31 * Revision 1.13  2004/07/14 04:44:44  reinelt
32 *
33 * Beckmann+Egle fix
34 * added smaple widget for the PPP plugin
35 *
36 * Revision 1.12  2004/06/29 04:49:30  reinelt
37 *
38 * B+E enhanced port detection
39 *
40 * Revision 1.11  2004/06/26 12:04:59  reinelt
41 *
42 * uh-oh... the last CVS log message messed up things a lot...
43 *
44 * Revision 1.10  2004/06/26 09:27:20  reinelt
45 *
46 * added '-W' to CFLAGS
47 * changed all C++ comments to C ones
48 * cleaned up a lot of signed/unsigned mistakes
49 *
50 * Revision 1.9  2004/06/26 06:12:15  reinelt
51 *
52 * support for Beckmann+Egle Compact Terminals
53 * some mostly cosmetic changes in the MatrixOrbital and USBLCD driver
54 * added debugging to the generic serial driver
55 * fixed a bug in the generic text driver where icons could be drawn outside
56 * the display bounds
57 *
58 * Revision 1.8  2004/06/20 10:09:54  reinelt
59 *
60 * 'const'ified the whole source
61 *
62 * Revision 1.7  2004/06/06 06:51:59  reinelt
63 *
64 * do not display end splash screen if quiet=1
65 *
66 * Revision 1.6  2004/06/05 06:41:39  reinelt
67 *
68 * chancged splash screen again
69 *
70 * Revision 1.5  2004/06/05 06:13:11  reinelt
71 *
72 * splash screen for all text-based display drivers
73 *
74 * Revision 1.4  2004/06/02 09:41:19  reinelt
75 *
76 * prepared support for startup splash screen
77 *
78 * Revision 1.3  2004/06/02 05:14:16  reinelt
79 *
80 * fixed models listing for Beckmann+Egle driver
81 * some cosmetic changes
82 *
83 * Revision 1.2  2004/05/31 05:38:02  reinelt
84 *
85 * fixed possible bugs with user-defined chars (clear high bits)
86 * thanks to Andy Baxter for debugging the MilfordInstruments driver!
87 *
88 * Revision 1.1  2004/05/28 14:36:10  reinelt
89 *
90 * added drv_BeckmannEgle.c (forgotten at first check in :-)
91 *
92 */
93
94/*
95 *
96 * exported fuctions:
97 *
98 * struct DRIVER drv_BeckmannEgle
99 *
100 */
101
102#include "config.h"
103
104#include <stdlib.h>
105#include <stdio.h>
106#include <string.h>
107#include <unistd.h>
108#include <termios.h>
109
110#include "debug.h"
111#include "cfg.h"
112#include "qprintf.h"
113#include "plugin.h"
114#include "widget.h"
115#include "widget_text.h"
116#include "widget_icon.h"
117#include "widget_bar.h"
118#include "drv.h"
119#include "drv_generic_text.h"
120#include "drv_generic_serial.h"
121
122
123#define ESC "\033"
124
125static char Name[] = "Beckmann+Egle";
126
127
128typedef struct {
129    char *name;
130    int rows;
131    int cols;
132    int protocol;
133    int type;
134} MODEL;
135
136
137static MODEL Models[] = {
138
139    /* MultiTerminals */
140    {"MT16x1", 1, 16, 1, 0},
141    {"MT16x2", 2, 16, 1, 1},
142    {"MT16x4", 4, 16, 1, 2},
143    {"MT20x1", 1, 20, 1, 3},
144    {"MT20x2", 2, 20, 1, 4},
145    {"MT20x4", 4, 20, 1, 5},
146    {"MT24x1", 1, 24, 1, 6},
147    {"MT24x2", 2, 24, 1, 7},
148    {"MT32x1", 1, 32, 1, 8},
149    {"MT32x2", 2, 32, 1, 9},
150    {"MT40x1", 1, 40, 1, 10},
151    {"MT40x2", 2, 40, 1, 11},
152    {"MT40x4", 4, 40, 1, 12},
153
154    /* CompactTerminal */
155    {"CT20x4", 4, 20, 2, 0},
156
157    {NULL, 0, 0, 0, 0},
158};
159
160
161static int Model;
162static int Protocol;
163
164
165
166/****************************************/
167/***  hardware dependant functions    ***/
168/****************************************/
169
170static void drv_BuE_clear(void)
171{
172    switch (Protocol) {
173    case 1:
174  drv_generic_serial_write(ESC "&#", 3);  /* clear display */
175  break;
176    case 2:
177  drv_generic_serial_write(ESC "LL", 3);  /* clear display */
178  break;
179    }
180}
181
182
183static void drv_BuE_MT_write(const int row, const int col, const char *data, const int len)
184{
185    char cmd[] = ESC "[y;xH";
186
187    cmd[2] = (char) row;
188    cmd[4] = (char) col;
189
190    drv_generic_serial_write(cmd, 6);
191    drv_generic_serial_write(data, len);
192}
193
194
195static void drv_BuE_MT_defchar(const int ascii, const unsigned char *matrix)
196{
197    int i;
198    char cmd[22] = ESC "&T";  /* enter transparent mode */
199
200    cmd[3] = '\0';    /* write cmd */
201    cmd[4] = 0x40 | 8 * ascii;  /* write CGRAM */
202
203    for (i = 0; i < 8; i++) {
204  cmd[2 * i + 5] = '\1';  /* write data */
205  cmd[2 * i + 6] = matrix[i] & 0x1f;  /* character bitmap */
206    }
207    cmd[21] = '\377';   /* leave transparent mode */
208
209    drv_generic_serial_write(cmd, 22);
210}
211
212
213static void drv_BuE_CT_write(const int row, const int col, const char *data, const int len)
214{
215    char cmd[] = ESC "LCzs\001";
216    cmd[3] = (char) row + 1;
217    cmd[4] = (char) col + 1;
218
219    drv_generic_serial_write(cmd, 6);
220    drv_generic_serial_write(data, len);
221
222}
223
224
225static void drv_BuE_CT_defchar(const int ascii, const unsigned char *matrix)
226{
227    int i;
228    char cmd[13] = ESC "LZ";  /* set custom char */
229
230    /* number of user-defined char (0..7) */
231    cmd[3] = (char) ascii - CHAR0;
232
233    /* ASCII code to replace */
234    cmd[4] = (char) ascii;
235
236    for (i = 0; i < 8; i++) {
237  cmd[i + 5] = matrix[i] & 0x1f;
238    }
239
240    drv_generic_serial_write(cmd, 13);
241}
242
243
244static int drv_BuE_CT_contrast(int contrast)
245{
246    static char Contrast = 7;
247    char cmd[4] = ESC "LKn";
248
249    /* -1 is used to query the current contrast */
250    if (contrast == -1)
251  return Contrast;
252
253    if (contrast < 0)
254  contrast = 0;
255    if (contrast > 15)
256  contrast = 15;
257    Contrast = contrast;
258
259    cmd[3] = Contrast;
260
261    drv_generic_serial_write(cmd, 4);
262
263    return Contrast;
264}
265
266
267static int drv_BuE_CT_backlight(int backlight)
268{
269    static char Backlight = 0;
270    char cmd[4] = ESC "LBn";
271
272    /* -1 is used to query the current backlight */
273    if (backlight == -1)
274  return Backlight;
275
276    if (backlight < 0)
277  backlight = 0;
278    if (backlight > 1)
279  backlight = 1;
280    Backlight = backlight;
281
282    cmd[3] = Backlight;
283
284    drv_generic_serial_write(cmd, 4);
285
286    return Backlight;
287}
288
289
290static int drv_BuE_CT_gpo(int num, int val)
291{
292    static int GPO[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
293    char cmd[4] = ESC "Pnx";
294
295    if (num < 0)
296  num = 0;
297    if (num > 7)
298  num = 7;
299
300    /* -1 is used to query the current GPO */
301    if (val == -1)
302  return GPO[num];
303
304    if (val < 0)
305  val = 0;
306    if (val > 255)
307  val = 255;
308    GPO[num] = val;
309
310    cmd[2] = (char) num;
311    cmd[3] = (char) val;
312
313    drv_generic_serial_write(cmd, 4);
314
315    return GPO[num];
316}
317
318
319static int drv_BuE_CT_gpi(int num)
320{
321    char cmd[4] = ESC "?Pn";
322    char buffer[4];
323
324    if (num < 0)
325  num = 0;
326    if (num > 7)
327  num = 7;
328
329    cmd[3] = (char) num;
330    drv_generic_serial_write(cmd, 4);
331
332    usleep(10000);
333
334    if (drv_generic_serial_read(buffer, 4) != 4) {
335  error("%s: error reading port %d", Name, num);
336  return -1;
337    }
338
339    return buffer[3];
340}
341
342
343static int drv_BuE_CT_adc(void)
344{
345    char buffer[4];
346
347    drv_generic_serial_write(ESC "?A", 3);
348
349    usleep(10000);
350
351    if ((drv_generic_serial_read(buffer, 4) != 4) || (buffer[0] != 'A') || (buffer[1] != ':')
352  ) {
353  error("%s: error reading ADC", Name);
354  return -1;
355    }
356
357    /* 10 bit value: 8 bit high, 2 bit low */
358    return 4 * (unsigned char) buffer[2] + (unsigned char) buffer[3];
359}
360
361
362static int drv_BuE_CT_pwm(int val)
363{
364    static int PWM = -1;
365    char cmd[4] = ESC "Adm";
366
367    /* -1 is used to query the current PWM */
368    if (val == -1)
369  return PWM;
370
371    if (val < 0)
372  val = 0;
373    if (val > 255)
374  val = 255;
375    PWM = val;
376
377    cmd[2] = (char) val;
378    cmd[3] = val == 0 ? 1 : 2;
379    drv_generic_serial_write(cmd, 4);
380
381    return val;
382}
383
384
385static int drv_BuE_MT_start(const char *section)
386{
387    char cmd[] = ESC "&sX";
388
389    /* CSTOPB: 2 stop bits */
390    if (drv_generic_serial_open(section, Name, CSTOPB) < 0)
391  return -1;
392
393    cmd[4] = Models[Model].type;
394    drv_generic_serial_write(cmd, 4); /* select display type */
395    drv_generic_serial_write(ESC "&D", 3);  /* cursor off */
396
397    return 0;
398}
399
400
401static int drv_BuE_CT_start(const char *section)
402{
403    char buffer[16];
404    char *size;
405    int i, len;
406
407    if (drv_generic_serial_open(section, Name, 0) < 0)
408  return -1;
409
410#if 0
411    /* restart terminal */
412    drv_generic_serial_write(ESC "Kr", 3);
413    usleep(10000);
414#endif
415
416    /* Fixme: the CT does not return a serial number in byte mode */
417    /* set parameter mode 'decimal' */
418    drv_generic_serial_write(ESC "KM\073", 4);
419
420    /* read version */
421    drv_generic_serial_write(ESC "?V", 3);
422    usleep(100000);
423    if ((len = drv_generic_serial_read(buffer, -sizeof(buffer))) > 0) {
424  int v, r, s;
425  if (sscanf(buffer, "V:%d.%d,%d;", &v, &r, &s) != 3) {
426      error("%s: error parsing display identification <%*s>", Name, len, buffer);
427  } else {
428      info("%s: display identified as version %d.%d, S/N %d", Name, v, r, s);
429  }
430    }
431
432    /* set parameter mode 'byte' */
433    drv_generic_serial_write(ESC "KM\072", 4);
434
435    /* the CT20x4 can control smaller displays, too */
436    size = cfg_get(section, "Size", NULL);
437    if (size != NULL && *size != '\0') {
438  int r, c;
439  char cmd[6] = ESC "LArc";
440  if (sscanf(size, "%dx%d", &c, &r) != 2 || r < 1 || c < 1) {
441      error("%s: bad %s.Size '%s' from %s", Name, section, size, cfg_source());
442      return -1;
443  }
444  info("%s: display size: %d rows %d columns", Name, r, c);
445  /* set display size */
446  cmd[3] = (char) r;
447  cmd[4] = (char) c;
448  drv_generic_serial_write(cmd, 5);
449  DCOLS = c;
450  DROWS = r;
451    }
452
453    /* set contrast */
454    if (cfg_number(section, "Contrast", 7, 0, 15, &i) > 0) {
455  drv_BuE_CT_contrast(i);
456    }
457
458    /* set backlight */
459    if (cfg_number(section, "Backlight", 0, 0, 1, &i) > 0) {
460  drv_BuE_CT_backlight(i);
461    }
462
463
464    /* identify modules */
465
466    for (i = 0; i < 8; i++) {
467  char cmd[5] = ESC "K?Pn";
468  cmd[4] = (char) i;
469  drv_generic_serial_write(cmd, 5); /* query I/O port */
470  usleep(10000);
471  if ((len = drv_generic_serial_read(buffer, 4)) == 4) {
472      char *type = NULL;
473      if (i == 0) {
474    if (buffer[3] == 8) {
475        /* internal port */
476        type = "CT 20x4 internal port";
477    } else {
478        error("%s: internal error: port 0 type %d should be type 8", Name, buffer[3]);
479        continue;
480    }
481      } else {
482    switch (buffer[3]) {
483    case 1: /* Key Module */
484        type = "XM-KEY-2x4-LED";
485        break;
486    case 8: /* I/O Module */
487        type = "XM-IO8-T";
488        break;
489    case 9: /* I/O Module */
490        type = "XM-IO4-R";
491        break;
492    case 15/* nothing */
493        continue;
494    default:  /* unhandled */
495        type = NULL;
496        break;
497    }
498      }
499      if (type != NULL) {
500    info("%s: Port %d: %s", Name, i, type);
501      } else {
502    error("%s: internal error: port % unknown type %d", Name, i, cmd[3]);
503      }
504  } else {
505      error("%s: error fetching type of port %d", Name, i);
506  }
507    }
508
509    return 0;
510}
511
512
513static int drv_BuE_start(const char *section)
514{
515    int i, ret;
516    char *model;
517
518    model = cfg_get(section, "Model", NULL);
519    if (model == NULL && *model == '\0') {
520  error("%s: no '%s.Model' entry from %s", Name, section, cfg_source());
521  return -1;
522    }
523
524    for (i = 0; Models[i].name != NULL; i++) {
525  if (strcasecmp(Models[i].name, model) == 0)
526      break;
527    }
528
529    if (Models[i].name == NULL) {
530  error("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source());
531  return -1;
532    }
533
534    Model = i;
535    Protocol = Models[Model].protocol;
536
537    info("%s: using model '%s'", Name, Models[Model].name);
538
539    /* initialize global variables */
540    DROWS = Models[Model].rows;
541    DCOLS = Models[Model].cols;
542
543    ret = 0;
544    switch (Protocol) {
545    case 1:
546  ret = drv_BuE_MT_start(section);
547  break;
548    case 2:
549  ret = drv_BuE_CT_start(section);
550  break;
551    }
552    if (ret != 0) {
553  return ret;
554    }
555
556    drv_BuE_clear();
557
558    return 0;
559}
560
561
562/****************************************/
563/***            plugins               ***/
564/****************************************/
565
566static void plugin_contrast(RESULT * result, const int argc, RESULT * argv[])
567{
568    double contrast;
569
570    switch (argc) {
571    case 0:
572  contrast = drv_BuE_CT_contrast(-1);
573  SetResult(&result, R_NUMBER, &contrast);
574  break;
575    case 1:
576  contrast = drv_BuE_CT_contrast(R2N(argv[0]));
577  SetResult(&result, R_NUMBER, &contrast);
578  break;
579    default:
580  error("%s::contrast(): wrong number of parameters", Name);
581  SetResult(&result, R_STRING, "");
582    }
583}
584
585
586static void plugin_backlight(RESULT * result, const int argc, RESULT * argv[])
587{
588    double backlight;
589
590    switch (argc) {
591    case 0:
592  backlight = drv_BuE_CT_backlight(-1);
593  SetResult(&result, R_NUMBER, &backlight);
594  break;
595    case 1:
596  backlight = drv_BuE_CT_backlight(R2N(argv[0]));
597  SetResult(&result, R_NUMBER, &backlight);
598  break;
599    default:
600  error("%s::backlight(): wrong number of parameters", Name);
601  SetResult(&result, R_STRING, "");
602    }
603}
604
605
606static void plugin_gpo(RESULT * result, const int argc, RESULT * argv[])
607{
608    double gpo;
609
610    switch (argc) {
611    case 1:
612  gpo = drv_BuE_CT_gpo(R2N(argv[0]), -1);
613  SetResult(&result, R_NUMBER, &gpo);
614  break;
615    case 2:
616  gpo = drv_BuE_CT_gpo(R2N(argv[0]), R2N(argv[1]));
617  SetResult(&result, R_NUMBER, &gpo);
618  break;
619    default:
620  error("%s::gpo(): wrong number of parameters", Name);
621  SetResult(&result, R_STRING, "");
622    }
623}
624
625
626static void plugin_gpi(RESULT * result, RESULT * arg1)
627{
628    double gpi;
629
630    gpi = drv_BuE_CT_gpi(R2N(arg1));
631    SetResult(&result, R_NUMBER, &gpi);
632}
633
634
635static void plugin_adc(RESULT * result)
636{
637    double adc;
638
639    adc = drv_BuE_CT_adc();
640    SetResult(&result, R_NUMBER, &adc);
641}
642
643
644static void plugin_pwm(RESULT * result, const int argc, RESULT * argv[])
645{
646    double pwm;
647
648    switch (argc) {
649    case 0:
650  pwm = drv_BuE_CT_pwm(-1);
651  SetResult(&result, R_NUMBER, &pwm);
652  break;
653    case 1:
654  pwm = drv_BuE_CT_pwm(R2N(argv[0]));
655  SetResult(&result, R_NUMBER, &pwm);
656  break;
657    default:
658  error("%s::pwm(): wrong number of parameters", Name);
659  SetResult(&result, R_STRING, "");
660    }
661}
662
663
664/****************************************/
665/***        widget callbacks          ***/
666/****************************************/
667
668/* using drv_generic_text_draw(W) */
669/* using drv_generic_text_icon_draw(W) */
670/* using drv_generic_text_bar_draw(W) */
671
672
673/****************************************/
674/***        exported functions        ***/
675/****************************************/
676
677
678/* list models */
679int drv_BuE_list(void)
680{
681    int i;
682
683    for (i = 0; Models[i].name != NULL; i++) {
684  printf("%s ", Models[i].name);
685    }
686    return 0;
687}
688
689
690/* initialize driver & display */
691int drv_BuE_init(const char *section, const int quiet)
692{
693    WIDGET_CLASS wc;
694    int ret;
695
696    /* start display */
697    if ((ret = drv_BuE_start(section)) != 0) {
698  return ret;
699    }
700
701    /* display preferences */
702    XRES = 5;     /* pixel width of one char  */
703    YRES = 8;     /* pixel height of one char  */
704    CHARS = 8;      /* number of user-defineable characters */
705
706    /* real worker functions */
707    switch (Protocol) {
708    case 1:
709  CHAR0 = 0;    /* ASCII of first user-defineable char */
710  GOTO_COST = 6;    /* number of bytes a goto command requires */
711  drv_generic_text_real_write = drv_BuE_MT_write;
712  drv_generic_text_real_defchar = drv_BuE_MT_defchar;
713  break;
714    case 2:
715  CHAR0 = 128;    /* ASCII of first user-defineable char */
716  GOTO_COST = 6;    /* number of bytes a goto command requires */
717  drv_generic_text_real_write = drv_BuE_CT_write;
718  drv_generic_text_real_defchar = drv_BuE_CT_defchar;
719  break;
720    }
721
722    if (!quiet) {
723  char buffer[40];
724  qprintf(buffer, sizeof(buffer), "%s %s", Name, Models[Model].name);
725  if (drv_generic_text_greet(buffer, "www.bue.com")) {
726      sleep(3);
727      drv_BuE_clear();
728  }
729    }
730
731    /* initialize generic text driver */
732    if ((ret = drv_generic_text_init(section, Name)) != 0)
733  return ret;
734
735    /* initialize generic icon driver */
736    if ((ret = drv_generic_text_icon_init()) != 0)
737  return ret;
738
739    /* initialize generic bar driver */
740    if ((ret = drv_generic_text_bar_init(0)) != 0)
741  return ret;
742
743    /* add fixed chars to the bar driver */
744    drv_generic_text_bar_add_segment(0, 0, 255, 32);  /* ASCII  32 = blank */
745    drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */
746
747    /* register text widget */
748    wc = Widget_Text;
749    wc.draw = drv_generic_text_draw;
750    widget_register(&wc);
751
752    /* register icon widget */
753    wc = Widget_Icon;
754    wc.draw = drv_generic_text_icon_draw;
755    widget_register(&wc);
756
757    /* register bar widget */
758    wc = Widget_Bar;
759    wc.draw = drv_generic_text_bar_draw;
760    widget_register(&wc);
761
762    /* register plugins */
763    AddFunction("LCD::contrast", -1, plugin_contrast);
764    AddFunction("LCD::backlight", -1, plugin_backlight);
765    AddFunction("LCD::gpo", -1, plugin_gpo);
766    AddFunction("LCD::gpi", 1, plugin_gpi);
767    AddFunction("LCD::adc", 0, plugin_adc);
768    AddFunction("LCD::pwm", -1, plugin_pwm);
769
770    return 0;
771}
772
773
774/* close driver & display */
775int drv_BuE_quit(const int quiet)
776{
777
778    info("%s: shutting down.", Name);
779
780    drv_generic_text_quit();
781
782    /* clear display  */
783    drv_BuE_clear();
784
785    /* say goodbye... */
786    if (!quiet) {
787  drv_generic_text_greet("goodbye!", NULL);
788    }
789
790    drv_generic_serial_close();
791
792    return (0);
793}
794
795
796DRIVER drv_BeckmannEgle = {
797  name:Name,
798  list:drv_BuE_list,
799  init:drv_BuE_init,
800  quit:drv_BuE_quit,
801};
Note: See TracBrowser for help on using the browser.