brewco/brewco.c

Sat, 12 Dec 2015 19:31:35 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 12 Dec 2015 19:31:35 +0100
changeset 455
f84501d8dd87
parent 454
78242696c15a
child 456
045db83dd013
permissions
-rw-r--r--

Most parts of the simulator are working, needs some tuning.

/*****************************************************************************
 * 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 "rdconfig.h"
#include "rdsession.h"
#include "util.h"
#include "xutil.h"
#include "lcd-pcf8574.h"
#include "slcd.h"
#include "lock.h"
#include "devices.h"
#include "keyboard.h"
#include "simulator.h"
#include "prompt.h"
#include "setup.h"



int			my_shutdown = FALSE;
extern int              debug;
extern sys_config       Config;
extern int              lcdHandle;
extern int		slcdHandle;
extern int		sock;

#ifndef HAVE_WIRINGPI_H
pthread_t               threads[5];
#endif


#define	MANUAL_NONE	0
#define	MANUAL_SELHLT	1
#define	MANUAL_SELMLT	2
#define	MANUAL_HLT	11
#define	MANUAL_MLT	12

int manual		= MANUAL_NONE;



/*
 * CGRAM characters
 */
unsigned char	degC[8] 	= { 0b01000, 0b10100, 0b01000, 0b00111, 0b01000, 0b01000, 0b01000, 0b00111 };
unsigned char	degF[8] 	= { 0b01000, 0b10100, 0b01000, 0b00111, 0b00100, 0b00110, 0b00100, 0b00100 };
unsigned char	SP_Symbol[8]	= { 0b11100, 0b10000, 0b11100, 0b00111, 0b11101, 0b00111, 0b00100, 0b00100 };
unsigned char	PumpONOFF[8]	= { 0b00000, 0b01110, 0b01010, 0b01110, 0b01000, 0b01000, 0b01000, 0b00000 };
unsigned char	RevPumpONOFF[8]	= { 0b11111, 0b10001, 0b10101, 0b10001, 0b10111, 0b10111, 0b10111, 0b11111 };
unsigned char	HeatONOFF[8]	= { 0b00000, 0b01010, 0b01010, 0b01110, 0b01110, 0b01010, 0b01010, 0b00000 };
unsigned char	RevHeatONOFF[8]	= { 0b11111, 0b10101, 0b10101, 0b10001, 0b10001, 0b10101, 0b10101, 0b11111 };
unsigned char	Language[8]	= { 0b11111, 0b00010, 0b01000, 0b11111, 0b00000, 0b10001, 0b10101, 0b11111 };


void help(void);
void die(int);



void help(void)
{
    fprintf(stdout, "mbsePi-apps brewco v%s starting\n\n", VERSION);
    fprintf(stdout, "Usage: brewco [-d] [-h]\n");
    fprintf(stdout, "  -d --debug              Debug and run in foreground\n");
    fprintf(stdout, "  -h --help               Display this help\n");
}



void die(int onsig)
{
    switch (onsig) {
	case SIGHUP:    syslog(LOG_NOTICE, "Got SIGHUP, shutting down");
			break;
	case SIGINT:    syslog(LOG_NOTICE, "Keyboard interrupt, shutting down");
			break;
	case SIGTERM:   syslog(LOG_NOTICE, "Got SIGTERM, shutting down");
			break;
	case SIGSEGV:   syslog(LOG_NOTICE, "Got SIGSEGV, shutting down");
			my_shutdown = TRUE;
			exit(SIGSEGV);
			break;
	default:        syslog(LOG_NOTICE, "die() on signal %d", onsig);
    }

    my_shutdown = TRUE;
}



