aboutsummaryrefslogtreecommitdiff
path: root/stm32f091/backlog.c
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-09-26 16:45:08 +0200
committerlonkaars <loek@pipeframe.xyz>2022-09-26 16:45:08 +0200
commitf9f1e5bf0bd7e8b0ca0708fb63f4d2c56d47ef49 (patch)
treecae4ea19f14938413f912a1c8b51a68d5fc6f69c /stm32f091/backlog.c
parent01279df4bd82e1f35599e4ca59a18e5756fda212 (diff)
parent5829fb58101a9edb6b9d31c9e22183f5159e1480 (diff)
merge dev into master
Diffstat (limited to 'stm32f091/backlog.c')
-rw-r--r--stm32f091/backlog.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/stm32f091/backlog.c b/stm32f091/backlog.c
new file mode 100644
index 0000000..3f21924
--- /dev/null
+++ b/stm32f091/backlog.c
@@ -0,0 +1,34 @@
+#include <stdlib.h>
+
+#include "backlog.h"
+
+ws_s_backlog_database* WS_G_BACKLOG_DATABASE = NULL;
+
+void ws_backlog_alloc(uint16_t record_amt) {
+ WS_G_BACKLOG_DATABASE = malloc(sizeof(ws_s_backlog_database) + sizeof(ws_s_backlog_record) * record_amt);
+ WS_G_BACKLOG_DATABASE->buffer_size = record_amt;
+ WS_G_BACKLOG_DATABASE->buffer_start = 0;
+ WS_G_BACKLOG_DATABASE->buffer_end = 0;
+}
+
+void ws_backlog_add_record(ws_s_backlog_record record) {
+ static uint16_t id = 0;
+
+ WS_G_BACKLOG_DATABASE->records[WS_G_BACKLOG_DATABASE->buffer_end].id = id++;
+ WS_G_BACKLOG_DATABASE->records[WS_G_BACKLOG_DATABASE->buffer_end].sens_atm_pressure = record.sens_atm_pressure;
+ WS_G_BACKLOG_DATABASE->records[WS_G_BACKLOG_DATABASE->buffer_end].sens_humidity = record.sens_humidity;
+ WS_G_BACKLOG_DATABASE->records[WS_G_BACKLOG_DATABASE->buffer_end].sens_temperature = record.sens_temperature;
+
+ // shift buffer start/end
+ WS_G_BACKLOG_DATABASE->buffer_end = (WS_G_BACKLOG_DATABASE->buffer_end + 1) % WS_G_BACKLOG_DATABASE->buffer_size;
+ if (WS_G_BACKLOG_DATABASE->buffer_end == WS_G_BACKLOG_DATABASE->buffer_start)
+ WS_G_BACKLOG_DATABASE->buffer_start = (WS_G_BACKLOG_DATABASE->buffer_start + 1) % WS_G_BACKLOG_DATABASE->buffer_size;
+}
+
+ws_s_backlog_record* ws_backlog_get_record(uint16_t record_index) {
+ return &WS_G_BACKLOG_DATABASE->records[record_index];
+}
+
+ws_s_backlog_record* ws_backlog_get_last_record(uint16_t record_offset) {
+ return ws_backlog_get_record((WS_G_BACKLOG_DATABASE->buffer_end - record_offset - 1) % WS_G_BACKLOG_DATABASE->buffer_size);
+}