The brewpanel works, but still needs a lot more

Sat, 14 Nov 2015 23:40:51 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 14 Nov 2015 23:40:51 +0100
changeset 422
13502d0dda65
parent 421
0258107a9e72
child 423
02653162b03b

The brewpanel works, but still needs a lot more

brewpanel/dlgBrew.c file | annotate | diff | comparison | revisions
brewpanel/sdlgui.c file | annotate | diff | comparison | revisions
brewpanel/sockio.c file | annotate | diff | comparison | revisions
thermferm/Makefile file | annotate | diff | comparison | revisions
thermferm/lcd-buffer.c file | annotate | diff | comparison | revisions
thermferm/panel.c file | annotate | diff | comparison | revisions
thermferm/panel.h file | annotate | diff | comparison | revisions
thermferm/slcd.c file | annotate | diff | comparison | revisions
thermferm/slcd.h file | annotate | diff | comparison | revisions
thermferm/thermferm.c file | annotate | diff | comparison | revisions
--- a/brewpanel/dlgBrew.c	Sat Nov 14 17:00:37 2015 +0100
+++ b/brewpanel/dlgBrew.c	Sat Nov 14 23:40:51 2015 +0100
@@ -34,6 +34,7 @@
 
 extern SDL_Surface	*PAN_surface;
 extern int		my_shutdown;
+extern uint16_t		keys;
 
 
 #define	MAINDLG_B1	 2
