Bläddra i källkod

Code cleanup and some fixes

Philipp Hinz 8 år sedan
förälder
incheckning
8a6fb5f86f

+ 4 - 35
CoffeeCode/README.md

@@ -1,25 +1,8 @@
-# Speedmanager Readme #
-Requirements: wiringpi
-## Usage Speedmanager ##
+# CoffeePi Readme #
+Requirements: wiringpi pthread
+## Usage CoffeePi ##
 
-    ~$ ./speedmanager -h
-    Speedmanager for Raspberry Pi by Philipp Hinz - Build number 921 short GUID 0x3c0
-    Usage: /opt/hinz-ba-can/code/Pi code/Debug/speedmanager [-hvVteilfads] [-r ohms]
-            -h              Prints this help
-            -v              Sets verbose output, can be used multiple times
-            -r ohms         Sets the start resistance of the digital potentiometer
-            -V              Prints only the version and exits
-            -t              Terminates the CAN Bus with a connected DiPoti
-            -e              Echoes all incoming CAN messages (Debugging)
-            -i              Increases the Wiper of the DigiPot every second
-            -l              Sets the MCP2515 to listen only mode on shutdown
-            -f              Forces a speedtest after startup
-            -s              Suppress a speedtest in case of errors
-            -a              Add new nodes if not known in file nodes
-            -d              Prints a timestamp in front of every message
-    Listening to the following signals:
-            SIGUSR1(10)     Prints monitor data of all nodes
-            SIGHUP(1)       Runs a speedtest starting at default speed
+    ~$ ./coffecode -h
 
 Debug levels:
  1. Basic debugging, process informations
@@ -28,20 +11,6 @@ Debug levels:
  4. SQL querys
  5. CAN error states
  6. SPI communication
