From 801bbca9298a2059f60f34140c22a219435cd0ef Mon Sep 17 00:00:00 2001 From: David Phillips Date: Sat, 13 Feb 2021 22:35:13 +1300 Subject: Add dep files to Makefile Also includes WIP data manager code. --- Makefile | 86 ++++++++++++++++++++++++++++++++++++++++------------------ altimeter.c | 62 ++++-------------------------------------- data_manager.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++ data_manager.h | 15 ++++++++++ util.c | 6 ---- 5 files changed, 150 insertions(+), 88 deletions(-) create mode 100644 data_manager.c create mode 100644 data_manager.h diff --git a/Makefile b/Makefile index 73d957d..1213f92 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,6 @@ REAL_ELF = build/altimeter.elf # options for native (i.e. unit test) UNITY_SRC ?= unity/src -UNITY_O = $(UNITY_SRC)/unity.o CFLAGS += -Wall -Wextra -I$(UNITY_SRC) LDFLAGS += -lm @@ -30,7 +29,7 @@ AVR_COMMON_LDFLAGS += -Wl,-u,vfprintf -lprintf_flt AVR_LDFLAGS += $(AVR_COMMON_LDFLAGS) -mmcu=$(MCU) SIM_LDFLAGS += $(AVR_COMMON_LDFLAGS) -mmcu=$(SIM_MCU) -$(shell mkdir -p build/{real,sim,native}) +$(shell mkdir -p build/{real,sim,native{,/$(UNITY_SRC)}}) all: build/altimeter_sim.elf build/altimeter.hex test size @@ -47,24 +46,44 @@ flash: build/altimeter.hex $(AVRDUDE) -F -V -c avr109 -p $(MCU) -P $(PORT) -b 115200 -U flash:w:$< clean: - rm -rf build/ $(UNITY_O) - -test: \ - $(UNITY_O) \ - build/native/test_runner.o \ - build/native/test_util.o \ - build/native/test_test.o \ - build/native/util.o + rm -rf build/ + +BUILD_DIR = build +SIM_DIR = $(BUILD_DIR)/sim +REAL_DIR = $(BUILD_DIR)/real +NATIVE_DIR = $(BUILD_DIR)/native + +TEST_OBJ = \ + $(NATIVE_DIR)/$(UNITY_SRC)/unity.o \ + $(NATIVE_DIR)/test_runner.o \ + $(NATIVE_DIR)/test_util.o \ + $(NATIVE_DIR)/test_test.o \ + $(NATIVE_DIR)/util.o + +REAL_OBJ = \ + $(REAL_DIR)/barometer_sim.o \ + $(REAL_DIR)/display_sim.o \ + $(REAL_DIR)/altimeter.o \ + $(REAL_DIR)/util.o \ + $(REAL_DIR)/data_manager.o + +SIM_OBJ = \ + $(SIM_DIR)/barometer_sim.o \ + $(SIM_DIR)/display_sim.o \ + $(SIM_DIR)/altimeter.o \ + $(SIM_DIR)/util.o \ + $(SIM_DIR)/data_manager.o + +ALL_OBJ = $(TEST_OBJ) $(REAL_OBJ) $(SIM_OBJ) +ALL_SOURCE = $(ALL_OBJ:.o=.c) + +test: $(TEST_OBJ) $(CC) $(LDFLAGS) -Wl,-T,linker_list.lds -o $@ $^ ./$@ # ELF for real hardware. No mock/sim hardware included (except barometer, # haven't written that code yet) -build/altimeter.elf: \ - build/real/barometer_sim.o \ - build/real/display_sim.o \ - build/real/altimeter.o \ - build/real/util.o +$(REAL_ELF): $(REAL_OBJ) $(AVR_CC) $(AVR_LDFLAGS) -o $@ $^ size: @@ -74,23 +93,38 @@ size: # versions of each component will provide a mocked interface. This allows for # operation in QEMU (see `emu` recipe) without requring SPI, I²C and # peripherals to be emulated. Useful for UI testing and development etc. -build/altimeter_sim.elf: \ - build/sim/barometer_sim.o \ - build/sim/display_sim.o \ - build/sim/altimeter.o \ - build/sim/util.o +build/altimeter_sim.elf: $(SIM_OBJ) $(AVR_CC) $(SIM_LDFLAGS) -o $@ $^ -build/sim/%.o: %.c - $(AVR_CC) $(SIM_CFLAGS) -c -o $@ $^ +build/sim/%.o: %.c build/sim/%.d + $(AVR_CC) $(SIM_CFLAGS) -c -o $@ $< -build/real/%.o: %.c - $(AVR_CC) $(AVR_CFLAGS) -c -o $@ $^ +build/real/%.o: %.c build/real/%.d + $(AVR_CC) $(AVR_CFLAGS) -c -o $@ $< -build/native/%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $^ +build/native/%.o: %.c build/native/%.d + $(CC) $(CFLAGS) -c -o $@ $< build/%.hex: build/%.elf $(OBJCOPY) -O ihex -R .eeprom $< $@ +build/sim/%.d: %.c + @set -e; rm -f $@; \ + $(AVR_CC) $(SIM_CFLAGS) -MM -MG $< > $@.$$$$; \ + sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ + rm -f $@.$$$$ + +build/real/%.d: %.c + @set -e; rm -f $@; \ + $(AVR_CC) $(AVR_CFLAGS) -MM -MG $< > $@.$$$$; \ + sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ + rm -f $@.$$$$ + +build/native/%.d: %.c + @set -e; rm -f $@; \ + $(CC) $(CFLAGS) -MM -MG $< > $@.$$$$; \ + sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ + rm -f $@.$$$$ + +include $(ALL_SOURCE:.c=.d) .PHONY: all emu gdb flash clean test size diff --git a/altimeter.c b/altimeter.c index 0aebbf7..d86bd23 100644 --- a/altimeter.c +++ b/altimeter.c @@ -7,9 +7,9 @@ #include #include -#include "barometer.h" + +#include "data_manager.h" #include "display.h" -#include "util.h" #ifdef WDT_DISABLE # warning "WDT disabled in this build" @@ -33,64 +33,12 @@ #define DEBUG_MARK_4_CLEAR do{PORTB &= ~(1 << 6);}while(0) #define DEBUG_MARK_5_SET do{PORTB |= 1 << 5;}while(0) #define DEBUG_MARK_5_CLEAR do{PORTB &= ~(1 << 5);}while(0) -#define DEBUG_MARK_6_SET do{PORTB |= 1 << 4;}while(0) -#define DEBUG_MARK_6_CLEAR do{PORTB &= ~(1 << 4);}while(0) /* ISR for collecting and displaying pressure, altitude data etc */ ISR(TIMER1_COMPA_vect) { - DEBUG_MARK_SET_ALL; - static volatile float setting = 1040.21; /* FIXME volatile for gdb hacks */ - static float last_height = 0; - float pressure = 0; - float height_m = 0; - float rate_m_s; - char rate_sym; - DEBUG_MARK_1_CLEAR; - pressure = barometer_read(); - DEBUG_MARK_2_CLEAR; - height_m = pressure_to_metres_asl(pressure, setting); - DEBUG_MARK_3_CLEAR; - rate_m_s = height_m - last_height; - last_height = height_m; - DEBUG_MARK_4_CLEAR; - - if (rate_m_s < 0) { - rate_sym = C_DESC; - } else if (rate_m_s == 0) { - rate_sym = C_IDLE; - } else { - rate_sym = C_ASC; - } - DEBUG_MARK_5_CLEAR; - - char line[LCD_WIDTH+1]; - /* line 1 */ - snprintf(line, sizeof(line), "%.1f m %c%d m/s", height_m, rate_sym, abs(rate_m_s)); - blank_to_eol(line, sizeof(line)); - display_set_cursor(0, 0); - display_write(line); - - - /* line 2 */ - snprintf(line, sizeof(line), "%.2f hPa", pressure); - blank_to_eol(line, sizeof(line)); - display_set_cursor(0, 1); - display_write(line); - - /* line 3 */ - snprintf(line, sizeof(line), "SET %.2f hPa", setting); - blank_to_eol(line, sizeof(line)); - display_set_cursor(0, 2); - display_write(line); - - /* line 4 */ - snprintf(line, sizeof(line), "%.1f ""\xdf""C", 12.3f); - blank_to_eol(line, sizeof(line)); - display_set_cursor(0, 3); - display_write(line); - - DEBUG_MARK_6_CLEAR; + data_manager_tick(); + //ui_tick(); WDT_PAT_MAYBE(); } @@ -113,6 +61,8 @@ int main(void) * interrupt which we're not handling for now */ USBCON &= ~(1 << USBE); #endif + // FIXME measure current saving here if any + //ACSR |= (1 << ACD); /* main body should take no longer than 1 second to run */ WDT_SETUP_MAYBE(WDTO_2S); diff --git a/data_manager.c b/data_manager.c new file mode 100644 index 0000000..bd34426 --- /dev/null +++ b/data_manager.c @@ -0,0 +1,69 @@ +#include +#include +#include + +#include "data_manager.h" +#include "barometer.h" + +static float pressure[PRESSURE_UNIT_MAX]; +static float setting[PRESSURE_UNIT_MAX]; +static float altitude[ALTITUDE_UNIT_MAX]; + +/* utility functions */ +static float pressure_to_metres_asl(float real, float setting) +{ + return 44330.f*(1.f-powf(real/setting, 1/5.255)); +} + + +/* exported functions */ +#define DATA_MANAGER_GETTER(name, store, bound, enum_type, return_type) \ +float name(enum_type unit) \ +{ \ + if (unit >= 0 && unit < bound) { \ + return store[unit]; \ + } else { \ + abort(); \ + } \ +} + +DATA_MANAGER_GETTER( + data_manager_get_pressure, + pressure, + PRESSURE_UNIT_MAX, + enum pressure_unit, + float) +DATA_MANAGER_GETTER( + data_manager_get_setting, + setting, + PRESSURE_UNIT_MAX, + enum pressure_unit, + float) +DATA_MANAGER_GETTER( + data_manager_get_altitude, + altitude, + ALTITUDE_UNIT_MAX, + enum altitude_unit, + float) + +void data_manager_init(void) +{ + /* FIXME load initial setting and force a tick synchronously */ +} + +void data_manager_tick(void) +{ + /* FIXME alt rate */ + /* FIXME calculate on demand? */ + /* FIXME hardcoding the conversion factors here is gross */ + float p = barometer_read(); + pressure[PRESSURE_UNIT_HPA] = p; + pressure[PRESSURE_UNIT_INHG] = p / 3.38639; + + /* FIXME we need atomic access to alt setting once user can set it. Check + * if AVR allows multiple/nested ISRs to run concurrently, pretty sure not + */ + float a = pressure_to_metres_asl(p, setting[PRESSURE_UNIT_HPA]); + altitude[ALTITUDE_UNIT_METRE] = a; + altitude[ALTITUDE_UNIT_FT] = a * 3.048; +} diff --git a/data_manager.h b/data_manager.h new file mode 100644 index 0000000..0d5c067 --- /dev/null +++ b/data_manager.h @@ -0,0 +1,15 @@ +#pragma once + +enum pressure_unit { + PRESSURE_UNIT_HPA, + PRESSURE_UNIT_INHG, + PRESSURE_UNIT_MAX +}; + +enum altitude_unit { + ALTITUDE_UNIT_METRE, + ALTITUDE_UNIT_FT, + ALTITUDE_UNIT_MAX +}; + +void data_manager_tick(void); diff --git a/util.c b/util.c index d78809f..0f766b6 100644 --- a/util.c +++ b/util.c @@ -1,13 +1,7 @@ -#include #include #include "util.h" -float pressure_to_metres_asl(float real, float setting) -{ - return 44330.f*(1.f-powf(real/setting, 1/5.255)); -} - void blank_to_eol(char *line, size_t len) { size_t i = 0; -- cgit v1.1