@@ -106,13 +107,29 @@
 	fprintf(stdout, "SDLGui_DoDialog retbut=%d\n", retbut);
 
 	switch (retbut) {
-	    case MAINDLG_B1:	fprintf(stdout, "Button 1\n");
+	    case MAINDLG_B1 + 1000:
+		    		keys |= 0x0008;
+				break;
+	    case MAINDLG_B1:	
+				keys &= ~0x0008;
 		    		break;
-	    case MAINDLG_B2:	fprintf(stdout, "Button 2\n");
+	    case MAINDLG_B2 + 1000:
+				keys |= 0x0004;
+				break;
+	    case MAINDLG_B2:	
+				keys &= ~0x0004;
 				break;
-	    case MAINDLG_B3:	fprintf(stdout, "Button 3\n");
+	    case MAINDLG_B3 + 1000:
+				keys |= 0x0002;
+				break;
+	    case MAINDLG_B3:
+				keys &= ~0x0002;
 				break;
-	    case MAINDLG_B4:	fprintf(stdout, "Button 4\n");
+	    case MAINDLG_B4 + 1000:
+				keys |= 0x0001;
+				break;
+	    case MAINDLG_B4:
+				keys &= ~0x0001;
 				break;
 	    case MAINDLG_QUIT:	my_shutdown = TRUE;
 				break;
--- a/brewpanel/sdlgui.c	Sat Nov 14 17:00:37 2015 +0100
+++ b/brewpanel/sdlgui.c	Sat Nov 14 23:40:51 2015 +0100
@@ -554,24 +554,6 @@
 
 
 /*
- * Search a button with a special flag (e.g. SG_DEFAULT or SG_CANCEL).
- */
-static int SDLGui_SearchFlaggedButton(const SGOBJ *dlg, int flag)
-{
-    int i = 0;
-
-    while (dlg[i].type != -1) {
-	if (dlg[i].flags & flag)
-	    return i;
-	i++;
-    }
-
-    return 0;
-}
-
-
-
-/*
  * Show dialog.
  */
 int SDLGui_DoDialogInit(SGOBJ *dlg)
@@ -617,26 +599,15 @@
  */
 int SDLGui_DoDialogLoop(SGOBJ *dlg)
 {
-    int         obj = 0, oldbutton = 0, retbutton = 0, b, i, j;
+    int         obj = 0, retbutton = 0;
     SDL_Event   sdlEvent;
 
-    /* 
-     * Is the left mouse button still pressed? Yes -> Handle TOUCHEXIT objects here
-     */
-    SDL_PumpEvents();
-    b = SDL_GetMouseState(&i, &j);
-    obj = SDLGui_FindObj(dlg, i, j);
-    if (obj > 0 && (dlg[obj].flags & SG_TOUCHEXIT) ) {
-	oldbutton = obj;
-	if (b & SDL_BUTTON(1)) {
-	    dlg[obj].state |= SG_SELECTED;
-	    retbutton = obj;
-	}
-    }
-
     /* The main loop */
     while (retbutton == 0 && !my_shutdown) {
 
+	/*
+	 * Poll network for data
+	 */
 	socket_recv();
 
 	if (SDL_PollEvent(&sdlEvent) == 1) { /* Wait for events */
@@ -657,11 +628,7 @@
 					dlg[obj].state |= SG_SELECTED;
 					SDLGui_DrawButton(dlg, obj);
 					SDL_UpdateRect(pSdlGuiScrn, dlg[0].x + dlg[obj].x - 2, dlg[0].y + dlg[obj].y - 2, dlg[obj].w + 4, dlg[obj].h + 4);
-					oldbutton=obj;
-				    }
-				    if ( dlg[obj].flags & SG_TOUCHEXIT ) {
-					dlg[obj].state |= SG_SELECTED;
-					retbutton = obj;
+					retbutton = obj + 1000;
 				    }
 				}
 				break;
@@ -674,33 +641,19 @@
 				/* It was the left button: Find the object under the mouse cursor */
 				obj = SDLGui_FindObj(dlg, sdlEvent.button.x, sdlEvent.button.y);
 				if (obj > 0) {
-				    switch (dlg[obj].type) {
-					case SGBUTTON:
-						if (oldbutton==obj)
-						    retbutton=obj;
-						break;
+				    if (dlg[obj].type == SGBUTTON) {
+					dlg[obj].state &= ~SG_SELECTED;
+					SDLGui_DrawButton(dlg, obj);
+					SDL_UpdateRect(pSdlGuiScrn, dlg[0].x + dlg[obj].x - 2, dlg[0].y + dlg[obj].y - 2, dlg[obj].w + 4, dlg[obj].h + 4);
+					retbutton = obj;
 				    }
 				}
-				if (oldbutton > 0) {
-				    dlg[oldbutton].state &= ~SG_SELECTED;
-				    SDLGui_DrawButton(dlg, oldbutton);
-				    SDL_UpdateRect(pSdlGuiScrn, dlg[0].x+dlg[oldbutton].x-2, dlg[0].y+dlg[oldbutton].y-2, dlg[oldbutton].w+4, dlg[oldbutton].h+4);
-				    oldbutton = 0;
-				}
-				if (obj >= 0 && (dlg[obj].flags & SG_EXIT)) {
-				    retbutton = obj;
-				}
 				break;
 
 		 case SDL_MOUSEMOTION:
 				break;
 
 		 case SDL_KEYDOWN:                     /* Key pressed */
-				if (sdlEvent.key.keysym.sym == SDLK_RETURN || sdlEvent.key.keysym.sym == SDLK_KP_ENTER) {
-				    retbutton = SDLGui_SearchFlaggedButton(dlg, SG_DEFAULT);
-				} else if (sdlEvent.key.keysym.sym == SDLK_ESCAPE) {
-				    retbutton = SDLGui_SearchFlaggedButton(dlg, SG_CANCEL);
-				}
 				break;
 	    }
 	} else {
--- a/brewpanel/sockio.c	Sat Nov 14 17:00:37 2015 +0100
+++ b/brewpanel/sockio.c	Sat Nov 14 23:40:51 2015 +0100
@@ -43,9 +43,9 @@
 #define SLCD_MDEV       0xe000
 
 
-int			sock = -1;	/* Unix datagram socket		*/
-struct sockaddr_in	servaddr;	/* Server socket address	*/
-
+int			sock = -1;		/* Unix datagram socket		*/
+struct sockaddr_in	servaddr;		/* Server socket address	*/
+uint16_t		keys = SLCD_KEYS;	/* Pressed keys bits		*/
 
 
 void socket_recv(void)
@@ -54,14 +54,14 @@
     struct sockaddr_in	clntaddr;
     socklen_t		clntlen = sizeof(clntaddr);
     ssize_t		recv_len;
-    int			fd, my_error;
+    int			fd = 0, my_error;
 
     recv_len = recvfrom(sock, &data, sizeof(uint16_t), MSG_DONTWAIT, (struct sockaddr *)&clntaddr, &clntlen);
     if (recv_len == sizeof(uint16_t)) {
 	/*
 	 * Get device from data
 	 */
-	fd = data & SLCD_MDEV >> 13;
+	fd = (data & SLCD_MDEV) >> 13;
 	if (fd)
 	    fprintf(stdout, "Device %d ", fd);
 	if ((data & SLCD_MCLEAR) == SLCD_CLEAR) {
@@ -73,9 +73,16 @@
 	    slcdPosition(fd, data & 0x001f, (data & 0x0060) >> 5);
 	} else if ((data & 0xfe00) == SLCD_DATA) {
 	    slcdPutchar(fd, data & 0x00ff);
-	} else {
+	} else if (data != 0x0000) {
 	    fprintf(stdout, "socket_recv got %04x\n", data);
 	}
+
+	/*
+	 * Reply with the current keys state
+	 */
+	if (sendto(sock, &keys, sizeof(uint16_t), MSG_DONTWAIT, (struct sockaddr *) &clntaddr, clntlen) == -1) {
+	    fprintf(stdout, "sendto error: %s\n", strerror(errno));
+	}
     } else if (recv_len < 0) {
 	my_error = errno;
 	if (my_error != EAGAIN) {
--- a/thermferm/Makefile	Sat Nov 14 17:00:37 2015 +0100
+++ b/thermferm/Makefile	Sat Nov 14 23:40:51 2015 +0100
@@ -55,8 +55,8 @@
 # DO NOT DELETE THIS LINE - MAKE DEPEND RELIES ON IT
 # Dependencies generated by make depend
 rc-switch.o: thermferm.h xutil.h rc-switch.h
-slcd.o: thermferm.h slcd.h
-panel.o: thermferm.h lcd-pcf8574.h panel.h
+slcd.o: thermferm.h slcd.h futil.h xutil.h
+panel.o: thermferm.h lcd-pcf8574.h slcd.h panel.h
 devices.o: thermferm.h devices.h rc-switch.h panel.h xutil.h
 lcd-buffer.o: thermferm.h lcd-buffer.h lcd-pcf8574.h slcd.h panel.h
 futil.o: thermferm.h futil.h
--- a/thermferm/lcd-buffer.c	Sat Nov 14 17:00:37 2015 +0100
+++ b/thermferm/lcd-buffer.c	Sat Nov 14 23:40:51 2015 +0100
@@ -149,26 +149,41 @@
 	return;
     }
 
-#ifdef HAVE_WIRINGPI_H
-    lcdPosition(lcdHandle, 0, r);
-    lcdPuts(lcdHandle, tmp->row);
-#endif
-    slcdPosition(slcdHandle, 0, r);
-    slcdPuts(slcdHandle, tmp->row);
-
-    r++;
-    if (r < Config.lcd_rows) {
-	if (tmp->next != NULL)
-	    tmp = tmp->next;
-	else
-	    tmp = my_lcd_rows;
+    for (r = 0; r < Config.lcd_rows; r++) {
 #ifdef HAVE_WIRINGPI_H
 	lcdPosition(lcdHandle, 0, r);
 	lcdPuts(lcdHandle, tmp->row);
 #endif
 	slcdPosition(slcdHandle, 0, r);
 	slcdPuts(slcdHandle, tmp->row);
+
+	if (tmp->next != NULL)
+	    tmp = tmp->next;
+	else
+	    tmp = my_lcd_rows;
     }
+
+#ifdef HAVE_WIRINGPI_H
+//    lcdPosition(lcdHandle, 0, r);
+//    lcdPuts(lcdHandle, tmp->row);
+#endif
+//    slcdPosition(slcdHandle, 0, r);
+//    slcdPuts(slcdHandle, tmp->row);
+
+//    r++;
+//    if (r < Config.lcd_rows) {
+//	if (tmp->next != NULL)
+//	    tmp = tmp->next;
+//	else
+//	    tmp = my_lcd_rows;
+#ifdef HAVE_WIRINGPI_H
+//	lcdPosition(lcdHandle, 0, r);
+//	lcdPuts(lcdHandle, tmp->row);
+#endif
+//	slcdPosition(slcdHandle, 0, r);
+//	slcdPuts(slcdHandle, tmp->row);
+//    }
+
 }
 
 
--- a/thermferm/panel.c	Sat Nov 14 17:00:37 2015 +0100
+++ b/thermferm/panel.c	Sat Nov 14 23:40:51 2015 +0100
@@ -22,11 +22,10 @@
 
 #include "thermferm.h"
 #include "lcd-pcf8574.h"
+#include "slcd.h"
 #include "panel.h"
 
 
-#ifdef HAVE_WIRINGPI_H
-
 
 /*
  * 10 Milliseconds counts for a key to be short or long pressed.
@@ -49,6 +48,7 @@
 extern int		my_shutdown;
 extern int		debug;
 extern int		setupmenu;
+extern uint16_t		keys;
 
 int			Key_Enter = FALSE;
 int			Key_Enter_Long = FALSE;
@@ -98,16 +98,21 @@
 }
 
 
-
+#ifdef HAVE_WIRINGPI_H
 PI_THREAD (my_panel_loop)
+#else
+void *my_panel_loop(void *threadid)
+#endif
 {
     int		Enter = 0, Up = 0, Down = 0, Backlight = LCD_SLEEP, AnyKey = FALSE;
     time_t	Last = (time_t)0, Now;
 
+#ifdef HAVE_WIRINGPI_H
     pinMode(PANEL_LED, OUTPUT);
     pinMode(PANEL_ENTER, INPUT);
     pinMode(PANEL_UP, INPUT);
     pinMode(PANEL_DOWN, INPUT);
+#endif
 
     syslog(LOG_NOTICE, "Thread my_panel_loop started");
 
@@ -118,7 +123,11 @@
 	if (my_shutdown)
 	    break;
 
-	if (digitalRead(PANEL_ENTER)) {
+#ifdef HAVE_WIRINGPI_H
+	if (digitalRead(PANEL_ENTER) && ((keys & 0x0001) == 0)) {
+#else
+	if ((keys & 0x0001) == 0) {
+#endif
 	    Enter = 0;
 	    Key_Enter = FALSE;
 	    Key_Enter_Long = FALSE;
@@ -128,13 +137,19 @@
 		Key_Enter = TRUE;
 	    if (Enter > PRESS_LONG)
 		Key_Enter_Long = TRUE;
+#ifdef HAVE_WIRINGPI_H
 	    if (Enter == PRESS_LONG)
 		digitalWrite(PANEL_LED, 1);
 	    if (Enter == (PRESS_LONG + 10))
 		digitalWrite(PANEL_LED, 0);
+#endif
 	}
 
-	if (digitalRead(PANEL_UP)) {
+#ifdef HAVE_WIRINGPI_H
+	if (digitalRead(PANEL_UP) && ((keys & 0x0008) == 0)) {
+#else
+	if ((keys & 0x0008) == 0) {
+#endif
 	    Up = 0;
 	    Key_Up = FALSE;
 	} else {
@@ -143,7 +158,11 @@
 		Key_Up = TRUE;
 	}
 
-	if (digitalRead(PANEL_DOWN)) {
+#ifdef HAVE_WIRINGPI_H
+	if (digitalRead(PANEL_DOWN) && ((keys & 0x0004) == 0)) {
+#else
+	if ((keys & 0x0004) == 0) {
+#endif
 	    Down = 0;
 	    Key_Down = FALSE;
 	} else {
@@ -157,9 +176,11 @@
 	    /*
 	     * Any key is pressed.
 	     */
+#ifdef HAVE_WIRINGPI_H
 	    piLock(LOCK_LCD);
 	    setBacklight(1);
 	    piUnlock(LOCK_LCD);
+#endif
 	    Backlight = LCD_SLEEP;
 	    menutimer = 0;
 	} else {
@@ -175,15 +196,19 @@
 
 	    if (AnyKey == FALSE) {
 		if (Backlight == 1) {
+#ifdef HAVE_WIRINGPI_H
 		    piLock(LOCK_LCD);
 		    setBacklight(0);
 		    piUnlock(LOCK_LCD);
+#endif
 		}
 		if (Backlight > 0) {
 		    Backlight--;
 		}
 
+#ifdef HAVE_WIRINGPI_H
 		piLock(LOCK_MENU);
+#endif
 	    	if (setupmenu != MENU_NONE) {
 		    if (menutimer < MENU_TIMEOUT)
 			menutimer++;
@@ -191,7 +216,9 @@
 			setupmenu = MENU_NONE;
 		    }
 		}
+#ifdef HAVE_WIRINGPI_H
 		piUnlock(LOCK_MENU);
+#endif
 	    }
 	}
 
@@ -206,4 +233,3 @@
 }
 
 
