ソースを参照

Merge branch 'master' of omi-gitlab.e-technik.uni-ulm.de:students/CoffeePi

Sebastian Vendt 7 年 前
コミット
5ffe950114

+ 2 - 2
CoffeeCode/.cproject

@@ -31,7 +31,7 @@
 									<listOptionValue builtIn="false" value="/home/sebastian/opencv_Source/ArmLibs/usr/local/include"/>
 								</option>
 								<option id="gnu.c.compiler.option.include.files.1577782333" name="Include files (-include)" superClass="gnu.c.compiler.option.include.files"/>
-								<option id="gnu.c.compiler.option.misc.other.6920715" name="Other flags" superClass="gnu.c.compiler.option.misc.other" value="-c -fmessage-length=0 -std=c++11 -DTHREADSAFE=1" valueType="string"/>
+								<option id="gnu.c.compiler.option.misc.other.6920715" name="Other flags" superClass="gnu.c.compiler.option.misc.other" value="-c -fmessage-length=0 -std=c++11 -DTHREADSAFE=1 -lpthread" valueType="string"/>
 								<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.435867427" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
 							</tool>
 							<tool id="cdt.managedbuild.tool.gnu.cross.cpp.compiler.212510575" name="Cross G++ Compiler" superClass="cdt.managedbuild.tool.gnu.cross.cpp.compiler">
@@ -43,7 +43,7 @@
 									<listOptionValue builtIn="false" value="/home/sebastian/opencv_Source/ArmLibs/usr/local/include"/>
 									<listOptionValue builtIn="false" value="/home/sebastian/opencv_Source/ArmLibs/usr/include"/>
 								</option>
-								<option id="gnu.cpp.compiler.option.other.other.822952785" name="Other flags" superClass="gnu.cpp.compiler.option.other.other" value="-c -fmessage-length=0 -std=c++11 -DTHREADSAFE=1" valueType="string"/>
+								<option id="gnu.cpp.compiler.option.other.other.822952785" name="Other flags" superClass="gnu.cpp.compiler.option.other.other" value="-c -fmessage-length=0 -std=c++11 -DTHREADSAFE=1 -lpthread" valueType="string"/>
 								<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.1449409074" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
 							</tool>
 							<tool id="cdt.managedbuild.tool.gnu.cross.c.linker.1150547366" name="Cross GCC Linker" superClass="cdt.managedbuild.tool.gnu.cross.c.linker"/>

+ 90 - 0
CoffeeCode/DS18B20.cpp

@@ -0,0 +1,90 @@
+/*
+ * DS18B20.cpp
+ *
+ *  Created on: Feb 28, 2018
+ *      Author: sebastian
+ */
+
+
+
+#define READ_SCRATCH	0xBE
+#define WRITE_SCRATCH	0x4E
+#define READ_ROM	0x33
+#define SKIP_ROM	0xCC
+#define CONVERT_T	0x44
+
+
+#include "DS18B20.h"
+
+/*
+ * | -- 8 BIT CRC -- | -- 48 BIT SERIAL NUMBER -- | -- 8 BIT FAMILY CODE (28h) -- |
+ */
+uint8_t rom[8] = {0,0,0,0,0,0,0,0};
+double deviceID;
+
+/**
+ * initialize the Temperature Sensor
+ * This lib is written for only ONE device on the 1-Wire Bus
+ */
+
+void DS18B20_init(void){
+	OneWire_init();
+	if(OW_reset()){
+		logger_error("Unable to reset 1-Wire Bus, no device present %s\n", strerror(errno));
+		return;
+	}
+	logger(V_HAL, "Checking 1-Wire Bus for sensors..\n");
+	DS18B20_readRom();
+	deviceID = double (rom[1]);// | ROM[5] << 8 | ROM[5] << 16 | ROM[5] << 24 | ROM[5] << 32 | ROM[5] << 40);
+	if(rom[7] == 0x28){
+		logger(V_HAL, "Found temperature sensor on 1-Wire Bus\n");
+		logger(V_HAL, "Device ID: %12X\n", deviceID);
+	}
+}
+
+void DS18B20_readRom(void){
+	OW_writeByte(READ_ROM);
+	int i;
+	for(i = 7; i >= 0; i--){
+		rom[i] = OW_readByte();
+	}
+}
+
+/**
+ * Transmits a command to the sensor
+ * @param cmd Command byte
+ */
+
+void DS18B20_cmd(uint8_t cmd) {
+	if(OW_reset()){
+		logger_error("Unable to reset 1-Wire Bus, no device present %s\n", strerror(errno));
+	}
+	OW_writeByte(SKIP_ROM);
+	OW_writeByte(cmd);
+}
+
+/**
+ * Reads the Temperature of the DS18B20 Sensor
+ * It rounds the value read from the Sensor rounded to the next integer
+ */
+
+int8_t DS18B20_readTemp (void){
+	DS18B20_cmd(CONVERT_T);
+	while(!OW_readBit()){
+		//wait to finish conversion
+		//TODO don't wait forever
+	}
+	uint8_t scratchPad[9];
+	uint8_t i;
+	int8_t temp;
+
+	DS18B20_cmd(READ_SCRATCH);
+	for(i = 0; i < 9; i++){
+		scratchPad[i] = OW_readByte();
+	}
+	//Information about temperature is stored in byte[0] and byte[1]
+	temp = scratchPad[0] >> 4;
+	temp |= (scratchPad[1] << 4) & 0xF0;
+	return temp;
+	//todo return a float value and do proper rounding
+}

