thermferm/panel.c

Thu, 25 Apr 2024 14:26:47 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 25 Apr 2024 14:26:47 +0200
changeset 708
13555c27b592
parent 660
a28ef4d9afa4
child 724
01e3936f62d4
permissions
-rw-r--r--

Version 0.9.19a6. Fixes after a short trial on the production controller. Fixed json for alternate beer termperature sensor. Fixed division by 1000 for the room temperature and humidity values. The dropdown list for devices shows the address instead of description in the list.

/*****************************************************************************
 * Copyright (C) 2014-2024
 *   
 * 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 "delay.h"
#include "lcd-pcf8574.h"
#include "slcd.h"
#include "panel.h"



/*
 * 10 Milliseconds counts for a key to be short or long pressed.
 */
#define PRESS_NORMAL    5
#define PRESS_LONG      200

/*
 * LCD timeout in seconds
 */
#define LCD_SLEEP       120

/*
 * Menu timeout in seconds
 */
#define MENU_TIMEOUT    60



extern int		debug;
extern int		setupmenu;
extern uint16_t		keys;
extern uint16_t		leds;
extern int		slcdHandle;
extern pthread_mutex_t  mutexes[5];

int			my_panel_state = 0;
int			my_panel_shutdown = 0;
int			Key_Enter = FALSE;
int			Key_Enter_Long = FALSE;
int			Key_Up = FALSE;
int			Key_Down = FALSE;

int			previous_key = KEY_NONE;
int			menutimer = 0;


int keypressed(void);


/*
 * Check for a key. Return last pressed key or none.
 */
int keycheck(void)
{
    int		key, retkey = KEY_NONE;

    key = keypressed();
    if ((key == KEY_NONE) && (previous_key != KEY_NONE)) {
	retkey = previous_key;
    }

    previous_key = key;
    return retkey;
}



int keypressed(void)
{
    if (Key_Enter && Key_Up && Key_Down)
	return KEY_ALL;
    if (Key_Up && Key_Down)
	return KEY_ESCAPE;
    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;
}


void *my_panel_loop(void *threadid)
{
    int		Enter = 0, Up = 0, Down = 0, Backlight = LCD_SLEEP, AnyKey = FALSE;
    time_t	Last = (time_t)0, Now;

    my_panel_state = 1;

#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");

    /*
     * Loop forever until the external shutdown variable is set.
     */
    for (;;) {
	if (my_panel_shutdown)
	    break;

#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;
	} else {
	    Enter++;
	    if (Enter > PRESS_NORMAL)
		Key_Enter = TRUE;
	    if (Enter > PRESS_LONG)
		Key_Enter_Long = TRUE;
	    if (Enter == PRESS_LONG) {
#ifdef HAVE_WIRINGPI_H
		digitalWrite(PANEL_LED, 1);
#endif
		leds |= SLED_TFLED;
		slcdLEDs(slcdHandle);
	    }
	    if (Enter == (PRESS_LONG + 10)) {
#ifdef HAVE_WIRINGPI_H
		digitalWrite(PANEL_LED, 0);
#endif
		leds &= ~SLED_TFLED;
		slcdLEDs(slcdHandle);
	    }
	}

#ifdef HAVE_WIRINGPI_H
	if (digitalRead(PANEL_UP) && ((keys & 0x0008) == 0)) {
#else
	if ((keys & 0x0008) == 0) {
#endif
	    Up = 0;
	    Key_Up = FALSE;
	} else {
	    Up++;
	    if (Up > PRESS_NORMAL)
		Key_Up = TRUE;
	}

#ifdef HAVE_WIRINGPI_H
	if (digitalRead(PANEL_DOWN) && ((keys & 0x0004) == 0)) {
#else
	if ((keys & 0x0004) == 0) {
#endif
	    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.
	     */
	    pthread_mutex_lock(&mutexes[LOCK_LCD]);
	    setBacklight(1);
	    pthread_mutex_unlock(&mutexes[LOCK_LCD]);
	    Backlight = LCD_SLEEP;
	    menutimer = 0;
	} else {
	    /*
	     * No key pressed.
	     */
	    AnyKey = FALSE;
	}

	Now = time(NULL);
	if (Now != Last) {
	    Last = Now;

	    if (AnyKey == FALSE) {
		if (Backlight == 1) {
		    pthread_mutex_lock(&mutexes[LOCK_LCD]);
		    setBacklight(0);
		    pthread_mutex_unlock(&mutexes[LOCK_LCD]);
		}
		if (Backlight > 0) {
		    Backlight--;
		}

		pthread_mutex_lock(&mutexes[LOCK_MENU]);
	    	if (setupmenu != MENU_NONE) {
		    if (menutimer < MENU_TIMEOUT)
			menutimer++;
		    else {
			setupmenu = MENU_NONE;
		    }
		}
		pthread_mutex_unlock(&mutexes[LOCK_MENU]);
	    }
	}

	/*
	 * Loop 10 milliseconds
	 */
	mDelay(10);
    }

    syslog(LOG_NOTICE, "Thread my_panel_loop stopped");
    my_panel_state = 0;
    return 0;
}

mercurial