From 49553af9f2922311e5834ef8a030c814e6902fe4 Mon Sep 17 00:00:00 2001 From: volpol Date: Sat, 1 Sep 2018 14:28:59 +0200 Subject: [PATCH] Added compile-time project configuration --- .gitignore | 2 ++ Makefile | 22 +++++++++++++++++----- config.def.h | 36 ++++++++++++++++++++++++++++++++++++ hx711.c | 5 +++-- hx711.h | 4 ++-- led.c | 32 ++++++++++++++++---------------- led.h | 4 ---- main.c | 4 +--- mgmt.c | 5 +++-- pump.c | 32 ++++++++++++++++---------------- 10 files changed, 96 insertions(+), 50 deletions(-) create mode 100644 config.def.h diff --git a/.gitignore b/.gitignore index 397e904..0c017fc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.o +*.d +config.h bbd diff --git a/Makefile b/Makefile index a167ffb..cf6b40e 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,25 @@ .PHONY: all clean CFLAGS=-Wall -std=gnu99 -#CFLAGS+=-DDEBUG -CFLAGS += -DHAVE_HX711 + +USRCFG=config.h + +OBJS=hx711.o c_gpio.o pump.o net.o main.o mgmt.o led.o event_gpio.o + +DEPS := $(OBJS:.o=.d) all: bbd +-include $(DEPS) + clean: - rm -f *.o bbd + rm -f $(OBJS) $(DEPS) bbd + +%.o: %.c + $(CC) $(CFLAGS) -MMD -MF $(patsubst %.o,%.d,$@) -c -o $@ $< + +bbd: $(OBJS) + $(CC) $(LDFLAGS) -o $@ -pthread $(OBJS) -bbd: hx711.o c_gpio.o pump.o net.o main.o mgmt.o led.o event_gpio.o - $(CC) $(LDFLAGS) -o $@ -pthread $^ +$(USRCFG): config.def.h + cp $< $@ diff --git a/config.def.h b/config.def.h new file mode 100644 index 0000000..8451f1c --- /dev/null +++ b/config.def.h @@ -0,0 +1,36 @@ +#ifndef CONFIG_H +#define CONFIG_H 1 + +#define DEBUG +#define HAVE_HX711 + +#define GPIO_HX711_SCK 5 +#define GPIO_HX711_DOUT 6 + +#define GPIO_LED_R 22 +#define GPIO_LED_Y 23 +#define GPIO_LED_B 24 + +#define GPIO_RESET 14 +#define RESET_BOUNCE_TIME 10 + +#define NUM_PUMPS 12 + +#define GPIO_PUMP1 9 +#define GPIO_PUMP2 21 +#define GPIO_PUMP3 12 +#define GPIO_PUMP4 7 +#define GPIO_PUMP5 26 +#define GPIO_PUMP6 13 +#define GPIO_PUMP7 19 +#define GPIO_PUMP8 8 +#define GPIO_PUMP9 20 +#define GPIO_PUMP10 11 +#define GPIO_PUMP11 16 +#define GPIO_PUMP12 25 + +#define MGMT_WEIGHT_GAIN 5 +#define MGMT_WEIGHT_GAIN_TIME 10 +#define MGMT_WEIGHT_LOSS 200 + +#endif /* CONFIG_H */ diff --git a/hx711.c b/hx711.c index cea20c6..4d9cea4 100644 --- a/hx711.c +++ b/hx711.c @@ -1,8 +1,9 @@ #include #include -#include "hx711.h" - #include + +#include "config.h" +#include "hx711.h" #include "c_gpio.h" diff --git a/hx711.h b/hx711.h index 5e53c1d..e8b053a 100644 --- a/hx711.h +++ b/hx711.h @@ -1,7 +1,7 @@ #ifndef HX711_h #define HX711_h - #define PD_SCK_PIN 5 + #define PD_SCK_PIN GPIO_HX711_SCK #define PD_SCK_SET_OUTPUT setup_gpio (PD_SCK_PIN, OUTPUT, PUD_OFF) #define PD_SCK_SET_INPUT setup_gpio (PD_SCK_PIN, INPUT, PUD_OFF) @@ -9,7 +9,7 @@ #define PD_SCK_SET_HIGH output_gpio(PD_SCK_PIN, HIGH) #define PD_SCK_SET_LOW output_gpio(PD_SCK_PIN, LOW) - #define DOUT_PIN 6 + #define DOUT_PIN GPIO_HX711_DOUT #define DOUT_READ input_gpio(DOUT_PIN) #define DOUT_SET_INPUT setup_gpio (DOUT_PIN, INPUT, PUD_OFF) diff --git a/led.c b/led.c index be8a1c7..cdc7e11 100644 --- a/led.c +++ b/led.c @@ -11,40 +11,40 @@ #include #include "log.h" -#include "led.h" +#include "config.h" #include "c_gpio.h" int led_init(void){ - setup_gpio(LED_R, OUTPUT, PUD_OFF); - setup_gpio(LED_Y, OUTPUT, PUD_OFF); - setup_gpio(LED_B, OUTPUT, PUD_OFF); + setup_gpio(GPIO_LED_R, OUTPUT, PUD_OFF); + setup_gpio(GPIO_LED_Y, OUTPUT, PUD_OFF); + setup_gpio(GPIO_LED_B, OUTPUT, PUD_OFF); return 0; } int led_deinit(void){ - setup_gpio(LED_R, INPUT, PUD_OFF); - setup_gpio(LED_Y, INPUT, PUD_OFF); - setup_gpio(LED_B, INPUT, PUD_OFF); + setup_gpio(GPIO_LED_R, INPUT, PUD_OFF); + setup_gpio(GPIO_LED_Y, INPUT, PUD_OFF); + setup_gpio(GPIO_LED_B, INPUT, PUD_OFF); return 0; } void status_ready(void){ WHOAMI; - output_gpio(LED_R, LOW); - output_gpio(LED_Y, LOW); - output_gpio(LED_B, HIGH); + output_gpio(GPIO_LED_R, LOW); + output_gpio(GPIO_LED_Y, LOW); + output_gpio(GPIO_LED_B, HIGH); } void status_busy(void){ WHOAMI; - output_gpio(LED_R, LOW); - output_gpio(LED_Y, HIGH); - output_gpio(LED_B, LOW); + output_gpio(GPIO_LED_R, LOW); + output_gpio(GPIO_LED_Y, HIGH); + output_gpio(GPIO_LED_B, LOW); } void status_error(void){ WHOAMI; - output_gpio(LED_R, HIGH); - output_gpio(LED_Y, LOW); - output_gpio(LED_B, LOW); + output_gpio(GPIO_LED_R, HIGH); + output_gpio(GPIO_LED_Y, LOW); + output_gpio(GPIO_LED_B, LOW); } diff --git a/led.h b/led.h index 60a9a71..b997675 100644 --- a/led.h +++ b/led.h @@ -1,10 +1,6 @@ #ifndef LED_H #define LED_H 1 -#define LED_R 22 -#define LED_Y 23 -#define LED_B 24 - int led_init(void); int led_deinit(void); void status_ready(void); diff --git a/main.c b/main.c index b0c6243..c5c86e5 100644 --- a/main.c +++ b/main.c @@ -12,6 +12,7 @@ #include +#include "config.h" #include "c_gpio.h" #include "event_gpio.h" #include "pump.h" @@ -37,9 +38,6 @@ static void handle_signal(int sig){ } } -#define GPIO_RESET 14 -#define RESET_BOUNCE_TIME 10 - static void reset_cb(unsigned int gpio){ if (GPIO_RESET != gpio) return; fprintf (stderr, "RESET EVENT on GPIO %u\n",gpio); diff --git a/mgmt.c b/mgmt.c index 9ea903c..9768197 100644 --- a/mgmt.c +++ b/mgmt.c @@ -1,5 +1,6 @@ #include #include +#include "config.h" #ifdef HAVE_HX711 #include @@ -37,7 +38,7 @@ void mgmt_init(void){ int mgmt_start(int ml){ HX711_power_up(); HX711_tare(3); - stop_time = time(NULL) + 10; + stop_time = time(NULL) + MGMT_WEIGHT_GAIN_TIME; start_weight = curweight(); stop_weight = start_weight + ml; return 0; @@ -47,7 +48,7 @@ int mgmt_stop(void){ int res = MGMT_CONTINUE; double cw = curweight(); if (cw >= stop_weight) res = MGMT_NORMAL_STOP; else - if (time(NULL) >= stop_time && MAX(cw, start_weight) - MIN(cw, start_weight) < 5) res = MGMT_ABORT; + if (time(NULL) >= stop_time && MAX(cw, start_weight) - MIN(cw, start_weight) < MGMT_WEIGHT_GAIN) res = MGMT_ABORT; return res; } diff --git a/pump.c b/pump.c index 8aceb39..96aa74e 100644 --- a/pump.c +++ b/pump.c @@ -11,26 +11,26 @@ #include #include "log.h" +#include "config.h" #include "pump.h" #include "c_gpio.h" -#define NUM_PUMPS 12 static int P2G[NUM_PUMPS] = { - 9, //21, - 21, //20, - 12, //16, - - 7, //26, - 26, //19, - 13, //13, - - 19, //12, - 8, //7, - 20, //8, - - 11, //25, - 16, //11, - 25 //9 + GPIO_PUMP1, + GPIO_PUMP2, + GPIO_PUMP3, + + GPIO_PUMP4, + GPIO_PUMP5, + GPIO_PUMP6, + + GPIO_PUMP7, + GPIO_PUMP8, + GPIO_PUMP9, + + GPIO_PUMP10, + GPIO_PUMP11, + GPIO_PUMP12 }; int pump_add(int num){ -- 2.30.2