+ 21 - 0
CoffeeCode/DS18B20.h

@@ -0,0 +1,21 @@
+/*
+ * DS18B20.h
+ *
+ *  Created on: Feb 28, 2018
+ *      Author: sebastian
+ */
+
+#ifndef DS18B20_H_
+#define DS18B20_H_
+
+
+#include <inttypes.h>
+#include "OneWire.h"
+
+
+void DS18B20_readRom(void);
+void DS18B20_init(void);
+int8_t DS18B20_readTemp (void);
+
+
+#endif /* DS18B20_H_ */

+ 140 - 0
CoffeeCode/OneWire.cpp

@@ -0,0 +1,140 @@
+/*
+ * OneWire.cpp
+ *
+ *  Created on: Feb 28, 2018
+ *      Author: sebastian
+ */
+
+#include "OneWire.h"
+/*
+ * timings in us from
+ * https://www.maximintegrated.com/en/app-notes/index.mvp/id/126
+ */
+uint16_t OWtiming[] = { 6, 64, 60, 10, 9, 55, 0, 480, 70, 410 };
+
+/**
+ * Initializes the 1-Wire communication
+ */
+
+void OneWire_init() {
+	pinMode(OW_PIN, OUTPUT);
+	//pullUpDnControl(OW_PIN, PUD_UP); the pull up is already on board
+}
+
+/**
+ * Writes one byte on the 1-Wire Bus
+ */
+
+void OW_writeByte(uint8_t byte) {
+	uint8_t loop;
+	// Loop to write each bit in the byte, LS-bit first
+	for (loop = 0; loop < 8; loop++) {
+		OW_writeBit(byte & 0x01);
+		// shift the data byte for the next bit
+		byte >>= 1;
+	}
+}
+
+/**
+ * Reads a full byte from the 1-Wire Bus
+ */
+
+uint8_t OW_readByte(void) {
+	uint8_t loop, result = 0;
+	for (loop = 0; loop < 8; loop++) {
+		// shift the result to get it ready for the next bit
+		result >>= 1;
+		// if result is one, then set MS bit
+		if (OW_readBit())
+			result |= 0x80;
+	}
+	return result;
+}
+
+/**
+ * Simultaneous write and read from the 1-Wire Bus
+ * OW_touchByte(0xFF) is equivalent to OW_readByte()
+ * and OW_touchByte(data) is equivalent to OW_writeByte(data)
+ */
+
+uint8_t OW_touchByte(uint8_t data) {
+	uint8_t loop, result = 0;
+	for (loop = 0; loop < 8; loop++) {
+		// shift the result to get it ready for the next bit
+		result >>= 1;
+
+		// If sending a '1' then read a bit else write a '0'
+		if (data & 0x01) {
+			if (OW_readBit())
+				result |= 0x80;
+		} else
+			OW_writeBit(0);
+
+		// shift the data byte for the next bit
+		data >>= 1;
+	}
+	return result;
+}
+
+/**
+ * function which sends and receives a block of data on the 1-Wire Bus
+ */
+
+void OW_block(uint8_t *data, uint8_t data_len) {
+	int loop;
+	for (loop = 0; loop < data_len; loop++) {
+		OW_touchByte(data[loop]);
+	}
+}
+
+/**
+ * Writes one bit on the 1-Wire Bus
+ */
+
+void OW_writeBit(uint8_t bit) {
+	if (bit) { //1
+		digitalWrite(OW_PIN, LOW);
+		delayMicroseconds(OWtiming[0]);
+		digitalWrite(OW_PIN, HIGH);
+		delayMicroseconds(OWtiming[1]);
+	} else {
+		digitalWrite(OW_PIN, LOW);
+		delayMicroseconds(OWtiming[2]);
+		digitalWrite(OW_PIN, HIGH);
+		delayMicroseconds(OWtiming[3]);
+	}
+}
+
+/**
+ * Reads one bit from the 1-Wire Bus and returns it
+ */
+
+uint8_t OW_readBit(void) {
+	digitalWrite(OW_PIN, LOW);
+	delayMicroseconds(OWtiming[0]);
+	digitalWrite(OW_PIN, HIGH);
+	delayMicroseconds(OWtiming[4]);
+	pinMode(OW_PIN, INPUT);
+	uint8_t value = digitalRead(OW_PIN);
+	delayMicroseconds(OWtiming[5]);
+	pinMode(OW_PIN, OUTPUT);
+	return value;
+}
+
+/**
+ * Performs a Reset on the 1-Wire Bus and returns either 1 or 0:
+ * 1: if no device is present and 0 if device(s) are present
+ */
+
+uint8_t OW_reset() {
+	delayMicroseconds(OWtiming[6]);
+	digitalWrite(OW_PIN, LOW);
+	delayMicroseconds(OWtiming[7]);
+	digitalWrite(OW_PIN, HIGH);
+	delayMicroseconds(OWtiming[8]);
+	pinMode(OW_PIN, INPUT);
+	uint8_t value = digitalRead(OW_PIN);
+	delayMicroseconds(OWtiming[9]);
+	pinMode(OW_PIN, OUTPUT);
+	return value;
+}