void tempstatus(double hlttemp, double mlttemp)
{
    char	text[81];

    snprintf(text, 7, "%5.1f\001", hlttemp);
#ifdef HAVE_WIRINGPI_H
    piLock(LOCK_LCD);
    lcdPosition(lcdHandle, 2, 1);
    lcdPuts(lcdHandle, text);
#endif
    slcdPosition(slcdHandle, 2, 1);
    slcdPuts(slcdHandle, text);

    snprintf(text, 7, "%5.1f\001", mlttemp);
#ifdef HAVE_WIRINGPI_H
    piLock(LOCK_LCD);
    lcdPosition(lcdHandle, 11, 1);
    lcdPuts(lcdHandle, text);
#endif
    slcdPosition(slcdHandle, 11, 1);
    slcdPuts(slcdHandle, text);
}



int manual_menu(units_list *, double, double);
int manual_menu(units_list *unit, double hlt, double mlt)
{
    int		key, i;
    static int	man_hlt_heat, man_mlt_heat, man_mlt_pump;

    switch (manual) {
        case MANUAL_SELHLT:     prompt(104, NULL);
                                prompt(219, NULL);
                                prompt(402, NULL);
                                key = keywait();
                                if (key == KEY_DOWN)
                                    manual = MANUAL_SELMLT;
                                if (key == KEY_RETURN)
                                    manual = MANUAL_NONE;
                                if (key == KEY_ENTER) {
                                                // TODO: prompt for water
                                    manual = MANUAL_HLT;
                                    prompt(0, NULL);
				    man_hlt_heat = 0, man_mlt_heat = 0, man_mlt_pump = 0;
                                    device_out(unit->hlt_heater.uuid, man_hlt_heat);
                                    device_out(unit->mlt_heater.uuid, man_mlt_heat);
                                    device_out(unit->mlt_pump.uuid, man_mlt_pump);
                                }
                                break;
        case MANUAL_SELMLT:     prompt(104, NULL);
                                prompt(220, NULL);
                                prompt(404, NULL);
                                key = keywait();
                                if (key == KEY_UP)
                                    manual = MANUAL_SELHLT;
                                if (key == KEY_RETURN)
                                    manual = MANUAL_NONE;
                                if (key == KEY_ENTER) {
                                                // TODO: prompt for water
                                    manual = MANUAL_MLT;
                                    prompt(0, NULL);
				    man_hlt_heat = 0, man_mlt_heat = 0, man_mlt_pump = 0;
                                    device_out(unit->hlt_heater.uuid, man_hlt_heat);
                                    device_out(unit->mlt_heater.uuid, man_mlt_heat);
                                    device_out(unit->mlt_pump.uuid, man_mlt_pump);
                                }
                                break;
        case MANUAL_HLT:        prompt(104, NULL);
                                prompt(413, NULL);
				tempstatus(*unit->PID_hlt->myInput, *unit->PID_mlt->myInput);
				
				for (i = 1; i < 100; i++) {
				    usleep(10000);
				    slcdDummy(slcdHandle);
                                    key = keycheck();
				    if ((key != KEY_NONE) || my_shutdown)
					break;
				}
                                if (key == KEY_RETURN) {
                                    if (man_hlt_heat)
                                        man_hlt_heat = 0;
                                    else
                                        man_hlt_heat = 1;
                                }
                                if (key == KEY_ESCAPE) {
                                    manual = MANUAL_SELHLT;
                                    man_hlt_heat = 0;
                                }
                                device_out(unit->hlt_heater.uuid, man_hlt_heat);
                                if (debug)
                                    fprintf(stdout, "device_out(%s, %d) HLT heater\n", unit->hlt_heater.uuid, man_hlt_heat);
                                break;
        case MANUAL_MLT:        prompt(104, NULL);
                                prompt(406, NULL);
				tempstatus(*unit->PID_hlt->myInput, *unit->PID_mlt->myInput);

				for (i = 1; i < 100; i++) {
				    usleep(10000);
				    slcdDummy(slcdHandle);
				    key = keycheck();
				    if ((key != KEY_NONE) || my_shutdown)
					break;
				}
                                if (key == KEY_RETURN) {
                                    if (man_mlt_heat)
                                        man_mlt_heat = 0;
                                    else
                                        man_mlt_heat = 1;
                                }
                                if (key == KEY_ENTER) {
                                    if (man_mlt_pump)
                                        man_mlt_pump = 0;
                                    else
                                        man_mlt_pump = 1;
                                }
                                if (key == KEY_ESCAPE) {
                                    manual = MANUAL_SELMLT;
                                    man_mlt_heat = man_mlt_pump = 0;
                                }
                                device_out(unit->mlt_heater.uuid, man_mlt_heat);
                                device_out(unit->mlt_pump.uuid, man_mlt_pump);
                                break;
    }

    return 0;
}



