thermferm/panel.c

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 204
9a14d6b2de7f
permissions
-rw-r--r--

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

/*****************************************************************************
 * Copyright (C) 2014
 *   
 * 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 "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)
	fprintf(stdout, "Thread my_panel_loop started\n");

    /*
     * Loop forever until the external shutdown variable is set.
     */
    for (;;) {
	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
	 */
	usleep(10000);
    }

    syslog(LOG_NOTICE, "Thread my_panel_loop stopped");
    if (debug)
	fprintf(stdout, "Thread my_panel_loop stopped\n");

    return 0;
}


#endif

mercurial