- 
-## Usage canspeed ##
-This helper programm outputs the register configuration for different CAN controllers. So far supported: MCP2510/15 and AT90CAN.
-
-    ~$ ./canspeed -h
-	Usage: /opt/hinz-ba-can/code/Pi code/Debug/canspeed -c Clockspeed -t Type [-ro] [-b Baudrate] [-q #TQ]
-			-h              Prints this help
-			-c              Cockspeed in MHz, 16 MHz recommended
-			-t              CAN controller type (1 - MCP2510/15, 2 - AT90CAN)
-			-b              Calculate settings for a single baudrate in bit/s
-			-q              Number of TQ per bit, default is 16
-			-r              Output register settings (use without -o)
-			-o              Output the constant definitions for the controller
-	Example: ./canspeed -c 16 -t 1 -o
 
 ## Sourcecode ##
 The program source is distributed between multiple files. Please refer to the documentation of the files for their content.

+ 4 - 10
CoffeeCode/Release/makefile

@@ -38,27 +38,21 @@ endif
 # Add inputs and outputs from these tool invocations to the build variables 
 
 # All Target
-all: speedmanager canspeed
+all: coffeecode
 
 # Tool invocations
-speedmanager: $(OBJS) $(USER_OBJS)
+coffeecode: $(OBJS) $(USER_OBJS)
 	@echo 'Building target: $@'
 	@echo 'Invoking: G++ Linker'
-	arm-linux-gnueabihf-g++ -o "speedmanager" $(OBJS) $(USER_OBJS) $(LIBS)
+	arm-linux-gnueabihf-g++ -o "coffeecode" $(OBJS) $(USER_OBJS) $(LIBS)
 	@echo 'Finished building target: $@'
 	@echo ' '
 
-canspeed:  $(OBJS2)
-	@echo 'Building target: $@'
-	@echo 'Invoking: Cross G++ Linker'
-	arm-linux-gnueabihf-g++ -Os -o canspeed $(OBJS2) $(LIBS)
-	@echo 'Finished building target: $@'
-	@echo ' '
 
 
 # Other Targets
 clean:
-	-$(RM) $(C++_DEPS)$(OBJS)$(C_DEPS)$(CC_DEPS)$(CPP_DEPS)$(EXECUTABLES)$(CXX_DEPS)$(C_UPPER_DEPS) speedmanager canspeed*
+	-$(RM) $(C++_DEPS)$(OBJS)$(C_DEPS)$(CC_DEPS)$(CPP_DEPS)$(EXECUTABLES)$(CXX_DEPS)$(C_UPPER_DEPS) coffeecode*
 	-@echo ' '
 
 .PHONY: all clean dependents

+ 6 - 41
CoffeeCode/Release/subdir.mk

@@ -4,68 +4,33 @@
 
 # Add inputs and outputs from these tool invocations to the build variables 
 CPP_SRCS += \
-../can.cpp \
-../cantiming.cpp \
 ../crc.cpp \
 ../database.cpp \
-../dipoti.cpp \
-../led.cpp \
+../hal.cpp \
+../lcd.cpp \
 ../logger.cpp \
 ../main.cpp \
-../mcp2510.cpp \
-../monitor.cpp \
-../node.cpp \
 ../spi.cpp \
-../test.cpp \
 ../timer.cpp 
 
 OBJS += \
-./can.o \
-./cantiming.o \
 ./crc.o \
 ./database.o \
-./dipoti.o \
-./led.o \
+./hal.o \
+./lcd.o \
 ./logger.o \
 ./main.o \
-./mcp2510.o \
-./monitor.o \
-./node.o \
 ./spi.o \
-./test.o \
 ./timer.o 
 
-OBJS2 += \
-./can.o \
-./cantiming.o \
-./canspeed.o \
-./crc.o \
-./database.o \
-./dipoti.o \
-./led.o \
-./logger.o \
-./monitor.o \
-./node.o \
-./spi.o \
-./test.o \
-./timer.o \
-./mcp2510.o \
-./sqlite/sqlite3.o
-
 CPP_DEPS += \
-./can.d \
-./cantiming.d \
 ./crc.d \
 ./database.d \
-./dipoti.d \
-./led.d \
+./hal.d \
+./lcd.d \
 ./logger.d \
 ./main.d \
-./mcp2510.d \
-./monitor.d \
-./node.d \
 ./spi.d \
-./test.d \
 ./timer.d 
 
 

+ 1 - 1
CoffeeCode/buildno

@@ -1 +1 @@
-14
+17

+ 0 - 18
CoffeeCode/global.h

@@ -14,10 +14,6 @@
 #define SPI_CHANNEL 	1		// 0 or 1 on Raspberry Pi
 #define SPI_SPEED		200000	// SPI clock speed in Hz
 
-#define LED_RED_PIN		7
-#define LED_YELLOW_PIN	3
-#define LED_GREEN_PIN	2
-
 #define TIMER_DELAY_US	50000	// Basic timer delay in us
 // SQL config
 
@@ -30,20 +26,6 @@ extern bool optEcho, optInc, optLom, optTerm, optAdd, optDate, optSuppress;
 extern pthread_mutex_t mutex;
 extern int lcd;
 
-// CAN Filter - Should Incoming messages be filtered in Hardware?
-// #defining FILTER_USAGE means yes, otherwise no filtering applies
-
-//#define FILTER_USAGE
-
-// If incoming messages are filtered, you have to define the filter properties.
-// If a bit n of MASK is set, the respective Message-Identifier-Bit must
-// be (equal to Bit n of MATCH0) OR (equal to Bit n of MATCH1).
-// Otherwise the message will be discarded.
-// Mask & Matches are in the range from 0x000 to 0x7FF.
-#define FILTER_MASK   0x0ff
-#define FILTER_MATCH0 0x084
-#define FILTER_MATCH1 0x094
-
 
 // Thread IDs
 #define THREAD_MAIN 	0

+ 22 - 1
CoffeeCode/hal.cpp

@@ -15,6 +15,9 @@
 
 int flowcnt = 0;
 
+/*
+ * Initializes HAL
+ */
 void halInit(void) {
 	halRelaisOn(RELAIS_HEAT);
 	halRelaisOff(RELAIS_PUMP);
@@ -38,14 +41,28 @@ void halInit(void) {
 		return;
 	}
 }
+
+/*
+ * Switches relais on
+ * @param relais Relais ID
+ */
 void halRelaisOn(int relais) {
 	halRelaisSet(relais, LOW);
 }
 
+/*
+ * Switches relais off
+ * @param relais Relais ID
+ */
 void halRelaisOff(int relais) {
 	halRelaisSet(relais, HIGH);
 }
 
+/*
+ * Switches relais to state
+ * @param relais Relais ID
+ * @param state LOW(0) or HIGH(1)
+ */
 void halRelaisSet(int relais, int state) {
 	if (state != HIGH && state != LOW)
 		return;
@@ -68,11 +85,12 @@ void halInt0(void) {
 void halInt1(void) {
 	//halRelaisOff(RELAIS_POWER);
 	logger(V_BASIC, "Int1 triggered\n");
+	flowcnt = 0;
 }
 
 void halIntFlow(void) {
 	//halRelaisOff(RELAIS_POWER);
-	logger(V_BASIC, "IntFlow triggered #%d total: %f\n", flowcnt, halGetFlow());
+	logger(V_BASIC, "IntFlow triggered #%d total: %.2fml\n", flowcnt, halGetFlow());
 	lcdPosition(lcd, 0, 0);
 	lcdPrintf(lcd, "ml = %.2f   ", halGetFlow());
 	if (flowcnt == 99) {
@@ -81,6 +99,9 @@ void halIntFlow(void) {
 	flowcnt++;
 }
 
+/*
+ * Returns total flow trough sensor in ml
+ */
 float halGetFlow(void) {
 	return flowcnt*FLOW_ML_PULSE;
 }

+ 7 - 7
CoffeeCode/lcd.cpp

@@ -108,7 +108,7 @@ static void strobe(const struct lcdDataStruct *lcd) {
 
 static void sendDataCmd(const struct lcdDataStruct *lcd, unsigned char data) {
 	register unsigned char myData = data;
-	unsigned char i, d4;
+	unsigned char d4;
 
 	if (lcd->bits == 4) {
 		d4 = (myData >> 4) & 0x0F;
@@ -131,17 +131,13 @@ static void sendDataCmd(const struct lcdDataStruct *lcd, unsigned char data) {
  */
 
 static void putCommand(const struct lcdDataStruct *lcd, unsigned char command) {
-	pthread_mutex_lock(&mutex);
 	digitalWrite(lcd->rsPin, 0);
 	sendDataCmd(lcd, command);
 	delay(2);
-	pthread_mutex_unlock(&mutex);
 }
 
 static void put4Command(const struct lcdDataStruct *lcd,
 		unsigned char command) {
-	register unsigned char myCommand = command;
-	register unsigned char i;
 
 	digitalWrite(lcd->rsPin, 0);
 	shift_data(command & 0x0F);
@@ -242,7 +238,9 @@ void lcdPosition(const int fd, int x, int y) {
 	if ((y > lcd->rows) || (y < 0))
 		return;
 
+	pthread_mutex_lock(&mutex);
 	putCommand(lcd, x + (LCD_DGRAM | rowOff[y]));
+	pthread_mutex_unlock(&mutex);
 
 	lcd->cx = x;
 	lcd->cy = y;
@@ -274,6 +272,7 @@ void lcdCharDef(const int fd, int index, unsigned char data[8]) {
 
 void lcdPutchar(const int fd, unsigned char data) {
 	struct lcdDataStruct *lcd = lcds[fd];
+	pthread_mutex_lock(&mutex);
 
 	digitalWrite(lcd->rsPin, 1);
 	sendDataCmd(lcd, data);
@@ -285,6 +284,7 @@ void lcdPutchar(const int fd, unsigned char data) {
 
 		putCommand(lcd, lcd->cx + (LCD_DGRAM | rowOff[lcd->cy]));
 	}
+	pthread_mutex_unlock(&mutex);
 }
 
 /*
@@ -308,9 +308,9 @@ void lcdPrintf(const int fd, const char *message, ...) {
 	va_list argp;
 	char buffer[1024];
 
-	va_start (argp, message);
+	va_start(argp, message);
 	vsnprintf(buffer, 1023, message, argp);
-	va_end (argp);
+	va_end(argp);
 
 	lcdPuts(fd, buffer);
 }

+ 0 - 146
CoffeeCode/led.cpp

@@ -1,146 +0,0 @@
-/*
- * led.cpp
- *
- *  Created on: Nov 9, 2015
- *      Author: Philipp Hinz
- */
-#include <wiringPi.h>
-#include <stdio.h>
-#include "global.h"
-#include "timer.h"
-#include "led.h"
-
-timer blinkTimer(&ledBlinkHandler); /**< timer for LED blinking */
-bool ledBlink[3]; /**< bool flag for LED blinking */
-bool ledBlinkO[3]; /**< bool flag for one time LED blinking */
-
-/**
- * Initializes the LEDs on the CAN Controller board
- */
-
-void initLed(void) {
-	pinMode(LED_RED_PIN, OUTPUT);
-	pinMode(LED_YELLOW_PIN, OUTPUT);
-	pinMode(LED_GREEN_PIN, OUTPUT);
-	ledOff(LED_RED);
-	ledOff(LED_YELLOW);
-	ledOff(LED_GREEN);
-
-	blinkTimer.setDivider(10);
-	blinkTimer.start();
-
-	for (int i = 0; i < 3; i++) {
-		ledBlink[i] = false;
-	}
-}
-
-/**
- * Internal function that returns the WiringPi pin to the given LED
- * @param led integer for one of the 3 LEDs
- * @return WiringPi pin
- */
-
-int ledGetPin(int led) {
-	switch (led) {
-	case LED_RED:
-		return LED_RED_PIN;
-		break;
-	case LED_YELLOW:
-		return LED_YELLOW_PIN;
-		break;
-	case LED_GREEN:
-		return LED_GREEN_PIN;
-		break;
-	default:
-		break;
-	}
-	return -1;
-}
-
-/**
- * Set the LED pin to a value.
- * @param led integer for one of the 3 LEDs
- * @param value HIGH or LOW
- */
-
-void ledSet(int led, int value) {
-	digitalWrite(ledGetPin(led), value);
-}
-
-/**
- * Turn the LED on
- * @param led integer for one of the 3 LEDs
- */
-
-void ledOn(int led) {
-	ledSet(led, LOW);
-}
-
-/**
- * Turn the LED off (including blinking)
- * @param led integer for one of the 3 LEDs
- */
-
-void ledOff(int led) {
-	ledBlinkOff(led);
-	ledSet(led, HIGH);
-}
-
-/**
- * Toggle the LED
- * @param led integer for one of the 3 LEDs
- */
-
-void ledToggle(int led) {
-	int pin = ledGetPin(led);
-
-	if (digitalRead(pin) == HIGH)
-		digitalWrite(pin, LOW);
-	else
-		digitalWrite(pin, HIGH);
-}
-
-/**
- * Enable the LED blinking
- * @param led integer for one of the 3 LEDs
- */
-
-void ledBlinkOn(int led) {
-	ledBlink[led] = true;
-	ledBlinkO[led] = false;
-}
-
-/**
- * Enable the LED blinking only once
- * @param led integer for one of the 3 LEDs
- */
-
-void ledBlinkOnce(int led) {
-	ledBlink[led] = true;
-	ledBlinkO[led] = true;
-}
-
-/**
- * Disable the LED blinking
- * @param led integer for one of the 3 LEDs
- */
-
-void ledBlinkOff(int led) {
-	ledBlink[led] = false;
-	ledBlinkO[led] = false;
-}
-
-/**
- * Function for LED blinking, called by a timer
- */
-
-void ledBlinkHandler(void) {
-	for (int i = 0; i < 3; i++) {
-		if (ledBlink[i] == true) {
-			ledToggle(i);
-			if (digitalRead(ledGetPin(i)) == HIGH && ledBlinkO[i])
-				ledBlink[i] = false;
-		}
-	}
-}
-

+ 0 - 31
CoffeeCode/led.h

@@ -1,31 +0,0 @@
-/*
- * led.h
- *
- *  Created on: Nov 9, 2015
- *      Author: Philipp Hinz
- */
-
-#ifndef LED_H_
-#define LED_H_
-
-#define LED_RED			0
-#define LED_YELLOW		1
-#define LED_GREEN		2
-
-void initLed(void);
-
-void ledSet(int led, int value);
-
-void ledOn(int led);
-void ledOff(int led);
-
-void ledToggle(int led);
-
-void ledBlinkOn(int led);
-void ledBlinkOnce(int led);
-void ledBlinkOff(int led);
-
-void ledBlinkHandler(void);
-
-
-#endif /* LED_H_ */

+ 8 - 23
CoffeeCode/main.cpp

@@ -17,7 +17,6 @@
 #include <csignal>
 #include <time.h>
 #include "global.h"
-#include "led.h"
 #include "timer.h"
 #include "database.h"
 #include "logger.h"
@@ -172,13 +171,11 @@ int main(int argc, char *argv[]) {
 	logger_reset();
 	initTimers();
 	halInit();
-	//initLed();
-
-
-//	timer statusT = timer(&printStatus);
-//	statusT.setDivider(40);
-//	statusT.start();
-//	statusT.call();
+	lcd = lcdInit();
+	if (lcd < 0) logger_error("Error: unable to init LCD (%d)\n", lcd);
+	lcdClear(lcd);
+	lcdHome(lcd);
+	lcdPrintf(lcd,"CoffeePi b%d", buildno);
 
 	// http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm
 
@@ -210,11 +207,8 @@ int main(int argc, char *argv[]) {
 	pthread_attr_destroy(&attr);
 
 	// Insert main stuff here.
-	lcd = lcdInit();
-	if (lcd < 0) logger_error("Error: unable to init LCD (%d)\n", lcd);
-	lcdClear(lcd);
-	lcdHome(lcd);
-	lcdPrintf(lcd,"Wer das liest ist doof.");
+	timeTimer.setDivider(1);
+	timeTimer.start();
 
 	sleep(2);
 
@@ -242,7 +236,6 @@ void terminationHandler(int signum) {
 	sqlExecute("end");
 
 	sqlClose(); // Closing DB connection
-	initLed(); // Turning all LEDs off
 
 	exit(EXIT_SUCCESS);
 }
@@ -285,17 +278,9 @@ void killThread(int threadid, int sig) {
 void *mainLoop(void *threadid) {
 	sqlOpen();
 
-	/*sqlInsertNode((uint8_t*)"123    1", true, true);
-	 sqlInsertNode((uint8_t*)"3525   2", false, false);
-	 sqlInsertNode((uint8_t*)"sbw452 3", false, false);
-	 sqlInsertNode((uint8_t*)"s4t4   4", true, true);
-	 sqlInsertNode((uint8_t*)"te456  5", true, false);
-	sqlSetup();*/
+	//sqlSetup();
 
 	// Do more stuff here
-	timeTimer.setDivider(1);
-	timeTimer.start();
-	lcdClear(lcd);
 
 	logger(V_BASIC, "Thread goes Sleeping..\n");
 	while (1) {

+ 2 - 9
CoffeeCode/makefile.targets

@@ -1,16 +1,9 @@
 OBJS2 += \
-./can.o \
-./cantiming.o \
-./canspeed.o \
 ./crc.o \
 ./database.o \
-./dipoti.o \
-./led.o \
 ./logger.o \
-./monitor.o \
-./node.o \
+./lcd.o \
+./hal.o \
 ./spi.o \
-./test.o \
 ./timer.o \
-./mcp2510.o \
 ./sqlite/sqlite3.o