Skip to content
Snippets Groups Projects
mensactrl.c 3.13 KiB
Newer Older
daniel's avatar
daniel committed
/*
daniel's avatar
daniel committed
 * Control Mensadisplay connected to the LCDIF
daniel's avatar
daniel committed
 *
daniel's avatar
daniel committed
 * (C) 2014 Daniel Willmann <daniel@totalueberwachung.de>
daniel's avatar
daniel committed

 * This program 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 3 of the License, or
 * (at your option) any later version.

 * This program 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, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <math.h>

#include <zmq.h>

daniel's avatar
daniel committed
#include "common.h"
daniel's avatar
daniel committed
struct mensa_fb {
daniel's avatar
daniel committed
	int x_res, y_res;
	int fd;
	uint8_t *inputfb;
	uint16_t *fbmem;
};

daniel's avatar
daniel committed
void encodeToFb(struct mensa_fb *mensafb) {
	int pix, row, idx;
	uint16_t pattern;

	for (row = 0; row < mensafb->y_res; row++) {
		for (pix=0; pix<mensafb->x_res; pix++) {
			idx = row*mensafb->x_res + pix;
			if (mensafb->inputfb[idx] > 0)
				pattern = 1;
			else
				pattern = 0;
			pattern |= row << 13;

			mensafb->fbmem[idx] = pattern;
daniel's avatar
daniel committed
void setPixel(struct mensa_fb *mensafb, int col, int row, uint8_t bright)
{
	int idx = row * mensafb->x_res + col;

	if (col < 0 || col >= mensafb->x_res)
		return;
	if (row < 0 || row >= mensafb->y_res)
		return;

	mensafb->inputfb[idx] = bright;
daniel's avatar
daniel committed
static struct mensa_fb *setup_fb(const char *devname, int x, int y)
daniel's avatar
daniel committed
{
daniel's avatar
daniel committed
	struct mensa_fb *mensafb = malloc(sizeof(struct mensa_fb));
daniel's avatar
daniel committed
	int i;

daniel's avatar
daniel committed
	mensafb->fd = open(devname, O_RDWR);
	if (mensafb->fd < 0) {
daniel's avatar
daniel committed
		perror("opening fb");
daniel's avatar
daniel committed
		free(mensafb);
daniel's avatar
daniel committed
		exit(1);
	}
daniel's avatar
daniel committed
	mensafb->inputfb = malloc(x*y*3);
	if (!mensafb->inputfb) {
		free(mensafb);
daniel's avatar
daniel committed
		exit(1);
	}
daniel's avatar
daniel committed
	memset(mensafb->inputfb, 0, x*y*3);
daniel's avatar
daniel committed
	mensafb->fbmem=mmap(NULL, x*5*24*2, PROT_READ|PROT_WRITE, MAP_SHARED, mensafb->fd, 0);
	if (mensafb->fbmem==NULL) {
daniel's avatar
daniel committed
		perror("mmap'ing fb");
daniel's avatar
daniel committed
		free(mensafb->inputfb);
		free(mensafb);
daniel's avatar
daniel committed
		exit(1);
	}
	for (i = 0; i < x*y*5*24; i++) {
		/* Init frame buffer bit clock. */
		if (i % 5 == 0)
daniel's avatar
daniel committed
			mensafb->fbmem[i] = 0xffff;
daniel's avatar
daniel committed
		if (i % 5 == 4)
daniel's avatar
daniel committed
			mensafb->fbmem[i] = 0x00;
daniel's avatar
daniel committed
	mensafb->x_res = x;
	mensafb->y_res = y;
daniel's avatar
daniel committed
	return mensafb;
daniel's avatar
daniel committed
}

int main(int argc, char *argv[]) {
daniel's avatar
daniel committed
	struct mensa_fb *mensafb;
daniel's avatar
daniel committed
	void *context = zmq_ctx_new ();
	void *subscriber = zmq_socket (context, ZMQ_SUB);
	int rc;

	if (argc != 2)
		exit(1);

	rc = zmq_connect (subscriber, "tcp://10.0.0.1:5556");
	if (rc < 0)
		perror("zmq_connect");

	rc = zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE,
			NULL, 0);
	if (rc < 0)
		perror("zmq_connect");

daniel's avatar
daniel committed
	mensafb = setup_fb(argv[1], 40, 7);
daniel's avatar
daniel committed

	while (1) {
		struct pixel pix;
		zmq_recv(subscriber, &pix, sizeof(pix), 0);
daniel's avatar
daniel committed
		printf("(%u, %u): bright=%02x\n", pix.x, pix.y,  pix.bright);
		setPixel(mensafb, pix.x, pix.y, pix.bright);
		encodeToFb(mensafb);
daniel's avatar
daniel committed
	}

	return 0;
}