aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Phillips <david@yeah.nah.nz>2021-02-27 22:26:22 +1300
committerDavid Phillips <david@yeah.nah.nz>2021-02-27 22:26:22 +1300
commiteaba2a7b0658f648bf32219aec0ce827c94333a0 (patch)
tree42f50d2f3c87abfb7af0149430a67afb778fd4f1
parent450829bab633da3bce84a9a0f6857e68e46c7eb4 (diff)
downloadaltimeter-eaba2a7b0658f648bf32219aec0ce827c94333a0.tar.xz
Make timer mandatory for data manager
The NULL check was a bit of a hack to get the test building. Probably a bad idea to leave it around, since there's no time it's sane to leave the timestamps on data manager contexts unset.
-rw-r--r--Makefile1
-rw-r--r--data_manager.c6
-rw-r--r--test_data_manager.c12
-rw-r--r--timer.c10
-rw-r--r--timer_linux.c29
5 files changed, 39 insertions, 19 deletions
diff --git a/Makefile b/Makefile
index 809c153..a1d6ab9 100644
--- a/Makefile
+++ b/Makefile
@@ -62,6 +62,7 @@ TEST_OBJ = \
$(NATIVE_DIR)/test_data_manager.o \
$(NATIVE_DIR)/data_manager.o \
$(NATIVE_DIR)/barometer_sim.o \
+ $(NATIVE_DIR)/timer_linux.o \
$(NATIVE_DIR)/util.o
REAL_OBJ = \
diff --git a/data_manager.c b/data_manager.c
index b77ba81..8202216 100644
--- a/data_manager.c
+++ b/data_manager.c
@@ -15,7 +15,7 @@ static float pressure_to_metres_asl(float real, float setting)
void data_manager_init(struct data_ctx *ctx, struct timer *timer, struct barometer *barometer)
{
- ctx->setting = 1013.25;
+ ctx->setting = 1013.25; /* FIXME don't hardcode, at least not here */
ctx->timer = timer;
ctx->barometer = barometer;
data_manager_tick(ctx);
@@ -24,9 +24,7 @@ void data_manager_init(struct data_ctx *ctx, struct timer *timer, struct baromet
void data_manager_tick(struct data_ctx *ctx)
{
/* FIXME altitude rate */
-// struct data_ctx previous_ctx = *ctx;
- if (ctx->timer)
- ctx->timestamp = ctx->timer->get_time();
+ ctx->timestamp = ctx->timer->get_time();
ctx->pressure = ctx->barometer->read_pressure();
ctx->altitude = pressure_to_metres_asl(ctx->pressure, ctx->setting);
}
diff --git a/test_data_manager.c b/test_data_manager.c
index 00b5a2a..044282b 100644
--- a/test_data_manager.c
+++ b/test_data_manager.c
@@ -3,6 +3,7 @@
#include "data_manager.h"
#include "test_runner.h"
#include "barometer.h"
+#include "timer.h"
#include "unity.h"
static int mock_baro_called;
@@ -23,18 +24,19 @@ static struct barometer mock_barometer = {
RUNNER_DECLARE_TEST(test_data_manager_init_setting)
{
struct data_ctx ctx;
+ struct timer timer;
+ get_system_timer(&timer);
reset_mock_get_baro();
- data_manager_init(&ctx, NULL, &mock_barometer);
+ data_manager_init(&ctx, &timer, &mock_barometer);
TEST_ASSERT_EQUAL(1, mock_baro_called);
}
RUNNER_DECLARE_TEST(test_data_manager_first_readings)
{
struct data_ctx ctx;
- reset_mock_get_baro();
- data_manager_init(&ctx, NULL, &mock_barometer);
- TEST_ASSERT_EQUAL(1, mock_baro_called);
-
+ struct timer timer;
+ get_system_timer(&timer);
+ data_manager_init(&ctx, &timer, &mock_barometer);
reset_mock_get_baro();
data_manager_tick(&ctx);
diff --git a/timer.c b/timer.c
index 7d82b0f..498e621 100644
--- a/timer.c
+++ b/timer.c
@@ -11,9 +11,6 @@
* interrupts already disabled in order to be safe
*/
-/* XXX hardcoded based upon TCCR3B selecting /256 prescaled clock */
-#define TIMER_HZ (F_CPU / 256)
-
static void timer_init(void)
{
/* timer1 has /1024 prescaler, 1 Hz comparator val, enable IRQ */
@@ -28,18 +25,11 @@ static void timer_init(void)
TCCR3B |= (1 << CS32);
}
-static float timer_stamps_to_seconds(uint16_t start, uint16_t end)
-{
- return (float)(end - start) / TIMER_HZ;
-}
-
static uint16_t timer_get_value(void)
{
return TCNT3;
}
-/**/
-
void get_system_timer(struct timer *timer)
{
timer->init = timer_init;
diff --git a/timer_linux.c b/timer_linux.c
new file mode 100644
index 0000000..56886e3
--- /dev/null
+++ b/timer_linux.c
@@ -0,0 +1,29 @@
+#include <time.h>
+#include <stdint.h>
+
+#include "timer.h"
+
+/* On POSIX, we emulate a monotonic 16-bit timer for timestamping events in
+ * lieu of timer3 on the AVR.
+ */
+
+static void timer_init(void)
+{
+ /* do nothing */
+}
+
+static uint16_t timer_get_value(void)
+{
+ struct timespec ts;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ /* FIXME abort on error */
+ /* return milliseconds as uint16_t, truncating the rest */
+ return ts.tv_nsec / 1e6;
+}
+
+
+void get_system_timer(struct timer *timer)
+{
+ timer->init = timer_init;
+ timer->get_time = timer_get_value;
+}