-#endif
--- a/thermferm/panel.h	Sat Nov 14 17:00:37 2015 +0100
+++ b/thermferm/panel.h	Sat Nov 14 23:40:51 2015 +0100
@@ -24,14 +24,12 @@
 #define	KEY_ALL		99
 
 
-#ifdef HAVE_WIRINGPI_H
-
 int keycheck(void);
 
-
+#ifdef HAVE_WIRINGPI_H
 PI_THREAD (my_panel_loop);
-
-
+#else
+void *my_panel_loop(void *);
 #endif
 
 #endif
--- a/thermferm/slcd.c	Sat Nov 14 17:00:37 2015 +0100
+++ b/thermferm/slcd.c	Sat Nov 14 23:40:51 2015 +0100
@@ -46,12 +46,16 @@
 
 struct sockaddr_in	sendaddr;	/* Server send socket		*/
 int			sock = -1;
+uint16_t		keys = 0x0000;
 
 extern int		debug;
 
 
 void putLCDsocket(int fd, uint16_t data)
 {
+    socklen_t	slen;
+    uint16_t	rdat;
+
     if (sock == -1)
 	return;
 
@@ -61,12 +65,31 @@
 	    syslog(LOG_NOTICE, "Can't shutdown socket: %s", strerror(errno));
 	}
 	sock = -1;
