Bladeren bron

Finished the Onewire Lib and the Temp sensor Lib (not tested yet), coffee now with the feature of a segmentation fault...

Sebastian 7 jaren geleden
bovenliggende
commit
341d6c7cd5

+ 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"/>

+ 14 - 2
CoffeeCode/DS18B20.cpp

@@ -28,12 +28,13 @@ double deviceID;
  */
 
 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;
 	}
 	DS18B20_readRom();
-	deviceID = (double (ROM[6]) | ROM[5] << 8 | ROM[5] << 16 | ROM[5] << 24 | ROM[5] << 32 | ROM[5] << 40);
+	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 Temperatur Sensor on 1-Wire Bus\n");
 		logger(V_HAL, "Device ID: %12X", deviceID);
@@ -48,7 +49,12 @@ void DS18B20_readRom(void){
 	}
 }
 
-uint16_t DS18B20_readTemp (void){
+/**
+ * Reads the Temperatur of the DS18B20 Sensor
+ * It rounds the value read from the Sensor rounded to the next integer
+ */
+int8_t DS18B20_readTemp (void){
+	OW_writeByte(SKIP_ROM);
 	OW_writeByte(CONVERT_T);
 	while(!OW_readBit()){
 		//wait to finish conversion
@@ -57,7 +63,13 @@ uint16_t DS18B20_readTemp (void){
 	OW_writeByte(READ_SCRATCH);
 	uint8_t scratchPad[9];
 	uint8_t i;
+	int8_t temp;
 	for(i = 0; i < 9; i++){
 		scratchPad[i] = OW_readByte();
 	}
+	//Information about temperatur 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
 }

+ 3 - 0
CoffeeCode/DS18B20.h

@@ -14,5 +14,8 @@
 
 
 void DS18B20_readRom(void);
+void DS18B20_init(void);
+int8_t DS18B20_readTemp (void);
+
 
 #endif /* DS18B20_H_ */

+ 2 - 3
CoffeeCode/OneWire.cpp

@@ -10,7 +10,7 @@
  * timings in us from
  * https://www.maximintegrated.com/en/app-notes/index.mvp/id/126
  */
-uint8_t OWtiming[] = { 6, 64, 60, 10, 9, 55, 0, 480, 70, 410 };
+uint16_t OWtiming[] = { 6, 64, 60, 10, 9, 55, 0, 480, 70, 410 };
 
 /**
  * Initializes the 1-Wire communication
@@ -18,8 +18,7 @@ uint8_t OWtiming[] = { 6, 64, 60, 10, 9, 55, 0, 480, 70, 410 };
 
 void OneWire_init() {
 	pinMode(OW_PIN, OUTPUT);
-	pullUpDnControl(OW_PIN, PUD_UP);
-	//digitalWrite(SHIFT_G, HIGH);
+	//pullUpDnControl(OW_PIN, PUD_UP); the pull up is already on board
 }
 
 /**

+ 9 - 0
CoffeeCode/OneWire.h

@@ -22,4 +22,13 @@
 #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
+185

+ 3 - 3
CoffeeCode/hal.cpp

@@ -226,12 +226,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;
 	}
 }
 

+ 1 - 1
CoffeeCode/hal.h

@@ -61,7 +61,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);

+ 9 - 8
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,8 @@ int main(int argc, char *argv[]) {
 	logger_reset();
 	initTimers();
 	displayInit();
-	temperatur_init();
+	//temperatur_init();
+	DS18B20_init();
 
 	// http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm
 
@@ -143,7 +144,7 @@ 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);
 		else if (i == THREAD_STRIPE)
@@ -239,11 +240,11 @@ void *mainLoop(void *threadid) {
 	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, "Temperatur: %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);