thermferm/slcd.c

Wed, 12 May 2021 21:17:59 +0200

author
Michiel Broek
date
Wed, 12 May 2021 21:17:59 +0200
changeset 611
732d482f47c8
parent 571
6f8eda55ec2c
permissions
-rw-r--r--

Improved logging if wiringpi failed.

/*****************************************************************************
 * Copyright (C) 2015-2019
 *   
 * Michiel Broek <mbroek at mbse dot eu>
 *
 * This file is part of the mbsePi-apps
 *
 * This 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.
 *
 * mbsePi-apps 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 ThermFerm; see the file COPYING.  If not, write to the Free
 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *****************************************************************************/

#include "thermferm.h"
#include "slcd.h"
#include "futil.h"
#include "xutil.h"



struct sockaddr_in	sendaddr;	/* Server send socket		*/
int			sock = -1;
uint16_t		keys = 0x0000;
uint16_t		leds = 0x0400;	/* LED's, buzzer, LCD backlight	*/
uint16_t		oleds = 0x0400;



void putLCDsocket(int fd, uint16_t data)
{
    socklen_t	slen = 0;
    uint16_t	rdat;

    if (sock == -1)
	return;

    if (sendto(sock, &data, sizeof(uint16_t), 0, (struct sockaddr *)&sendaddr, sizeof(sendaddr)) != sizeof(uint16_t)) {
	syslog(LOG_NOTICE, "Socket %d send failed, closing socket: %s", sock, strerror(errno));
	if (shutdown(sock, SHUT_RDWR)) {
	    syslog(LOG_NOTICE, "Can't shutdown socket: %s", strerror(errno));
	}
	sock = -1;
    } else {
	if (recvfrom(sock, &rdat, sizeof(uint16_t), 0, (struct sockaddr *) &sendaddr, &slen) != sizeof(uint16_t)) {
	    syslog(LOG_NOTICE, "Socket %d recvfrom failed, closing socket: %s", sock, strerror(errno));
	    if (shutdown(sock, SHUT_RDWR)) {
		syslog(LOG_NOTICE, "Can't shutdown socket: %s", strerror(errno));
	    }
	    sock = -1;
	} else {
	    if ((rdat & SLCD_MKEYS) == SLCD_KEYS) {
		keys = rdat & 0x00ff;
	    }

	}
    }
}



void slcdDummy(int fd)
{
    putLCDsocket(fd, SLCD_NULL);
}



void slcdLEDs(int fd)
{
    if (leds != oleds)
	putLCDsocket(fd, leds);
    oleds = leds;
}



void slcdClear(int fd)
{
    putLCDsocket(fd, SLCD_CLEAR);
    putLCDsocket(fd, SLCD_HOME);
}



void slcdPosition(int fd, int x, int y)
{
    uint16_t	data = SLCD_DGRAM;

    data += (x & 0x1f) + ((y & 0x03) << 5);
    putLCDsocket(fd, data);
}



void slcdCharDef(int fd, int idx, unsigned char data[8])
{
    int		i;

    putLCDsocket(fd, SLCD_CGRAM | ((idx & 7) << 3));
    for (i = 0 ; i < 8 ; ++i)
	putLCDsocket(fd, data[i]);
}



void slcdPutchar(int fd, unsigned char c)
{
    uint16_t	data = SLCD_DATA;

    data += c & 0x0ff;
    putLCDsocket(fd, data);
}



void slcdPuts(int fd, const char *string)
{
    while (*string)
	slcdPutchar(fd, *string++);
}



/*
 * Try to setup a udp connection to 127.0.0.1 so we duplicate the panel
 * display and keys of the real panel. This should fail on a production
 * system because there should no brewpanel program be running. If it
 * succeeds, all io will be duplicated over the network.
 */
int slcdInit(int fd, int cols, int rows)
{
    if ((sock = socket(AF_INET, SOCK_DGRAM /*| SOCK_NONBLOCK */, 0)) < 0) {
	syslog(LOG_NOTICE, "slcdInit() can't create socket: %s", strerror(errno));
	return -1;
    }

    /*
     * Setup address structure for the server socket.
     */
    memset(&sendaddr, 0, sizeof(struct sockaddr_in));
    sendaddr.sin_family = AF_INET;
    sendaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    sendaddr.sin_port = htons(SEND_PORT);

    if (connect(sock, (struct sockaddr *)&sendaddr, sizeof(sendaddr)) < 0) {
	close(sock);
	sock = -1;
	syslog(LOG_NOTICE, "slcdInit() can't bind sendsock %s", strerror(errno));
	return -1;
    }

    syslog(LOG_NOTICE, "slcdInit() socket %d to brewpanel is active", sock);
    return 0;
}

mercurial