+ 34 - 0
CoffeeCode/OneWire.h

@@ -0,0 +1,34 @@
+/*
+ * OneWire.h
+ *
+ *  Created on: Feb 28, 2018
+ *      Author: sebastian
+ */
+
+#ifndef ONEWIRE_H_
+#define ONEWIRE_H_
+
+#include <wiringPi.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <unistd.h>
+
+#include "global.h"
+#include "logger.h"
+
+
+#define OW_PIN	5
+
+
+void OneWire_init(void);
+void OW_writeByte(uint8_t byte);
+uint8_t OW_readByte(void);
+uint8_t OW_touchByte(uint8_t data);
+void OW_block(uint8_t *data, uint8_t data_len);
+void OW_writeBit(uint8_t bit);
+uint8_t OW_readBit(void);
+uint8_t OW_reset(void);
+
+#endif /* ONEWIRE_H_ */

+ 1 - 1
CoffeeCode/buildno

@@ -1 +1 @@
-181
+216

+ 118 - 25
CoffeeCode/coffee.cpp

@@ -22,15 +22,19 @@
 #include "logger.h"
 #include "timer.h"
 #include "database.h"
-#include "events.h"
+
 
 coffee_status_t state;
 int sigValue;
 int brewTime; //Brew time in ms
 timer brewTimer(&brewTimeHandler);
 
+uint64_t totalHeatingTime; //local copies of the corresponding database entries
+uint16_t brewCounter;
+coffee_status_t lastState;
+
 
