aboutsummaryrefslogtreecommitdiffstats
path: root/drv_LEDMatrix.c
blob: f4b31a880cefbd6ec67b8131a767091b53caa747 (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
/* $Id: drv_LEDMatrix.c 975 2009-01-18 11:16:20Z michael $
 * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/drv_LEDMatrix.c $
 *
 * 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,
};