aboutsummaryrefslogtreecommitdiffstats
path: root/README.MatrixOrbital
blob: a8e889cb24f3a5e7536019a20f90fa0d216bf6b7 (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
#
# $Id: README.MatrixOrbital,v 1.2 2000/03/22 07:33:50 reinelt Exp $
#

This is the README file for the MatrixOrbital display driver for lcd4linux

This driver supports the serial interface alphanumeric display modules by
Matrix Orbital Corporation (http://www.matrixorbital.com).

I could only test it with the LCD2041 model, but I think every other (LCD) model 
should work. These displays are supported:

  LCD0821: 2 lines by  8 characters
  LCD1621: 2 lines by 16 characters
  LCD2021: 2 lines by 20 characters
  LCD2041: 4 lines by 20 characters (tested)
  LCD4021: 2 lines by 40 characters

I could not test the vacuum fluorescent display models, but I think they should work, too.
There are no entries for this models in the driver table (at the bottom of MatrixOrbital.c),
but they could be easily added.

The displays come with an RS-232 and an I2C interface. The driver supports the RS-232 interface
only (because I have no idea how to find the I2C bus on my motherboard). 

Power can be applied either via an external DC power supply, a modified floppy power connector
(be aware that you can destroy your display if you get the pins wrong!) or via the RI (ring) 
signal of the RS-232 port. I choosed the latter, and modified a serial card so that it supplies 
+5V from the ISA bus to this pin (again, be aware that this is dangerous if you connect any other 
serial device to this modified port).

The driver supports vertical, horizontal and split bars (two independent bars in one line),
all bar types can be used simultanously. As the displays only have 8 user-defined characters,
the needed characters to display all the bars must be reduced to 8. This is done by replacing
characters with similar ones. To reduce flicker, a character which is displayed at the moment,
will not be redefined, even if it's not used in this run. Only if the character compaction 
fails, this characters will be redefined, too.

The displays have a GPO (general purpose output), where you can connect a LED or something. 
The driver supports controlling this GPO, but this function is unused by now.


Configuration:

The driver needs/supports the following entries in lcd4linux.conf:

Display: a valid Matrix Orbital Display name (e.g. "LCD2041")
Port: serial device the display is attached to (e.g. /dev/ttyS2)
Speed: the baud rate from the display (configured via jumpers) must match 
       this value. Possible values are 1200, 2400, 9600 and 19200
Contrast: sets the LCD display contrast to a level between 0 (light) 
          and 256 (dark). Default value: 160
'n306' href='#n306'>306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
/* $Id$
 * $URL$
 *
 * LED matrix driver for LCD4Linux 
 * (see http://www.harbaum.org/till/ledmatrix for hardware)
 *
 * Copyright (C) 2006 Till Harbaum <till@harbaum.org>
 *
 * 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.
 *
 */

/* 
 *
 * exported fuctions:
 *
 * struct DRIVER drv_LEDMatrix
 *
 */

/*
 * Options:
 * IPAddress
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/time.h>

/* include network specific headers */
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <netdb.h>

#include "debug.h"
#include "cfg.h"
#include "qprintf.h"
#include "plugin.h"
#include "widget.h"
#include "widget_text.h"
#include "widget_icon.h"
#include "widget_bar.h"
#include "drv.h"
#include "drv_generic_graphic.h"

/* display command bytes */
#define DSP_CMD_ECHO  0
#define DSP_CMD_NOP   1
#define DSP_CMD_IMAGE 2
#define DSP_CMD_ACK   3
#define DSP_CMD_IR    4
#define DSP_CMD_BEEP  5

#define DSP_DEFAULT_PORT 4711

#define DSP_MEM (80 * 32 * 2 / 8)

#define DEFAULT_X_OFFSET   1	/* with a font width of 6 */

static char Name[] = "LEDMatrix";
static char *IPAddress = NULL;
static int sock = -1;
static struct sockaddr_in dsp_addr;
static unsigned char tx_buffer[DSP_MEM + 1];
static int port = DSP_DEFAULT_PORT;

static void drv_LEDMatrix_blit(const int row, const int col, const int height, const int width)
{
    int r, c, i;
    fd_set rfds;
    struct timeval tv;
    unsigned char reply[256];
    struct sockaddr_in cli_addr;
    socklen_t fromlen;
    int ack = 0;
    int timeout = 10;

    for (r = row; r < row + height; r++) {
	for (c = col; c < col + width; c++) {
	    /* LEDMATRIX supports three colors: 10b == green, 01b == red, 11b == amber */

	    unsigned char color = 0;
	    RGBA p = drv_generic_graphic_rgb(r, c);
	    if (p.G >= 128)
		color |= 0x80;
	    if (p.R >= 128)
		color |= 0x40;
	    /* ignore blue ... */

	    tx_buffer[1 + 20 * r + c / 4] &= ~(0xc0 >> (2 * (c & 3)));
	    tx_buffer[1 + 20 * r + c / 4] |= color >> (2 * (c & 3));
	}
    }

    /* scan entire display */
    tx_buffer[0] = DSP_CMD_IMAGE;

    do {

	if ((sendto(sock, tx_buffer, DSP_MEM + 1, 0, (struct sockaddr *) &dsp_addr, sizeof(dsp_addr))) != DSP_MEM + 1)
	    error("%s: sendto error on socket", Name);

	/* now wait for reply */

	FD_ZERO(&rfds);
	FD_SET(sock, &rfds);
	tv.tv_sec = 0;
	tv.tv_usec = 100000;

	/* wait 1 sec for ack */
	if ((i = select(FD_SETSIZE, &rfds, NULL, NULL, &tv)) < 0) {
	    info("%s: Select error: %s", Name, strerror(errno));
	}

	if (FD_ISSET(sock, &rfds)) {
	    /* wait for ack */
	    fromlen = sizeof(dsp_addr);
	    i = recvfrom(sock, reply, sizeof(reply), 0, (struct sockaddr *) &cli_addr, &fromlen);
	    if (i < 0) {
		info("%s: Receive error: %s", Name, strerror(errno));
	    } else {
		if ((i == 2) && (reply[0] == DSP_CMD_ACK) && (reply[1] == DSP_CMD_IMAGE)) {
		    ack = 1;
		} else if ((i > 1) && (reply[0] == DSP_CMD_IR)) {
		    /* maybe used later: */
		    /* ir_receive(reply+1, i-1); */
		} else {
		    info("%s: Unexpected reply message", Name);
		}
	    }
	}
	timeout--;
    } while ((!ack) && (timeout > 0));

    if (timeout == 0) {
	error("%s: display reply timeout", Name);
    }
}

static int drv_LEDMatrix_start(const char *section)
{
    char *s;
    struct sockaddr_in cli_addr;
    struct hostent *hp;
    int val;

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

    if (cfg_number(section, "Port", 0, 0, 65535, &val) > 0) {
	info("%s: port set to %d", Name, val);
	port = val;
    } else {
	info("%s: using default port %d", Name, port);
    }

    /* display size is hard coded */
    DCOLS = 80;
    DROWS = 32;

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

    /* contact display */
    info("%s: contacting %s", Name, IPAddress);

    /* try to resolve as a hostname */
    if ((hp = gethostbyname(IPAddress)) == NULL) {
	error("%s: unable to resolve hostname %s: %s", Name, IPAddress, strerror(errno));
	return -1;
    }

    /* open datagram socket */
    if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
	error("%s: could not create socket: %s", Name, strerror(errno));
	return -1;
    }

    memset((char *) &dsp_addr, 0, sizeof(dsp_addr));
    dsp_addr.sin_family = AF_INET;
    dsp_addr.sin_addr.s_addr = *(int *) hp->h_addr;
    dsp_addr.sin_port = htons(port);

    cli_addr.sin_family = AF_INET;
    cli_addr.sin_addr.s_addr = htons(INADDR_ANY);
    cli_addr.sin_port = htons(port);

    if (bind(sock, (struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0) {
	error("%s: can't bind local address: %s", Name, strerror(errno));
	return -1;
    }

    memset(tx_buffer, 0, sizeof(tx_buffer));

    return 0;
}



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

/* none at the moment... */


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


/* using drv_generic_graphic_draw(W) */
/* using drv_generic_graphic_icon_draw(W) */
/* using drv_generic_graphic_bar_draw(W) */


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


/* list models */
int drv_LEDMatrix_list(void)
{
    printf("LEDMATRIX by Till Harbaum");
    return 0;
}


/* initialize driver & display */
int drv_LEDMatrix_init(const char *section, const __attribute__ ((unused))
		       int quiet)
{
    WIDGET_CLASS wc;
    int ret;

    /* real worker functions */
    drv_generic_graphic_real_blit = drv_LEDMatrix_blit;

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

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

    /* register text widget */
    wc = Widget_Text;
    wc.draw = drv_generic_graphic_draw;
    widget_register(&wc);

    /* register icon widget */
    wc = Widget_Icon;
    wc.draw = drv_generic_graphic_icon_draw;
    widget_register(&wc);

    /* register bar widget */
    wc = Widget_Bar;
    wc.draw = drv_generic_graphic_bar_draw;
    widget_register(&wc);

    /* register plugins */
    /* none at the moment... */


    return 0;
}


/* close driver & display */
int drv_LEDMatrix_quit(const __attribute__ ((unused))
		       int quiet)
{

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

    if (sock != -1)
	close(sock);

    return (0);
}


DRIVER drv_LEDMatrix = {
    .name = Name,
    .list = drv_LEDMatrix_list,
    .init = drv_LEDMatrix_init,
    .quit = drv_LEDMatrix_quit,
};