log activity to file

This commit is contained in:
joeycastillo
2024-10-25 08:28:11 -04:00
parent c0514ad39a
commit af409b82f1
4 changed files with 62 additions and 34 deletions

View File

@@ -25,6 +25,7 @@
#include <stdlib.h>
#include <string.h>
#include "activity_logging_face.h"
#include "filesystem.h"
#include "watch.h"
#ifdef HAS_ACCELEROMETER
@@ -37,14 +38,27 @@ extern uint8_t active_minutes;
static void _activity_logging_face_log_data(activity_logging_state_t *state) {
watch_date_time_t date_time = movement_get_local_date_time();
size_t pos = state->data_points % ACTIVITY_LOGGING_NUM_DATA_POINTS;
activity_logging_data_point_t data_point = {0};
data_point.bit.day = date_time.unit.day;
data_point.bit.month = date_time.unit.month;
data_point.bit.hour = date_time.unit.hour;
data_point.bit.minute = date_time.unit.minute;
data_point.bit.active_minutes = active_minutes;
data_point.bit.orientation_changes = orientation_changes;
// print size of thing
printf("Size of data point: %d\n", sizeof(activity_logging_data_point_t));
if (filesystem_append_file("activity.dat", (char *)&data_point, sizeof(activity_logging_data_point_t))) {
printf("Data point written\n", state->data_points);
} else {
printf("Failed to write data point\n");
}
state->data[pos].reg = data_point.reg;
state->data_points++;
state->data[pos].timestamp.reg = date_time.reg;
state->data[pos].active_minutes = active_minutes;
state->data[pos].orientation_changes = orientation_changes;
active_minutes = 0;
orientation_changes = 0;
state->data_points++;
}
static void _activity_logging_face_update_display(activity_logging_state_t *state, bool clock_mode_24h) {
@@ -63,26 +77,25 @@ static void _activity_logging_face_update_display(activity_logging_state_t *stat
watch_display_text(WATCH_POSITION_TOP_RIGHT, buf);
} else if (state->ts_ticks) {
// we are displaying the timestamp in response to a button press
watch_date_time_t date_time = state->data[pos].timestamp;
watch_set_colon();
if (clock_mode_24h) {
watch_set_indicator(WATCH_INDICATOR_24H);
} else {
if (date_time.unit.hour > 11) watch_set_indicator(WATCH_INDICATOR_PM);
date_time.unit.hour %= 12;
if (date_time.unit.hour == 0) date_time.unit.hour = 12;
if (state->data[pos].bit.hour > 11) watch_set_indicator(WATCH_INDICATOR_PM);
state->data[pos].bit.hour %= 12;
if (state->data[pos].bit.hour == 0) state->data[pos].bit.hour = 12;
}
watch_display_text(WATCH_POSITION_TOP_LEFT, "AT");
sprintf(buf, "%2d", date_time.unit.day);
sprintf(buf, "%2d", state->data[pos].bit.day);
watch_display_text(WATCH_POSITION_TOP_RIGHT, buf);
sprintf(buf, "%2d%02d%02d", date_time.unit.hour, date_time.unit.minute, date_time.unit.second);
sprintf(buf, "%2d%02d%02d", state->data[pos].bit.hour, state->data[pos].bit.minute, 0);
watch_display_text(WATCH_POSITION_BOTTOM, buf);
} else {
// we are displaying the number of accelerometer wakeups and orientation changes
watch_display_text(WATCH_POSITION_TOP, "AC");
sprintf(buf, "%2d", state->display_index);
watch_display_text(WATCH_POSITION_TOP_RIGHT, buf);
sprintf(buf, "%-3lu/%2d", state->data[pos].orientation_changes > 999 ? 999 : state->data[pos].orientation_changes, state->data[pos].active_minutes);
sprintf(buf, "%-3lu/%2d", state->data[pos].bit.orientation_changes > 999 ? 999 : state->data[pos].bit.orientation_changes, state->data[pos].bit.active_minutes);
watch_display_text(WATCH_POSITION_BOTTOM, buf);
}
}
@@ -92,6 +105,8 @@ void activity_logging_face_setup(uint8_t watch_face_index, void ** context_ptr)
if (*context_ptr == NULL) {
*context_ptr = malloc(sizeof(activity_logging_state_t));
memset(*context_ptr, 0, sizeof(activity_logging_state_t));
// create file if it doesn't exist
if (!filesystem_file_exists("activity.dat")) filesystem_write_file("activity.dat", "", 0);
}
}
@@ -146,9 +161,8 @@ movement_watch_face_advisory_t activity_logging_face_advise(void *context) {
(void) context;
movement_watch_face_advisory_t retval = { 0 };
// this will get called at the top of each minute, so all we check is if we're at the top of the hour as well.
// if we are, we ask for a background task.
retval.wants_background_task = watch_rtc_get_date_time().unit.minute == 0;
// log data every 5 minutes
retval.wants_background_task = (movement_get_local_date_time().unit.minute % 5) == 0;
return retval;
}

View File

@@ -41,10 +41,16 @@
#define ACTIVITY_LOGGING_NUM_DATA_POINTS (36)
typedef struct {
watch_date_time_t timestamp;
uint8_t active_minutes;
uint32_t orientation_changes;
typedef union {
struct {
uint32_t day: 5;
uint32_t month: 4;
uint32_t hour: 5;
uint32_t minute: 6;
uint32_t active_minutes: 3;
uint32_t orientation_changes: 9;
} bit;
uint32_t reg;
} activity_logging_data_point_t;
typedef struct {