Getting code on the watch
-------------------------
-The watch library in this repository is very work-in-progress, but it should allow you to get started. To create a new project, copy the “Sensor Watch Starter Project” and write your code in the app.c file.
+The watch library in this repository is very work-in-progress, but it should allow you to get started. To create a new project, copy the “starter-project” folder in the apps folder, and write your code in the app.c file.
You will need to install [the GNU Arm Embedded Toolchain](https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads/) to build projects for the watch. The watch library has been tested with the `9-2019-q4-major` version and the `10.3-2021.07` versions.
+++ /dev/null
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <math.h>
-#include "watch.h"
-#include "lis2dh.h"
-
-// This application displays data from the old Sensor Watch Motion sensor board.
-// Note that this board required A0 to be set high to power the sensor.
-// Future accelerometer boards will be powered directly from VCC.
-// Also note that this board has its INT1 pin wired to A1, which is not an external
-// wake pin. Future accelerometer boards will wire interrupt pins to A2 and A4.
-
-void cb_light_pressed() {
-}
-
-void cb_mode_pressed() {
-}
-
-void cb_alarm_pressed() {
-}
-
-uint8_t interrupts = 0;
-uint8_t last_interrupts = 0;
-uint8_t ticks = 0;
-char buf[13] = {0};
-
-void cb_interrupt_1() {
- interrupts++;
-}
-
-void cb_tick() {
- if (++ticks == 30) {
- last_interrupts = interrupts;
- interrupts = 0;
- ticks = 0;
- }
-}
-
-void app_init() {
- gpio_set_pin_direction(A0, GPIO_DIRECTION_OUT);
- gpio_set_pin_function(A0, GPIO_PIN_FUNCTION_OFF);
- gpio_set_pin_level(A0, true);
-
- watch_enable_display();
- watch_display_string("IN 0 0 0", 0);
-
- watch_enable_external_interrupts();
- watch_register_interrupt_callback(BTN_MODE, cb_mode_pressed, INTERRUPT_TRIGGER_RISING);
- watch_register_interrupt_callback(BTN_LIGHT, cb_light_pressed, INTERRUPT_TRIGGER_RISING);
- watch_register_interrupt_callback(BTN_ALARM, cb_alarm_pressed, INTERRUPT_TRIGGER_RISING);
-
- watch_enable_i2c();
-
- lis2dh_begin();
- lis2dh_set_data_rate(LIS2DH_DATA_RATE_10_HZ);
- lis2dh_configure_aoi_int1(
- LIS2DH_INTERRUPT_CONFIGURATION_OR |
- LIS2DH_INTERRUPT_CONFIGURATION_X_HIGH_ENABLE |
- LIS2DH_INTERRUPT_CONFIGURATION_Y_HIGH_ENABLE |
- LIS2DH_INTERRUPT_CONFIGURATION_Z_HIGH_ENABLE, 96, 0, true);
-
- watch_register_interrupt_callback(A1, cb_interrupt_1, INTERRUPT_TRIGGER_RISING);
- watch_rtc_register_tick_callback(cb_tick);
-}
-
-void app_wake_from_backup() {
-}
-
-void app_setup() {
-}
-
-void app_prepare_for_standby() {
-}
-
-void app_wake_from_standby() {
-}
-
-bool app_loop() {
- sprintf(buf, "IN%2d%3d%3d", ticks, interrupts, last_interrupts);
- watch_display_string(buf, 0);
-
- return true;
-}
+++ /dev/null
-TOP = ../../..
-include $(TOP)/make.mk
-
-INCLUDES += \
- -I../
-
-SRCS += \
- ../app.c
-
-include $(TOP)/rules.mk
+++ /dev/null
-#include <stdio.h>
-#include <string.h>
-#include <math.h>
-#include "watch.h"
-#include "bme280.h"
-
-#include "app.h"
-
-ApplicationState application_state;
-char buf[16] = {0};
-
-/**
- * @brief Zeroes out the application state struct.
- */
-void app_init() {
- memset(&application_state, 0, sizeof(application_state));
-}
-
-void app_wake_from_backup() {
- // This app does not support BACKUP mode.
-}
-
-void app_setup() {
- watch_enable_external_interrupts();
- watch_register_interrupt_callback(BTN_MODE, cb_mode_pressed, INTERRUPT_TRIGGER_RISING);
- watch_register_interrupt_callback(BTN_LIGHT, cb_light_pressed, INTERRUPT_TRIGGER_RISING);
- watch_register_extwake_callback(BTN_ALARM, cb_alarm_pressed, true);
-
- watch_enable_buzzer();
- watch_enable_leds();
-
- // pin A0 powers the sensor on this board.
- watch_enable_digital_output(A0);
- watch_set_pin_level(A0, true);
- delay_ms(10);
-
- watch_enable_i2c();
-
- watch_i2c_write8(BME280_ADDRESS, BME280_REGISTER_SOFTRESET, BME280_SOFT_RESET_CODE);
- delay_ms(10);
- application_state.dig_T1 = watch_i2c_read16(BME280_ADDRESS, BME280_REGISTER_DIG_T1);
- application_state.dig_T2 = (int16_t)watch_i2c_read16(BME280_ADDRESS, BME280_REGISTER_DIG_T2);
- application_state.dig_T3 = (int16_t)watch_i2c_read16(BME280_ADDRESS, BME280_REGISTER_DIG_T3);
- application_state.dig_H1 = watch_i2c_read8(BME280_ADDRESS, BME280_REGISTER_DIG_H1);
- application_state.dig_H2 = (int16_t)watch_i2c_read16(BME280_ADDRESS, BME280_REGISTER_DIG_H2);
- application_state.dig_H3 = watch_i2c_read8(BME280_ADDRESS, BME280_REGISTER_DIG_H3);
- application_state.dig_H4 = ((int8_t)watch_i2c_read8(BME280_ADDRESS, BME280_REGISTER_DIG_H4) << 4) |
- (watch_i2c_read8(BME280_ADDRESS, BME280_REGISTER_DIG_H4 + 1) & 0xF);
- application_state.dig_H5 = ((int8_t)watch_i2c_read8(BME280_ADDRESS, BME280_REGISTER_DIG_H5 + 1) << 4) |
- (watch_i2c_read8(BME280_ADDRESS, BME280_REGISTER_DIG_H5) >> 4);
- application_state.dig_H6 = (int8_t)watch_i2c_read8(BME280_ADDRESS, BME280_REGISTER_DIG_H6);
-
- watch_i2c_write8(BME280_ADDRESS, BME280_REGISTER_CONTROL_HUMID, BME280_CONTROL_HUMID_SAMPLING_NONE);
- watch_i2c_write8(BME280_ADDRESS, BME280_REGISTER_CONTROL, BME280_CONTROL_TEMPERATURE_SAMPLING_X16 |
- BME280_CONTROL_PRESSURE_SAMPLING_NONE |
- BME280_CONTROL_MODE_FORCED);
-
- watch_enable_display();
-
- watch_rtc_register_tick_callback(cb_tick);
-}
-
-/**
- * Nothing to do here.
- */
-void app_prepare_for_standby() {
-}
-
-/**
- * @todo restore the BME280's calibration values from backup memory
- */
-void app_wake_from_standby() {
-}
-
-/**
- * Displays the temperature and humidity on screen, or a string indicating no measurements are being taken.
- */
-bool app_loop() {
- // play a beep if the mode has changed in response to a user's press of the MODE button
- if (application_state.mode_changed) {
- // low note for nonzero case, high note for return to clock
- watch_buzzer_play_note(application_state.mode ? BUZZER_NOTE_C7 : BUZZER_NOTE_C8, 100);
- application_state.mode_changed = false;
- }
-
- // If the user is not in clock mode and the mode timeout has expired, return them to clock mode
- if (application_state.mode != MODE_CLOCK && application_state.mode_ticks == 0) {
- application_state.mode = MODE_CLOCK;
- application_state.mode_changed = true;
- }
-
- // If the LED is off and should be on, turn it on
- if (application_state.light_ticks > 0 && !application_state.led_on) {
- watch_set_led_green();
- application_state.led_on = true;
- }
-
- // if the LED is on and should be off, turn it off
- if (application_state.led_on && application_state.light_ticks == 0) {
- // unless the user is holding down the LIGHT button, in which case, give them more time.
- if (watch_get_pin_level(BTN_LIGHT)) {
- application_state.light_ticks = 3;
- } else {
- watch_set_led_off();
- application_state.led_on = false;
- }
- }
-
- switch (application_state.mode) {
- case MODE_CLOCK:
- do_clock_mode();
- break;
- case MODE_TEMP:
- do_temp_mode();
- break;
- case MODE_LOG:
- do_log_mode();
- break;
- case MODE_PREFS:
- do_prefs_mode();
- break;
- case MODE_SET:
- do_set_time_mode();
- break;
- case NUM_MODES:
- // dummy case, just silences a warning
- break;
- }
-
- application_state.mode_changed = false;
-
- return true;
-}
-
-/**
- * Reads the temperature from the BME280
- * @param p_t_fine - an optional pointer to an int32_t; if provided, the t_fine measurement
- * (required for humidity calculation) will be returned by reference.
- * Pass in NULL if you do not care about this value.
- * @return a float indicating the temperature in degrees celsius.
- */
-float read_temperature(int32_t *p_t_fine) {
- // read24 reads the bytes into a uint32 which works for little-endian values (MSB is 0)
- uint32_t raw_data = watch_i2c_read24(BME280_ADDRESS, BME280_REGISTER_TEMP_DATA) >> 8;
- // alas the sensor's register layout is big-endian-ish, with a nibble of zeroes at the end of the LSB.
- // this line shuffles everything back into place (swaps LSB and MSB and shifts the zeroes off the end)
- int32_t adc_value = (((raw_data >> 16) | (raw_data & 0xFF00) | (raw_data << 16)) & 0xFFFFFF) >> 4;
-
- // this bit is cribbed from Adafruit's BME280 driver. support their open source efforts by buying some stuff!
- int32_t var1 = ((((adc_value >> 3) - ((int32_t)application_state.dig_T1 << 1))) * ((int32_t)application_state.dig_T2)) >> 11;
- int32_t var2 = (((((adc_value >> 4) - ((int32_t)application_state.dig_T1)) * ((adc_value >> 4) - ((int32_t)application_state.dig_T1))) >> 12) * ((int32_t)application_state.dig_T3)) >> 14;
- int32_t t_fine = var1 + var2;
-
- // if we got a pointer to a t_fine, return it by reference (for humidity calculation).
- if (p_t_fine != NULL) *p_t_fine = t_fine;
-
- if (application_state.is_fahrenheit) {
- return (((t_fine * 5 + 128) >> 8) / 100.0) * 1.8 + 32;
- } else {
- return ((t_fine * 5 + 128) >> 8) / 100.0;
- }
-}
-
-/**
- * Reads the humidity from the BME280
- * @param t_fine - the t_fine measurement from a call to read_temperature
- * @return a float indicating the relative humidity as a percentage from 0-100.
- * @todo the returned value is glitchy, need to fix.
- */
-float read_humidity(int32_t t_fine) {
- int32_t adc_value = watch_i2c_read16(BME280_ADDRESS, BME280_REGISTER_HUMID_DATA);
-
- // again, cribbed from Adafruit's BME280 driver. they sell a great breakout board for this sensor!
- int32_t v_x1_u32r = (t_fine - ((int32_t)76800));
- v_x1_u32r = (((((adc_value << 14) - (((int32_t)application_state.dig_H4) << 20) - (((int32_t)application_state.dig_H5) * v_x1_u32r)) +
- ((int32_t)16384)) >> 15) * (((((((v_x1_u32r * ((int32_t)application_state.dig_H6)) >> 10) * (((v_x1_u32r * ((int32_t)application_state.dig_H3)) >> 11) +
- ((int32_t)32768))) >> 10) + ((int32_t)2097152)) * ((int32_t)application_state.dig_H2) + 8192) >> 14));
- v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * ((int32_t)application_state.dig_H1)) >> 4));
- v_x1_u32r = (v_x1_u32r < 0) ? 0 : v_x1_u32r;
- v_x1_u32r = (v_x1_u32r > 419430400) ? 419430400 : v_x1_u32r;
- float h = (v_x1_u32r >> 12);
-
- return h / 1024.0;
-}
-
-void log_data() {
- watch_date_time date_time = watch_rtc_get_date_time();
- uint8_t hour = date_time.unit.hour;
- int8_t temperature = read_temperature(NULL);
-
- for(int i = 0; i < MAX_DATA_POINTS - 1; i++) {
- application_state.logged_data[i] = application_state.logged_data[i + 1];
- }
- application_state.logged_data[MAX_DATA_POINTS - 1].is_valid = true;
- application_state.logged_data[MAX_DATA_POINTS - 1].hour = hour;
- application_state.logged_data[MAX_DATA_POINTS - 1].temperature = temperature;
-}
-
-void do_clock_mode() {
- watch_date_time date_time = watch_rtc_get_date_time();
- const char months[12][3] = {"JA", "FE", "MR", "AR", "MA", "JN", "JL", "AU", "SE", "OC", "NO", "dE"};
-
- watch_display_string((char *)months[date_time.unit.month - 1], 0);
- sprintf(buf, "%2d%2d%02d%02d", date_time.unit.day, date_time.unit.hour, date_time.unit.minute, date_time.unit.second);
- watch_display_string(buf, 2);
- watch_set_colon();
-}
-
-void do_temp_mode() {
- int32_t t_fine;
- float temperature;
- float humidity;
-
- // take one reading
- watch_i2c_write8(BME280_ADDRESS, BME280_REGISTER_CONTROL, BME280_CONTROL_TEMPERATURE_SAMPLING_X16 |
- BME280_CONTROL_MODE_FORCED);
- // wait for reading to finish
- while(watch_i2c_read8(BME280_ADDRESS, BME280_REGISTER_STATUS) & BME280_STATUS_UPDATING_MASK);
- temperature = read_temperature(&t_fine);
- humidity = read_humidity(t_fine);
- if (application_state.show_humidity) {
- sprintf(buf, "TE%2d%4.1f#%c", (int)(humidity / 10), temperature, application_state.is_fahrenheit ? 'F' : 'C');
- } else {
- sprintf(buf, "TE %4.1f#%c", temperature, application_state.is_fahrenheit ? 'F' : 'C');
- }
- watch_display_string(buf, 0);
- watch_clear_colon();
-}
-
-void do_log_mode() {
- bool is_valid = (uint8_t)(application_state.logged_data[MAX_DATA_POINTS - 1 - application_state.page].is_valid);
- uint8_t hour = (uint8_t)(application_state.logged_data[MAX_DATA_POINTS - 1 - application_state.page].hour);
- int8_t temperature = (int8_t)(application_state.logged_data[MAX_DATA_POINTS - 1 - application_state.page].temperature);
- if (!is_valid) {
- sprintf(buf, "LO%2d------", application_state.page);
- watch_clear_colon();
- } else {
- sprintf(buf, "LO%2d%2d%4d", application_state.page, hour, temperature);
- watch_set_colon();
- }
- watch_display_string(buf, 0);
-}
-
-void log_mode_handle_primary_button() {
- application_state.page++;
- if (application_state.page == MAX_DATA_POINTS) application_state.page = 0;
-}
-
-void do_prefs_mode() {
- sprintf(buf, "PR CorF %c", application_state.is_fahrenheit ? 'F' : 'C');
- watch_display_string(buf, 0);
- watch_clear_colon();
-}
-
-void prefs_mode_handle_primary_button() {
- // TODO: add rest of preferences (12/24, humidity, LED color, etc.)
- // for now only one, C or F
-}
-
-void prefs_mode_handle_secondary_button() {
- application_state.is_fahrenheit = !application_state.is_fahrenheit;
-}
-
-void do_set_time_mode() {
- watch_date_time date_time = watch_rtc_get_date_time();
-
- watch_display_string(" ", 0);
- switch (application_state.page) {
- case 0: // hour
- sprintf(buf, "ST t%2d", date_time.unit.hour);
- break;
- case 1: // minute
- sprintf(buf, "ST t %02d", date_time.unit.minute);
- break;
- case 2: // second
- sprintf(buf, "ST t %02d", date_time.unit.second);
- break;
- case 3: // year
- sprintf(buf, "ST d%2d", date_time.unit.year + 20);
- break;
- case 4: // month
- sprintf(buf, "ST d %02d", date_time.unit.month);
- break;
- case 5: // day
- sprintf(buf, "ST d %02d", date_time.unit.day);
- break;
- }
- watch_display_string(buf, 0);
- watch_set_pixel(1, 12); // required for T in position 1
-}
-
-void set_time_mode_handle_primary_button() {
- application_state.page++;
- if (application_state.page == 6) application_state.page = 0;
-}
-
-void set_time_mode_handle_secondary_button() {
- watch_date_time date_time = watch_rtc_get_date_time();
- const uint8_t days_in_month[12] = {31, 28, 31, 30, 31, 30, 30, 31, 30, 31, 30, 31};
-
- switch (application_state.page) {
- case 0: // hour
- date_time.unit.hour = (date_time.unit.hour + 1) % 24;
- break;
- case 1: // minute
- date_time.unit.minute = (date_time.unit.minute + 1) % 60;
- break;
- case 2: // second
- date_time.unit.second = 0;
- break;
- case 3: // year
- // only allow 2021-2030. fix this sometime next decade
- date_time.unit.year = ((date_time.unit.year % 10) + 1);
- break;
- case 4: // month
- date_time.unit.month = ((date_time.unit.month + 1) % 12);
- break;
- case 5: // day
- date_time.unit.day = date_time.unit.day + 1;
- // can't set to the 29th on a leap year. if it's february 29, set to 11:59 on the 28th.
- // and it should roll over.
- if (date_time.unit.day > days_in_month[date_time.unit.month - 1]) {
- date_time.unit.day = 1;
- }
- break;
- }
- watch_rtc_set_date_time(date_time);
-}
-
-void cb_mode_pressed() {
- application_state.mode = (application_state.mode + 1) % NUM_MODES;
- application_state.mode_changed = true;
- application_state.mode_ticks = 300;
- application_state.page = 0;
-}
-
-void cb_light_pressed() {
- switch (application_state.mode) {
- case MODE_PREFS:
- prefs_mode_handle_secondary_button();
- break;
- case MODE_SET:
- set_time_mode_handle_secondary_button();
- break;
- default:
- application_state.light_ticks = 3;
- break;
- }
-}
-
-void cb_alarm_pressed() {
- switch (application_state.mode) {
- case MODE_LOG:
- log_mode_handle_primary_button();
- break;
- case MODE_PREFS:
- prefs_mode_handle_primary_button();
- break;
- case MODE_SET:
- set_time_mode_handle_primary_button();
- break;
- default:
- break;
- }
-}
-
-void cb_tick() {
- // TODO: use alarm interrupt to trigger data acquisition.
- watch_date_time date_time = watch_rtc_get_date_time();
- if (date_time.unit.minute == 0 && date_time.unit.second == 0) {
- log_data();
- }
-
- if (application_state.light_ticks > 0) {
- application_state.light_ticks--;
- }
- if (application_state.mode_ticks > 0) {
- application_state.mode_ticks--;
- }
-}
+++ /dev/null
-// Sensor Watch: Hiking Log Demo App
-// This app displays a clock and temperature data from a BME280 temperature and humidiity sensor.
-// It also logs up to 36 hours of temperature data for playback.
-// You can use this app on backcountry treks: take the watch off at night and place it outside your tent.
-// It will log overnight low temperatures for review in the morning and optional transfer to your notepad.
-
-#define MAX_DATA_POINTS 36
-
-typedef enum ApplicationMode {
- MODE_CLOCK = 0, // Displays month, day and current time.
- MODE_TEMP, // (TE) Displays temperature and an optional humidity reading (0-10 representing 0-100%)
- MODE_LOG, // (LO) Plays back temperature data (temperature in seconds slot)
- MODE_PREFS, // (PR) Allows setting options for the application
- MODE_SET, // (ST) Set time and date
- NUM_MODES // Last item in the enum, it's the number of cases.
-} ApplicationMode;
-
-typedef struct SensorReading {
- bool is_valid;
- uint8_t hour;
- int8_t temperature;
-} SensorReading;
-
-typedef struct ApplicationState {
- // Internal application state
- ApplicationMode mode; // Current mode
- bool mode_changed; // Lets us perform one-time setup for a given mode
- uint16_t mode_ticks; // Timeout for the mode (returns to clock after timeout expires)
- uint8_t light_ticks; // Timeout for the light
- bool led_on; // Indicates that the LED is on
- uint8_t page; // Tracks the current page in log, prefs or settings.
- bool is_fahrenheit; // User preference, C or F
-
- // Data logging
- SensorReading logged_data[MAX_DATA_POINTS];
-
- // User preference
- bool show_humidity; // Indicates that the LED is on
-
- // BME280 calibration values
- uint16_t dig_T1;
- int16_t dig_T2;
- int16_t dig_T3;
- uint8_t dig_H1;
- int16_t dig_H2;
- uint8_t dig_H3;
- int16_t dig_H4;
- int16_t dig_H5;
- int8_t dig_H6;
-} ApplicationState;
-
-float read_temperature(int32_t *p_t_fine);
-float read_humidity(int32_t t_fine);
-
-void log_data();
-
-void do_clock_mode();
-
-void do_temp_mode();
-void temp_mode_handle_primary_button();
-
-void do_log_mode();
-void log_mode_handle_primary_button();
-
-void do_prefs_mode();
-void prefs_mode_handle_primary_button();
-void prefs_mode_handle_secondary_button();
-
-void do_set_time_mode();
-void set_time_mode_handle_primary_button();
-void set_time_mode_handle_secondary_button();
-
-void cb_light_pressed();
-void cb_mode_pressed();
-void cb_alarm_pressed();
-void cb_tick();
+++ /dev/null
-#define BME280_ADDRESS (0x77)
-#define BME280_SOFT_RESET_CODE (0xB6)
-#define BME280_STATUS_UPDATING_MASK (1 << 3)
-
-typedef enum BME280Register {
- BME280_REGISTER_DIG_T1 = 0x88,
- BME280_REGISTER_DIG_T2 = 0x8A,
- BME280_REGISTER_DIG_T3 = 0x8C,
-
- BME280_REGISTER_DIG_P1 = 0x8E,
- BME280_REGISTER_DIG_P2 = 0x90,
- BME280_REGISTER_DIG_P3 = 0x92,
- BME280_REGISTER_DIG_P4 = 0x94,
- BME280_REGISTER_DIG_P5 = 0x96,
- BME280_REGISTER_DIG_P6 = 0x98,
- BME280_REGISTER_DIG_P7 = 0x9A,
- BME280_REGISTER_DIG_P8 = 0x9C,
- BME280_REGISTER_DIG_P9 = 0x9E,
-
- BME280_REGISTER_DIG_H1 = 0xA1,
- BME280_REGISTER_DIG_H2 = 0xE1,
- BME280_REGISTER_DIG_H3 = 0xE3,
- BME280_REGISTER_DIG_H4 = 0xE4,
- BME280_REGISTER_DIG_H5 = 0xE5,
- BME280_REGISTER_DIG_H6 = 0xE7,
-
- BME280_REGISTER_CHIPID = 0xD0,
- BME280_REGISTER_VERSION = 0xD1,
- BME280_REGISTER_SOFTRESET = 0xE0,
-
- BME280_REGISTER_CAL26 = 0xE1,
-
- BME280_REGISTER_CONTROL_HUMID = 0xF2,
- BME280_REGISTER_STATUS = 0XF3,
- BME280_REGISTER_CONTROL = 0xF4,
- BME280_REGISTER_CONFIG = 0xF5,
- BME280_REGISTER_PRESSURE_DATA = 0xF7,
- BME280_REGISTER_TEMP_DATA = 0xFA,
- BME280_REGISTER_HUMID_DATA = 0xFD
-} BME280Register;
-
-typedef enum BME280Control {
- BME280_CONTROL_MODE_SLEEP = 0b00,
- BME280_CONTROL_MODE_FORCED = 0b01,
- BME280_CONTROL_MODE_NORMAL = 0b11,
- BME280_CONTROL_PRESSURE_SAMPLING_NONE = 0b000 << 2,
- BME280_CONTROL_PRESSURE_SAMPLING_X1 = 0b001 << 2,
- BME280_CONTROL_PRESSURE_SAMPLING_X2 = 0b010 << 2,
- BME280_CONTROL_PRESSURE_SAMPLING_X4 = 0b011 << 2,
- BME280_CONTROL_PRESSURE_SAMPLING_X8 = 0b100 << 2,
- BME280_CONTROL_PRESSURE_SAMPLING_X16 = 0b101 << 2,
- BME280_CONTROL_TEMPERATURE_SAMPLING_NONE = 0b000 << 5,
- BME280_CONTROL_TEMPERATURE_SAMPLING_X1 = 0b001 << 5,
- BME280_CONTROL_TEMPERATURE_SAMPLING_X2 = 0b010 << 5,
- BME280_CONTROL_TEMPERATURE_SAMPLING_X4 = 0b011 << 5,
- BME280_CONTROL_TEMPERATURE_SAMPLING_X8 = 0b100 << 5,
- BME280_CONTROL_TEMPERATURE_SAMPLING_X16 = 0b101 << 5
-} BME280Control;
-
-typedef enum BME280ControlHumidity {
- BME280_CONTROL_HUMID_SAMPLING_NONE = 0b000,
- BME280_CONTROL_HUMID_SAMPLING_X1 = 0b001,
- BME280_CONTROL_HUMID_SAMPLING_X2 = 0b010,
- BME280_CONTROL_HUMID_SAMPLING_X4 = 0b011,
- BME280_CONTROL_HUMID_SAMPLING_X8 = 0b100,
- BME280_CONTROL_HUMID_SAMPLING_X16 = 0b101
-} BME280ControlHumidity;
-
-typedef enum BME280Filter {
- BME280_CONFIG_FILTER_OFF = 0b000 << 2,
- BME280_CONFIG_FILTER_X2 = 0b001 << 2,
- BME280_CONFIG_FILTER_X4 = 0b010 << 2,
- BME280_CONFIG_FILTER_X8 = 0b011 << 2,
- BME280_CONFIG_FILTER_X16 = 0b10 << 2,
- BME280_CONFIG_STANDBY_MS_0_5 = 0b000 << 5,
- BME280_CONFIG_STANDBY_MS_10 = 0b110 << 5,
- BME280_CONFIG_STANDBY_MS_20 = 0b111 << 5,
- BME280_CONFIG_STANDBY_MS_62_5 = 0b001 << 5,
- BME280_CONFIG_STANDBY_MS_125 = 0b010 << 5,
- BME280_CONFIG_STANDBY_MS_250 = 0b011 << 5,
- BME280_CONFIG_STANDBY_MS_500 = 0b100 << 5,
- BME280_CONFIG_STANDBY_MS_1000 = 0b101 << 5
-} BME280Filter;
-
-inline uint16_t make_le_16(uint16_t val) { return (val >> 8) | (val << 8); }
+++ /dev/null
-TOP = ../../..
-include $(TOP)/make.mk
-
-INCLUDES += \
- -I../
-
-SRCS += \
- ../app.c
-
-include $(TOP)/rules.mk
+++ /dev/null
-#include <stdio.h>
-#include <string.h>
-#include "watch.h"
-
-typedef struct ApplicationState {
- bool play;
-} ApplicationState;
-
-ApplicationState application_state;
-
-
-void cb_alarm_pressed() {
- application_state.play = true;
-}
-
-void app_init() {
- memset(&application_state, 0, sizeof(application_state));
-}
-
-void app_wake_from_backup() {
-}
-
-void app_setup() {
- watch_register_extwake_callback(BTN_ALARM, cb_alarm_pressed, true);
-
- watch_enable_display();
-
- watch_enable_buzzer();
-}
-
-void app_prepare_for_standby() {
- watch_display_string(" rains ", 2);
-}
-
-void app_wake_from_standby() {
-}
-
-bool app_loop() {
- if (application_state.play) {
- printf("Playing song...\n");
- const BuzzerNote rains[] = {
- BUZZER_NOTE_A4,
- BUZZER_NOTE_F5,
- BUZZER_NOTE_REST,
- BUZZER_NOTE_A4,
- BUZZER_NOTE_E5,
- BUZZER_NOTE_REST,
- BUZZER_NOTE_A4,
- BUZZER_NOTE_F5,
- BUZZER_NOTE_G5,
- BUZZER_NOTE_E5,
- BUZZER_NOTE_REST,
- BUZZER_NOTE_A4,
- BUZZER_NOTE_G5,
- BUZZER_NOTE_F5,
- BUZZER_NOTE_E5,
- BUZZER_NOTE_D5,
- BUZZER_NOTE_E5,
- BUZZER_NOTE_REST,
-
- BUZZER_NOTE_A5,
- BUZZER_NOTE_REST,
- BUZZER_NOTE_A5,
- BUZZER_NOTE_A5SHARP_B5FLAT,
- BUZZER_NOTE_G5,
- BUZZER_NOTE_REST,
- BUZZER_NOTE_C5,
- BUZZER_NOTE_A5,
- BUZZER_NOTE_A5SHARP_B5FLAT,
- BUZZER_NOTE_G5,
- BUZZER_NOTE_REST,
- BUZZER_NOTE_D5,
- BUZZER_NOTE_A5SHARP_B5FLAT,
- BUZZER_NOTE_A5,
- BUZZER_NOTE_G5,
- BUZZER_NOTE_F5,
- BUZZER_NOTE_E5,
- };
- const uint16_t durations[] = {
- 200,
- 600,
- 100,
- 200,
- 600,
- 100,
- 200,
- 400,
- 400,
- 600,
- 100,
- 200,
- 400,
- 400,
- 400,
- 400,
- 800,
- 600,
-
- 200,
- 50,
- 400,
- 200,
- 400,
- 100,
- 200,
- 400,
- 400,
- 400,
- 200,
- 200,
- 400,
- 400,
- 400,
- 400,
- 900,
- };
- application_state.play = false;
- for(size_t i = 0; i < sizeof(rains); i++) {
- char buf[9] = {0};
- if (rains[i] == BUZZER_NOTE_REST) {
- printf("rest for %d ms\n", durations[i]);
- sprintf(buf, "%2drESt ", i);
- } else {
- printf("playing note %2d: %3.0f Hz for %d ms\n", i, 1000000.0 / (float)NotePeriods[rains[i]], durations[i]);
- sprintf(buf, "%2d%6d", i, NotePeriods[rains[i]]);
- }
- watch_display_string(buf, 2);
- watch_buzzer_play_note(rains[i], durations[i]);
- }
- printf("done!\n\n");
- }
-
- return true;
-}
+++ /dev/null
-TOP = ../../..
-include $(TOP)/make.mk
-
-INCLUDES += \
- -I../
-
-SRCS += \
- ../app.c
-
-include $(TOP)/rules.mk
+++ /dev/null
-#include <stdio.h>
-#include <string.h>
-#include <peripheral_clk_config.h>
-#include "watch.h"
-
-// NOTE: as of this writing (10/25/21) there is no SPI controller functionality in the watch library.
-// this is a very basic app to confirm that SPI is working, tested with board OSO-MISC-21-001 and a GD25Q16C Flash chip.
-// The updated SPI Flash sensor board design is OSO-MISC-21-017 (it's easier to build, 0603 passives instead of 0402's).
-
-struct io_descriptor *io;
-struct spi_m_sync_descriptor SPI_0;
-
-void app_init() {
- // SPI_0_CLOCK_init
- hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM3_GCLK_ID_CORE, CONF_GCLK_SERCOM3_CORE_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
- hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM3_GCLK_ID_SLOW, CONF_GCLK_SERCOM3_SLOW_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
- hri_mclk_set_APBCMASK_SERCOM3_bit(MCLK);
-
- spi_m_sync_init(&SPI_0, SERCOM3);
-
- // SPI_0_PORT_init
- gpio_set_pin_level(A3, true);
- gpio_set_pin_direction(A3, GPIO_DIRECTION_OUT);
- gpio_set_pin_function(A3, GPIO_PIN_FUNCTION_OFF);
-
- gpio_set_pin_level(A2, false);
- gpio_set_pin_direction(A2, GPIO_DIRECTION_OUT);
- gpio_set_pin_function(A2, PINMUX_PB02C_SERCOM3_PAD0);
-
- gpio_set_pin_direction(A4, GPIO_DIRECTION_IN);
- gpio_set_pin_pull_mode(A4, GPIO_PULL_OFF);
- gpio_set_pin_function(A4, PINMUX_PB00C_SERCOM3_PAD2);
-
- gpio_set_pin_level(A1, false);
- gpio_set_pin_direction(A1, GPIO_DIRECTION_OUT);
- gpio_set_pin_function(A1, PINMUX_PB01C_SERCOM3_PAD3);
-
- spi_m_sync_get_io_descriptor(&SPI_0, &io);
- spi_m_sync_enable(&SPI_0);
-}
-
-void app_wake_from_backup() {
-}
-
-void app_setup() {
-}
-
-void app_prepare_for_standby() {
-}
-
-void app_wake_from_standby() {
-}
-
-static uint8_t get_id_command[4] = {0x9F};
-
-bool app_loop() {
- watch_set_pin_level(A3, false);
- io_write(io, get_id_command, 1);
- uint8_t buf[3] = {0};
-
- // should print 0, 0, 0
- printf("blank: %x, %x, %x\n", buf[0], buf[1], buf[2]);
- io_read(io, buf, 3);
- watch_set_pin_level(A3, true);
- // should print c8, 40, 15
- printf("ident: %x, %x, %x\n", buf[0], buf[1], buf[2]);
-
- delay_ms(100);
-
- return false;
-}
+++ /dev/null
-TOP = ../../..
-include $(TOP)/make.mk
-
-INCLUDES += \
- -I../
-
-SRCS += \
- ../app.c
-
-include $(TOP)/rules.mk
+++ /dev/null
-#include <stdio.h>
-#include <string.h>
-#include "watch.h"
-
-//////////////////////////////////////////////////////////////////////////////////////////
-// This section sets up types and storage for our application state.
-// You can tear this out and replace it with whatever you want.
-typedef enum ApplicationMode {
- MODE_HELLO = 0,
- MODE_THERE
-} ApplicationMode;
-
-typedef enum LightColor {
- COLOR_RED = 0,
- COLOR_GREEN,
- COLOR_YELLOW
-} LightColor;
-
-typedef struct ApplicationState {
- ApplicationMode mode;
- LightColor color;
- bool light_on;
- bool beep;
- uint8_t wake_count;
- bool enter_sleep_mode;
-} ApplicationState;
-
-ApplicationState application_state;
-
-
-//////////////////////////////////////////////////////////////////////////////////////////
-// This section defines the callbacks for our button press events (implemented at bottom).
-// Add any other callbacks you may need either here or in another file.
-void cb_light_pressed();
-void cb_mode_pressed();
-void cb_alarm_pressed();
-
-
-//////////////////////////////////////////////////////////////////////////////////////////
-// This section contains the required functions for any watch app. You should tear out
-// all the code in these functions when writing your app, but you must implement all
-// of the functions, even if they are empty stubs. You can also replace the documentation
-// lines with documentation that describes what your functions do!
-
-/**
- * @brief the app_init function is called before anything else. Use it to set up any
- * internal data structures or application state required by your app.
- */
-void app_init() {
- memset(&application_state, 0, sizeof(application_state));
-}
-
-/**
- * @brief the app_wake_from_backup function is only called if your app is waking from
- * the ultra-low power BACKUP sleep mode. You may have chosen to store some state in the
- * RTC's backup registers prior to entering this mode. You may restore that state here.
- *
- * @see watch_enter_deep_sleep()
- */
-void app_wake_from_backup() {
- // This app does not support BACKUP mode.
-}
-
-/**
- * @brief the app_setup function is like setup() in Arduino. It is called once when the
- * program begins. You should set pin modes and enable any peripherals you want to
- * set up (real-time clock, I2C, etc.) Depending on your application, you may or may not
- * want to configure sensors on your sensor board here. For example, a low-power
- * accelerometer that will run at all times should be configured here, whereas you may
- * want to enable a more power-hungry environmental sensor only when you need it.
- *
- * @note If your app enters the Sleep or Deep Sleep modes, this function will be called
- * again on wake, since those modes will have disabled all pins and peripherals; you'll
- * likely need to set them up again. This function will also be called again if your app
- * entered the ultra-low power BACKUP mode, since BACKUP mode will have done all that and
- * also wiped out the system RAM. Note that when this is called after waking from sleep,
- * the RTC will still be configured with the correct date and time.
- */
-void app_setup() {
- watch_enable_leds();
- watch_enable_buzzer();
-
- watch_enable_external_interrupts();
- // This starter app demonstrates three different ways of using the button interrupts.
- // The BTN_MODE interrupt only triggers on a rising edge, so the mode changes once per press.
- watch_register_interrupt_callback(BTN_MODE, cb_mode_pressed, INTERRUPT_TRIGGER_RISING);
- // The BTN_LIGHT interrupt triggers on both rising and falling edges. The callback then checks
- // the pin state when triggered: on a button down event, it increments the color and turns the
- // LED on, whereas on a button up event, it turns the light off.
- watch_register_interrupt_callback(BTN_LIGHT, cb_light_pressed, INTERRUPT_TRIGGER_BOTH);
- // The BTN_ALARM callback is on an external wake pin; we can avoid using the EIC for this pin
- // by using the extwake interrupt — but note that it can only trigger on either a rising or
- // a falling edge, not both.
- watch_register_extwake_callback(BTN_ALARM, cb_alarm_pressed, true);
-
- watch_enable_display();
-}
-
-/**
- * @brief the app_prepare_for_standby function is called before the watch goes into STANDBY mode.
- * In STANDBY mode, most peripherals are shut down, and no code will run until the watch receives
- * an interrupt (generally either the 1Hz tick or a press on one of the buttons).
- */
-void app_prepare_for_standby() {
-}
-
-/**
- * @brief the app_wake_from_standby function is called after the watch wakes from STANDBY mode,
- * but before your main app_loop.
- */
-void app_wake_from_standby() {
- application_state.wake_count++;
-}
-
-/**
- * @brief the app_loop function is called once on app startup and then again each time the
- * watch exits STANDBY mode.
- */
-bool app_loop() {
- if (application_state.beep) {
- watch_buzzer_play_note(BUZZER_NOTE_C7, 50);
- application_state.beep = false;
- }
-
- // set the LED to a color
- if (application_state.light_on) {
- switch (application_state.color) {
- case COLOR_RED:
- watch_set_led_red();
- break;
- case COLOR_GREEN:
- watch_set_led_green();
- break;
- case COLOR_YELLOW:
- watch_set_led_yellow();
- break;
- }
- } else {
- watch_set_led_off();
- }
-
- // Display the number of times we've woken up (modulo 32 to fit in 2 digits at top right)
- char buf[3] = {0};
- sprintf(buf, "%2d", application_state.wake_count % 32);
- watch_display_string(buf, 2);
-
- // display "Hello there" text
- switch (application_state.mode) {
- case MODE_HELLO:
- watch_display_string("Hello", 5);
- break;
- case MODE_THERE:
- watch_display_string("there", 5);
- break;
- }
-
- if (application_state.enter_sleep_mode) {
- // wait a moment for the user's finger to be off the button
- delay_ms(250);
-
- // nap time :)
- watch_enter_deep_sleep_mode();
-
- // we just woke up; wait a moment again for the user's finger to be off the button...
- delay_ms(250);
-
- // and prevent ourselves from going right back to sleep.
- application_state.enter_sleep_mode = false;
-
- // finally, after sleep, return false so that our app loop runs again and updates the display.
- return false;
- }
-
- return true;
-}
-
-
-//////////////////////////////////////////////////////////////////////////////////////////
-// Implementations for our callback functions. Replace these with whatever functionality
-// your app requires.
-void cb_light_pressed() {
- // always turn the light off when the pin goes low
- if (watch_get_pin_level(BTN_LIGHT) == 0) {
- application_state.light_on = false;
- return;
- }
- application_state.color = (application_state.color + 1) % 3;
- application_state.light_on = true;
-}
-
-void cb_mode_pressed() {
- application_state.mode = (application_state.mode + 1) % 2;
- application_state.beep = true;
-}
-
-void cb_alarm_pressed() {
- application_state.enter_sleep_mode = true;
-}
+++ /dev/null
-# Leave these lines at the top of the file.
-# TOP should get us to the root of the project...
-TOP = ../../..
-# ...and make.mk has all the watch library sources and includes.
-include $(TOP)/make.mk
-
-# If you add any other subdirectories with header files you wish to include, add them after ../
-# Note that you will need to add a backslash at the end of any line you wish to continue, i.e.
-# INCLUDES += \
-# -I../ \
-# -I../drivers/ \
-# -I../utils/
-INCLUDES += \
- -I../ \
-
-# If you add any other source files you wish to compile, add them after ../app.c
-# Note that you will need to add a backslash at the end of any line you wish to continue, i.e.
-# SRCS += \
-# ../app.c \
-# ../drivers/bmp280.c \
-# ../utils/temperature.c
-SRCS += \
- ../app.c \
-
-# Leave this line at the bottom of the file; rules.mk has all the targets for making your project.
-include $(TOP)/rules.mk
--- /dev/null
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <math.h>
+#include "watch.h"
+#include "lis2dh.h"
+
+// This application displays data from the old Sensor Watch Motion sensor board.
+// Note that this board required A0 to be set high to power the sensor.
+// Future accelerometer boards will be powered directly from VCC.
+// Also note that this board has its INT1 pin wired to A1, which is not an external
+// wake pin. Future accelerometer boards will wire interrupt pins to A2 and A4.
+
+void cb_light_pressed() {
+}
+
+void cb_mode_pressed() {
+}
+
+void cb_alarm_pressed() {
+}
+
+uint8_t interrupts = 0;
+uint8_t last_interrupts = 0;
+uint8_t ticks = 0;
+char buf[13] = {0};
+
+void cb_interrupt_1() {
+ interrupts++;
+}
+
+void cb_tick() {
+ if (++ticks == 30) {
+ last_interrupts = interrupts;
+ interrupts = 0;
+ ticks = 0;
+ }
+}
+
+void app_init() {
+ gpio_set_pin_direction(A0, GPIO_DIRECTION_OUT);
+ gpio_set_pin_function(A0, GPIO_PIN_FUNCTION_OFF);
+ gpio_set_pin_level(A0, true);
+
+ watch_enable_display();
+ watch_display_string("IN 0 0 0", 0);
+
+ watch_enable_external_interrupts();
+ watch_register_interrupt_callback(BTN_MODE, cb_mode_pressed, INTERRUPT_TRIGGER_RISING);
+ watch_register_interrupt_callback(BTN_LIGHT, cb_light_pressed, INTERRUPT_TRIGGER_RISING);
+ watch_register_interrupt_callback(BTN_ALARM, cb_alarm_pressed, INTERRUPT_TRIGGER_RISING);
+
+ watch_enable_i2c();
+
+ lis2dh_begin();
+ lis2dh_set_data_rate(LIS2DH_DATA_RATE_10_HZ);
+ lis2dh_configure_aoi_int1(
+ LIS2DH_INTERRUPT_CONFIGURATION_OR |
+ LIS2DH_INTERRUPT_CONFIGURATION_X_HIGH_ENABLE |
+ LIS2DH_INTERRUPT_CONFIGURATION_Y_HIGH_ENABLE |
+ LIS2DH_INTERRUPT_CONFIGURATION_Z_HIGH_ENABLE, 96, 0, true);
+
+ watch_register_interrupt_callback(A1, cb_interrupt_1, INTERRUPT_TRIGGER_RISING);
+ watch_rtc_register_tick_callback(cb_tick);
+}
+
+void app_wake_from_backup() {
+}
+
+void app_setup() {
+}
+
+void app_prepare_for_standby() {
+}
+
+void app_wake_from_standby() {
+}
+
+bool app_loop() {
+ sprintf(buf, "IN%2d%3d%3d", ticks, interrupts, last_interrupts);
+ watch_display_string(buf, 0);
+
+ return true;
+}
--- /dev/null
+TOP = ../../..
+include $(TOP)/make.mk
+
+INCLUDES += \
+ -I../
+
+SRCS += \
+ ../app.c
+
+include $(TOP)/rules.mk
--- /dev/null
+#include <stdio.h>
+#include <string.h>
+#include "watch.h"
+
+typedef struct ApplicationState {
+ bool play;
+} ApplicationState;
+
+ApplicationState application_state;
+
+
+void cb_alarm_pressed() {
+ application_state.play = true;
+}
+
+void app_init() {
+ memset(&application_state, 0, sizeof(application_state));
+}
+
+void app_wake_from_backup() {
+}
+
+void app_setup() {
+ watch_register_extwake_callback(BTN_ALARM, cb_alarm_pressed, true);
+
+ watch_enable_display();
+
+ watch_enable_buzzer();
+}
+
+void app_prepare_for_standby() {
+ watch_display_string(" rains ", 2);
+}
+
+void app_wake_from_standby() {
+}
+
+bool app_loop() {
+ if (application_state.play) {
+ printf("Playing song...\n");
+ const BuzzerNote rains[] = {
+ BUZZER_NOTE_A4,
+ BUZZER_NOTE_F5,
+ BUZZER_NOTE_REST,
+ BUZZER_NOTE_A4,
+ BUZZER_NOTE_E5,
+ BUZZER_NOTE_REST,
+ BUZZER_NOTE_A4,
+ BUZZER_NOTE_F5,
+ BUZZER_NOTE_G5,
+ BUZZER_NOTE_E5,
+ BUZZER_NOTE_REST,
+ BUZZER_NOTE_A4,
+ BUZZER_NOTE_G5,
+ BUZZER_NOTE_F5,
+ BUZZER_NOTE_E5,
+ BUZZER_NOTE_D5,
+ BUZZER_NOTE_E5,
+ BUZZER_NOTE_REST,
+
+ BUZZER_NOTE_A5,
+ BUZZER_NOTE_REST,
+ BUZZER_NOTE_A5,
+ BUZZER_NOTE_A5SHARP_B5FLAT,
+ BUZZER_NOTE_G5,
+ BUZZER_NOTE_REST,
+ BUZZER_NOTE_C5,
+ BUZZER_NOTE_A5,
+ BUZZER_NOTE_A5SHARP_B5FLAT,
+ BUZZER_NOTE_G5,
+ BUZZER_NOTE_REST,
+ BUZZER_NOTE_D5,
+ BUZZER_NOTE_A5SHARP_B5FLAT,
+ BUZZER_NOTE_A5,
+ BUZZER_NOTE_G5,
+ BUZZER_NOTE_F5,
+ BUZZER_NOTE_E5,
+ };
+ const uint16_t durations[] = {
+ 200,
+ 600,
+ 100,
+ 200,
+ 600,
+ 100,
+ 200,
+ 400,
+ 400,
+ 600,
+ 100,
+ 200,
+ 400,
+ 400,
+ 400,
+ 400,
+ 800,
+ 600,
+
+ 200,
+ 50,
+ 400,
+ 200,
+ 400,
+ 100,
+ 200,
+ 400,
+ 400,
+ 400,
+ 200,
+ 200,
+ 400,
+ 400,
+ 400,
+ 400,
+ 900,
+ };
+ application_state.play = false;
+ for(size_t i = 0; i < sizeof(rains); i++) {
+ char buf[9] = {0};
+ if (rains[i] == BUZZER_NOTE_REST) {
+ printf("rest for %d ms\n", durations[i]);
+ sprintf(buf, "%2drESt ", i);
+ } else {
+ printf("playing note %2d: %3.0f Hz for %d ms\n", i, 1000000.0 / (float)NotePeriods[rains[i]], durations[i]);
+ sprintf(buf, "%2d%6d", i, NotePeriods[rains[i]]);
+ }
+ watch_display_string(buf, 2);
+ watch_buzzer_play_note(rains[i], durations[i]);
+ }
+ printf("done!\n\n");
+ }
+
+ return true;
+}
--- /dev/null
+TOP = ../../..
+include $(TOP)/make.mk
+
+INCLUDES += \
+ -I../
+
+SRCS += \
+ ../app.c
+
+include $(TOP)/rules.mk
--- /dev/null
+#include <stdio.h>
+#include <string.h>
+#include <peripheral_clk_config.h>
+#include "watch.h"
+
+// NOTE: as of this writing (10/25/21) there is no SPI controller functionality in the watch library.
+// this is a very basic app to confirm that SPI is working, tested with board OSO-MISC-21-001 and a GD25Q16C Flash chip.
+// The updated SPI Flash sensor board design is OSO-MISC-21-017 (it's easier to build, 0603 passives instead of 0402's).
+
+struct io_descriptor *io;
+struct spi_m_sync_descriptor SPI_0;
+
+void app_init() {
+ // SPI_0_CLOCK_init
+ hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM3_GCLK_ID_CORE, CONF_GCLK_SERCOM3_CORE_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
+ hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM3_GCLK_ID_SLOW, CONF_GCLK_SERCOM3_SLOW_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
+ hri_mclk_set_APBCMASK_SERCOM3_bit(MCLK);
+
+ spi_m_sync_init(&SPI_0, SERCOM3);
+
+ // SPI_0_PORT_init
+ gpio_set_pin_level(A3, true);
+ gpio_set_pin_direction(A3, GPIO_DIRECTION_OUT);
+ gpio_set_pin_function(A3, GPIO_PIN_FUNCTION_OFF);
+
+ gpio_set_pin_level(A2, false);
+ gpio_set_pin_direction(A2, GPIO_DIRECTION_OUT);
+ gpio_set_pin_function(A2, PINMUX_PB02C_SERCOM3_PAD0);
+
+ gpio_set_pin_direction(A4, GPIO_DIRECTION_IN);
+ gpio_set_pin_pull_mode(A4, GPIO_PULL_OFF);
+ gpio_set_pin_function(A4, PINMUX_PB00C_SERCOM3_PAD2);
+
+ gpio_set_pin_level(A1, false);
+ gpio_set_pin_direction(A1, GPIO_DIRECTION_OUT);
+ gpio_set_pin_function(A1, PINMUX_PB01C_SERCOM3_PAD3);
+
+ spi_m_sync_get_io_descriptor(&SPI_0, &io);
+ spi_m_sync_enable(&SPI_0);
+}
+
+void app_wake_from_backup() {
+}
+
+void app_setup() {
+}
+
+void app_prepare_for_standby() {
+}
+
+void app_wake_from_standby() {
+}
+
+static uint8_t get_id_command[4] = {0x9F};
+
+bool app_loop() {
+ watch_set_pin_level(A3, false);
+ io_write(io, get_id_command, 1);
+ uint8_t buf[3] = {0};
+
+ // should print 0, 0, 0
+ printf("blank: %x, %x, %x\n", buf[0], buf[1], buf[2]);
+ io_read(io, buf, 3);
+ watch_set_pin_level(A3, true);
+ // should print c8, 40, 15
+ printf("ident: %x, %x, %x\n", buf[0], buf[1], buf[2]);
+
+ delay_ms(100);
+
+ return false;
+}
--- /dev/null
+TOP = ../../..
+include $(TOP)/make.mk
+
+INCLUDES += \
+ -I../
+
+SRCS += \
+ ../app.c
+
+include $(TOP)/rules.mk
--- /dev/null
+#include <stdio.h>
+#include <string.h>
+#include "watch.h"
+
+//////////////////////////////////////////////////////////////////////////////////////////
+// This section sets up types and storage for our application state.
+// You can tear this out and replace it with whatever you want.
+typedef enum ApplicationMode {
+ MODE_HELLO = 0,
+ MODE_THERE
+} ApplicationMode;
+
+typedef enum LightColor {
+ COLOR_RED = 0,
+ COLOR_GREEN,
+ COLOR_YELLOW
+} LightColor;
+
+typedef struct ApplicationState {
+ ApplicationMode mode;
+ LightColor color;
+ bool light_on;
+ bool beep;
+ uint8_t wake_count;
+ bool enter_sleep_mode;
+} ApplicationState;
+
+ApplicationState application_state;
+
+
+//////////////////////////////////////////////////////////////////////////////////////////
+// This section defines the callbacks for our button press events (implemented at bottom).
+// Add any other callbacks you may need either here or in another file.
+void cb_light_pressed();
+void cb_mode_pressed();
+void cb_alarm_pressed();
+
+
+//////////////////////////////////////////////////////////////////////////////////////////
+// This section contains the required functions for any watch app. You should tear out
+// all the code in these functions when writing your app, but you must implement all
+// of the functions, even if they are empty stubs. You can also replace the documentation
+// lines with documentation that describes what your functions do!
+
+/**
+ * @brief the app_init function is called before anything else. Use it to set up any
+ * internal data structures or application state required by your app.
+ */
+void app_init() {
+ memset(&application_state, 0, sizeof(application_state));
+}
+
+/**
+ * @brief the app_wake_from_backup function is only called if your app is waking from
+ * the ultra-low power BACKUP sleep mode. You may have chosen to store some state in the
+ * RTC's backup registers prior to entering this mode. You may restore that state here.
+ *
+ * @see watch_enter_deep_sleep()
+ */
+void app_wake_from_backup() {
+ // This app does not support BACKUP mode.
+}
+
+/**
+ * @brief the app_setup function is like setup() in Arduino. It is called once when the
+ * program begins. You should set pin modes and enable any peripherals you want to
+ * set up (real-time clock, I2C, etc.) Depending on your application, you may or may not
+ * want to configure sensors on your sensor board here. For example, a low-power
+ * accelerometer that will run at all times should be configured here, whereas you may
+ * want to enable a more power-hungry environmental sensor only when you need it.
+ *
+ * @note If your app enters the Sleep or Deep Sleep modes, this function will be called
+ * again on wake, since those modes will have disabled all pins and peripherals; you'll
+ * likely need to set them up again. This function will also be called again if your app
+ * entered the ultra-low power BACKUP mode, since BACKUP mode will have done all that and
+ * also wiped out the system RAM. Note that when this is called after waking from sleep,
+ * the RTC will still be configured with the correct date and time.
+ */
+void app_setup() {
+ watch_enable_leds();
+ watch_enable_buzzer();
+
+ watch_enable_external_interrupts();
+ // This starter app demonstrates three different ways of using the button interrupts.
+ // The BTN_MODE interrupt only triggers on a rising edge, so the mode changes once per press.
+ watch_register_interrupt_callback(BTN_MODE, cb_mode_pressed, INTERRUPT_TRIGGER_RISING);
+ // The BTN_LIGHT interrupt triggers on both rising and falling edges. The callback then checks
+ // the pin state when triggered: on a button down event, it increments the color and turns the
+ // LED on, whereas on a button up event, it turns the light off.
+ watch_register_interrupt_callback(BTN_LIGHT, cb_light_pressed, INTERRUPT_TRIGGER_BOTH);
+ // The BTN_ALARM callback is on an external wake pin; we can avoid using the EIC for this pin
+ // by using the extwake interrupt — but note that it can only trigger on either a rising or
+ // a falling edge, not both.
+ watch_register_extwake_callback(BTN_ALARM, cb_alarm_pressed, true);
+
+ watch_enable_display();
+}
+
+/**
+ * @brief the app_prepare_for_standby function is called before the watch goes into STANDBY mode.
+ * In STANDBY mode, most peripherals are shut down, and no code will run until the watch receives
+ * an interrupt (generally either the 1Hz tick or a press on one of the buttons).
+ */
+void app_prepare_for_standby() {
+}
+
+/**
+ * @brief the app_wake_from_standby function is called after the watch wakes from STANDBY mode,
+ * but before your main app_loop.
+ */
+void app_wake_from_standby() {
+ application_state.wake_count++;
+}
+
+/**
+ * @brief the app_loop function is called once on app startup and then again each time the
+ * watch exits STANDBY mode.
+ */
+bool app_loop() {
+ if (application_state.beep) {
+ watch_buzzer_play_note(BUZZER_NOTE_C7, 50);
+ application_state.beep = false;
+ }
+
+ // set the LED to a color
+ if (application_state.light_on) {
+ switch (application_state.color) {
+ case COLOR_RED:
+ watch_set_led_red();
+ break;
+ case COLOR_GREEN:
+ watch_set_led_green();
+ break;
+ case COLOR_YELLOW:
+ watch_set_led_yellow();
+ break;
+ }
+ } else {
+ watch_set_led_off();
+ }
+
+ // Display the number of times we've woken up (modulo 32 to fit in 2 digits at top right)
+ char buf[3] = {0};
+ sprintf(buf, "%2d", application_state.wake_count % 32);
+ watch_display_string(buf, 2);
+
+ // display "Hello there" text
+ switch (application_state.mode) {
+ case MODE_HELLO:
+ watch_display_string("Hello", 5);
+ break;
+ case MODE_THERE:
+ watch_display_string("there", 5);
+ break;
+ }
+
+ if (application_state.enter_sleep_mode) {
+ // wait a moment for the user's finger to be off the button
+ delay_ms(250);
+
+ // nap time :)
+ watch_enter_deep_sleep_mode();
+
+ // we just woke up; wait a moment again for the user's finger to be off the button...
+ delay_ms(250);
+
+ // and prevent ourselves from going right back to sleep.
+ application_state.enter_sleep_mode = false;
+
+ // finally, after sleep, return false so that our app loop runs again and updates the display.
+ return false;
+ }
+
+ return true;
+}
+
+
+//////////////////////////////////////////////////////////////////////////////////////////
+// Implementations for our callback functions. Replace these with whatever functionality
+// your app requires.
+void cb_light_pressed() {
+ // always turn the light off when the pin goes low
+ if (watch_get_pin_level(BTN_LIGHT) == 0) {
+ application_state.light_on = false;
+ return;
+ }
+ application_state.color = (application_state.color + 1) % 3;
+ application_state.light_on = true;
+}
+
+void cb_mode_pressed() {
+ application_state.mode = (application_state.mode + 1) % 2;
+ application_state.beep = true;
+}
+
+void cb_alarm_pressed() {
+ application_state.enter_sleep_mode = true;
+}
--- /dev/null
+# Leave these lines at the top of the file.
+# TOP should get us to the root of the project...
+TOP = ../../..
+# ...and make.mk has all the watch library sources and includes.
+include $(TOP)/make.mk
+
+# If you add any other subdirectories with header files you wish to include, add them after ../
+# Note that you will need to add a backslash at the end of any line you wish to continue, i.e.
+# INCLUDES += \
+# -I../ \
+# -I../drivers/ \
+# -I../utils/
+INCLUDES += \
+ -I../ \
+
+# If you add any other source files you wish to compile, add them after ../app.c
+# Note that you will need to add a backslash at the end of any line you wish to continue, i.e.
+# SRCS += \
+# ../app.c \
+# ../drivers/bmp280.c \
+# ../utils/temperature.c
+SRCS += \
+ ../app.c \
+
+# Leave this line at the bottom of the file; rules.mk has all the targets for making your project.
+include $(TOP)/rules.mk