int server(void);
int server(void)
{
    int 		rc = 0, run = 1, key, temp;
    int			do_init = TRUE, seconds = 0, minutes = 0;
    units_list		*unit;
    brew_session	*brew = NULL;
#ifndef HAVE_WIRINGPI_H
    long		t = 0;
#endif
    time_t		now, last = (time_t)0;
    static double	hltInput, hltOutput, hltSetpoint, mltInput, mltOutput, mltSetpoint;

    prompt(101, NULL);

    /*
     * Define special characters in the display CGRAM
     */
    if (Config.tempFormat == 'C')
    	slcdCharDef(slcdHandle, 1, degC);
    else
	slcdCharDef(slcdHandle, 1, degF);
    slcdCharDef(slcdHandle, 2, SP_Symbol);
    slcdCharDef(slcdHandle, 3, PumpONOFF);
    slcdCharDef(slcdHandle, 4, RevPumpONOFF);
    slcdCharDef(slcdHandle, 5, HeatONOFF);
    slcdCharDef(slcdHandle, 6, RevHeatONOFF);
    slcdCharDef(slcdHandle, 7, Language);

//    if (lockprog((char *)"brewco")) {
//	syslog(LOG_NOTICE, "Can't lock");
//	return 1;
//    }

    if (debug)
	fprintf(stdout, "Begin server()\n");

    if ((rc = devices_detect())) {
	syslog(LOG_NOTICE, "Detected %d new devices", rc);
	if (debug)
	    fprintf(stdout, "Detected %d new devices\n", rc);
	wrconfig();
    }

#ifdef HAVE_WIRINGPI_H
    rc = piThreadCreate(my_devices_loop);
#else
    rc = pthread_create(&threads[t], NULL, my_devices_loop, (void *)t );
#endif
    if (rc) {
	fprintf(stderr, "my_devices_loop thread didn't start rc=%d\n", rc);
	syslog(LOG_NOTICE, "my_devices_loop thread didn't start rc=%d", rc);
#ifndef HAVE_WIRINGPI_H
    } else {
	t++;
#endif
    }

#ifdef HAVE_WIRINGPI_H
    rc = piThreadCreate(my_keyboard_loop);
#else
    rc = pthread_create(&threads[t], NULL, my_keyboard_loop, (void *)t );
#endif
    if (rc) {
	fprintf(stderr, "my_keyboard_loop thread didn't start rc=%d\n", rc);
	syslog(LOG_NOTICE, "my_keyboard_loop thread didn't start rc=%d", rc);
#ifndef HAVE_WIRINGPI_H
    } else {
	t++;
#endif
    }

#ifdef USE_SIMULATOR
#ifdef HAVE_WIRINGPI_H
    rc = piThreadCreate(my_simulator_loop);
#else
    rc = pthread_create(&threads[t], NULL, my_simulator_loop, (void *)t );
#endif
    if (rc) {
	fprintf(stderr, "my_simulator_loop thread didn't start rc=%d\n", rc);
	syslog(LOG_NOTICE, "my_simulator_loop thread didn't start rc=%d", rc);
#ifndef HAVE_WIRINGPI_H
    } else {
	t++;
#endif
    }
#endif

    if (! Config.units) {
	/*
	 * No brewsystems defined, add the first
	 */
	prompt(218, NULL);	/*   Add Brewsystem?   */
	prompt(407, NULL);	/* ---  ---   Ok   --- */

	do {
	    key = keywait();
	} while (key != KEY_RETURN);

	if (key == KEY_RETURN) {
	    addUnit(1);
	}
    }

    /*
     * Initialize units for processing
     */
    for (unit = Config.units; unit; unit = unit->next) {
	if (unit->active)
	    break;
    }

    if (! unit->active) {
	fprintf(stdout, "No active units found\n");
    }

    /*
     * Safety, turn everything off
     */
    if (unit->active) {
	if (debug)
	    fprintf(stdout, "Starting brewsystem %d `%s'\n", unit->number, unit->name);
	syslog(LOG_NOTICE, "Starting brewsystem %d `%s'", unit->number, unit->name);
    }

    /*
     * During automation there will be a state file:
     * ~/.brewco/var/brewing.xml
     * If this file is present, there has been a crash.
     */
    brew = (brew_session *)malloc(sizeof(brew_session));
    if (rdsession(brew) == 0) {
    } else {
	/*
	 * No active brew session, make that permanent.
	 */
	free(brew);
	brew = NULL;
    }

    do {
	if (my_shutdown) {
	    run = 0;
	    break;
	}

	/*
	 * Do we need to initialize this unit?
	 */
	if (do_init) {
	    if (debug)
		fprintf(stdout, "Initialize brewsystem %d `%s'\n", unit->number, unit->name);
	    syslog(LOG_NOTICE, "Initialize brewsystem %d `%s'", unit->number, unit->name);
	    /*
	     * Turn everything off
	     */
	    unit->hlt_heater.value = 0;
	    unit->mlt_heater.value = 0;
	    unit->mlt_pump.value = 0;
	    device_out(unit->hlt_heater.uuid, 0);
	    device_out(unit->mlt_heater.uuid, 0);
	    device_out(unit->mlt_pump.uuid, 0);

	    /*
	     * Initialize PID's
	     */
	    hltInput = hltSetpoint = mltInput = mltSetpoint = 20.0;
	    hltOutput = mltOutput = 0;
	    PID_init(unit->PID_hlt, &hltInput, &hltOutput, &hltSetpoint, unit->PID_hlt->dispKd, unit->PID_hlt->dispKi, unit->PID_hlt->dispKd, unit->PID_hlt->Direction);
	    PID_setOutputLimits(unit->PID_hlt, 0, 5000);
	    PID_setSampleTime(unit->PID_hlt, unit->PID_hlt->SampleTime);
	    PID_init(unit->PID_mlt, &mltInput, &mltOutput, &mltSetpoint, unit->PID_mlt->dispKd, unit->PID_mlt->dispKi, unit->PID_mlt->dispKd, unit->PID_mlt->Direction);
	    PID_setOutputLimits(unit->PID_mlt, 0, 5000);
	    PID_setSampleTime(unit->PID_mlt, unit->PID_mlt->SampleTime);

	    prompt(0, NULL);
	    prompt(101, NULL);
	    prompt(401, NULL);
	    manual = MANUAL_NONE;

	    do_init = FALSE;
	}

	/* run_pause code here */

	/*
	 * Update PID's, even if they are off. Both PID's will do
	 * the scheduling by themselves.
	 */
	rc = PID_compute(unit->PID_hlt);
//	if (seconds < 3)
//	fprintf(stdout, "hlt rc=%d");
	rc = PID_compute(unit->PID_mlt);
//	if (seconds < 3)
//		        fprintf(stdout, "   mlt rc=%d\n");

	now = time(NULL);
	if (now != last) {
	    /*
	     * Each second
	     */
	    last = now;
	    seconds++;
	    if (seconds > 59) {
		seconds = 0;
		minutes++;
	    }

fprintf(stdout, "%d seconds %d minutes %ld millis\n", seconds, minutes, millis());

	    rc = device_in(unit->hlt_sensor.uuid, &temp);
	    if (rc == DEVPRESENT_YES) {
		hltInput = temp / 1000.0;
		unit->hlt_sensor.state = 0;
	    } else if (rc == DEVPRESENT_ERROR) {
		unit->hlt_sensor.state = 1;
	    } else {
		unit->hlt_sensor.state = 2;
	    }
	    rc = device_in(unit->mlt_sensor.uuid, &temp);
	    if (rc == DEVPRESENT_YES) {
		mltInput = temp / 1000.0;
		unit->mlt_sensor.state = 0;
	    } else if (rc == DEVPRESENT_ERROR) {
		unit->mlt_sensor.state = 1;
	    } else {
		unit->mlt_sensor.state = 2;
	    }

	    if (debug && ((seconds % 10) == 1)) {
		fprintf(stdout, "MLT: In=%.2lf Out=%.2lf Set=%.2lf  HLT: In=%.2lf Out=%.2lf Set=%.2lf\n",
				mltInput, mltOutput, mltSetpoint, hltInput, hltOutput, hltSetpoint);
	    }

	}

	if (brew) {
	    /*
	     * Automate mode
	     */

	} else if (manual != MANUAL_NONE) {
	    /*
	     * Manual mode
	     */
	    manual_menu(unit, hltInput, mltInput);
	    if (manual == MANUAL_NONE) {
		/*
		 * Rewrite the display
		 */
		prompt(0, NULL);
		prompt(101, NULL);
		prompt(401, NULL);
	    }
	} else {
	    /*
	     * Not running.
	     */
	    tempstatus(hltInput, mltInput);
	    key = keycheck();
	    if (key == KEY_ENTER) {
		setup();
		prompt(0, NULL);
		prompt(101, NULL);
		prompt(401, NULL);
	    } else if (key == KEY_DOWN) {
		manual = MANUAL_SELHLT;
	    }
	}

	usleep(5000);	/* 5 mSec */

    } while (run);

    syslog(LOG_NOTICE, "Out of loop");
    if (debug)
	fprintf(stdout, (char *)"Out of loop\n");

    prompt(0, NULL);
    prompt(101, NULL);
    prompt(302, NULL);

    /*
     * Stop units processing in a neat way
     */
    for (unit = Config.units; unit; unit = unit->next) {

    }

    /*
     * Give threads time to cleanup
     */
    usleep(1500000);

    prompt(0, NULL);
//    stopLCD();
    if (sock != -1) {
	if (shutdown(sock, SHUT_RDWR)) {
	    syslog(LOG_NOTICE, "Can't shutdown socket: %s", strerror(errno));
	}
	sock = -1;
    }
    wrconfig();
//    ulockprog((char *)"brewco");
    return 0;
}



