From 56e5fc7a6dcf6ebb45ed8cf9af3bc371be725e83 Mon Sep 17 00:00:00 2001 From: Alexander Rosenberg Date: Wed, 28 Feb 2024 05:31:35 -0800 Subject: [PATCH] Makefile, options, and lcd stuff --- Makefile | 18 ++++++++++-------- config.mk | 3 ++- lcd.c => src/lcd.c | 4 +++- lcd.h => src/lcd.h | 0 main.c => src/main.c | 22 +++++++++++++++++----- ths.c => src/ths.c | 0 ths.h => src/ths.h | 0 util.c => src/util.c | 0 util.h => src/util.h | 0 9 files changed, 32 insertions(+), 15 deletions(-) rename lcd.c => src/lcd.c (97%) rename lcd.h => src/lcd.h (100%) rename main.c => src/main.c (94%) rename ths.c => src/ths.c (100%) rename ths.h => src/ths.h (100%) rename util.c => src/util.c (100%) rename util.h => src/util.h (100%) diff --git a/Makefile b/Makefile index 58bb4f5..f65f7ed 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,22 @@ CC=clang -CFLAGS=-std=c99 -g +CFLAGS=-std=c99 LD=clang LDFLAGS=-lgpio -lfigpar -OBJS=main.o util.o lcd.o ths.o -OUTFILE=rpi4b-temp-humidity +SRCS=src/main.c src/util.c src/lcd.c src/ths.c +PROG=rpi4b-temp-humidity .include "config.mk" -${OUTFILE}: ${OBJS} - ${LD} ${LDFLAGS} -o $@ ${OBJS} +OBJS=${SRCS:C/^src/bin/:C/.c$/.o/} +bin/${PROG}: ${OBJS} + ${LD} ${LDFLAGS} -o ${@} ${OBJS} main.o util.o lcd.o ths.o: util.h main.o lcd.o: lcd.h main.o ths.o: ths.h -.c.o: +${OBJS}: ${.TARGET:C/^bin/src/:C/.o$/.c/} + @mkdir -p bin/ ${CC} ${CFLAGS} -c\ -DDEFAULT_CONFIG_PATH="\"${DEFAULT_CONFIG_PATH}\""\ -DDEFAULT_GPIO_DEVICE="\"${DEFAULT_GPIO_DEVICE}\""\ @@ -23,10 +25,10 @@ main.o ths.o: ths.h -DDEFAULT_FAIL_KEY="\"${DEFAULT_FAIL_KEY}\""\ -DDEFAULT_FAIL_LIMIT="${DEFAULT_FAIL_LIMIT}"\ -DDEFAULT_LCD_VERSION="\"${DEFAULT_LCD_VERSION}\""\ -$< +-o ${@} ${.ALLSRC} clean: - rm -f ${OUTFILE} ${OBJS} + rm -rf bin/ .SUFFIXES: .c .o .PHONY: clean diff --git a/config.mk b/config.mk index d77f527..7b22248 100644 --- a/config.mk +++ b/config.mk @@ -2,6 +2,7 @@ DEFAULT_CONFIG_PATH=config.conf DEFAULT_GPIO_DEVICE=/dev/gpioc0 DEFAULT_TEMP_KEY=dev.gpioths.0.temperature DEFAULT_HUMID_KEY=dev.gpioths.0.humidity -DEFAULT_FAIL_KEY=dev.gpioths.0.fails +#DEFAULT_FAIL_KEY=dev.gpioths.0.fails +DEFAULT_FAIL_KEY= DEFAULT_FAIL_LIMIT=5 DEFAULT_LCD_VERSION=jp diff --git a/lcd.c b/src/lcd.c similarity index 97% rename from lcd.c rename to src/lcd.c index 94ea69b..d05cc75 100644 --- a/lcd.c +++ b/src/lcd.c @@ -61,7 +61,9 @@ LCD *lcd_open(gpio_handle_t handle, gpio_pin_t rs, gpio_pin_t rw, gpio_pin_t en, lcd->d5 = d5; lcd->d6 = d6; lcd->d7 = d7; - lcd_set_read(lcd, true, true); + gpio_pin_output(lcd->handle, lcd->rw); + gpio_pin_output(lcd->handle, lcd->en); + gpio_pin_output(lcd->handle, lcd->rs); lcd_clear(lcd); lcd_call(lcd, 0, 0, 0, 0, 1, 1, 1, 0, 0); // 5x8 font, 2 lines, 8bit lcd_entry_mode(lcd, LCD_INCREMENT, LCD_CURSOR_MOVE); diff --git a/lcd.h b/src/lcd.h similarity index 100% rename from lcd.h rename to src/lcd.h diff --git a/main.c b/src/main.c similarity index 94% rename from main.c rename to src/main.c index 38ce2e5..929dfd2 100644 --- a/main.c +++ b/src/main.c @@ -54,8 +54,11 @@ int main(int argc, char *const *argv) { lcd_display_control(lcd, LCD_CURSOR_BLINK, LCD_CURSOR_ON, LCD_DISPLAY_ON); THS *ths = ths_open(GLOBAL_OPTS.temp_key, GLOBAL_OPTS.humid_key, GLOBAL_OPTS.fail_key); + if (!GLOBAL_OPTS.fail_key) { + warnx("it's probably a bad idea to not set fail_key"); + } while (true) { - if (ths_read_fails(ths) > GLOBAL_OPTS.fail_limit) { + if (GLOBAL_OPTS.fail_key && ths_read_fails(ths) > GLOBAL_OPTS.fail_limit) { errx(1, "THS fail limit reached"); } lcd_clear(lcd); @@ -79,7 +82,7 @@ int main(int argc, char *const *argv) { } void print_help(const char *exec_name) { - printf("usage: %s [-h] [-v] [-s] [-f CONFIG_PATH]", exec_name); + printf("usage: %s [-h] [-v] [-s] [-f CONFIG_PATH]\n", exec_name); } void parse_arguments(int argc, char *const *argv) { @@ -196,6 +199,15 @@ static int parse_str_callback(struct figpar_config *opt, uint32_t line, return 0; } +static char *steal_opt_if_set(char *str) { + if (str && !*str) { + free(str); + return NULL; + } + return str; +} + + #define REQUIRE_KEY(ind, name) if (entries[ind].type == FIGPAR_TYPE_NONE) {\ errx(1, "%s must be specified", # name);\ } @@ -222,15 +234,15 @@ static void set_options_from_entries(struct figpar_config *entries, memcpy(GLOBAL_OPTS.data_pins, arr->arr, sizeof(uint32_t) * 8); entries[5].type = FIGPAR_TYPE_NONE; - GLOBAL_OPTS.temp_key = entries[5].value.str; + GLOBAL_OPTS.temp_key = steal_opt_if_set(entries[5].value.str); LOG_VERBOSE("Using temp_key: \"%s\"\n", GLOBAL_OPTS.temp_key); entries[6].type = FIGPAR_TYPE_NONE; - GLOBAL_OPTS.humid_key = entries[6].value.str; + GLOBAL_OPTS.humid_key = steal_opt_if_set(entries[6].value.str); LOG_VERBOSE("Using humid_key: \"%s\"\n", GLOBAL_OPTS.humid_key); entries[7].type = FIGPAR_TYPE_NONE; - GLOBAL_OPTS.fail_key = entries[7].value.str; + GLOBAL_OPTS.fail_key = steal_opt_if_set(entries[7].value.str); LOG_VERBOSE("Using fail_key: \"%s\"\n", GLOBAL_OPTS.fail_key); GLOBAL_OPTS.fail_limit = entries[8].value.u_num; diff --git a/ths.c b/src/ths.c similarity index 100% rename from ths.c rename to src/ths.c diff --git a/ths.h b/src/ths.h similarity index 100% rename from ths.h rename to src/ths.h diff --git a/util.c b/src/util.c similarity index 100% rename from util.c rename to src/util.c diff --git a/util.h b/src/util.h similarity index 100% rename from util.h rename to src/util.h