Merge branch 'main' of github.com:joeycastillo/Sensor-Watch
This commit is contained in:
commit
ef428bd4e1
4191
PCB/Main Boards/OSO-SWAT-A1-05.brd
Normal file
4191
PCB/Main Boards/OSO-SWAT-A1-05.brd
Normal file
File diff suppressed because it is too large
Load Diff
5324
PCB/Main Boards/OSO-SWAT-A1-05.sch
Normal file
5324
PCB/Main Boards/OSO-SWAT-A1-05.sch
Normal file
File diff suppressed because it is too large
Load Diff
@ -104,7 +104,7 @@ void movement_request_tick_frequency(uint8_t freq) {
|
|||||||
RTC->MODE2.INTENCLR.reg = 0xFE; // disable all callbacks except the 128 Hz one
|
RTC->MODE2.INTENCLR.reg = 0xFE; // disable all callbacks except the 128 Hz one
|
||||||
movement_state.subsecond = 0;
|
movement_state.subsecond = 0;
|
||||||
movement_state.tick_frequency = freq;
|
movement_state.tick_frequency = freq;
|
||||||
watch_rtc_register_periodic_callback(cb_tick, freq);
|
if (freq) watch_rtc_register_periodic_callback(cb_tick, freq);
|
||||||
}
|
}
|
||||||
|
|
||||||
void movement_illuminate_led() {
|
void movement_illuminate_led() {
|
||||||
|
@ -12,6 +12,7 @@ void character_set_face_activate(movement_settings_t *settings, void *context) {
|
|||||||
(void) settings;
|
(void) settings;
|
||||||
char *c = (char *)context;
|
char *c = (char *)context;
|
||||||
*c = '@';
|
*c = '@';
|
||||||
|
movement_request_tick_frequency(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool character_set_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
|
bool character_set_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
|
||||||
@ -33,8 +34,6 @@ bool character_set_face_loop(movement_event_t event, movement_settings_t *settin
|
|||||||
sprintf(buf, "%c%c%c%c%c%c%c%c%c%c", *c, *c, *c, *c, *c, *c, *c, *c, *c, *c);
|
sprintf(buf, "%c%c%c%c%c%c%c%c%c%c", *c, *c, *c, *c, *c, *c, *c, *c, *c, *c);
|
||||||
watch_display_string(buf, 0);
|
watch_display_string(buf, 0);
|
||||||
break;
|
break;
|
||||||
case EVENT_TICK:
|
|
||||||
break;
|
|
||||||
case EVENT_TIMEOUT:
|
case EVENT_TIMEOUT:
|
||||||
movement_move_to_face(0);
|
movement_move_to_face(0);
|
||||||
break;
|
break;
|
||||||
@ -48,4 +47,5 @@ bool character_set_face_loop(movement_event_t event, movement_settings_t *settin
|
|||||||
void character_set_face_resign(movement_settings_t *settings, void *context) {
|
void character_set_face_resign(movement_settings_t *settings, void *context) {
|
||||||
(void) settings;
|
(void) settings;
|
||||||
(void) context;
|
(void) context;
|
||||||
|
movement_request_tick_frequency(1);
|
||||||
}
|
}
|
||||||
|
@ -12,14 +12,78 @@
|
|||||||
// Pressing the alarm button enters the log mode, where the main display shows the number of interrupts detected in each of the last
|
// Pressing the alarm button enters the log mode, where the main display shows the number of interrupts detected in each of the last
|
||||||
// 24 hours (the hour is shown in the top right digit and AM/PM indicator, if the clock is set to 12 hour mode)
|
// 24 hours (the hour is shown in the top right digit and AM/PM indicator, if the clock is set to 12 hour mode)
|
||||||
|
|
||||||
|
void _lis2dh_logging_face_update_display(movement_settings_t *settings, lis2dh_logger_state_t *logger_state, lis2dh_interrupt_state interrupt_state, watch_date_time date_time) {
|
||||||
|
char buf[14];
|
||||||
|
char time_indication_character;
|
||||||
|
int8_t pos;
|
||||||
|
|
||||||
|
if (logger_state->log_ticks) {
|
||||||
|
pos = (logger_state->data_points - 1 - logger_state->display_index) % LIS2DH_LOGGING_NUM_DATA_POINTS;
|
||||||
|
if (pos < 0) {
|
||||||
|
watch_clear_colon();
|
||||||
|
sprintf(buf, "NO data ");
|
||||||
|
} else {
|
||||||
|
date_time = logger_state->data[pos].timestamp;
|
||||||
|
watch_set_colon();
|
||||||
|
if (settings->bit.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;
|
||||||
|
}
|
||||||
|
switch (logger_state->axis_index) {
|
||||||
|
case 0:
|
||||||
|
sprintf(buf, "3A%2d%02d%4ld", date_time.unit.hour, date_time.unit.minute, logger_state->data[pos].x_interrupts + logger_state->data[pos].y_interrupts + logger_state->data[pos].z_interrupts);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
sprintf(buf, "XA%2d%02d%4ld", date_time.unit.hour, date_time.unit.minute, logger_state->data[pos].x_interrupts);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
sprintf(buf, "YA%2d%02d%4ld", date_time.unit.hour, date_time.unit.minute, logger_state->data[pos].y_interrupts);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
sprintf(buf, "ZA%2d%02d%4ld", date_time.unit.hour, date_time.unit.minute, logger_state->data[pos].z_interrupts);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
date_time = watch_rtc_get_date_time();
|
||||||
|
watch_clear_colon();
|
||||||
|
watch_clear_indicator(WATCH_INDICATOR_PM);
|
||||||
|
watch_clear_indicator(WATCH_INDICATOR_24H);
|
||||||
|
if ((59 - date_time.unit.second) < 10) time_indication_character = '0' + (59 - date_time.unit.second);
|
||||||
|
else time_indication_character = (date_time.unit.second % 2) ? 'i' : '_';
|
||||||
|
sprintf(buf, "%c%c%c%c%2d%2d%2d",
|
||||||
|
(interrupt_state & LIS2DH_INTERRUPT_STATE_Y_HIGH) ? 'Y' : ' ',
|
||||||
|
(interrupt_state & LIS2DH_INTERRUPT_STATE_X_HIGH) ? 'X' : ' ',
|
||||||
|
(interrupt_state & LIS2DH_INTERRUPT_STATE_Z_HIGH) ? 'Z' : ' ',
|
||||||
|
time_indication_character,
|
||||||
|
logger_state->interrupts[0],
|
||||||
|
logger_state->interrupts[1],
|
||||||
|
logger_state->interrupts[2]);
|
||||||
|
}
|
||||||
|
watch_display_string(buf, 0);
|
||||||
|
}
|
||||||
|
|
||||||
void _lis2dh_logging_face_log_data(lis2dh_logger_state_t *logger_state) {
|
void _lis2dh_logging_face_log_data(lis2dh_logger_state_t *logger_state) {
|
||||||
watch_date_time date_time = watch_rtc_get_date_time();
|
watch_date_time date_time = watch_rtc_get_date_time();
|
||||||
date_time.unit.hour = (date_time.unit.hour + 23) % 24; // log this as the number of events in the previous hour
|
// we get this call 15 minutes late; i.e. at 6:15 we're logging events for 6:00.
|
||||||
|
// so: if we're at the top of the hour, roll the hour back too (7:00 task logs data for 6:45)
|
||||||
|
if (date_time.unit.minute == 0) date_time.unit.hour = (date_time.unit.hour + 23) % 24;
|
||||||
|
|
||||||
|
// // then roll the minute back.
|
||||||
|
date_time.unit.minute = (date_time.unit.minute + 45) % 60;
|
||||||
|
|
||||||
size_t pos = logger_state->data_points % LIS2DH_LOGGING_NUM_DATA_POINTS;
|
size_t pos = logger_state->data_points % LIS2DH_LOGGING_NUM_DATA_POINTS;
|
||||||
logger_state->data[pos].timestamp.reg = date_time.reg;
|
logger_state->data[pos].timestamp.reg = date_time.reg;
|
||||||
logger_state->data[pos].interrupts = logger_state->interrupts_this_hour;
|
logger_state->data[pos].x_interrupts = logger_state->x_interrupts_this_hour;
|
||||||
|
logger_state->data[pos].y_interrupts = logger_state->y_interrupts_this_hour;
|
||||||
|
logger_state->data[pos].z_interrupts = logger_state->z_interrupts_this_hour;
|
||||||
logger_state->data_points++;
|
logger_state->data_points++;
|
||||||
logger_state->interrupts_this_hour = 0;
|
logger_state->x_interrupts_this_hour = 0;
|
||||||
|
logger_state->y_interrupts_this_hour = 0;
|
||||||
|
logger_state->z_interrupts_this_hour = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void lis2dh_logging_face_setup(movement_settings_t *settings, void ** context_ptr) {
|
void lis2dh_logging_face_setup(movement_settings_t *settings, void ** context_ptr) {
|
||||||
@ -42,81 +106,58 @@ void lis2dh_logging_face_setup(movement_settings_t *settings, void ** context_pt
|
|||||||
}
|
}
|
||||||
|
|
||||||
void lis2dh_logging_face_activate(movement_settings_t *settings, void *context) {
|
void lis2dh_logging_face_activate(movement_settings_t *settings, void *context) {
|
||||||
(void) settings;
|
|
||||||
lis2dh_logger_state_t *logger_state = (lis2dh_logger_state_t *)context;
|
lis2dh_logger_state_t *logger_state = (lis2dh_logger_state_t *)context;
|
||||||
|
// force two settings: never enter low energy mode, and always snap back to screen 0.
|
||||||
|
// this assumes the accelerometer face is first in the watch_faces list.
|
||||||
|
settings->bit.le_interval = 0;
|
||||||
|
settings->bit.to_always = true;
|
||||||
|
|
||||||
logger_state->display_index = 0;
|
logger_state->display_index = 0;
|
||||||
logger_state->log_ticks = 0;
|
logger_state->log_ticks = 0;
|
||||||
watch_enable_digital_input(A1);
|
watch_enable_digital_input(A1);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool tick = false;
|
|
||||||
|
|
||||||
bool lis2dh_logging_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
|
bool lis2dh_logging_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
|
||||||
lis2dh_logger_state_t *logger_state = (lis2dh_logger_state_t *)context;
|
lis2dh_logger_state_t *logger_state = (lis2dh_logger_state_t *)context;
|
||||||
lis2dh_interrupt_state interrupt_state = 0;
|
lis2dh_interrupt_state interrupt_state = 0;
|
||||||
watch_date_time date_time;
|
watch_date_time date_time;
|
||||||
char buf[14];
|
|
||||||
char time_indication_character;
|
|
||||||
int8_t pos;
|
|
||||||
|
|
||||||
switch (event.event_type) {
|
switch (event.event_type) {
|
||||||
case EVENT_MODE_BUTTON_UP:
|
case EVENT_MODE_BUTTON_UP:
|
||||||
movement_move_to_next_face();
|
movement_move_to_next_face();
|
||||||
break;
|
break;
|
||||||
case EVENT_LIGHT_LONG_PRESS:
|
case EVENT_LIGHT_LONG_PRESS:
|
||||||
|
movement_illuminate_led();
|
||||||
break;
|
break;
|
||||||
case EVENT_LIGHT_BUTTON_DOWN:
|
case EVENT_LIGHT_BUTTON_DOWN:
|
||||||
|
logger_state->axis_index = (logger_state->axis_index + 1) % 4;
|
||||||
|
logger_state->log_ticks = 255;
|
||||||
|
_lis2dh_logging_face_update_display(settings, logger_state, interrupt_state, date_time);
|
||||||
break;
|
break;
|
||||||
case EVENT_ALARM_BUTTON_UP:
|
case EVENT_ALARM_BUTTON_UP:
|
||||||
if (logger_state->log_ticks) {
|
if (logger_state->log_ticks) logger_state->display_index = (logger_state->display_index + 1) % LIS2DH_LOGGING_NUM_DATA_POINTS;
|
||||||
logger_state->display_index = (logger_state->display_index + 1) % LIS2DH_LOGGING_NUM_DATA_POINTS;
|
logger_state->log_ticks = 255;
|
||||||
}
|
logger_state->axis_index = 0;
|
||||||
logger_state->log_ticks = 60;
|
_lis2dh_logging_face_update_display(settings, logger_state, interrupt_state, date_time);
|
||||||
// fall through
|
break;
|
||||||
case EVENT_ACTIVATE:
|
case EVENT_ACTIVATE:
|
||||||
case EVENT_TICK:
|
case EVENT_TICK:
|
||||||
tick = !tick;
|
if (logger_state->log_ticks > 0) {
|
||||||
|
logger_state->log_ticks--;
|
||||||
|
} else {
|
||||||
|
logger_state->display_index = 0;
|
||||||
|
}
|
||||||
if (watch_get_pin_level(A1)) {
|
if (watch_get_pin_level(A1)) {
|
||||||
watch_set_indicator(WATCH_INDICATOR_SIGNAL);
|
watch_set_indicator(WATCH_INDICATOR_SIGNAL);
|
||||||
interrupt_state = lis2dh_get_int1_state();
|
interrupt_state = lis2dh_get_int1_state();
|
||||||
logger_state->interrupts[0]++;
|
logger_state->interrupts[0]++;
|
||||||
logger_state->interrupts_this_hour++;
|
if (interrupt_state & LIS2DH_INTERRUPT_STATE_X_HIGH) logger_state->x_interrupts_this_hour++;
|
||||||
|
if (interrupt_state & LIS2DH_INTERRUPT_STATE_Y_HIGH) logger_state->y_interrupts_this_hour++;
|
||||||
|
if (interrupt_state & LIS2DH_INTERRUPT_STATE_Z_HIGH) logger_state->z_interrupts_this_hour++;
|
||||||
} else {
|
} else {
|
||||||
watch_clear_indicator(WATCH_INDICATOR_SIGNAL);
|
watch_clear_indicator(WATCH_INDICATOR_SIGNAL);
|
||||||
}
|
}
|
||||||
if (logger_state->log_ticks) {
|
_lis2dh_logging_face_update_display(settings, logger_state, interrupt_state, date_time);
|
||||||
pos = (logger_state->data_points - 1 - logger_state->display_index) % LIS2DH_LOGGING_NUM_DATA_POINTS;
|
|
||||||
if (pos < 0) {
|
|
||||||
watch_clear_colon();
|
|
||||||
sprintf(buf, "NO data ");
|
|
||||||
} else {
|
|
||||||
date_time = logger_state->data[pos].timestamp;
|
|
||||||
watch_set_colon();
|
|
||||||
if (settings->bit.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;
|
|
||||||
}
|
|
||||||
sprintf(buf, "AT%2d1n%4ld", date_time.unit.hour, logger_state->data[pos].interrupts);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
date_time = watch_rtc_get_date_time();
|
|
||||||
watch_clear_colon();
|
|
||||||
if ((59 - date_time.unit.second) < 10) time_indication_character = '0' + (59 - date_time.unit.second);
|
|
||||||
else time_indication_character = (date_time.unit.second % 2) ? 'i' : '_';
|
|
||||||
sprintf(buf, "%c%c%c%c%2d%2d%2d",
|
|
||||||
(interrupt_state & LIS2DH_INTERRUPT_STATE_Y_HIGH) ? 'Y' : ' ',
|
|
||||||
(interrupt_state & LIS2DH_INTERRUPT_STATE_X_HIGH) ? 'X' : ' ',
|
|
||||||
(interrupt_state & LIS2DH_INTERRUPT_STATE_Z_HIGH) ? '2' : ' ',
|
|
||||||
time_indication_character,
|
|
||||||
logger_state->interrupts[0],
|
|
||||||
logger_state->interrupts[1],
|
|
||||||
logger_state->interrupts[2]);
|
|
||||||
}
|
|
||||||
|
|
||||||
watch_display_string(buf, 0);
|
|
||||||
break;
|
break;
|
||||||
case EVENT_BACKGROUND_TASK:
|
case EVENT_BACKGROUND_TASK:
|
||||||
_lis2dh_logging_face_log_data(logger_state);
|
_lis2dh_logging_face_log_data(logger_state);
|
||||||
@ -137,12 +178,13 @@ void lis2dh_logging_face_resign(movement_settings_t *settings, void *context) {
|
|||||||
bool lis2dh_logging_face_wants_background_task(movement_settings_t *settings, void *context) {
|
bool lis2dh_logging_face_wants_background_task(movement_settings_t *settings, void *context) {
|
||||||
(void) settings;
|
(void) settings;
|
||||||
lis2dh_logger_state_t *logger_state = (lis2dh_logger_state_t *)context;
|
lis2dh_logger_state_t *logger_state = (lis2dh_logger_state_t *)context;
|
||||||
|
watch_date_time date_time = watch_rtc_get_date_time();
|
||||||
|
|
||||||
// this is kind of an abuse of the API, but, let's use the 1 minute tick to shift all our data over.
|
// this is kind of an abuse of the API, but, let's use the 1 minute tick to shift all our data over.
|
||||||
logger_state->interrupts[2] = logger_state->interrupts[1];
|
logger_state->interrupts[2] = logger_state->interrupts[1];
|
||||||
logger_state->interrupts[1] = logger_state->interrupts[0];
|
logger_state->interrupts[1] = logger_state->interrupts[0];
|
||||||
logger_state->interrupts[0] = 0;
|
logger_state->interrupts[0] = 0;
|
||||||
|
|
||||||
// and do our logging task at the top of the hour
|
// and do our logging task every 15 minutes
|
||||||
return watch_rtc_get_date_time().unit.minute == 0;
|
return (date_time.unit.minute % 15) == 0;
|
||||||
}
|
}
|
||||||
|
@ -4,19 +4,24 @@
|
|||||||
#include "movement.h"
|
#include "movement.h"
|
||||||
#include "watch.h"
|
#include "watch.h"
|
||||||
|
|
||||||
#define LIS2DH_LOGGING_NUM_DATA_POINTS (24)
|
#define LIS2DH_LOGGING_NUM_DATA_POINTS (96)
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
watch_date_time timestamp;
|
watch_date_time timestamp;
|
||||||
uint32_t interrupts;
|
uint32_t x_interrupts;
|
||||||
|
uint32_t y_interrupts;
|
||||||
|
uint32_t z_interrupts;
|
||||||
} lis2dh_logger_data_point_t;
|
} lis2dh_logger_data_point_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t display_index; // the index we are displaying on screen
|
uint8_t display_index; // the index we are displaying on screen
|
||||||
|
uint8_t axis_index; // the index we are displaying on screen
|
||||||
uint8_t log_ticks; // when the user taps the ALARM button, we enter log mode
|
uint8_t log_ticks; // when the user taps the ALARM button, we enter log mode
|
||||||
int32_t data_points; // the absolute number of data points logged
|
int32_t data_points; // the absolute number of data points logged
|
||||||
uint8_t interrupts[3]; // the number of interrupts we have logged in each of the last 3 minutes
|
uint8_t interrupts[3]; // the number of interrupts we have logged in each of the last 3 minutes
|
||||||
uint32_t interrupts_this_hour; // the number of interrupts we have logged in the last hour
|
uint32_t x_interrupts_this_hour; // the number of interrupts we have logged in the last hour
|
||||||
|
uint32_t y_interrupts_this_hour; // the number of interrupts we have logged in the last hour
|
||||||
|
uint32_t z_interrupts_this_hour; // the number of interrupts we have logged in the last hour
|
||||||
lis2dh_logger_data_point_t data[LIS2DH_LOGGING_NUM_DATA_POINTS];
|
lis2dh_logger_data_point_t data[LIS2DH_LOGGING_NUM_DATA_POINTS];
|
||||||
} lis2dh_logger_state_t;
|
} lis2dh_logger_state_t;
|
||||||
|
|
||||||
|
@ -65,6 +65,38 @@ void _watch_init() {
|
|||||||
a4_callback = NULL;
|
a4_callback = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline void _watch_wait_for_entropy() {
|
||||||
|
while (!hri_trng_get_INTFLAG_reg(TRNG, TRNG_INTFLAG_DATARDY));
|
||||||
|
}
|
||||||
|
|
||||||
|
// this function is called by arc4random to get entropy for random number generation.
|
||||||
|
// let's use the SAM L22's true random number generator to seed the PRNG!
|
||||||
|
int getentropy(void *buf, size_t buflen) {
|
||||||
|
hri_mclk_set_APBCMASK_TRNG_bit(MCLK);
|
||||||
|
hri_trng_set_CTRLA_ENABLE_bit(TRNG);
|
||||||
|
|
||||||
|
size_t i = 0;
|
||||||
|
while(i < buflen / 4) {
|
||||||
|
_watch_wait_for_entropy();
|
||||||
|
((uint32_t *)buf)[i++] = hri_trng_read_DATA_reg(TRNG);
|
||||||
|
}
|
||||||
|
|
||||||
|
// but what if they asked for an awkward number of bytes?
|
||||||
|
if (buflen % 4) {
|
||||||
|
// all good: let's fill in one, two or three bytes at the end of the buffer.
|
||||||
|
_watch_wait_for_entropy();
|
||||||
|
uint32_t last_little_bit = hri_trng_read_DATA_reg(TRNG);
|
||||||
|
for(size_t j = 0; j <= (buflen % 4); j++) {
|
||||||
|
((uint8_t *)buf)[i * 4 + j] = (last_little_bit >> (j * 8)) & 0xFF;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hri_trng_clear_CTRLA_ENABLE_bit(TRNG);
|
||||||
|
hri_mclk_clear_APBCMASK_TRNG_bit(MCLK);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void _watch_enable_tcc() {
|
void _watch_enable_tcc() {
|
||||||
// clock TCC0 with the main clock (8 MHz) and enable the peripheral clock.
|
// clock TCC0 with the main clock (8 MHz) and enable the peripheral clock.
|
||||||
hri_gclk_write_PCHCTRL_reg(GCLK, TCC0_GCLK_ID, GCLK_PCHCTRL_GEN_GCLK0_Val | GCLK_PCHCTRL_CHEN);
|
hri_gclk_write_PCHCTRL_reg(GCLK, TCC0_GCLK_ID, GCLK_PCHCTRL_GEN_GCLK0_Val | GCLK_PCHCTRL_CHEN);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user