--- a/brewco/brewco.h Wed Nov 25 16:39:25 2015 +0100 +++ b/brewco/brewco.h Wed Nov 25 22:49:35 2015 +0100 @@ -5,6 +5,7 @@ #define FALSE 0 #include "../config.h" +#include "pid.h" #include <stdlib.h> #include <stdio.h> @@ -13,6 +14,7 @@ #include <string.h> #include <ctype.h> #include <sys/types.h> +#include <sys/socket.h> #include <sys/stat.h> #include <sys/time.h> #include <time.h> @@ -23,6 +25,13 @@ #include <signal.h> #include <getopt.h> #include <limits.h> +#include <sys/socket.h> +#include <arpa/inet.h> +#include <netdb.h> +#include <poll.h> +#include <dirent.h> +#include <uuid/uuid.h> +#include <math.h> #ifndef HAVE_WIRINGPI_H #include <pthread.h> @@ -74,24 +83,137 @@ #define MBSE_SS(x) (x)?(x):"(null)" + +/* + * Brewing units. The sensors of the units are connected via the 1-wire bus. + * The heaters, pumps (or mixer) are connected on the Pi GPIO. The hardware + * is configured in the devices lists. + */ +typedef struct _units_list { + struct _units_list *next; + int version; /* Record version */ + char *uuid; /* uuid code */ + char *name; /* Unit name */ + char *hlt_sensor_address; /* HLT sensor address */ + int hlt_sensor_state; /* HLT sensor state */ + int hlt_sensor_value; /* HLT sensor value */ + char *hlt_heater_address; /* HLT heater address */ + int hlt_heater_state; /* HLT heater state 0..100 */ + int hlt_heater_delay; /* HLT heater delay / 15 sec */ + int hlt_heater_mltfirst; /* HLT heater MLT high priority */ + char *mlt_sensor_address; /* MLT sensor address */ + int mlt_sensor_state; /* MLT sensor state */ + int mlt_sensor_value; /* MLT sensor value */ + char *mlt_heater_address; /* MLT heater address */ + int mlt_heater_state; /* MLT heater state 0..100 */ + int mlt_heater_delay; /* MLT heater delay / 15 sec */ + char *mlt_pump_address; /* MLT pump address */ + int mlt_pump_state; /* MLT pump state 0..100 */ + int mlt_pump_delay; /* MLT_pump_delay */ + pid_var *PID_hlt; /* HLT PID */ + pid_var *PID_mlt; /* MLT PID */ +} units_list; + + + +/* + * External devices like sensors, relays, SSR. + */ +typedef struct _dev_list { + struct _dev_list *next; + int version; /* Version 1 */ + char *uuid; /* UUID of this device */ + int type; /* Device type */ + int direction; /* Device direction */ + int value; /* Device value */ + int offset; /* Device offset value */ + int present; /* Device present */ + char *address; /* Device address */ + int subdevice; /* Device sub address */ + int gpiopin; /* Device GPIO pin or -1 */ + char *description; /* Device description */ + int inuse; /* In use counter */ + char *comment; /* What we think it is */ + time_t timestamp; /* Last updated */ +} devices_list; + + +#define DEVTYPE_NA 0 /* Unknown device type */ +#define DEVTYPE_W1 1 /* 1-Wire bus */ +#define DEVTYPE_GPIO 2 /* GPIO I/O device */ +#define DEVTYPE_I2C 3 /* I2C bus device */ +#define DEVTYPE_SPI 4 /* SPI bus device */ +#ifdef USE_SIMULATOR +#define DEVTYPE_SIM 5 /* Simulated device */ +#endif + +#define DEVPRESENT_UNDEF 0 /* Precence not testable */ +#define DEVPRESENT_NO 1 /* Device is missing */ +#define DEVPRESENT_YES 2 /* Device is detected */ +#define DEVPRESENT_ERROR 3 /* Device is in error */ + +#define DEVDIR_UNDEF 0 /* Undefined */ +#define DEVDIR_IN_BIN 1 /* Binary input */ +#define DEVDIR_OUT_BIN 2 /* Binary output */ +#define DEVDIR_IN_ANALOG 3 /* Temperature input etc. */ +#define DEVDIR_OUT_ANALOG 4 /* Analog steering */ +#define DEVDIR_OUT_PWM 5 /* PWM outout */ +#define DEVDIR_INTERN 6 /* Internal function */ + + +#ifdef USE_SIMULATOR + +/* + * Simulate a HLT and MLT. + */ +typedef struct _simulator { + struct _simulator *next; + int version; /* Version of this record */ + char *uuid; /* Simulator uuid */ + char *name; /* Simulator name */ + double room_temperature; /* Simulated envionment temp */ + double hlt_temperature; /* Simulated HLT temperature */ + double hlt_heater_temp; /* Maximum heater temp */ + int hlt_heater_time; /* Time to reach temperature */ + float hlt_heater_size; /* Size of the heater */ + int hlt_heater_state; /* Heater status */ + double mlt_temperature; /* Simulated MLT temperature */ + double mlt_heater_temp; /* Maximum heater temp */ + int mlt_heater_time; /* Time to reach temperature */ + float mlt_heater_size; /* Size of the heater */ + int mlt_heater_state; /* Heater status */ + /* + * Status values, maintained by the simulator but stored + * here so they don't get lost over program restarts. + */ + double s_hlt_temp; /* Temp HLT */ + double s_mlt_temp; /* Temp MLT */ +} simulator_list; + +#endif + + + typedef struct _sys_config { char *name; /* Configuration name */ + int my_port; /* my client/server port */ unsigned char tempFormat; /* Temperature format, C or F */ - char *hlt_sensor_address; /* HLT sensor address */ - int hlt_sensor_state; /* HLT sensor state */ - int hlt_sensor_value; /* HLT sensor value */ - - char *mlt_sensor_address; /* MLT sensor address */ - int mlt_sensor_state; /* MLT sensor state */ - int mlt_sensor_value; /* MLT sensor value */ - -#ifdef HAVE_WIRINGPI_H + char *temp_address; /* Environment temperature */ + int temp_state; /* 0=ok, 1=missing, 2=error */ + int temp_value; /* Air temperature in C * 1000 */ + char *hum_address; /* Environment huminity */ + int hum_state; /* 0=ok, 1=missing, 2=error */ + int hum_value; /* Huminity in % * 1000 */ int lcd_cols; /* LCD display columns */ int lcd_rows; /* LCD display rows */ int lcd_address; /* LCD display i2c address */ + units_list *units; /* Brewing units */ + devices_list *devices; /* Sensors and switches */ +#ifdef USE_SIMULATOR + simulator_list *simulators; /* Simulators */ #endif - } sys_config; + #endif