aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Phillips <david@yeah.nah.nz>2020-12-20 22:22:17 +1300
committerDavid Phillips <david@yeah.nah.nz>2020-12-20 22:22:17 +1300
commit75154ebdfbbfe7a04d21d910254058a757d2eef6 (patch)
treea4b2276da1b23a88fb15c434a2aaa7d6939c79b0
parent74dbe89a7574ccfc0238c9ec4f55ac4fd9d40949 (diff)
downloadaltimeter-75154ebdfbbfe7a04d21d910254058a757d2eef6.tar.xz
Use float-enabled printf implementation
I hadn't realised there is a float-enabled printf implementation in AVR libc, so let's use that instead of our own hack.
-rw-r--r--.gitmodules3
m---------BMP3-Sensor-API0
-rw-r--r--Makefile13
-rw-r--r--altimeter.c19
-rw-r--r--display_sim.c2
-rw-r--r--util.c7
-rw-r--r--util.h1
7 files changed, 17 insertions, 28 deletions
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..694f6ad
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "BMP3-Sensor-API"]
+ path = BMP3-Sensor-API
+ url = https://github.com/BoschSensortec/BMP3-Sensor-API
diff --git a/BMP3-Sensor-API b/BMP3-Sensor-API
new file mode 160000
+Subproject 5c13e49e7649099696ff6ca5f5fe3ad4ab3f5d9
diff --git a/Makefile b/Makefile
index 3c15800..bb26ce1 100644
--- a/Makefile
+++ b/Makefile
@@ -5,8 +5,13 @@ QEMU_AVR ?= qemu-system-avr
MCU ?= atmega32u4
SIM_MCU ?= atmega2560
-CFLAGS += -Wall -Wextra -DF_CPU=16000000UL -mmcu=$(MCU) -O3
-SIM_CFLAGS += -Wall -Wextra -DF_CPU=16000000UL -mmcu=$(SIM_MCU) -g3 -gdwarf-2 -DWDT_DISABLE
+COMMON_CFLAGS += -Wall -Wextra -DF_CPU=16000000UL
+CFLAGS += $(COMMON_CFLAGS) -mmcu=$(MCU) -O3
+SIM_CFLAGS += $(COMMON_CFLAGS) -mmcu=$(SIM_MCU) -g3 -gdwarf-2 -DWDT_DISABLE
+
+COMMON_LDFLAGS += -Wl,-u,vfprintf -lprintf_flt
+LDFLAGS += $(COMMON_LDFLAGS) -mmcu=$(MCU)
+SIM_LDFLAGS += $(COMMON_LDFLAGS) -mmcu=$(SIM_MCU)
$(shell mkdir -p build/{real,sim})
@@ -26,14 +31,14 @@ build/altimeter.elf: \
build/real/display_sim.o \
build/real/altimeter.o \
build/real/util.o
- $(CC) -o $@ $^ $(CFLAGS)
+ $(CC) -o $@ $^ $(LDFLAGS)
build/altimeter_sim.elf: \
build/sim/barometer_sim.o \
build/sim/display_sim.o \
build/sim/altimeter.o \
build/sim/util.o
- $(CC) -o $@ $^ $(SIM_CFLAGS)
+ $(CC) -o $@ $^ $(SIM_LDFLAGS)
include config.mk
diff --git a/altimeter.c b/altimeter.c
index 429976c..acaa95e 100644
--- a/altimeter.c
+++ b/altimeter.c
@@ -24,7 +24,6 @@
ISR(TIMER1_COMPA_vect)
{
PORTD ^= 1 << 5;
- int tcnt1_start = TCNT1;
static volatile float setting = 1040.21; /* FIXME volatile for gdb hacks */
static float last_height = 0;
float pressure = 0;
@@ -46,36 +45,26 @@ ISR(TIMER1_COMPA_vect)
char line[LCD_WIDTH+1];
/* line 1 */
- int alt_m_int;
- float alt_m_float;
- split_float(height_m, &alt_m_int, &alt_m_float);
- snprintf(line, sizeof(line), "%d.%d m %c%d m/s", alt_m_int, (int)(round(alt_m_float*10)), rate_sym, abs(rate_m_s));
+ 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 */
- int pressure_hpa_int;
- float pressure_hpa_float;
- split_float(pressure, &pressure_hpa_int, &pressure_hpa_float);
- snprintf(line, sizeof(line), "%d.%d hPa", pressure_hpa_int, (int)(round(pressure_hpa_float*100)));
+ snprintf(line, sizeof(line), "%.2f hPa", pressure);
blank_to_eol(line, sizeof(line));
display_set_cursor(0, 1);
display_write(line);
/* line 3 */
- int setting_hpa_int;
- float setting_hpa_float;
- split_float(setting, &setting_hpa_int, &setting_hpa_float);
- snprintf(line, sizeof(line), "SET %d.%d hPa", setting_hpa_int, (int)(round(setting_hpa_float*100)));
+ snprintf(line, sizeof(line), "SET %.2f hPa", setting);
blank_to_eol(line, sizeof(line));
display_set_cursor(0, 2);
display_write(line);
- int tcnt1_end = TCNT1;
/* line 4 */
- snprintf(line, sizeof(line), "TCNT1 delta %u", tcnt1_end - tcnt1_start);
+ snprintf(line, sizeof(line), "%.1f ""\xdf""C", 12.3f);
blank_to_eol(line, sizeof(line));
display_set_cursor(0, 3);
display_write(line);
diff --git a/display_sim.c b/display_sim.c
index 4737981..c87329e 100644
--- a/display_sim.c
+++ b/display_sim.c
@@ -104,7 +104,7 @@ void display_write(const char *text)
while (cursor_x < LCD_WIDTH && *text) {
c = *(text++);
/* 0-7 are custom chars in CGRAM on real LCD */
- if (c < 8)
+ if ((unsigned int)c < 8)
c = '_';
display[cursor_y][cursor_x++] = c;
}
diff --git a/util.c b/util.c
index 794eea7..bd3f71f 100644
--- a/util.c
+++ b/util.c
@@ -16,10 +16,3 @@ void blank_to_eol(char *line, size_t len)
}
line[i] = '\0';
}
-
-void split_float(float input, int *integer_part, float *float_part)
-{
- *integer_part = (int)input;
- *float_part = fabs(input - *integer_part);
-}
-
diff --git a/util.h b/util.h
index 49812b9..71adadf 100644
--- a/util.h
+++ b/util.h
@@ -4,4 +4,3 @@
float pressure_to_metres_asl(float real, float setting);
void blank_to_eol(char *line, size_t len);
-void split_float(float input, int *integer_part, float *float_part);