thermferm/slcd.c

Fri, 11 Mar 2016 20:27:02 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 11 Mar 2016 20:27:02 +0100
changeset 492
750f2468dec5
parent 485
08543a9ca288
child 561
fcfc3dbe85fa
permissions
-rw-r--r--

Changed PID code. PID parameters are now stored 3 digits instead of 2 behind the decimal point. Prevent extreme heating or cooling in Beer mode. Heat and Cool lockdown now allows the lagest value to win instead of zero them both. PID output treshold from 2% to 50%.

/*****************************************************************************
 * Copyright (C) 2015
 *   
 * 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;

extern int		debug;


void putLCDsocket(int fd, uint16_t data)
{
    socklen_t	slen;
    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) {
		if (((rdat & 0x00ff) != keys) && debug)
		    fprintf(stdout, "received keys %04x was %04x\n", rdat & 0x00ff, keys);
		keys = rdat & 0x00ff;
	    } else {
	        if (debug)
		    fprintf(stdout, "received %04x\n", rdat);
	    }

	}
    }
}



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



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


//void slcdHome(int fd)
//{
//}



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



//void slcdDisplay(int fd, int state)
//{
//}



//void slcdCursor(int fd, int state)
//{
//}



//void slcdCursorBlink(int fd, int state)
//{
//}



//void slcdSendCommand(int fd, unsigned char command)
//{
//}



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++);
}



//void slcdPrintf(int fd, const char *message, ...)
//{
//}



/*
 * 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