-//    } else {
+    } else {
+	if (recvfrom(sock, &rdat, sizeof(uint16_t), 0, (struct sockaddr *) &sendaddr, &slen) != sizeof(uint16_t)) {
+	    syslog(LOG_NOTICE, "Socket recvfrom failed, closing socket: %s", strerror(errno));
+	} 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 slcdHome(int fd)
 //{
 //}
--- a/thermferm/slcd.h	Sat Nov 14 17:00:37 2015 +0100
+++ b/thermferm/slcd.h	Sat Nov 14 23:40:51 2015 +0100
@@ -2,6 +2,7 @@
 #define	_SLCD_H
 
 
+void slcdDummy(int fd);
 void slcdClear(int fd);
 void slcdPosition(int fd, int x, int y);
 void slcdPutchar(int fd, unsigned char c);
--- a/thermferm/thermferm.c	Sat Nov 14 17:00:37 2015 +0100
+++ b/thermferm/thermferm.c	Sat Nov 14 23:40:51 2015 +0100
@@ -396,7 +396,6 @@
 /*
  * Handle panel key events
  */
-#ifdef HAVE_WIRINGPI_H
 void panel_key_events(int key)
 {
     units_list		*unit;
@@ -799,8 +798,6 @@
 			break;
     }
 }
-#endif
-
 
 
 
