Added first panel key routines. The LCD display steps manual only and has a 2 minutes backlight timeout.

Sat, 09 Aug 2014 21:42:28 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 09 Aug 2014 21:42:28 +0200
changeset 200
a215ddaabbe2
parent 199
3f5d277a69e3
child 201
e589247c779c

Added first panel key routines. The LCD display steps manual only and has a 2 minutes backlight timeout.

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/thermferm.c file | annotate | diff | comparison | revisions
--- a/thermferm/lcd-buffer.c	Sat Aug 09 17:24:32 2014 +0200
+++ b/thermferm/lcd-buffer.c	Sat Aug 09 21:42:28 2014 +0200
@@ -23,13 +23,15 @@
 #include "thermferm.h"
 #include "lcd-buffer.h"
 #include "lcd-pcf8574.h"
+#include "panel.h"
 
 
 #ifdef HAVE_WIRINGPI_H
 
 int			current_lines = 0;
 int			current_offset = 0;
-int			current_second = 0;
+int			first_time = 1;
+int			previous_key = KEY_NONE;
 lcd_rows		*my_lcd_rows = NULL;
 
 extern int		lcdHandle;
@@ -89,13 +91,43 @@
 
 
 /*
- * This will be called each second.
+ * This will be called from the main thread if not in edit mode.
  */
 void lcd_buf_show(void)
 {
-    int		i = 0, r = 0;
+    int		i = 0, r = 0, key, doit = FALSE;
     lcd_rows	*tmp;
 
+    key = keycheck();
+
+    if ((key == KEY_NONE) && (previous_key == KEY_DOWN)) {
+	if (current_offset < (current_lines - Config.lcd_rows))
+	    current_offset = current_offset + Config.lcd_rows;
+	else
+	    current_offset = 0;
+	doit = TRUE;
+    }
+    if ((key == KEY_NONE) && (previous_key == KEY_UP)) {
+	if (current_offset > Config.lcd_rows)
+	    current_offset = current_offset - Config.lcd_rows;
+	else
+	    current_offset = (current_lines - Config.lcd_rows);
+	doit = TRUE;
+    }
+    previous_key = key;
+
+
+    /*
+     * Make sure the display works when the program starts.
+     */
+    if (first_time) {
+	first_time = 0;
+	doit = TRUE;
+    }
+
+    if (! doit)
+	return;
+
     for (tmp = my_lcd_rows; tmp; tmp = tmp->next) {
 	if (i == current_offset)
 	    break;
@@ -114,19 +146,6 @@
 	lcdPosition(lcdHandle, 0, r);
 	mb_lcdPuts(lcdHandle, tmp->row);
     }
-
-    current_second++;
-    if (current_second < 5)
-	return;
-
-    /*
-     * Wrap display
-     */
-    current_second = 0;
-    if (current_offset < (current_lines - Config.lcd_rows))
-	current_offset = current_offset + Config.lcd_rows;
-    else
-	current_offset = 0;
 }
 
 
--- a/thermferm/panel.c	Sat Aug 09 17:24:32 2014 +0200
+++ b/thermferm/panel.c	Sat Aug 09 21:42:28 2014 +0200
@@ -21,18 +21,51 @@
  *****************************************************************************/
 
 #include "thermferm.h"
+#include "lcd-pcf8574.h"
 #include "panel.h"
 
+
 #ifdef HAVE_WIRINGPI_H
 
 
 extern int		my_shutdown;
 extern int		debug;
 
