brewco/keyboard.c

Mon, 21 Dec 2015 21:21:16 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 21 Dec 2015 21:21:16 +0100
changeset 468
e8203e891b0e
parent 451
2247970de278
permissions
-rw-r--r--

Removed LCD auto dim mode, the display is allways on if this program runs.

/*****************************************************************************
 * 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 "lcd-pcf8574.h"
#include "slcd.h"
#include "keyboard.h"



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



extern int		my_shutdown;
extern int		debug;
extern int		setupmenu;
extern uint16_t		keys;
extern uint16_t		leds;
extern int		slcdHandle;

int			Key_Enter = FALSE;
int			Key_Return = FALSE;
int			Key_Up = FALSE;
int			Key_Down = FALSE;

int			previous_key = KEY_NONE;


int keypressed(void);



/*
 * Wait for a key. Return the pressed key.
 */
int keywait(void)
{
    int	key;

    do {
	usleep(50000);
	slcdDummy(slcdHandle);
	key = keycheck();
	if (my_shutdown)
	    return KEY_NONE;
    } while (key == KEY_NONE);

    fprintf(stdout, "keywait %d\n", key);
    return key;
}



/*
 * 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 && Key_Return)
	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)
	return KEY_ENTER;
    if (Key_Return)
	return KEY_RETURN;
    return KEY_NONE;
}


#ifdef HAVE_WIRINGPI_H
PI_THREAD (my_keyboard_loop)
#else
void *my_keyboard_loop(void *threadid)
#endif
{
    int		Return = 0, Enter = 0, Up = 0, Down = 0;
    time_t	Last = (time_t)0, Now;

#ifdef HAVE_WIRINGPI_H
    pinMode(PANEL_RETURN, INPUT);
    pinMode(PANEL_ENTER, INPUT);
    pinMode(PANEL_UP, INPUT);
    pinMode(PANEL_DOWN, INPUT);
#endif

    syslog(LOG_NOTICE, "Thread my_keyboard_loop started");
    if (debug)
	fprintf(stdout, "Thread my_keyboard_loop started\n");

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

#ifdef HAVE_WIRINGPI_H
	if (digitalRead(PANEL_ENTER) && ((keys & 0x0001) == 0)) {
#else
	if ((keys & 0x0001) == 0) {
#endif
	    Enter = 0;
	    Key_Enter = FALSE;
	} else {
	    Enter++;
	    if (Enter > PRESS_NORMAL)
		Key_Enter = TRUE;
	}

#ifdef HAVE_WIRINGPI_H
	if (digitalRead(PANEL_RETURN) && ((keys & 0x0002) == 0)) {
#else
	if ((keys & 0x0002) == 0) {
#endif
	    Return = 0;
	    Key_Return = FALSE;
	} else {
	    Return++;
	    if (Return > PRESS_NORMAL)
	    	Key_Return = TRUE;
	}

#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;
	}

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

	/*
	 * Loop 10 milliseconds
	 */
	usleep(10000);
    }

    syslog(LOG_NOTICE, "Thread my_keyboard_loop stopped");
    if (debug)
	fprintf(stdout, "Thread my_keyboard_loop stopped\n");
    return 0;
}

mercurial