Skip to content

Commit

Permalink
Merge branch 'main' into fix-months
Browse files Browse the repository at this point in the history
  • Loading branch information
voloved committed Oct 24, 2024
2 parents 54b58a7 + 118c6f1 commit 64a6998
Show file tree
Hide file tree
Showing 21 changed files with 754 additions and 149 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ INCLUDES += \
-I./filesystem \
-I./shell \
-I./movement/lib/sunriset \
-I./movement/lib/chirpy_tx \
-I./watch-library/shared/watch \
-I./watch-library/shared/driver \
-I./watch-faces/clock \
-I./watch-faces/complication \
-I./watch-faces/demo \
-I./watch-faces/sensor \
-I./watch-faces/settings \
-I./watch-faces/io \

# Add your source files here.
SRCS += \
Expand All @@ -62,6 +64,7 @@ SRCS += \
./shell/shell.c \
./shell/shell_cmd_list.c \
./movement/lib/sunriset/sunriset.c \
./movement/lib/chirpy_tx/chirpy_tx.c \
./watch-library/shared/driver/thermistor_driver.c \
./watch-library/shared/watch/watch_common_buzzer.c \
./watch-library/shared/watch/watch_common_display.c \
Expand Down
87 changes: 63 additions & 24 deletions movement.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ void cb_tick(void);

#ifdef HAS_ACCELEROMETER
void cb_motion_interrupt_1(void);
void cb_motion_interrupt_2(void);
uint32_t orientation_changes = 0;
uint8_t active_minutes = 0;
#endif

#if __EMSCRIPTEN__
Expand Down Expand Up @@ -155,6 +158,11 @@ 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();

#ifdef HAS_ACCELEROMETER
// every minute, we want to log whether the accelerometer is asleep or awake.
if (!HAL_GPIO_A3_read()) active_minutes++;
#endif

