brewco/slcd.c

changeset 434
eb724767860d
child 443
6b80a37fdf8d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/brewco/slcd.c	Wed Nov 25 22:49:35 2015 +0100
@@ -0,0 +1,210 @@
+/*****************************************************************************
+ * 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 "brewco.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;
+extern sys_config       Config;
+
+
+
+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 index, unsigned char data[8])
+//{
+//}
+
+
+
+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, ...)
+{
+    char	buf[81 * sizeof(char)];
+    va_list	va_ptr;
+
+    va_start(va_ptr, message);
+    vsnprintf(buf, (Config.lcd_cols + 1) * sizeof(char), message, va_ptr);
+    va_end(va_ptr);
+    slcdPuts(fd, buf);
+}
+
+
+
+/*
+ * 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