@@ -932,11 +929,9 @@
     profiles_list	*profile;
     prof_step		*step;
     int			row, rc, run = 1, seconds = 0, minutes = 0, temp, deviation;
-    int			run_seconds, run_minutes, run_hours, tot_minutes;
+    int			run_seconds, run_minutes, run_hours, tot_minutes, key;
     struct tm		*tm;
-#ifdef HAVE_WIRINGPI_H
-    int			key;
-#else
+#ifndef HAVE_WIRINGPI_H
     long		t = 0;
 #endif
     int			current_step, valid_step, time_until_now, previous_fridge_mode;
@@ -982,12 +977,18 @@
     }
 
 #ifdef HAVE_WIRINGPI_H
-    if ((rc = piThreadCreate(my_panel_loop))) {
+    rc = piThreadCreate(my_panel_loop) {
+#else
+    rc = pthread_create(&threads[t], NULL, my_panel_loop, (void *)t );
+#endif
+    if (rc) {
 	fprintf(stderr, "my_panel_loop thread didn't start rc=%d\n", rc);
 	syslog(LOG_NOTICE, "my_panel_loop thread didn't start rc=%d", rc);
-    }
+#ifndef HAVE_WIRINGPI_H
+    } else {
+	t++;
 #endif
-
+    }
 
 #ifdef USE_SIMULATOR
 #ifdef HAVE_WIRINGPI_H
@@ -1784,11 +1785,10 @@
 	    }
 	}
 
-#ifdef HAVE_WIRINGPI_H
+	slcdDummy(slcdHandle);
 	key = keycheck();
 	if (key != KEY_NONE)
 	    panel_key_events(key);
-#endif
 
 	usleep(100000);
 

mercurial