simplify activity tracking, just active minutes for now

This commit is contained in:
Joey Castillo 2025-05-17 13:54:37 -04:00
parent f9c30d1edd
commit 7d8ad1b87b
10 changed files with 83 additions and 270 deletions

View File

@ -142,7 +142,6 @@ include watch-faces.mk
SRCS += \
./movement.c \
./movement_activity.c \
# Finally, leave this line at the bottom of the file.
include $(GOSSAMER_PATH)/rules.mk

View File

@ -44,7 +44,6 @@
#include "tc.h"
#include "evsys.h"
#include "delay.h"
#include "movement_activity.h"
#include "thermistor_driver.h"
#include "movement_config.h"
@ -77,7 +76,6 @@ void cb_tick(void);
void cb_accelerometer_event(void);
void cb_accelerometer_wake(void);
uint8_t active_minutes = 0;
#if __EMSCRIPTEN__
void yield(void) {
@ -155,18 +153,6 @@ static inline void _movement_disable_fast_tick_if_possible(void) {
static void _movement_handle_top_of_minute(void) {
watch_date_time_t date_time = watch_rtc_get_date_time();
if (movement_state.has_lis2dw) {
bool accelerometer_is_alseep = HAL_GPIO_A4_read();
if (!accelerometer_is_alseep) active_minutes++;
printf("Active minutes: %d\n", active_minutes);
// log data every five minutes, and reset the active_minutes count.
if ((date_time.unit.minute % 5) == 0) {
_movement_log_data();
active_minutes = 0;
}
}
// update the DST offset cache every 30 minutes, since someplace in the world could change.
if (date_time.unit.minute % 30 == 0) {
_movement_update_dst_offset_cache();

View File

@ -1,72 +0,0 @@
/*
* MIT License
*
* Copyright (c) 2025 Joey Castillo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "movement.h"
#include "movement_activity.h"
#include "tc.h"
#include "thermistor_driver.h"
// RAM to stash the data points.
movement_activity_data_point movement_activity_log[MOVEMENT_NUM_DATA_POINTS] = {0};
// the absolute number of data points logged
uint32_t data_points = 0;
// hacky: we're just tapping into Movement's global state for activity detection.
// do we need better API for this? i'm less bothered now that it's all in Movement.
extern uint8_t active_minutes;
void _movement_log_data(void) {
size_t pos = data_points % MOVEMENT_NUM_DATA_POINTS;
movement_activity_data_point data_point = {0};
// Movement tracks active minutes when deciding whether to sleep.
data_point.bit.active_minutes = active_minutes;
if (tc_is_enabled(2)) {
// orientation changes are counted in TC2. stash them in the data point...
data_point.bit.orientation_changes = tc_count16_get_count(2);
// ...and then reset the number of orientation changes.
tc_count16_set_count(2, 0);
}
// log the temperature
float temperature_c = movement_get_temperature();
// offset the temperature by 30, so -30°C is 0, and 72.3°C is 102.3
temperature_c = temperature_c + 30;
if (temperature_c < 0) temperature_c = 0;
if (temperature_c > 102.3) temperature_c = 102.3;
// now we have can fit temperature into a 10-bit value
data_point.bit.measured_temperature = temperature_c * 10;
/// TODO: log light level
// log the data point
movement_activity_log[pos].reg = data_point.reg;
data_points++;
}
movement_activity_data_point *movement_get_data_log(uint32_t *count) {
*count = data_points;
return movement_activity_log;
}

View File

@ -1,48 +0,0 @@
/*
* MIT License
*
* Copyright (c) 2025 Joey Castillo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include <stdint.h>
// Log 36 hours of data points. Each data point captures 5 minutes.
#define MOVEMENT_NUM_DATA_POINTS (36 * (60 / 5))
typedef union {
struct {
uint32_t active_minutes: 3;
uint32_t orientation_changes: 9;
uint32_t measured_temperature: 10;
uint32_t measured_light: 10;
} bit;
uint32_t reg;
} movement_activity_data_point;
/// @brief Internal function, called every 5 minutes to log data.
void _movement_log_data(void);
/// @brief Returns a pointer to the data log.
/// @param count is a pointer to a uint32_t. The absolute number of data points logged is returned by reference.
/// You can assume that log[count % MOVEMENT_NUM_DATA_POINTS] is the latest data point, and work backwards from there.
movement_activity_data_point *movement_get_data_log(uint32_t *count);

View File

@ -35,7 +35,7 @@
#include "moon_phase_face.h"
#include "days_since_face.h"
#include "character_set_face.h"
#include "accel_interrupt_count_face.h"
#include "accelerometer_status_face.h"
#include "all_segments_face.h"
#include "float_demo_face.h"
#include "temperature_display_face.h"

View File

@ -9,7 +9,7 @@ SRCS += \
./watch-faces/complication/sunrise_sunset_face.c \
./watch-faces/complication/moon_phase_face.c \
./watch-faces/complication/days_since_face.c \
./watch-faces/demo/accel_interrupt_count_face.c \
./watch-faces/demo/accelerometer_status_face.c \
./watch-faces/demo/all_segments_face.c \
./watch-faces/demo/character_set_face.c \
./watch-faces/demo/float_demo_face.c \

View File

@ -24,34 +24,26 @@
#include <stdlib.h>
#include <string.h>
#include "accel_interrupt_count_face.h"
#include "accelerometer_status_face.h"
#include "lis2dw.h"
#include "tc.h"
#include "watch.h"
// hacky: we're just tapping into Movement's global state.
// we should make better API for this.
extern uint8_t active_minutes;
static void _accel_interrupt_count_face_update_display(accel_interrupt_count_state_t *state) {
static void _accelerometer_status_face_update_display(accel_interrupt_count_state_t *state) {
(void) state;
char buf[8];
// Accelerometer title
watch_display_text(WATCH_POSITION_TOP_LEFT, "AC");
watch_display_text_with_fallback(WATCH_POSITION_TOP, "ACCEL", "AC");
// sensing is live!
watch_set_indicator(WATCH_INDICATOR_SIGNAL);
// Sleep/active state
if (HAL_GPIO_A4_read()) watch_display_text(WATCH_POSITION_TOP_RIGHT, " S");
else watch_display_text(WATCH_POSITION_TOP_RIGHT, " A");
// Orientation changes / active minutes
uint16_t orientation_changes = 0;
if (tc_is_enabled(2)) orientation_changes = tc_count16_get_count(2);
sprintf(buf, "%-3u/%2d", orientation_changes > 999 ? 999 : orientation_changes, active_minutes);
watch_display_text(WATCH_POSITION_BOTTOM, buf);
if (HAL_GPIO_A4_read()) watch_display_text(WATCH_POSITION_BOTTOM, "Still ");
else watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "Active", " ACtiv");
}
void accel_interrupt_count_face_setup(uint8_t watch_face_index, void ** context_ptr) {
void accelerometer_status_face_setup(uint8_t watch_face_index, void ** context_ptr) {
(void) watch_face_index;
if (*context_ptr == NULL) {
*context_ptr = malloc(sizeof(accel_interrupt_count_state_t));
@ -59,7 +51,7 @@ void accel_interrupt_count_face_setup(uint8_t watch_face_index, void ** context_
}
}
void accel_interrupt_count_face_activate(void *context) {
void accelerometer_status_face_activate(void *context) {
accel_interrupt_count_state_t *state = (accel_interrupt_count_state_t *)context;
// never in settings mode at the start
@ -72,10 +64,11 @@ void accel_interrupt_count_face_activate(void *context) {
state->threshold = lis2dw_get_wakeup_threshold();
}
bool accel_interrupt_count_face_loop(movement_event_t event, void *context) {
bool accelerometer_status_face_loop(movement_event_t event, void *context) {
accel_interrupt_count_state_t *state = (accel_interrupt_count_state_t *)context;
if (state->is_setting) {
watch_clear_indicator(WATCH_INDICATOR_SIGNAL);
switch (event.event_type) {
case EVENT_LIGHT_BUTTON_DOWN:
state->new_threshold = (state->new_threshold + 1) % 64;
@ -106,7 +99,7 @@ bool accel_interrupt_count_face_loop(movement_event_t event, void *context) {
switch (event.event_type) {
case EVENT_ACTIVATE:
case EVENT_TICK:
_accel_interrupt_count_face_update_display(state);
_accelerometer_status_face_update_display(state);
break;
case EVENT_ALARM_LONG_PRESS:
state->new_threshold = state->threshold;
@ -121,13 +114,6 @@ bool accel_interrupt_count_face_loop(movement_event_t event, void *context) {
return true;
}
void accel_interrupt_count_face_resign(void *context) {
void accelerometer_status_face_resign(void *context) {
(void) context;
}
movement_watch_face_advisory_t accel_interrupt_count_face_advise(void *context) {
(void) context;
movement_watch_face_advisory_t retval = { 0 };
return retval;
}

View File

@ -41,16 +41,15 @@ typedef struct {
bool is_setting;
} accel_interrupt_count_state_t;
void accel_interrupt_count_face_setup(uint8_t watch_face_index, void ** context_ptr);
void accel_interrupt_count_face_activate(void *context);
bool accel_interrupt_count_face_loop(movement_event_t event, void *context);
void accel_interrupt_count_face_resign(void *context);
movement_watch_face_advisory_t accel_interrupt_count_face_advise(void *context);
void accelerometer_status_face_setup(uint8_t watch_face_index, void ** context_ptr);
void accelerometer_status_face_activate(void *context);
bool accelerometer_status_face_loop(movement_event_t event, void *context);
void accelerometer_status_face_resign(void *context);
#define accel_interrupt_count_face ((const watch_face_t){ \
accel_interrupt_count_face_setup, \
accel_interrupt_count_face_activate, \
accel_interrupt_count_face_loop, \
accel_interrupt_count_face_resign, \
accel_interrupt_count_face_advise, \
#define accelerometer_status_face ((const watch_face_t){ \
accelerometer_status_face_setup, \
accelerometer_status_face_activate, \
accelerometer_status_face_loop, \
accelerometer_status_face_resign, \
NULL, \
})

View File

@ -27,55 +27,44 @@
#include "activity_logging_face.h"
#include "filesystem.h"
#include "watch.h"
#include "movement_activity.h"
#include "watch_utility.h"
static void _activity_logging_face_update_display(activity_logging_state_t *state, bool clock_mode_24h) {
static void _activity_logging_face_update_display(activity_logging_state_t *state) {
char buf[8];
uint32_t count = 0;
movement_activity_data_point *data_points = movement_get_data_log(&count);
int32_t pos = ((int32_t)count - 1 - (int32_t)state->display_index) % MOVEMENT_NUM_DATA_POINTS;
watch_date_time_t timestamp = movement_get_local_date_time();
// round to previous 5 minute increment
timestamp.unit.minute = timestamp.unit.minute - (timestamp.unit.minute % 5);
// advance backward by 5 minutes for each increment of state->display_index
uint32_t unix_timestamp = watch_utility_date_time_to_unix_time(timestamp, movement_get_current_timezone_offset());
unix_timestamp -= 300 * state->display_index;
timestamp = watch_utility_date_time_from_unix_time(unix_timestamp, movement_get_current_timezone_offset());
watch_display_text_with_fallback(WATCH_POSITION_TOP_LEFT, "ACT", "AC");
watch_clear_indicator(WATCH_INDICATOR_24H);
watch_clear_indicator(WATCH_INDICATOR_PM);
watch_clear_colon();
if (pos < 0) {
// no data at this index
watch_display_text_with_fallback(WATCH_POSITION_TOP_LEFT, "LOG", "AC");
watch_display_text(WATCH_POSITION_BOTTOM, "no dat");
sprintf(buf, "%2d", state->display_index);
if (state->display_index == 0) {
// if we are at today, just show the count so far
snprintf(buf, 8, "%2d", timestamp.unit.day);
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_set_colon();
if (clock_mode_24h) {
watch_set_indicator(WATCH_INDICATOR_24H);
} else {
if (timestamp.unit.hour > 11) watch_set_indicator(WATCH_INDICATOR_PM);
timestamp.unit.hour %= 12;
if (timestamp.unit.hour == 0) timestamp.unit.hour = 12;
}
watch_display_text_with_fallback(WATCH_POSITION_TOP_LEFT, "T*D", "AT");
sprintf(buf, "%2d", timestamp.unit.day);
watch_display_text(WATCH_POSITION_TOP_RIGHT, buf);
sprintf(buf, "%2d%02d%02d", timestamp.unit.hour, timestamp.unit.minute, 0);
snprintf(buf, 8, "%4d ", state->active_minutes_today);
watch_display_text(WATCH_POSITION_BOTTOM, buf);
// also indicate that this is the active day — we are still sensing active minutes!
watch_set_indicator(WATCH_INDICATOR_SIGNAL);
} else {
// we are displaying the number of accelerometer wakeups and orientation changes
watch_display_text_with_fallback(WATCH_POSITION_TOP_LEFT, "LOG", "AC");
sprintf(buf, "%2d", state->display_index);
// otherwise we need to go into the log.
watch_clear_indicator(WATCH_INDICATOR_SIGNAL);
int32_t pos = ((int16_t)state->data_points - (int32_t)state->display_index) % ACTIVITY_LOGGING_NUM_DAYS;
// get day of month for today - display_index
uint32_t unixtime = watch_utility_date_time_to_unix_time(timestamp, movement_get_current_timezone_offset());
unixtime -= 86400 * state->display_index;
timestamp = watch_utility_date_time_from_unix_time(unixtime, movement_get_current_timezone_offset());
// display date
snprintf(buf, 8, "%2d", timestamp.unit.day);
watch_display_text(WATCH_POSITION_TOP_RIGHT, buf);
sprintf(buf, "%-3u/%2d", data_points[pos].bit.orientation_changes > 999 ? 999 : data_points[pos].bit.orientation_changes, data_points[pos].bit.active_minutes);
watch_display_text(WATCH_POSITION_BOTTOM, buf);
if (pos < 0) {
// no data at this index
watch_display_text(WATCH_POSITION_BOTTOM, "no dat");
} else {
// we are displaying the number active minutes
snprintf(buf, 8, "%4d ", state->activity_log[pos]);
watch_display_text(WATCH_POSITION_BOTTOM, buf);
}
}
}
@ -90,65 +79,23 @@ void activity_logging_face_setup(uint8_t watch_face_index, void ** context_ptr)
void activity_logging_face_activate(void *context) {
activity_logging_state_t *state = (activity_logging_state_t *)context;
state->display_index = 0;
state->ts_ticks = 0;
state->data_dump_idx = -1;
}
bool activity_logging_face_loop(movement_event_t event, void *context) {
activity_logging_state_t *state = (activity_logging_state_t *)context;
switch (event.event_type) {
case EVENT_TIMEOUT:
if (state->data_dump_idx == -1) movement_move_to_face(0);
break;
case EVENT_LIGHT_LONG_PRESS:
// light button shows the timestamp, but if you need the light, long press it.
movement_illuminate_led();
break;
case EVENT_LIGHT_BUTTON_DOWN:
state->ts_ticks = 2;
_activity_logging_face_update_display(state, movement_clock_mode_24h());
break;
case EVENT_ALARM_BUTTON_DOWN:
state->display_index = (state->display_index + 1) % ACTIVITY_LOGGING_NUM_DATA_POINTS;
state->ts_ticks = 0;
state->display_index = (state->display_index + 1) % ACTIVITY_LOGGING_NUM_DAYS;
// fall through
case EVENT_ACTIVATE:
_activity_logging_face_update_display(state, movement_clock_mode_24h());
_activity_logging_face_update_display(state);
break;
case EVENT_ALARM_LONG_PRESS:
state->data_dump_idx = 0;
watch_set_indicator(WATCH_INDICATOR_ARROWS);
movement_request_tick_frequency(4);
watch_set_decimal_if_available();
// fall through
case EVENT_TICK:
if (state->ts_ticks && --state->ts_ticks == 0) {
_activity_logging_face_update_display(state, movement_clock_mode_24h());
}
if (state->data_dump_idx != -1) {
// dance through the full buffer
char buf[8];
uint32_t count = 0;
movement_activity_data_point *data_points = movement_get_data_log(&count);
int32_t pos = ((int32_t)count - 1 - (int32_t)state->data_dump_idx) % MOVEMENT_NUM_DATA_POINTS;
sprintf(buf, "%03d ", state->data_dump_idx);
watch_display_text_with_fallback(WATCH_POSITION_TOP_LEFT, buf, buf + 2);
sprintf(buf, "%3d%3d", data_points[pos].bit.measured_temperature - 300, data_points[pos].bit.orientation_changes > 999 ? 999 : data_points[pos].bit.orientation_changes);
buf[6] = 0;
watch_display_text(WATCH_POSITION_BOTTOM, buf);
sprintf(buf, "%2d", data_points[pos].bit.active_minutes);
watch_display_text(WATCH_POSITION_TOP_RIGHT, buf);
state->data_dump_idx++;
if (state->data_dump_idx >= MOVEMENT_NUM_DATA_POINTS) {
state->data_dump_idx = -1;
watch_clear_indicator(WATCH_INDICATOR_ARROWS);
watch_clear_decimal_if_available();
movement_request_tick_frequency(1);
state->display_index = 0;
_activity_logging_face_update_display(state, movement_clock_mode_24h());
}
case EVENT_BACKGROUND_TASK:
{
size_t pos = state->data_points % ACTIVITY_LOGGING_NUM_DAYS;
state->activity_log[pos] = state->active_minutes_today;
state->data_points++;
state->active_minutes_today = 0;
}
break;
default:
@ -162,3 +109,18 @@ bool activity_logging_face_loop(movement_event_t event, void *context) {
void activity_logging_face_resign(void *context) {
(void) context;
}
movement_watch_face_advisory_t activity_logging_face_advise(void *context) {
activity_logging_state_t *state = (activity_logging_state_t *)context;
movement_watch_face_advisory_t retval = { 0 };
if (HAL_GPIO_A4_read()) state->active_minutes_today++;
watch_date_time_t datetime = movement_get_local_date_time();
// request a background task at midnight to shuffle the data into the log
if (datetime.unit.hour == 0 && datetime.unit.minute == 0) {
retval.wants_background_task = true;
}
return retval;
}

View File

@ -54,12 +54,13 @@
#include "movement.h"
#include "watch.h"
#define ACTIVITY_LOGGING_NUM_DATA_POINTS (100)
#define ACTIVITY_LOGGING_NUM_DAYS (14)
typedef struct {
uint8_t display_index; // the index we are displaying on screen
uint8_t ts_ticks; // when the user taps the LIGHT button, we show the timestamp for a few ticks.
int16_t data_dump_idx; // for dumping the full activity log on long press of Alarm
uint16_t activity_log[ACTIVITY_LOGGING_NUM_DAYS]; // the activity log
uint16_t data_points; // the number of days logged
uint8_t display_index; // the index we are displaying on screen
uint16_t active_minutes_today; // the number of active minutes logged today
} activity_logging_state_t;
void activity_logging_face_setup(uint8_t watch_face_index, void ** context_ptr);
@ -73,5 +74,5 @@ movement_watch_face_advisory_t activity_logging_face_advise(void *context);
activity_logging_face_activate, \
activity_logging_face_loop, \
activity_logging_face_resign, \
NULL, \
activity_logging_face_advise, \
})