-const char* StateName[] = {"OFF", "HEATING", "INITHEAT", "IDLE", "BREW", "BREWMAN", "CLEAN", "ERROR"};
+const char* StateName[] = {"OFF", "HEATING", "INITHEAT", "IDLE", "BREW", "BREWMAN", "CLEAN", "ERROR", "WAITOFF"};
 
 /**
  * Thread for the finite state machine
@@ -53,6 +57,22 @@ void *coffeeThread(void *threadid) {
 	brewTimer.stop();
 	brewTime = 0;
 
+	lastState = STATE_OFF;
+
+	//read the database values
+	if(!(totalHeatingTime = sqlGetConf(CFGHeatingTime))){
+		logger_error("coffee.cpp: Couldn't read the heating time from the database");
+		//pthread_exit(EXIT_SUCCESS);
+		exit(EXIT_FAILURE);
+	}
+	if(!(brewCounter = sqlGetConf(CFGbrewcounter))){
+		logger_error("coffee.cpp: Couldn't read the brew counter from the database");
+		//pthread_exit(EXIT_SUCCESS);
+		exit(EXIT_FAILURE);
+	}
+
+	event_subscribe("terminate", &coffeeTerminate);
+
 	logger(V_BREW, "Determining inital state\n");
 	//determine inital state
 	if (halGetRelaisState(RELAIS_POWER) && halGetRelaisState(RELAIS_HEAT)
@@ -82,7 +102,7 @@ void *coffeeThread(void *threadid) {
 			if (SigValueEmpty())
 				pause();
 			if (getSigValue() == SigInt0Rls) {
-				if (halProxSensorCovered()) { //Check Waterlevel
+				if (!halProxSensorCovered()) { //Check waterlevel in gray water tank
 					//turn machine on
 					halMachineOn();
 					sleep(1);
@@ -92,12 +112,38 @@ void *coffeeThread(void *threadid) {
 						changeState(STATE_IDLE);
 					}
 				} else {
+					logger_error("Empty Tank please!\n");
 					changeState(STATE_ERROR);
 				}
 				break;
 			}
 			break;
+			/*
+			 *
+			*/
+		case STATE_WAIT_OFF:
+			if (halIsHeating()) {
+				halMachineOff();
+				coffeeIncreaseHeatingTime(halgetHeatingTime());
+				writeBackCache();
+				changeState(STATE_OFF);
+			}
+			if (SigValueEmpty())
+				pause();
+			switch (getSigValue()) {
+			case SigPressOpn:
+				halMachineOff();
+				coffeeIncreaseHeatingTime(halgetHeatingTime());
+				writeBackCache();
+				changeState(STATE_OFF);
+				break;
 
+			case SigInt0Psh:
+			case SigInt1Psh:
+				changeState(STATE_INITALHEATING);
+				break;
+			}
+			break;
 			/*
 			 *
 			 */
