1
/* $Id: drv_LEDMatrix.c 975 2009-01-18 11:16:20Z michael $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/drv_LEDMatrix.c $
4
* LED matrix driver for LCD4Linux
5
* (see http://www.harbaum.org/till/ledmatrix for hardware)
7
* Copyright (C) 2006 Till Harbaum <till@harbaum.org>
9
* This file is part of LCD4Linux.
11
* LCD4Linux is free software; you can redistribute it and/or modify
12
* it under the terms of the GNU General Public License as published by
13
* the Free Software Foundation; either version 2, or (at your option)
16
* LCD4Linux is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU General Public License for more details.
21
* You should have received a copy of the GNU General Public License
22
* along with this program; if not, write to the Free Software
23
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31
* struct DRIVER drv_LEDMatrix
51
/* include network specific headers */
52
#include <arpa/inet.h>
53
#include <sys/socket.h>
54
#include <netinet/in.h>
55
#include <sys/select.h>
63
#include "widget_text.h"
64
#include "widget_icon.h"
65
#include "widget_bar.h"
67
#include "drv_generic_graphic.h"
69
/* display command bytes */
70
#define DSP_CMD_ECHO 0
72
#define DSP_CMD_IMAGE 2
75
#define DSP_CMD_BEEP 5
77
#define DSP_DEFAULT_PORT 4711
79
#define DSP_MEM (80 * 32 * 2 / 8)
81
#define DEFAULT_X_OFFSET 1 /* with a font width of 6 */
83
static char Name[] = "LEDMatrix";
84
static char *IPAddress = NULL;
86
static struct sockaddr_in dsp_addr;
87
static unsigned char tx_buffer[DSP_MEM + 1];
88
static int port = DSP_DEFAULT_PORT;
90
static void drv_LEDMatrix_blit(const int row, const int col, const int height, const int width)
95
unsigned char reply[256];
96
struct sockaddr_in cli_addr;
101
for (r = row; r < row + height; r++) {
102
for (c = col; c < col + width; c++) {
103
/* LEDMATRIX supports three colors: 10b == green, 01b == red, 11b == amber */
105
unsigned char color = 0;
106
RGBA p = drv_generic_graphic_rgb(r, c);
111
/* ignore blue ... */
113
tx_buffer[1 + 20 * r + c / 4] &= ~(0xc0 >> (2 * (c & 3)));
114
tx_buffer[1 + 20 * r + c / 4] |= color >> (2 * (c & 3));
118
/* scan entire display */
119
tx_buffer[0] = DSP_CMD_IMAGE;
123
if ((sendto(sock, tx_buffer, DSP_MEM + 1, 0, (struct sockaddr *) &dsp_addr, sizeof(dsp_addr))) != DSP_MEM + 1)
124
error("%s: sendto error on socket", Name);
126
/* now wait for reply */
133
/* wait 1 sec for ack */
134
if ((i = select(FD_SETSIZE, &rfds, NULL, NULL, &tv)) < 0) {
135
info("%s: Select error: %s", Name, strerror(errno));
138
if (FD_ISSET(sock, &rfds)) {
140
fromlen = sizeof(dsp_addr);
141
i = recvfrom(sock, reply, sizeof(reply), 0, (struct sockaddr *) &cli_addr, &fromlen);
143
info("%s: Receive error: %s", Name, strerror(errno));
145
if ((i == 2) && (reply[0] == DSP_CMD_ACK) && (reply[1] == DSP_CMD_IMAGE)) {
147
} else if ((i > 1) && (reply[0] == DSP_CMD_IR)) {
148
/* maybe used later: */
149
/* ir_receive(reply+1, i-1); */
151
info("%s: Unexpected reply message", Name);
156
} while ((!ack) && (timeout > 0));
159
error("%s: display reply timeout", Name);
163
static int drv_LEDMatrix_start(const char *section)
166
struct sockaddr_in cli_addr;
170
IPAddress = cfg_get(section, "IPAddress", NULL);
171
if (IPAddress == NULL || *IPAddress == '\0') {
172
error("%s: no '%s.IPAddress' entry from %s", Name, section, cfg_source());
176
if (cfg_number(section, "Port", 0, 0, 65535, &val) > 0) {
177
info("%s: port set to %d", Name, val);
180
info("%s: using default port %d", Name, port);
183
/* display size is hard coded */
187
if (sscanf(s = cfg_get(section, "font", "6x8"), "%dx%d", &XRES, &YRES) != 2 || XRES < 1 || YRES < 1) {
188
error("%s: bad %s.Font '%s' from %s", Name, section, s, cfg_source());
194
/* contact display */
195
info("%s: contacting %s", Name, IPAddress);
197
/* try to resolve as a hostname */
198
if ((hp = gethostbyname(IPAddress)) == NULL) {
199
error("%s: unable to resolve hostname %s: %s", Name, IPAddress, strerror(errno));
203
/* open datagram socket */
204
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
205
error("%s: could not create socket: %s", Name, strerror(errno));
209
memset((char *) &dsp_addr, 0, sizeof(dsp_addr));
210
dsp_addr.sin_family = AF_INET;
211
dsp_addr.sin_addr.s_addr = *(int *) hp->h_addr;
212
dsp_addr.sin_port = htons(port);
214
cli_addr.sin_family = AF_INET;
215
cli_addr.sin_addr.s_addr = htons(INADDR_ANY);
216
cli_addr.sin_port = htons(port);
218
if (bind(sock, (struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0) {
219
error("%s: can't bind local address: %s", Name, strerror(errno));
223
memset(tx_buffer, 0, sizeof(tx_buffer));
230
/****************************************/
232
/****************************************/
234
/* none at the moment... */
237
/****************************************/
238
/*** widget callbacks ***/
239
/****************************************/
242
/* using drv_generic_graphic_draw(W) */
243
/* using drv_generic_graphic_icon_draw(W) */
244
/* using drv_generic_graphic_bar_draw(W) */
247
/****************************************/
248
/*** exported functions ***/
249
/****************************************/
253
int drv_LEDMatrix_list(void)
255
printf("LEDMATRIX by Till Harbaum");
260
/* initialize driver & display */
261
int drv_LEDMatrix_init(const char *section, const __attribute__ ((unused))
267
/* real worker functions */
268
drv_generic_graphic_real_blit = drv_LEDMatrix_blit;
271
if ((ret = drv_LEDMatrix_start(section)) != 0)
274
/* initialize generic graphic driver */
275
if ((ret = drv_generic_graphic_init(section, Name)) != 0)
278
/* register text widget */
280
wc.draw = drv_generic_graphic_draw;
281
widget_register(&wc);
283
/* register icon widget */
285
wc.draw = drv_generic_graphic_icon_draw;
286
widget_register(&wc);
288
/* register bar widget */
290
wc.draw = drv_generic_graphic_bar_draw;
291
widget_register(&wc);
293
/* register plugins */
294
/* none at the moment... */
301
/* close driver & display */
302
int drv_LEDMatrix_quit(const __attribute__ ((unused))
306
info("%s: shutting down.", Name);
307
drv_generic_graphic_quit();
316
DRIVER drv_LEDMatrix = {
318
.list = drv_LEDMatrix_list,
319
.init = drv_LEDMatrix_init,
320
.quit = drv_LEDMatrix_quit,