Merge branch 'main' of github.com:joeycastillo/Sensor-Watch into main
This commit is contained in:
commit
7817e6696e
@ -33,7 +33,10 @@ SRCS += \
|
||||
../watch_faces/thermistor/thermistor_readout_face.c \
|
||||
../watch_faces/thermistor/thermistor_logging_face.c \
|
||||
../watch_faces/demos/character_set_face.c \
|
||||
../watch_faces/demos/voltage_face.c \
|
||||
../watch_faces/complications/beats_face.c \
|
||||
../watch_faces/complications/day_one_face.c \
|
||||
../watch_faces/complications/stopwatch_face.c \
|
||||
|
||||
# Leave this line at the bottom of the file; it has all the targets for making your project.
|
||||
include $(TOP)/rules.mk
|
||||
|
@ -105,7 +105,7 @@ void app_setup() {
|
||||
watch_faces[i].setup(&movement_state.settings, &watch_face_contexts[i]);
|
||||
}
|
||||
|
||||
watch_faces[0].activate(&movement_state.settings, watch_face_contexts[0]);
|
||||
watch_faces[movement_state.current_watch_face].activate(&movement_state.settings, watch_face_contexts[movement_state.current_watch_face]);
|
||||
event.subsecond = 0;
|
||||
event.event_type = EVENT_ACTIVATE;
|
||||
}
|
||||
@ -143,11 +143,6 @@ bool app_loop() {
|
||||
}
|
||||
}
|
||||
|
||||
// if we have timed out of our timeout countdown, give the app a hint that they can resign.
|
||||
if (movement_state.timeout_ticks == 0) {
|
||||
event.event_type = EVENT_TIMEOUT;
|
||||
}
|
||||
|
||||
// handle background tasks, if the alarm handler told us we need to
|
||||
if (movement_state.needs_background_tasks_handled) _movement_handle_background_tasks();
|
||||
|
||||
@ -181,9 +176,18 @@ bool app_loop() {
|
||||
event.subsecond = movement_state.subsecond;
|
||||
can_sleep = watch_faces[movement_state.current_watch_face].loop(event, &movement_state.settings, watch_face_contexts[movement_state.current_watch_face]);
|
||||
event.event_type = EVENT_NONE;
|
||||
event.subsecond = 0;
|
||||
}
|
||||
|
||||
// if we have timed out of our timeout countdown, give the app a hint that they can resign.
|
||||
if (movement_state.timeout_ticks == 0) {
|
||||
event.event_type = EVENT_TIMEOUT;
|
||||
event.subsecond = movement_state.subsecond;
|
||||
watch_faces[movement_state.current_watch_face].loop(event, &movement_state.settings, watch_face_contexts[movement_state.current_watch_face]);
|
||||
event.event_type = EVENT_NONE;
|
||||
}
|
||||
|
||||
event.subsecond = 0;
|
||||
|
||||
return can_sleep && !movement_state.led_on;
|
||||
}
|
||||
|
||||
|
@ -3,27 +3,77 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// TODO: none of this is implemented
|
||||
// Movement Preferences
|
||||
// These four 32-bit structs store information about the wearer and their preferences. Tentatively, the plan is
|
||||
// for Movement to use four 32-bit registers for these preferences and to store them in the RTC's backup registers
|
||||
// 0-3, leaving registers 4-7 available for third party watch faces to use as they see fit.
|
||||
// * The movement_settings_t struct is provided to all watch faces in the callback functions, and will eventually
|
||||
// be stored in the RTC's first backup register (BKUP[0]).
|
||||
// * The movement_location_t and movement_birthdate_t types are defined here, and are tentatively meant to be
|
||||
// stored in BKUP[1] and BKUP[2], respectively.
|
||||
// * The movement_reserved_t type is here as a placeholder, because I sense there's some other generally useful
|
||||
// stuff we'll want to make available to all watch faces and stash in the BKUP[3] register.
|
||||
// Anyway: Eventually, if Movement supports the BACKUP sleep mode, this will allow these preferences to be stored
|
||||
// before entering BACKUP mode and and restored after waking from reset.
|
||||
|
||||
// movement_settings_t contains global settings that cover watch behavior, including preferences around clock and unit
|
||||
// display, time zones, buzzer behavior, LED color and low energy mode timeouts.
|
||||
// Eventually it will be stored in BKUP[0] when Movement enters BACKUP mode.
|
||||
typedef union {
|
||||
struct {
|
||||
uint32_t reserved : 14;
|
||||
uint32_t button_should_sound : 1; // if true, pressing a button emits a sound.
|
||||
uint32_t to_interval : 2; // an inactivity interval for asking the active face to resign.
|
||||
uint32_t le_interval : 3; // 0 to disable low energy mode, or an inactivity interval for going into low energy mode.
|
||||
uint32_t led_duration : 2; // how many seconds to shine the LED for (x2), or 0 to disable it.
|
||||
uint32_t led_red_color : 4; // for general purpose illumination, the red LED value (0-15)
|
||||
uint32_t led_green_color : 4; // for general purpose illumination, the green LED value (0-15)
|
||||
bool button_should_sound : 1; // if true, pressing a button emits a sound.
|
||||
uint8_t to_interval : 2; // an inactivity interval for asking the active face to resign.
|
||||
uint8_t le_interval : 3; // 0 to disable low energy mode, or an inactivity interval for going into low energy mode.
|
||||
uint8_t led_duration : 2; // how many seconds to shine the LED for (x2), or 0 to disable it.
|
||||
uint8_t led_red_color : 4; // for general purpose illumination, the red LED value (0-15)
|
||||
uint8_t led_green_color : 4; // for general purpose illumination, the green LED value (0-15)
|
||||
uint8_t time_zone : 6; // TODO: an integer representing an index in the (to be implemented) time zone table.
|
||||
|
||||
// while Movement itself doesn't implement a clock or display units, it may make sense to include some
|
||||
// global settings for watch faces to check. The 12/24 hour preference could inform a clock or a
|
||||
// time-oriented complication like a sunrise/sunset timer, and a simple locale preference could tell an
|
||||
// altimeter to display feet or meters as easily as it tells a thermometer to display degrees in F or C.
|
||||
uint32_t clock_mode_24h : 1; // indicates whether clock should use 12 or 24 hour mode.
|
||||
uint32_t use_imperial_units : 1; // indicates whether to use metric units (the default) or imperial.
|
||||
bool clock_mode_24h : 1; // indicates whether clock should use 12 or 24 hour mode.
|
||||
bool use_imperial_units : 1; // indicates whether to use metric units (the default) or imperial.
|
||||
uint8_t reserved : 8; // room for more preferences if needed.
|
||||
} bit;
|
||||
uint32_t value;
|
||||
uint32_t reg;
|
||||
} movement_settings_t;
|
||||
|
||||
// movement_location_t is for storing the wearer's location. This will be useful for astronomical calculations such as
|
||||
// sunrise and sunset, or predictions of visible satellite passes.
|
||||
// If you create a UI for this register or need to access it, look for it in the RTC's BKUP[1] register.
|
||||
typedef union {
|
||||
struct {
|
||||
int16_t latitude : 16; // signed latutide in hundredths of a degree
|
||||
int16_t longitude : 16; // signed longitude in hundredths of a degree
|
||||
} bit;
|
||||
uint32_t reg;
|
||||
} movement_location_t;
|
||||
|
||||
// movement_birthdate_t is for storing the user's birth date. This will be useful for calculating the user's age — or
|
||||
// hey, playing happy birthday at midnight? Fields for birth time (with hour and minute resolution) are also available,
|
||||
// partly because they fit so nicely, but also because they can be useful for certain astrological calculations.
|
||||
// If you create a UI for birth date or need to access it, look for it in the RTC's BKUP[2] register.
|
||||
typedef union {
|
||||
struct {
|
||||
uint16_t year : 12; // good through the year 4095
|
||||
uint8_t month : 4;
|
||||
uint8_t day : 5;
|
||||
uint8_t hour : 5;
|
||||
uint8_t minute : 6;
|
||||
} bit;
|
||||
uint32_t reg;
|
||||
} movement_birthdate_t;
|
||||
|
||||
// movement_reserved_t is a placeholder for future use of the BKUP[3] register.
|
||||
typedef union {
|
||||
struct {
|
||||
uint32_t reserved : 32;
|
||||
} bit;
|
||||
uint32_t reg;
|
||||
} movement_reserved_t;
|
||||
|
||||
typedef enum {
|
||||
EVENT_NONE = 0, // There is no event to report.
|
||||
EVENT_ACTIVATE, // Your watch face is entering the foreground.
|
||||
|
@ -9,6 +9,9 @@
|
||||
#include "thermistor_logging_face.h"
|
||||
#include "character_set_face.h"
|
||||
#include "beats_face.h"
|
||||
#include "day_one_face.h"
|
||||
#include "voltage_face.h"
|
||||
#include "stopwatch_face.h"
|
||||
|
||||
const watch_face_t watch_faces[] = {
|
||||
simple_clock_face,
|
||||
|
@ -28,7 +28,6 @@ bool simple_clock_face_loop(movement_event_t event, movement_settings_t *setting
|
||||
switch (event.event_type) {
|
||||
case EVENT_ACTIVATE:
|
||||
case EVENT_TICK:
|
||||
case EVENT_TIMEOUT:
|
||||
case EVENT_LOW_ENERGY_UPDATE:
|
||||
date_time = watch_rtc_get_date_time();
|
||||
previous_date_time = *((uint32_t *)context);
|
||||
|
@ -26,6 +26,7 @@ bool beats_face_loop(movement_event_t event, movement_settings_t *settings, void
|
||||
|
||||
watch_date_time date_time;
|
||||
switch (event.event_type) {
|
||||
case EVENT_ACTIVATE:
|
||||
case EVENT_TICK:
|
||||
date_time = watch_rtc_get_date_time();
|
||||
beats = clock2beats(date_time.unit.hour, date_time.unit.minute, date_time.unit.second, event.subsecond, UTC_OFFSET);
|
||||
@ -34,6 +35,7 @@ bool beats_face_loop(movement_event_t event, movement_settings_t *settings, void
|
||||
watch_display_string(buf, 0);
|
||||
break;
|
||||
case EVENT_LOW_ENERGY_UPDATE:
|
||||
if (!watch_tick_animation_is_running()) watch_start_tick_animation(432);
|
||||
date_time = watch_rtc_get_date_time();
|
||||
beats = clock2beats(date_time.unit.hour, date_time.unit.minute, date_time.unit.second, event.subsecond, UTC_OFFSET);
|
||||
sprintf(buf, "bt %4d ", (int)beats);
|
||||
|
177
movement/watch_faces/complications/day_one_face.c
Normal file
177
movement/watch_faces/complications/day_one_face.c
Normal file
@ -0,0 +1,177 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "day_one_face.h"
|
||||
#include "watch.h"
|
||||
|
||||
uint32_t _day_one_face_juliandaynum(uint16_t year, uint16_t month, uint16_t day) {
|
||||
// from here: https://en.wikipedia.org/wiki/Julian_day#Julian_day_number_calculation
|
||||
return (1461 * (year + 4800 + (month - 14) / 12)) / 4 + (367 * (month - 2 - 12 * ((month - 14) / 12))) / 12 - (3 * ((year + 4900 + (month - 14) / 12) / 100))/4 + day - 32075;
|
||||
}
|
||||
|
||||
void _day_one_face_update(day_one_state_t state) {
|
||||
char buf[14];
|
||||
watch_date_time date_time = watch_rtc_get_date_time();
|
||||
uint32_t julian_date = _day_one_face_juliandaynum(date_time.unit.year + WATCH_RTC_REFERENCE_YEAR, date_time.unit.month, date_time.unit.day);
|
||||
uint32_t julian_birthdate = _day_one_face_juliandaynum(state.birth_year, state.birth_month, state.birth_day);
|
||||
sprintf(buf, "DA %6ld", julian_date - julian_birthdate);
|
||||
watch_display_string(buf, 0);
|
||||
}
|
||||
|
||||
void day_one_face_setup(movement_settings_t *settings, void ** context_ptr) {
|
||||
(void) settings;
|
||||
if (*context_ptr == NULL) {
|
||||
*context_ptr = malloc(sizeof(day_one_state_t));
|
||||
memset(*context_ptr, 0, sizeof(day_one_state_t));
|
||||
movement_birthdate_t movement_birthdate = (movement_birthdate_t) watch_get_backup_data(2);
|
||||
if (movement_birthdate.reg == 0) {
|
||||
// if birth date is totally blank, set a reasonable starting date. this works well for anyone under 63, but
|
||||
// you can keep pressing to go back to 1900; just pass the current year. also picked this date because if you
|
||||
// set it to 1959-01-02, it counts up from the launch of Luna-1, the first spacecraft to leave the well.
|
||||
movement_birthdate.bit.year = 1959;
|
||||
movement_birthdate.bit.month = 1;
|
||||
movement_birthdate.bit.day = 1;
|
||||
watch_store_backup_data(movement_birthdate.reg, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void day_one_face_activate(movement_settings_t *settings, void *context) {
|
||||
(void) settings;
|
||||
day_one_state_t *state = (day_one_state_t *)context;
|
||||
|
||||
// stash the current year, useful in birthday setting mode.
|
||||
watch_date_time date_time = watch_rtc_get_date_time();
|
||||
state->current_year = date_time.unit.year + WATCH_RTC_REFERENCE_YEAR;
|
||||
// reset the current page to 0, display days alive.
|
||||
state->current_page = 0;
|
||||
|
||||
// fetch the user's birth date from the birthday register.
|
||||
movement_birthdate_t movement_birthdate = (movement_birthdate_t) watch_get_backup_data(2);
|
||||
state->birth_year = movement_birthdate.bit.year;
|
||||
state->birth_month = movement_birthdate.bit.month;
|
||||
state->birth_day = movement_birthdate.bit.day;
|
||||
}
|
||||
|
||||
bool day_one_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
|
||||
(void) settings;
|
||||
day_one_state_t *state = (day_one_state_t *)context;
|
||||
|
||||
const uint8_t days_in_month[12] = {31, 29, 31, 30, 31, 30, 30, 31, 30, 31, 30, 31};
|
||||
char buf[6];
|
||||
|
||||
switch (event.event_type) {
|
||||
case EVENT_ACTIVATE:
|
||||
_day_one_face_update(*state);
|
||||
break;
|
||||
case EVENT_LOW_ENERGY_UPDATE:
|
||||
case EVENT_TICK:
|
||||
if (state->current_page != 0) {
|
||||
// if in settings mode, update whatever the current page is
|
||||
switch (state->current_page) {
|
||||
case 1:
|
||||
watch_display_string("YR ", 0);
|
||||
if (event.subsecond % 2) {
|
||||
sprintf(buf, "%4d", state->birth_year);
|
||||
watch_display_string(buf, 4);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
watch_display_string("MO ", 0);
|
||||
if (event.subsecond % 2) {
|
||||
sprintf(buf, "%2d", state->birth_month);
|
||||
watch_display_string(buf, 4);
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
watch_display_string("DA ", 0);
|
||||
if (event.subsecond % 2) {
|
||||
sprintf(buf, "%2d", state->birth_day);
|
||||
watch_display_string(buf, 6);
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// otherwise, check if we have to update. the display only needs to change at midnight!
|
||||
watch_date_time date_time = watch_rtc_get_date_time();
|
||||
if (date_time.unit.hour == 0 && date_time.unit.minute == 0 && date_time.unit.second == 0) {
|
||||
_day_one_face_update(*state);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case EVENT_MODE_BUTTON_UP:
|
||||
movement_move_to_next_face();
|
||||
break;
|
||||
case EVENT_LIGHT_BUTTON_DOWN:
|
||||
// only illuminate if we're in display mode
|
||||
if (state->current_page == 0) movement_illuminate_led();
|
||||
break;
|
||||
case EVENT_LIGHT_BUTTON_UP:
|
||||
// otherwise use the light button to advance settings pages.
|
||||
if (state->current_page != 0) {
|
||||
// go to next setting page...
|
||||
state->current_page = (state->current_page + 1) % 4;
|
||||
if (state->current_page == 0) {
|
||||
// ...unless we've been pushed back to display mode.
|
||||
movement_request_tick_frequency(1);
|
||||
// force display since it normally won't update til midnight.
|
||||
_day_one_face_update(*state);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case EVENT_ALARM_BUTTON_UP:
|
||||
// if we are on a settings page, increment whatever value we're setting.
|
||||
if (state->current_page != 0) {
|
||||
state->birthday_changed = true;
|
||||
switch (state->current_page) {
|
||||
case 1:
|
||||
state->birth_year = state->birth_year + 1;
|
||||
if (state->birth_year > state->current_year) state->birth_year = 1900;
|
||||
break;
|
||||
case 2:
|
||||
state->birth_month = (state->birth_month % 12) + 1;
|
||||
break;
|
||||
case 3:
|
||||
state->birth_day = state->birth_day + 1;
|
||||
if (state->birth_day == 0 || state->birth_day > days_in_month[state->birth_month - 1]) {
|
||||
state->birth_day = 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case EVENT_ALARM_LONG_PRESS:
|
||||
// if we aren't already in settings mode, put us there.
|
||||
if (state->current_page == 0) {
|
||||
state->current_page++;
|
||||
movement_request_tick_frequency(4);
|
||||
}
|
||||
break;
|
||||
case EVENT_TIMEOUT:
|
||||
// return home if we're on a settings page (this saves our changes when we resign).
|
||||
if (state->current_page != 0) {
|
||||
movement_move_to_face(0);
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void day_one_face_resign(movement_settings_t *settings, void *context) {
|
||||
(void) settings;
|
||||
day_one_state_t *state = (day_one_state_t *)context;
|
||||
|
||||
movement_request_tick_frequency(1);
|
||||
|
||||
// if the user changed their birth date, store it to the birth date register
|
||||
if (state->birthday_changed) {
|
||||
day_one_state_t *state = (day_one_state_t *)context;
|
||||
movement_birthdate_t movement_birthdate = (movement_birthdate_t) watch_get_backup_data(2);
|
||||
movement_birthdate.bit.year = state->birth_year;
|
||||
movement_birthdate.bit.month = state->birth_month;
|
||||
movement_birthdate.bit.day = state->birth_day;
|
||||
watch_store_backup_data(movement_birthdate.reg, 2);
|
||||
state->birthday_changed = false;
|
||||
}
|
||||
}
|
31
movement/watch_faces/complications/day_one_face.h
Normal file
31
movement/watch_faces/complications/day_one_face.h
Normal file
@ -0,0 +1,31 @@
|
||||
#ifndef DAY_ONE_FACE_H_
|
||||
#define DAY_ONE_FACE_H_
|
||||
|
||||
#include "movement.h"
|
||||
|
||||
// The Day One face is designed to count upwards from the wearer's date of birth. It also functions as an
|
||||
// interface for setting the birth date register, which other watch faces can use for various purposes.
|
||||
|
||||
typedef struct {
|
||||
uint8_t current_page;
|
||||
uint16_t current_year;
|
||||
uint16_t birth_year;
|
||||
uint8_t birth_month;
|
||||
uint8_t birth_day;
|
||||
bool birthday_changed;
|
||||
} day_one_state_t;
|
||||
|
||||
void day_one_face_setup(movement_settings_t *settings, void ** context_ptr);
|
||||
void day_one_face_activate(movement_settings_t *settings, void *context);
|
||||
bool day_one_face_loop(movement_event_t event, movement_settings_t *settings, void *context);
|
||||
void day_one_face_resign(movement_settings_t *settings, void *context);
|
||||
|
||||
static const watch_face_t day_one_face = {
|
||||
day_one_face_setup,
|
||||
day_one_face_activate,
|
||||
day_one_face_loop,
|
||||
day_one_face_resign,
|
||||
NULL
|
||||
};
|
||||
|
||||
#endif // DAY_ONE_FACE_H_
|
76
movement/watch_faces/complications/stopwatch_face.c
Normal file
76
movement/watch_faces/complications/stopwatch_face.c
Normal file
@ -0,0 +1,76 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "stopwatch_face.h"
|
||||
#include "watch.h"
|
||||
|
||||
void stopwatch_face_setup(movement_settings_t *settings, void ** context_ptr) {
|
||||
(void) settings;
|
||||
if (*context_ptr == NULL) *context_ptr = malloc(sizeof(stopwatch_state_t));
|
||||
}
|
||||
|
||||
void stopwatch_face_activate(movement_settings_t *settings, void *context) {
|
||||
(void) settings;
|
||||
memset(context, 0, sizeof(stopwatch_state_t));
|
||||
}
|
||||
|
||||
bool stopwatch_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
|
||||
(void) settings;
|
||||
|
||||
stopwatch_state_t *stopwatch_state = (stopwatch_state_t *)context;
|
||||
char buf[14];
|
||||
|
||||
switch (event.event_type) {
|
||||
case EVENT_ACTIVATE:
|
||||
watch_set_colon();
|
||||
stopwatch_state->running = false;
|
||||
watch_display_string("st 00000", 0);
|
||||
break;
|
||||
case EVENT_TICK:
|
||||
if (stopwatch_state->running) {
|
||||
stopwatch_state->seconds++;
|
||||
if (stopwatch_state->seconds == 60) {
|
||||
stopwatch_state->minutes++;
|
||||
stopwatch_state->seconds = 0;
|
||||
}
|
||||
if (stopwatch_state->minutes == 60) {
|
||||
stopwatch_state->hours++;
|
||||
stopwatch_state->minutes = 0;
|
||||
}
|
||||
}
|
||||
|
||||
sprintf(buf, "st%2d%02d%02d", stopwatch_state->hours, stopwatch_state->minutes, stopwatch_state->seconds);
|
||||
watch_display_string(buf, 0);
|
||||
break;
|
||||
case EVENT_MODE_BUTTON_UP:
|
||||
movement_move_to_next_face();
|
||||
break;
|
||||
case EVENT_LIGHT_BUTTON_DOWN:
|
||||
movement_illuminate_led();
|
||||
if (!stopwatch_state->running) {
|
||||
stopwatch_state->seconds = 0;
|
||||
stopwatch_state->minutes = 0;
|
||||
stopwatch_state->hours = 0;
|
||||
watch_display_string("st 00000", 0);
|
||||
}
|
||||
break;
|
||||
case EVENT_ALARM_BUTTON_DOWN:
|
||||
stopwatch_state->running = !stopwatch_state->running;
|
||||
break;
|
||||
case EVENT_TIMEOUT:
|
||||
// explicitly ignore the timeout event so we stay on screen
|
||||
break;
|
||||
case EVENT_LOW_ENERGY_UPDATE:
|
||||
stopwatch_state->running = false;
|
||||
watch_set_indicator(WATCH_INDICATOR_BELL);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void stopwatch_face_resign(movement_settings_t *settings, void *context) {
|
||||
(void) settings;
|
||||
(void) context;
|
||||
}
|
26
movement/watch_faces/complications/stopwatch_face.h
Normal file
26
movement/watch_faces/complications/stopwatch_face.h
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef STOPWATCH_FACE_H_
|
||||
#define STOPWATCH_FACE_H_
|
||||
|
||||
#include "movement.h"
|
||||
|
||||
typedef struct {
|
||||
bool running;
|
||||
uint8_t seconds;
|
||||
uint8_t minutes;
|
||||
uint8_t hours;
|
||||
} stopwatch_state_t;
|
||||
|
||||
void stopwatch_face_setup(movement_settings_t *settings, void ** context_ptr);
|
||||
void stopwatch_face_activate(movement_settings_t *settings, void *context);
|
||||
bool stopwatch_face_loop(movement_event_t event, movement_settings_t *settings, void *context);
|
||||
void stopwatch_face_resign(movement_settings_t *settings, void *context);
|
||||
|
||||
static const watch_face_t stopwatch_face = {
|
||||
stopwatch_face_setup,
|
||||
stopwatch_face_activate,
|
||||
stopwatch_face_loop,
|
||||
stopwatch_face_resign,
|
||||
NULL
|
||||
};
|
||||
|
||||
#endif // STOPWATCH_FACE_H_
|
66
movement/watch_faces/demos/voltage_face.c
Normal file
66
movement/watch_faces/demos/voltage_face.c
Normal file
@ -0,0 +1,66 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "voltage_face.h"
|
||||
#include "watch.h"
|
||||
|
||||
void _voltage_face_update_display() {
|
||||
char buf[14];
|
||||
float voltage = (float)watch_get_vcc_voltage() / 1000.0;
|
||||
sprintf(buf, "BA %4.2f V", voltage);
|
||||
// printf("%s\n", buf);
|
||||
watch_display_string(buf, 0);
|
||||
}
|
||||
|
||||
void voltage_face_setup(movement_settings_t *settings, void ** context_ptr) {
|
||||
(void) settings;
|
||||
(void) context_ptr;
|
||||
}
|
||||
|
||||
void voltage_face_activate(movement_settings_t *settings, void *context) {
|
||||
(void) settings;
|
||||
(void) context;
|
||||
watch_enable_adc();
|
||||
// if we set the reference voltage here, watch_get_vcc_voltage won't do it over and over
|
||||
watch_set_analog_reference_voltage(ADC_REFERENCE_INTREF);
|
||||
}
|
||||
|
||||
bool voltage_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
|
||||
(void) settings;
|
||||
(void) context;
|
||||
watch_date_time date_time;
|
||||
switch (event.event_type) {
|
||||
case EVENT_MODE_BUTTON_UP:
|
||||
movement_move_to_next_face();
|
||||
break;
|
||||
case EVENT_LIGHT_BUTTON_DOWN:
|
||||
movement_illuminate_led();
|
||||
break;
|
||||
case EVENT_ACTIVATE:
|
||||
_voltage_face_update_display();
|
||||
break;
|
||||
case EVENT_TICK:
|
||||
date_time = watch_rtc_get_date_time();
|
||||
if (date_time.unit.second % 5 == 4) {
|
||||
watch_set_indicator(WATCH_INDICATOR_SIGNAL);
|
||||
} else if (date_time.unit.second % 5 == 0) {
|
||||
_voltage_face_update_display();
|
||||
watch_clear_indicator(WATCH_INDICATOR_SIGNAL);
|
||||
}
|
||||
break;
|
||||
case EVENT_TIMEOUT:
|
||||
movement_move_to_face(0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void voltage_face_resign(movement_settings_t *settings, void *context) {
|
||||
(void) settings;
|
||||
(void) context;
|
||||
// make sure to restore the default in the end.
|
||||
watch_set_analog_reference_voltage(ADC_REFCTRL_REFSEL_INTVCC2_Val);
|
||||
watch_disable_adc();
|
||||
}
|
19
movement/watch_faces/demos/voltage_face.h
Normal file
19
movement/watch_faces/demos/voltage_face.h
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef VOLTAGE_FACE_H_
|
||||
#define VOLTAGE_FACE_H_
|
||||
|
||||
#include "movement.h"
|
||||
|
||||
void voltage_face_setup(movement_settings_t *settings, void ** context_ptr);
|
||||
void voltage_face_activate(movement_settings_t *settings, void *context);
|
||||
bool voltage_face_loop(movement_event_t event, movement_settings_t *settings, void *context);
|
||||
void voltage_face_resign(movement_settings_t *settings, void *context);
|
||||
|
||||
static const watch_face_t voltage_face = {
|
||||
voltage_face_setup,
|
||||
voltage_face_activate,
|
||||
voltage_face_loop,
|
||||
voltage_face_resign,
|
||||
NULL
|
||||
};
|
||||
|
||||
#endif // VOLTAGE_FACE_H_
|
File diff suppressed because it is too large
Load Diff
@ -138,6 +138,35 @@ void watch_set_analog_sampling_length(uint8_t cycles) {
|
||||
_watch_sync_adc();
|
||||
}
|
||||
|
||||
void watch_set_analog_reference_voltage(watch_adc_reference_voltage reference) {
|
||||
ADC->CTRLA.bit.ENABLE = 0;
|
||||
|
||||
if (reference == ADC_REFERENCE_INTREF) SUPC->VREF.bit.VREFOE = 1;
|
||||
else SUPC->VREF.bit.VREFOE = 0;
|
||||
|
||||
ADC->REFCTRL.bit.REFSEL = reference;
|
||||
ADC->CTRLA.bit.ENABLE = 1;
|
||||
_watch_sync_adc();
|
||||
// throw away one measurement after reference change (the channel doesn't matter).
|
||||
_watch_get_analog_value(ADC_INPUTCTRL_MUXPOS_SCALEDCOREVCC);
|
||||
}
|
||||
|
||||
uint16_t watch_get_vcc_voltage() {
|
||||
// stash the previous reference so we can restore it when we're done.
|
||||
uint8_t oldref = ADC->REFCTRL.bit.REFSEL;
|
||||
|
||||
// if we weren't already using the internal reference voltage, select it now.
|
||||
if (oldref != ADC_REFERENCE_INTREF) watch_set_analog_reference_voltage(ADC_REFERENCE_INTREF);
|
||||
|
||||
// get the data
|
||||
uint32_t raw_val = _watch_get_analog_value(ADC_INPUTCTRL_MUXPOS_SCALEDIOVCC_Val);
|
||||
|
||||
// restore the old reference, if needed.
|
||||
if (oldref != ADC_REFERENCE_INTREF) watch_set_analog_reference_voltage(oldref);
|
||||
|
||||
return (uint16_t)((raw_val * 1000) / (1024 * 1 << ADC->AVGCTRL.bit.SAMPLENUM));
|
||||
}
|
||||
|
||||
inline void watch_disable_analog_input(const uint8_t pin) {
|
||||
gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF);
|
||||
}
|
||||
|
@ -94,6 +94,53 @@ void watch_set_analog_num_samples(uint16_t samples);
|
||||
**/
|
||||
void watch_set_analog_sampling_length(uint8_t cycles);
|
||||
|
||||
typedef enum {
|
||||
ADC_REFERENCE_INTREF = ADC_REFCTRL_REFSEL_INTREF_Val,
|
||||
ADC_REFERENCE_VCC_DIV1POINT6 = ADC_REFCTRL_REFSEL_INTVCC0_Val,
|
||||
ADC_REFERENCE_VCC_DIV2 = ADC_REFCTRL_REFSEL_INTVCC1_Val,
|
||||
ADC_REFERENCE_VCC = ADC_REFCTRL_REFSEL_INTVCC2_Val,
|
||||
} watch_adc_reference_voltage;
|
||||
|
||||
|
||||
/** @brief Selects the reference voltage to use for analog readings. Default is ADC_REFERENCE_VCC.
|
||||
* @param reference One of ADC_REFERENCE_VCC, ADC_REFERENCE_VCC_DIV1POINT6, ADC_REFERENCE_VCC_DIV2
|
||||
* or ADC_REFERENCE_INTREF.
|
||||
* @details In order to turn an analog voltage into a 16-bit integer, the ADC needs to compare the
|
||||
* measured voltage to a reference point. For example, if you were powering the watch with
|
||||
* VCC == 3.0V and you had two 10K resistors connected in series from 3V to GND, you could
|
||||
* expect to get 3 volts when you measure the top of the voltage divider, 0 volts at the
|
||||
* bottom, and 1.5 volts in the middle. If you read these values uising a reference voltage
|
||||
* of ADC_REFERENCE_VCC, the top value would be about 65535, the bottom about 0, and the
|
||||
* middle about 32768. However! If we used ADC_REFERENCE_VCC_DIV2 as our reference, we would
|
||||
* expect to get 65535 both at the top and the middle, because the largest value the ADC can
|
||||
* measure in this configutation is 1.5V (VCC / 2).
|
||||
*
|
||||
* By changing the reference voltage from ADC_REFERENCE_VCC to ADC_REFERENCE_VCC_DIV1POINT6
|
||||
* or ADC_REFERENCE_VCC_DIV2, you can get more resolution when measuring small voltages (i.e.
|
||||
* a phototransistor circuit in low light).
|
||||
*
|
||||
* There is also a special reference voltage called ADC_REFERENCE_INTREF. The SAM L22's
|
||||
* Supply Controller provides a selectable voltage reference (by default, 1.024 V) that you
|
||||
* can select as a reference voltage for ADC conversions. Unlike the three references we
|
||||
* talked about in the last paragraph, this reference voltage does not depend on VCC, which
|
||||
* makes it very useful for measuring the battery voltage (since you can't really compare
|
||||
* VCC to itself). You can change the INTREF voltage to 2.048 or 4.096 V by poking at the
|
||||
* supply controller's VREF register, but the watch library does not support this use case.
|
||||
**/
|
||||
void watch_set_analog_reference_voltage(watch_adc_reference_voltage reference);
|
||||
|
||||
/** @brief Returns the voltage of the VCC supply in millivolts (i.e. 3000 mV == 3.0 V). If running on
|
||||
* a coin cell, this will be the battery voltage.
|
||||
* @details Unlike other ADC functions, this function does not return a raw value from the ADC, but
|
||||
* rather scales it to an actual number of millivolts. This is because the ADC doesn't let
|
||||
* us measure VCC per se; it instead lets us measure VCC / 4, and we choose to measure it
|
||||
* against the internal reference voltage of 1.024 V. In short, the ADC gives us a number
|
||||
* that's complicated to deal with, so we just turn it into a useful number for you :)
|
||||
* @note This function depends on INTREF being 1.024V. If you have changed it by poking at the supply
|
||||
* controller's VREF.SEL bits, this function will return inaccurate values.
|
||||
*/
|
||||
uint16_t watch_get_vcc_voltage();
|
||||
|
||||
/** @brief Disables the analog circuitry on the selected pin.
|
||||
* @param pin One of pins A0-A4.
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user