// 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();
Expand Down Expand Up @@ -626,37 +634,28 @@ void app_setup(void) {
#endif
watch_enable_i2c();
if (lis2dw_begin()) {
lis2dw_set_mode(LIS2DW_MODE_LOW_POWER); // select low power (not high performance)
lis2dw_set_low_power_mode(LIS2DW_LP_MODE_1); // lowest power mode, 12-bit, up this if needed
lis2dw_set_low_noise_mode(true); // only marginally raises power consumption
lis2dw_enable_sleep(); // sleep at 1.6Hz, wake to 12.5Hz?
lis2dw_set_range(LIS2DW_CTRL6_VAL_RANGE_2G); // data sheet recommends 2G range
lis2dw_set_data_rate(LIS2DW_DATA_RATE_LOWEST); // 1.6Hz in low power mode
lis2dw_set_mode(LIS2DW_MODE_LOW_POWER); // select low power (not high performance) mode
lis2dw_set_low_power_mode(LIS2DW_LP_MODE_1); // lowest power mode, 12-bit
lis2dw_set_low_noise_mode(false); // low noise mode raises power consumption slightly; we don't need it
lis2dw_set_data_rate(LIS2DW_DATA_RATE_LOWEST); // sample at 1.6 Hz, lowest rate available
lis2dw_enable_stationary_motion_detection(); // stationary/motion detection mode keeps the data rate at 1.6 Hz even in sleep
lis2dw_set_range(LIS2DW_RANGE_2_G); // Application note AN5038 recommends 2g range
lis2dw_enable_sleep(); // allow acceleromter to sleep and wake on activity
lis2dw_configure_wakeup_threshold(24); // threshold is 1/64th of full scale, so for a FS of ±2G this is 1.5G
lis2dw_configure_wakeup_threshold(24); // g threshold to wake up: (2 * FS / 64) where FS is "full scale" of ±2g.
lis2dw_configure_6d_threshold(3); // 0-3 is 80, 70, 60, or 50 degrees. 50 is least precise, hopefully most sensitive?

// set up interrupts:
// INT1 is on A4 which can wake from deep sleep. Wake on 6D orientation change.
lis2dw_configure_int1(LIS2DW_CTRL4_INT1_6D);
watch_register_interrupt_callback(HAL_GPIO_A4_pin(), cb_motion_interrupt_1, INTERRUPT_TRIGGER_RISING);
lis2dw_configure_int1(LIS2DW_CTRL4_INT1_6D | LIS2DW_CTRL4_INT1_WU | LIS2DW_CTRL4_INT1_TAP | LIS2DW_CTRL4_INT1_SINGLE_TAP);
watch_register_extwake_callback(HAL_GPIO_A4_pin(), cb_motion_interrupt_1, true);

// configure the accelerometer to fire INT2 when its sleep state changes.
lis2dw_configure_int2(LIS2DW_CTRL5_INT2_SLEEP_CHG);
// configure the accelerometer to output the sleep state on INT2.
lis2dw_configure_int2(LIS2DW_CTRL5_INT2_SLEEP_STATE | LIS2DW_CTRL5_INT2_SLEEP_CHG);
// INT2 is wired to pin A3. set it up on the external interrupt controller.
HAL_GPIO_A3_in();
HAL_GPIO_A3_pmuxen(HAL_GPIO_PMUX_EIC);
eic_configure_pin(HAL_GPIO_A3_pin(), INTERRUPT_TRIGGER_FALLING);
// but rather than firing an interrupt, we'll have it generate an event instead.
eic_enable_event(HAL_GPIO_A3_pin());
// we can route the EXTINT3 event generator to the TC2 event user...
evsys_configure_channel(0, EVSYS_ID_GEN_EIC_EXTINT_3, EVSYS_ID_USER_TC2_EVU, true, true);
// and use the TC2 event to count the number of times the sleep state changes.
// note that this doesn't actually wake the watch — we can maintain this count even in standby.
tc_init(2, GENERIC_CLOCK_3, TC_PRESCALER_DIV1);
tc_set_event_action(2, TC_EVENT_ACTION_COUNT);
tc_set_counter_mode(2, TC_COUNTER_MODE_16BIT);
tc_enable(2);
eic_configure_pin(HAL_GPIO_A3_pin(), INTERRUPT_TRIGGER_BOTH);
watch_register_interrupt_callback(HAL_GPIO_A3_pin(), cb_motion_interrupt_2, INTERRUPT_TRIGGER_BOTH);

lis2dw_enable_interrupts();
}
Expand Down Expand Up @@ -698,6 +697,17 @@ static void _sleep_mode_app_loop(void) {
bool app_loop(void) {
const watch_face_t *wf = &watch_faces[movement_state.current_face_idx];
bool woke_up_for_buzzer = false;

// REMOVE THIS before shipping the accelerometer board: test beeps.
if (movement_state.settings.bit.button_should_sound && event.event_type == EVENT_ACCELEROMETER_WAKE) {
watch_buzzer_play_note_with_volume(BUZZER_NOTE_C6, 20, WATCH_BUZZER_VOLUME_SOFT);
}
if (movement_state.settings.bit.button_should_sound && event.event_type == EVENT_ACCELEROMETER_SLEEP) {
watch_buzzer_play_note_with_volume(BUZZER_NOTE_C5, 15, WATCH_BUZZER_VOLUME_SOFT);
watch_buzzer_play_note_with_volume(BUZZER_NOTE_REST, 10, WATCH_BUZZER_VOLUME_SOFT);
watch_buzzer_play_note_with_volume(BUZZER_NOTE_C5, 15, WATCH_BUZZER_VOLUME_SOFT);
}

if (movement_state.watch_face_changed) {
if (movement_state.settings.bit.button_should_sound) {
// low note for nonzero case, high note for return to watch_face 0
Expand Down Expand Up @@ -928,7 +938,36 @@ void cb_tick(void) {

#ifdef HAS_ACCELEROMETER
void cb_motion_interrupt_1(void) {
// TODO: Find out what motion event woke us up.
printf("INT1\n");
uint8_t int_src = lis2dw_get_interrupt_source();
if (int_src & LIS2DW_REG_ALL_INT_SRC_6D_IA) {
event.event_type = EVENT_ORIENTATION_CHANGE;
orientation_changes++;
printf("Orientation change\n");
}
if (int_src & LIS2DW_REG_ALL_INT_SRC_DOUBLE_TAP) event.event_type = EVENT_DOUBLE_TAP;
if (int_src & LIS2DW_REG_ALL_INT_SRC_SINGLE_TAP) event.event_type = EVENT_SINGLE_TAP;
if (int_src & LIS2DW_REG_ALL_INT_SRC_FF_IA) event.event_type = EVENT_FREE_FALL;

// These are handled on INT2, which is not available in low energy mode.
// If we want wakeup events on INT1, we could ask for LIS2DW_CTRL4_INT1_WU and get wake events here.
// If we want sleep change events on INT1, we'd have to set LIS2DW_CTRL5_INT2_SLEEP_CHG and LIS2DW_CTRL7_VAL_INT2_ON_INT1
// That would give us these two cases:
// if (int_src & LIS2DW_REG_ALL_INT_SRC_WU_IA) printf(" Wake up");
// if (int_src & LIS2DW_REG_ALL_INT_SRC_SLEEP_CHANGE_IA) printf(" Sleep change");
}

void cb_motion_interrupt_2(void) {
if (HAL_GPIO_A3_read()) {
event.event_type = EVENT_ACCELEROMETER_SLEEP;
printf("Sleep on INT2\n");
} else {
event.event_type = EVENT_ACCELEROMETER_WAKE;
printf("Wake on INT2\n");
// Not sure if it's useful to know what axis exceeded the threshold, but here's that:
// uint8_t int_src = lis2dw_get_wakeup_source();
// if (int_src & LIS2DW_WAKE_UP_SRC_VAL_X_WU) printf("Wake on X");
// if (int_src & LIS2DW_WAKE_UP_SRC_VAL_Y_WU) printf("Wake on Y");
// if (int_src & LIS2DW_WAKE_UP_SRC_VAL_Z_WU) printf("Wake on Z");
}
}
#endif
7 changes: 7 additions & 0 deletions movement.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ typedef enum {
EVENT_ALARM_BUTTON_UP, // The alarm button was pressed for less than half a second, and released.
EVENT_ALARM_LONG_PRESS, // The alarm button was held for over half a second, but not yet released.
EVENT_ALARM_LONG_UP, // The alarm button was held for over half a second, and released.

EVENT_ACCELEROMETER_WAKE, // The accelerometer has detected motion and woken up.
EVENT_ACCELEROMETER_SLEEP, // The accelerometer has returned to sleep.
EVENT_ORIENTATION_CHANGE, // The orientation of the watch has changed. Available in low energy mode.
EVENT_SINGLE_TAP, // Accelerometer detected a single tap. This event is not yet implemented.
EVENT_DOUBLE_TAP, // Accelerometer detected a double tap. This event is not yet implemented.
EVENT_FREE_FALL, // Accelerometer detected the watch in free fall. This event is not yet implemented.
} movement_event_type_t;

typedef struct {
Expand Down
2 changes: 1 addition & 1 deletion movement_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const watch_face_t watch_faces[] = {
* Some folks also like to use this to hide the preferences and time set faces from the normal rotation.
* If you don't want any faces to be excluded, set this to 0 and a long Mode press will have no effect.
*/
#define MOVEMENT_SECONDARY_FACE_INDEX (MOVEMENT_NUM_FACES - 4)
#define MOVEMENT_SECONDARY_FACE_INDEX (MOVEMENT_NUM_FACES - 3)

/* Custom hourly chime tune. Check movement_custom_signal_tunes.h for options. */
#define SIGNAL_TUNE_DEFAULT
Expand Down
4 changes: 4 additions & 0 deletions movement_faces.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@
#include "float_demo_face.h"
#include "temperature_display_face.h"
#include "temperature_logging_face.h"
#include "activity_logging_face.h"
#include "voltage_face.h"
#include "set_time_face.h"
#include "preferences_face.h"
#include "light_sensor_face.h"
#include "accelerometer_sleep_state_face.h"
#include "irda_demo_face.h"
#include "chirpy_demo_face.h"
// New includes go above this line.
4 changes: 4 additions & 0 deletions watch-faces.mk
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ SRCS += \
./watch-faces/demo/float_demo_face.c \
./watch-faces/sensor/temperature_display_face.c \
./watch-faces/sensor/temperature_logging_face.c \
./watch-faces/sensor/activity_logging_face.c \
./watch-faces/sensor/voltage_face.c \
./watch-faces/settings/set_time_face.c \
./watch-faces/settings/preferences_face.c \
./watch-faces/demo/light_sensor_face.c \
./watch-faces/demo/accelerometer_sleep_state_face.c \
./watch-faces/demo/irda_demo_face.c \
./watch-faces/io/chirpy_demo_face.c \
# New watch faces go above this line.
39 changes: 25 additions & 14 deletions watch-faces/demo/accel_interrupt_count_face.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,25 @@

#ifdef HAS_ACCELEROMETER

// hacky: we're just tapping into Movement's global state.
// we should make better API for this.
extern uint32_t orientation_changes;
extern uint8_t active_minutes;

static void _accel_interrupt_count_face_update_display(accel_interrupt_count_state_t *state) {
(void) state;
char buf[7];
char buf[8];

// "AC"celerometer "IN"terrupts
// Accelerometer title
watch_display_text(WATCH_POSITION_TOP_LEFT, "AC");
watch_display_text(WATCH_POSITION_TOP_RIGHT, "1N");
uint16_t count = tc_count16_get_count(2);
snprintf(buf, 7, "%6d", count);

// Sleep/active state
if (HAL_GPIO_A3_read()) watch_display_text(WATCH_POSITION_TOP_RIGHT, " S");
else watch_display_text(WATCH_POSITION_TOP_RIGHT, " A");

// Orientation changes / active minutes
sprintf(buf, "%-3lu/%2d", orientation_changes > 999 ? 999 : orientation_changes, active_minutes);
watch_display_text(WATCH_POSITION_BOTTOM, buf);
printf("%s\n", buf);
}

void accel_interrupt_count_face_setup(uint8_t watch_face_index, void ** context_ptr) {
Expand All @@ -60,6 +68,9 @@ void accel_interrupt_count_face_activate(void *context) {

// never in settings mode at the start
state->is_setting = false;

// update more quickly to catch changes, also to blink setting
movement_request_tick_frequency(4);
}

bool accel_interrupt_count_face_loop(movement_event_t event, void *context) {
Expand All @@ -73,10 +84,14 @@ bool accel_interrupt_count_face_loop(movement_event_t event, void *context) {
case EVENT_TICK:
{
char buf[11];
watch_display_text(WATCH_POSITION_TOP_RIGHT, " ");
watch_display_text_with_fallback(WATCH_POSITION_TOP, "W_THS", "TH");
watch_display_float_with_best_effort(state->new_threshold * 0.0625, " G");
printf("%s\n", buf);
if (event.subsecond % 2) {
watch_display_text(WATCH_POSITION_BOTTOM, " ");
} else {
watch_display_text(WATCH_POSITION_TOP_RIGHT, " ");
watch_display_text_with_fallback(WATCH_POSITION_TOP, "W_THS", "TH");
watch_display_float_with_best_effort(state->new_threshold * 0.03125, " G");
printf("%s\n", buf);
}
}
break;
case EVENT_ALARM_BUTTON_UP:
Expand All @@ -90,10 +105,6 @@ bool accel_interrupt_count_face_loop(movement_event_t event, void *context) {
}
} else {
switch (event.event_type) {
case EVENT_ALARM_BUTTON_UP:
// reset the counter
tc_count16_set_count(2, 0);
// fall through
case EVENT_ACTIVATE:
case EVENT_TICK:
_accel_interrupt_count_face_update_display(state);
Expand Down
102 changes: 102 additions & 0 deletions watch-faces/demo/accelerometer_sleep_state_face.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* MIT License
*
* Copyright (c) 2024 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 <stdlib.h>
#include <string.h>
#include "accelerometer_sleep_state_face.h"

void accelerometer_sleep_state_face_setup(uint8_t watch_face_index, void ** context_ptr) {
(void) watch_face_index;
(void) context_ptr;
}

void accelerometer_sleep_state_face_activate(void *context) {
(void) context;
movement_request_tick_frequency(8);
}

bool accelerometer_sleep_state_face_loop(movement_event_t event, void *context) {
(void) context;
char buf[7];
char state[4];

switch (event.event_type) {
case EVENT_ACTIVATE:
watch_display_text(WATCH_POSITION_TOP_LEFT, "A3");
watch_set_colon();
// fall through
case EVENT_TICK:
if (HAL_GPIO_A3_read()) {
strcpy(state, "SLP");
printf("1\n");
} else {
strcpy(state, "Act");
printf("0\n");
}
sprintf(buf, "ac %s", state);
// printf("%s\n", buf);
watch_display_text(WATCH_POSITION_BOTTOM, buf);
// If needed, update your display here.
break;
case EVENT_LIGHT_BUTTON_UP:
// You can use the Light button for your own purposes. Note that by default, Movement will also
// illuminate the LED in response to EVENT_LIGHT_BUTTON_DOWN; to suppress that behavior, add an
// empty case for EVENT_LIGHT_BUTTON_DOWN.
break;
case EVENT_ALARM_BUTTON_UP:
// Just in case you have need for another button.
break;
case EVENT_TIMEOUT:
// Your watch face will receive this event after a period of inactivity. If it makes sense to resign,
// you may uncomment this line to move back to the first watch face in the list:
// movement_move_to_face(0);
break;
case EVENT_LOW_ENERGY_UPDATE:
// If you did not resign in EVENT_TIMEOUT, you can use this event to update the display once a minute.
// Avoid displaying fast-updating values like seconds, since the display won't update again for 60 seconds.
// You should also consider starting the tick animation, to show the wearer that this is sleep mode:
// watch_start_sleep_animation(500);
break;
default:
// Movement's default loop handler will step in for any cases you don't handle above:
// * EVENT_LIGHT_BUTTON_DOWN lights the LED
// * EVENT_MODE_BUTTON_UP moves to the next watch face in the list
// * EVENT_MODE_LONG_PRESS returns to the first watch face (or skips to the secondary watch face, if configured)
// You can override any of these behaviors by adding a case for these events to this switch statement.
return movement_default_loop_handler(event);
}

// return true if the watch can enter standby mode. Generally speaking, you should always return true.
// Exceptions:
// * If you are displaying a color using the low-level watch_set_led_color function, you should return false.
// * If you are sounding the buzzer using the low-level watch_set_buzzer_on function, you should return false.
// Note that if you are driving the LED or buzzer using Movement functions like movement_illuminate_led or
// movement_play_alarm, you can still return true. This guidance only applies to the low-level watch_ functions.
return true;
}

void accelerometer_sleep_state_face_resign(void *context) {
(void) context;
}

Loading

0 comments on commit 64a6998

Please sign in to comment.