@@ -109,8 +155,14 @@ void *coffeeThread(void *threadid) {
 				//Turn machine off again
 				halMachineOff();
 				coffeeIncreaseHeatingTime(halgetHeatingTime());
+				writeBackCache();
 				changeState(STATE_OFF);
 				break;
+
+			case SigInt1Rls:
+				changeState(STATE_WAIT_OFF);
+				break;
+
 			case SigPressOpn:
 				//Inital heating finished
 				coffeeIncreaseHeatingTime(halgetHeatingTime());
@@ -127,19 +179,28 @@ void *coffeeThread(void *threadid) {
 				pause();
 			switch (getSigValue()) {
 			case SigInt1RlsLong:
-				//Turn machine off again
+				//Turn machine _immediately_ off again
 				halMachineOff();
 				coffeeIncreaseHeatingTime(halgetHeatingTime());
+				writeBackCache();
 				changeState(STATE_OFF);
 				break;
+
+			case SigInt1Rls:
+				//turn machine off when heating is finished
+				changeState(STATE_WAIT_OFF);
+			break;
+
 			case SigPressOpn:
 				coffeeIncreaseHeatingTime(halgetHeatingTime());
 				changeState(STATE_IDLE);
 				break;
+
 			case SigInt0Psh:
 				//start to brew a delicious coffee
 				changeState(STATE_BREW);
 				break;
+
 			case SigBrewOn:
 				//someone brews manually
 				changeState(STATE_BREWMANUAL);
@@ -154,17 +215,31 @@ void *coffeeThread(void *threadid) {
 				pause();
 			switch (getSigValue()) {
 			case SigInt1RlsLong:
-				//Turn machine off again
+				//turn machine _immediately_ off
 				halMachineOff();
+				writeBackCache();
 				changeState(STATE_OFF);
 				break;
+
+			case SigInt1Rls:
+				//turn machine off when heating is finished
+				changeState(STATE_WAIT_OFF);
+				break;
+
 			case SigPressCls:
 				changeState(STATE_HEATING);
 				break;
+
 			case SigInt0Psh:
 				//start to brew a delicious coffee
-				changeState(STATE_BREW);
+				if(!halProxSensorCovered()){
+					changeState(STATE_BREW);
+				}
+				else {
+					changeState(STATE_ERROR);
+				}
 				break;
+
 			case SigBrewOn:
 				//someone brews manually
 				changeState(STATE_BREWMANUAL);
@@ -177,7 +252,12 @@ void *coffeeThread(void *threadid) {
 		case STATE_BREW:
 			coffeeBrew();
 			logger(V_BREW, "Finishing brewing\n");
-			changeState(STATE_IDLE);
+			if(!halProxSensorCovered()){
+				changeState(STATE_IDLE);
+			}
+			else {
+				changeState(STATE_ERROR);
+			}
 			break;
 
 			/*
@@ -202,8 +282,13 @@ void *coffeeThread(void *threadid) {
 		case STATE_ERROR:
 			if (SigValueEmpty())
 				pause();
+		switch (getSigValue()) {
+		case SigInt1Psh:
+		case SigInt0Psh:
+			changeState(RETURN_STATE);
 			break;
 		}
+		}
 	}
 	pthread_exit(EXIT_SUCCESS);
 }
@@ -218,7 +303,7 @@ void *coffeeThread(void *threadid) {
 void coffeeHandler(int signum, siginfo_t *siginfo, void *context) {
 	sigval_t sigVal = (siginfo->si_value);
 	sigValue = sigVal.sival_int;
-	logger(V_BREW, "CoffeeHandler called with %d\n", sigValue);
+	logger(V_BREW, "coffee.cpp: CoffeeHandler called with Signal %d\n", sigValue);
 }
 
 /**
@@ -244,9 +329,12 @@ bool SigValueEmpty(void) {
  * @param newState
  */
 void changeState(coffee_status_t newState) {
+	if(newState == RETURN_STATE)
+		newState = lastState;
+
 	logger(V_BREW, "Changing state to %s\n", StateName[newState]);
+	lastState = state;
 	state = newState;
-
 	event_trigger("statechange", &state, sizeof(state));
 }
 
@@ -268,11 +356,28 @@ void brewTimeHandler(void) {
 /**
  * handles program termination
  */
-void coffeeTerminate(void) {
-	logger_error("Coffee thread terminated");
+void coffeeTerminate(event_t *event) {
+	logger(V_BREW, "Coffee.cpp: Thread terminating");
 	//stop brewing
 	halRelaisOff(RELAIS_PUMP);
 	brewTimer.stop();
+	writeBackCache();
+}
+
+/**
+ * Function to write back the values of the local copies
+ * brewCounter and totalHeatingTime
+ *
+ */
+void writeBackCache(void){
+	if (sqlSetConf(CFGbrewcounter, brewCounter)) {
+		logger_error("coffee.cpp: Couldn't write brewcounter to database");
+		return;
+	}
+	if (sqlSetConf(CFGHeatingTime, totalHeatingTime)) {
+		logger_error("coffee.cpp: Couldn't write heating time to database");
+		return;
+	}
 }
 
 /**
@@ -326,7 +431,6 @@ void coffeeBrew(void) {
 	halResetFlow();
 	brewTimer.stop();
 	brewTime = 0;
-	changeState(STATE_IDLE);
 	return;
 }
 
@@ -334,24 +438,13 @@ void coffeeBrew(void) {
  *
  */
 void coffeeIncreaseBrewCounter(void) {
-	uint64_t brewcounter = sqlGetConf(CFGbrewcounter);
-	if (sqlSetConf(CFGbrewcounter, ++brewcounter)) {
-		logger_error("Couldn't write brewcounter to database");
-	}
+	brewCounter++;
 }
-/**
- *
- */
-void coffeeIncreaseHeatingTime(clock_t begin, clock_t end){
 
-}
 
 /**
  *
  */
 void coffeeIncreaseHeatingTime(uint64_t heatingTime) {
-	uint64_t totalHeatingTime = sqlGetConf(CFGHeatingTime);
-	if (sqlSetConf(CFGHeatingTime, (totalHeatingTime + heatingTime))) {
-		logger_error("Couldn't write heating time to database");
-	}
+	totalHeatingTime += heatingTime;
 }

+ 7 - 2
CoffeeCode/coffee.h

@@ -7,7 +7,9 @@
 
 #ifndef COFFEE_H_
 #define COFFEE_H_
+
 #include <csignal>
+#include "events.h"
 
 //define status
 typedef enum {
@@ -18,7 +20,9 @@ typedef enum {
 	STATE_BREW,
 	STATE_BREWMANUAL,
 	STATE_CLEANING,
-	STATE_ERROR
+	STATE_ERROR,
+	STATE_WAIT_OFF,
+	RETURN_STATE
 } coffee_status_t;
 
 extern const char* StateName[];
@@ -35,7 +39,8 @@ bool SigValueEmpty(void);
 void changeState(coffee_status_t newState);
 coffee_status_t getState(void);
 void brewTimeHandler(void);
-void coffeeTerminate(void);
+void writeBackCache(void);
+void coffeeTerminate(event_t *event);
 void coffeeBrew(void);
 void coffeeIncreaseBrewCounter(void);
 void coffeeIncreaseHeatingTime(uint64_t heatingTime);

+ 21 - 0
CoffeeCode/display.cpp

@@ -4,7 +4,22 @@
  *  Created on: Sep 26, 2017
  *      Author: Philipp Hinz
  */
+#include <stdlib.h>
+#include <pthread.h>
+#include <time.h>
+#include <unistd.h>
+#include <string.h>
+
+#include "global.h"
 #include "display.h"
+#include "logger.h"
+#include "database.h"
+#include "timer.h"
+#include "lcd.h"
+#include "coffee.h"
+#include "hal.h"
+#include "events.h"
+
 
 display_lang_t displayLang;
 timer displayTimer(displayTimerHandler);
@@ -154,6 +169,7 @@ void* displayTimerHandler(void* threadid) {
 		case STATE_IDLE:
 		case STATE_CLEANING:
 		case STATE_ERROR:
+		case STATE_WAIT_OFF:
 			displayRefresh();
 			break;
 		default:
@@ -251,6 +267,11 @@ void displayRefresh(void) {
 		displayPrintLn(1, displayGetString(str_error), true);
 		break;
 
+	case STATE_WAIT_OFF:
+		displayPrintLn(0, "CoffeePi", true);
+		displayPrintLn(1, displayGetString(str_waitoff), true);
+		break;
+
 	case STATE_OFF:
 	default:
 		displayPrintLn(0, "CoffeePi", true);

+ 7 - 12
CoffeeCode/display.h

@@ -8,19 +8,7 @@
 #ifndef DISPLAY_H_
 #define DISPLAY_H_
 #include <string>
-#include <stdlib.h>
-#include <pthread.h>
-#include <time.h>
-#include <unistd.h>
-#include <string.h>
-#include "global.h"
-#include "logger.h"
-#include "database.h"
-#include "timer.h"
-#include "lcd.h"
 #include "coffee.h"
-#include "hal.h"
-#include "events.h"
 
 typedef enum {
 	lang_de,
@@ -37,6 +25,7 @@ typedef enum {
 	str_error,
 	str_flow,
 	str_bye,
+	str_waitoff,
 	str_last
 } display_strings_t;
 
@@ -93,6 +82,12 @@ static const display_string_t display_strings[str_last] =
 						"Auf wiedersehen",
 						"Good bye"
 				}
+		},
+		{
+				{
+						"Ausschalten...",
+						"Turning off..."
+				}
 		}
 };
 

+ 29 - 13
CoffeeCode/hal.cpp

@@ -20,11 +20,17 @@ volatile int flowcnt = 0;
 int Int0Time, Int1Time;
 bool flagIgnoreRlsInt0, flagIgnoreRlsInt1;
 
+//storage for the last state of the buttons and the proximity sensor
+int pinState[3] = {0, 0, 0};
+
 timer Int0Timer(&halInt0TimerHandler);
 timer Int1Timer(&halInt1TimerHandler);
 
 clock_t heatingCycle[2] = {0, 0};
 
+//delay of the debounce in milliseconds
+#define DELAY_DEBOUNCE	50
+
 /**
  * Initializes HAL
  */
@@ -128,8 +134,11 @@ int halGetRelaisState(int relais) {
  * Interrupt routine for Int0 (Top button)
  */
 void halInt0(void) {
-	if (halGetInt0()) { //released
+	//wait for a debounce
+	delay(DELAY_DEBOUNCE);
+	if (halGetInt0() && !pinState[0]) { //released
 		logger(V_HAL, "Int0 released\n");
+		pinState[0] = 1;
 		if (flagIgnoreRlsInt0) {
 			flagIgnoreRlsInt0 = false;
 		} else {
@@ -137,8 +146,9 @@ void halInt0(void) {
 			Int0Timer.stop();
 			halSendSignal(SigInt0Rls);
 		}
-	} else { //pressed
+	} else if(!halGetInt0() && pinState[0]) { //pressed
 		logger(V_HAL, "Int0 pushed\n");
+		pinState[0] = 0;
 		halSendSignal(SigInt0Psh);
 		Int0Time = 0;
 		Int0Timer.start();
@@ -162,8 +172,10 @@ void halInt0TimerHandler(void) {
  * Interrupt routine for Int1 (Bottom button)
  */
 void halInt1(void) {
-	if (halGetInt1()) {
+	delay(DELAY_DEBOUNCE);
+	if (halGetInt1() && !pinState[1]) {
 		logger(V_HAL, "Int1 released\n");
+		pinState[1] = 1;
 		if (flagIgnoreRlsInt1) {
 			flagIgnoreRlsInt1 = false;
 		} else {
@@ -171,8 +183,9 @@ void halInt1(void) {
 			Int0Timer.stop();
 			halSendSignal(SigInt1Rls);
 		}
-	} else {
+	} else if(!halGetInt1() && pinState[1]) {
 		logger(V_HAL, "Int1 pushed\n");
+		pinState[1] = 0;
 		halSendSignal(SigInt1Psh);
 		Int1Time = 0;
 		Int1Timer.start();
@@ -198,7 +211,7 @@ void halInt1TimerHandler(void) {
  */
 void halIntFlow(void) {
 	//halRelaisOff(RELAIS_POWER);
-	//logger(V_HAL, "IntFlow triggered #%d total: %.2fml\n", flowcnt, halGetFlow());
+	logger(V_HAL, "IntFlow triggered #%d total: %.2fml\n", flowcnt, halGetFlow());
 	if (flowcnt == 99) {
 		halRelaisOff(RELAIS_PUMP);
 	}
@@ -226,12 +239,12 @@ void halIntPressure(void) {
  * If called during a heating process, it returns the time elapsed since the heating started
  * If called after a heating process, it returns the total time elapes during the heating cycle
  */
-uint64_t halgetHeatingTime(void){
+double halgetHeatingTime(void){
 	if (halIsHeating()) {
-		return (uint64_t)(clock() - heatingCycle[0]) / CLOCKS_PER_SEC;
+		return (double)(clock() - heatingCycle[0]) / CLOCKS_PER_SEC;
 	}
 	else {
-		return (uint64_t)(heatingCycle[1] - heatingCycle[0]) / CLOCKS_PER_SEC;
+		return (double)(heatingCycle[1] - heatingCycle[0]) / CLOCKS_PER_SEC;
 	}
 }
 
@@ -239,11 +252,14 @@ uint64_t halgetHeatingTime(void){
  * Method to handle toggle of the proximity sensor
  */
 void halIntProximity(void) {
-	logger(V_HAL, "IntProximity triggered\n");
-	return;
-	if (halProxSensorCovered()) {
+	delay(DELAY_DEBOUNCE);
+	if (halProxSensorCovered() && !pinState[2]) {
+		logger(V_HAL, "IntProximity triggered\n");
+		pinState[2] = 1;
 		halSendSignal(SigProxCvrd);
-	} else {
+	} else if(!halProxSensorCovered() && pinState[2]){
+		logger(V_HAL, "IntProximity triggered\n");
+		pinState[2] = 0;
 		halSendSignal(SigProxOpn);
 	}
 }
@@ -313,7 +329,7 @@ void halSendSignal(HalSig val) {
 
 	try {
 		if (pthread_sigqueue(thread[THREAD_COFFEE], SIGUSR2, value)) {
-			logger_error("Failed to queue signal %d %s\n", val,
+			logger_error("hal.cpp: Failed to queue signal %d %s\n", val,
 					strerror(errno));
 			//No Signals reach the state machine anymore...
 			exit(EXIT_FAILURE);

+ 3 - 1
CoffeeCode/hal.h

@@ -52,6 +52,8 @@ enum HalSig {
 	SigBrewOff = 12
 };
 
+
+
 void halInit(void);
 void halRelaisOn(int relais);
 void halRelaisOff(int relais);
@@ -61,7 +63,7 @@ void halInt0(void);
 void halInt1(void);
 void halIntFlow(void);
 void halIntPressure(void);
-uint64_t halgetHeatingTime(void);
+double halgetHeatingTime(void);
 void halIntProximity(void);
 float halGetFlow(void);
 void halResetFlow(void);

+ 15 - 12
CoffeeCode/main.cpp

@@ -26,7 +26,7 @@
 #include "display.h"
 #include "server.h"
 #include "events.h"
-#include "temperatur.h"
+#include "DS18B20.h"
 
 const int buildno = (1+(int)(
 #include "buildno"
@@ -126,7 +126,11 @@ int main(int argc, char *argv[]) {
 	logger_reset();
 	initTimers();
 	displayInit();
-	temperatur_init();
+	//temperatur_init();
+	DS18B20_init();
+	sqlOpen();
+	sqlSetup();
+
 
 	// http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm
 
@@ -143,9 +147,9 @@ int main(int argc, char *argv[]) {
 	if (rc != 0)
 		throw(L"pthread_mutexattr_destroy returns " + rc);
 
-	for (int i = 0; i < 5; i++) {
+	for (int i = 0; i < 5; i++) {//TODO this can be simplified!
 		if (i == THREAD_MAIN)
-			rc = pthread_create(&thread[i], NULL, mainLoop, (void *) i);
+			rc = pthread_create(&thread[i], &attr, mainLoop, (void *) i);
 		else if (i == THREAD_STRIPE)
 			rc = pthread_create(&thread[i], NULL, stripeThread, (void *) i);
 		else if (i == THREAD_DISPLAY)
@@ -159,9 +163,10 @@ int main(int argc, char *argv[]) {
 			exit(-1);
 		}
 	}
-
+	//hal might send signals to processes which must exist!
 	halInit();
 
+
 	// free attribute and wait for the other threads
 	pthread_attr_destroy(&attr);
 
@@ -234,16 +239,14 @@ void killThread(int threadid, int sig) {
  */
 
 void *mainLoop(void *threadid) {
-	sqlOpen();
 
-	sqlSetup();
 
 	// Do more stuff here
-//	while (1){
-//		sleep(4);
-//		int16_t temp = get_temperatur();
-//		logger(V_BASIC, "Temperatur: %d\n", temp);
-//	}
+	while (1){
+		sleep(4);
+		int8_t temp = DS18B20_readTemp();
+		logger(V_BASIC, "Temperature: %d\n", temp);
+	}
 
 	logger(V_BASIC, "Thread goes Sleeping..\n");
 	while (1) {

+ 1 - 1
CoffeeCode/timer.cpp

@@ -167,7 +167,7 @@ void initTimers(void) {
 
 	/* Establish handler for timer signal */
 
-	logger(V_BASIC, "Establishing handler for signal %d\n", SIG);
+	logger(V_BASIC, "timer.cpp: Establishing handler for signal %d\n", SIG);
 	sa.sa_flags = SA_SIGINFO;
 	sa.sa_sigaction = &timer_handler;
 	sigemptyset(&sa.sa_mask);