+int			Key_Enter = FALSE;
+int			Key_Enter_Long = FALSE;
+int			Key_Up = FALSE;
+int			Key_Down = FALSE;
+
+
+int keycheck(void)
+{
+    if (Key_Enter && Key_Up && Key_Down)
+	return KEY_ALL;
+    if (Key_Up && Key_Down)
+	return KEY_UPDOWN;
+    if (Key_Up)
+	return KEY_UP;
+    if (Key_Down)
+	return KEY_DOWN;
+    if (Key_Enter_Long)
+	return KEY_CONFIRM;
+    if (Key_Enter)
+	return KEY_ENTER;
+    return KEY_NONE;
+}
+
 
 
 PI_THREAD (my_panel_loop)
 {
+    int		Enter = 0, Up = 0, Down = 0, Backlight = LCD_SLEEP, AnyKey = FALSE;
+    time_t	Last = (time_t)0, Now;
+
+    pinMode(PANEL_LED, OUTPUT);
+    pinMode(PANEL_ENTER, INPUT);
+    pinMode(PANEL_UP, INPUT);
+    pinMode(PANEL_DOWN, INPUT);
+
 
     syslog(LOG_NOTICE, "Thread my_panel_loop started");
     if (debug)
@@ -45,6 +78,71 @@
 	if (my_shutdown)
 	    break;
 
+	if (digitalRead(PANEL_ENTER)) {
+	    Enter = 0;
+	    Key_Enter = FALSE;
+	    Key_Enter_Long = FALSE;
+	} else {
+	    Enter++;
+	    if (Enter > PRESS_NORMAL)
+		Key_Enter = TRUE;
+	    if (Enter > PRESS_LONG)
+		Key_Enter_Long = TRUE;
+	}
+
+	if (digitalRead(PANEL_UP)) {
+	    Up = 0;
+	    Key_Up = FALSE;
+	} else {
+	    Up++;
+	    if (Up > PRESS_NORMAL)
+		Key_Up = TRUE;
+	}
+
+	if (digitalRead(PANEL_DOWN)) {
+	    Down = 0;
+	    Key_Down = FALSE;
+	} else {
+	    Down++;
+	    if (Down > PRESS_NORMAL)
+		Key_Down = TRUE;
+	}
+
+	if (Key_Enter || Key_Up || Key_Down) {
+	    AnyKey = TRUE;
+	    /*
+	     * Any key is pressed.
+	     */
+	    if (Backlight == 0)
+		setBacklight(1);
+	    Backlight = LCD_SLEEP;
+	} else {
+	    /*
+	     * No key pressed.
+	     */
+	    AnyKey = FALSE;
+	}
+
+//	if (debug && AnyKey)
+//	    fprintf(stdout, "keys Enter=%d,%s,%s  Up=%d,%s  Down=%d,%s\n", 
+//			    Enter, Key_Enter ?"True":"False", Key_Enter_Long ?"True":"False", Up, Key_Up ?"True":"False", Down, Key_Down ?"True":"False");
+
+	Now = time(NULL);
+	if (Now != Last) {
+	    Last = Now;
+
+//	    if (debug)
+//		fprintf(stdout, "AnyKey=%s  Backlight=%d\n", AnyKey ?"True":"False", Backlight);
+	    if (AnyKey == FALSE) {
+		if (Backlight == 1)
+		    setBacklight(0);
+		if (Backlight > 0) {
+		    Backlight--;
+		}
+	    }
+
+	}
+
 	/*
 	 * Loop 10 milliseconds
 	 */
--- a/thermferm/panel.h	Sat Aug 09 17:24:32 2014 +0200
+++ b/thermferm/panel.h	Sat Aug 09 21:42:28 2014 +0200
@@ -13,10 +13,10 @@
 #define	PANEL_UP	6
 
 /*
- * Milliseconds for a key to be short or long pressed.
+ * 10 Milliseconds counts for a key to be short or long pressed.
  */
-#define	KEY_NORMAL	50
-#define	KEY_LONG	2000
+#define	PRESS_NORMAL	5
+#define	PRESS_LONG	200
 
 /*
  * LCD timeout in seconds
@@ -28,6 +28,19 @@
  */
 #define	MENU_TIMEOUT	60
 
+/*
+ * Key names
+ */
+#define	KEY_NONE	0
+#define	KEY_UP		1
+#define	KEY_DOWN	2
+#define	KEY_UPDOWN	3
+#define	KEY_ENTER	4
+#define	KEY_CONFIRM	5	/* Long Enter */
+#define	KEY_ALL		99
+
+
+int keycheck(void);
 
 PI_THREAD (my_panel_loop);
 
--- a/thermferm/thermferm.c	Sat Aug 09 17:24:32 2014 +0200
+++ b/thermferm/thermferm.c	Sat Aug 09 21:42:28 2014 +0200
@@ -498,10 +498,6 @@
 #endif
 	    }
 
-#ifdef HAVE_WIRINGPI_H
-	    lcd_buf_show();
-#endif
-
 	    piddelay++;
 	    if (piddelay == 15) {
 		piddelay = 0;
@@ -624,6 +620,12 @@
 		}
 	    }
 	}
+
+#ifdef HAVE_WIRINGPI_H
+	// FIXME: not in editmode.
+	lcd_buf_show();
+#endif
+
 	usleep(100000);
 
     } while (run);

mercurial