diff options
author | Joey Castillo <joeycastillo@utexas.edu> | 2022-10-09 22:50:31 -0500 |
---|---|---|
committer | Joey Castillo <joeycastillo@utexas.edu> | 2022-10-09 22:50:31 -0500 |
commit | 5bb058f5854c582c95e87ced5c5100f55e3408c7 (patch) | |
tree | 23984f46994c1229efb0b2530f0006d9d46616c2 /movement/watch_faces | |
parent | 33dec21cf1cb0ebd340e8d42253611540ca29b7c (diff) | |
download | Sensor-Watch-5bb058f5854c582c95e87ced5c5100f55e3408c7.tar.gz Sensor-Watch-5bb058f5854c582c95e87ced5c5100f55e3408c7.tar.bz2 Sensor-Watch-5bb058f5854c582c95e87ced5c5100f55e3408c7.zip |
add watch face for frequency correction and measurement
Diffstat (limited to 'movement/watch_faces')
-rw-r--r-- | movement/watch_faces/demo/frequency_correction_face.c | 142 | ||||
-rw-r--r-- | movement/watch_faces/demo/frequency_correction_face.h | 48 |
2 files changed, 190 insertions, 0 deletions
diff --git a/movement/watch_faces/demo/frequency_correction_face.c b/movement/watch_faces/demo/frequency_correction_face.c new file mode 100644 index 00000000..99601735 --- /dev/null +++ b/movement/watch_faces/demo/frequency_correction_face.c @@ -0,0 +1,142 @@ +/* + * MIT License + * + * Copyright (c) 2022 <#author_name#> + * + * 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 "frequency_correction_face.h" + +// NOTE: since this face deals directly with the SAM L22's SUPC and RTC registers, +// it won't build for the simulator or really do anything. so let's not. +#ifndef __EMSCRIPTEN__ + +// Waveform output. Comes out on pin A1 of the 9-pin connector. Output is enabled +// when the watch face is activated and disabled when deactivated. +// * The RTC's PEREOn enables an event at a frequency of 1024/(2^(n+3)), allowing +// event generation from 128 Hz to 1 Hz. +// * The SUPC's RTCTGL causes the selected output to toggle on an RTC event. +// Putting it together, PEREO0 generates a 64 Hz square wave on A1, PEREO1 a 32 Hz +// wave, on and on to an 0.5 Hz square wave for PEREO7. +static void _enable_output(uint8_t period_event_output) { + SUPC->BKOUT.bit.EN = 1; + SUPC->BKOUT.bit.RTCTGL = 1; + RTC->MODE2.CTRLA.bit.ENABLE = 0; + while (RTC->MODE2.SYNCBUSY.bit.ENABLE); + RTC->MODE2.EVCTRL.reg = RTC_MODE2_EVCTRL_PEREO(1 << period_event_output); + RTC->MODE2.CTRLA.bit.ENABLE = 1; + while (RTC->MODE2.SYNCBUSY.bit.ENABLE); +} + +// The display: "FC" for "Frequency Correction" in the title slot; at the top right, +// the periodic event output that's enabled:: 0 for PEREO0, 1 for PEREO1, etc. +// The main line displays the frequency correction value from -127 to 127. +// Controls: A short press on the Light button increases FREQCORR by 1 and a short +// press on the Alarm button decreases FREQCORR by 1. A long press on ALARM cycles +// through the available output frequencies. +static void _update_display(uint8_t period_event_output) { + char buf[14]; + sprintf(buf, "FC%2d%6d", period_event_output, (int8_t)RTC->MODE2.FREQCORR.reg); + watch_display_string(buf, 0); +} + +static void _disable_output() { + SUPC->BKOUT.bit.EN = 0; + SUPC->BKOUT.bit.RTCTGL = 0; + RTC->MODE2.CTRLA.bit.ENABLE = 0; + while (RTC->MODE2.SYNCBUSY.bit.ENABLE); + RTC->MODE2.EVCTRL.reg = RTC_MODE2_EVCTRL_PEREO(0); + RTC->MODE2.CTRLA.bit.ENABLE = 1; + while (RTC->MODE2.SYNCBUSY.bit.ENABLE); +} + +void frequency_correction_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) { + (void) settings; + (void) watch_face_index; + if (*context_ptr == NULL) { + *context_ptr = malloc(sizeof(frequency_correction_state_t)); + frequency_correction_state_t *state = (frequency_correction_state_t *)*context_ptr; + state->period_event_output = 0; + } +} + +void frequency_correction_face_activate(movement_settings_t *settings, void *context) { + (void) settings; + frequency_correction_state_t *state = (frequency_correction_state_t *)context; + _enable_output(state->period_event_output); +} + +bool frequency_correction_face_loop(movement_event_t event, movement_settings_t *settings, void *context) { + (void) settings; + frequency_correction_state_t *state = (frequency_correction_state_t *)context; + int8_t freqcorr; + + switch (event.event_type) { + case EVENT_ACTIVATE: + _update_display(state->period_event_output); + break; + case EVENT_TICK: + break; + case EVENT_MODE_BUTTON_UP: + movement_move_to_next_face(); + break; + case EVENT_LIGHT_BUTTON_UP: + freqcorr = RTC->MODE2.FREQCORR.reg; + if (freqcorr < 127) { + RTC->MODE2.FREQCORR.reg++; + while(RTC->MODE2.SYNCBUSY.reg); + } + _update_display(state->period_event_output); + break; + case EVENT_ALARM_BUTTON_UP: + freqcorr = RTC->MODE2.FREQCORR.reg; + if (freqcorr > -127) { + RTC->MODE2.FREQCORR.reg--; + while(RTC->MODE2.SYNCBUSY.reg); + } + _update_display(state->period_event_output); + break; + case EVENT_ALARM_LONG_PRESS: + state->period_event_output = (state->period_event_output + 1) % 8; + _update_display(state->period_event_output); + _enable_output(state->period_event_output); + break; + case EVENT_TIMEOUT: + break; + case EVENT_LOW_ENERGY_UPDATE: + watch_start_tick_animation(500); + break; + default: + break; + } + + return true; +} + +void frequency_correction_face_resign(movement_settings_t *settings, void *context) { + (void) settings; + (void) context; + + _disable_output(); +} + +#endif diff --git a/movement/watch_faces/demo/frequency_correction_face.h b/movement/watch_faces/demo/frequency_correction_face.h new file mode 100644 index 00000000..52c4e621 --- /dev/null +++ b/movement/watch_faces/demo/frequency_correction_face.h @@ -0,0 +1,48 @@ +/* + * MIT License + * + * Copyright (c) 2022 <#author_name#> + * + * 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. + */ + +#ifndef FREQUENCY_CORRECTION_FACE_H_ +#define FREQUENCY_CORRECTION_FACE_H_ + +#include "movement.h" + +typedef struct { + uint8_t period_event_output; +} frequency_correction_state_t; + +void frequency_correction_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr); +void frequency_correction_face_activate(movement_settings_t *settings, void *context); +bool frequency_correction_face_loop(movement_event_t event, movement_settings_t *settings, void *context); +void frequency_correction_face_resign(movement_settings_t *settings, void *context); + +#define frequency_correction_face ((const watch_face_t){ \ + frequency_correction_face_setup, \ + frequency_correction_face_activate, \ + frequency_correction_face_loop, \ + frequency_correction_face_resign, \ + NULL, \ +}) + +#endif // FREQUENCY_CORRECTION_FACE_H_ + |