int main(int argc, char *argv[])
{
    int		rc = 0, c, i;

    while (1) {
	int option_index = 0;
	static struct option long_options[] = {
	    {"debug", 0, 0, 'c'},
	    {"help", 0, 0, 'h'},
	    {0, 0, 0, 0}
	};

	c = getopt_long(argc, argv, "dh", long_options, &option_index);
	if (c == -1)
	    break;

	switch (c) {
	    case 'd':   debug = TRUE;
			break;
	    case 'h':   help();
			return 1;
	}
    }

    openlog("brewco", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER);
    syslog(LOG_NOTICE, "mbsePi-apps brewco v%s starting", VERSION);
    if (debug)
	fprintf(stdout, "mbsePi-apps brewco v%s starting\n", VERSION);

    if (rdconfig()) {
	fprintf(stderr, "Error reading configuration\n");
	syslog(LOG_NOTICE, "Error reading configuration: halted");
	return 1;
    }
    if (debug)
	fprintf(stdout, "configuration loaded\n");

    /*
     *  Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored
     *  but that's live. This daemon should only be stopped by SIGTERM.
     *  Don't catch SIGCHLD.
     */
    for (i = 0; i < NSIG; i++) {
	if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP))
	    signal(i, (void (*))die);
    }

#ifdef HAVE_WIRINGPI_H
    if (wiringPiSetup () )
	return 1;
#endif

    if ((rc = initLCD (Config.lcd_cols, Config.lcd_rows))) {
	fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc);
	return 1;
    }

    rc = server();

    syslog(LOG_NOTICE, "Finished, rc=%d", rc);
    if (debug)
	fprintf(stdout, "Finished, rc=%d\n", rc);
    return rc;
}

mercurial