summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
Diffstat (limited to 'apps')
-rw-r--r--apps/Sensor Watch BME280 Project/app.c70
-rw-r--r--apps/Sensor Watch Buzzer Demo/app.c6
-rw-r--r--apps/Sensor Watch Starter Project/app.c75
-rw-r--r--apps/beats-time/app.c282
-rwxr-xr-xapps/beats-time/make/.gitignore1
-rwxr-xr-xapps/beats-time/make/Makefile24
6 files changed, 378 insertions, 80 deletions
diff --git a/apps/Sensor Watch BME280 Project/app.c b/apps/Sensor Watch BME280 Project/app.c
index e07b35f8..85439d9b 100644
--- a/apps/Sensor Watch BME280 Project/app.c
+++ b/apps/Sensor Watch BME280 Project/app.c
@@ -16,18 +16,11 @@ void app_init() {
memset(&application_state, 0, sizeof(application_state));
}
-void app_wake_from_deep_sleep() {
- // This app does not support deep sleep mode.
+void app_wake_from_backup() {
+ // This app does not support BACKUP mode.
}
void app_setup() {
- struct calendar_date_time date_time;
- watch_get_date_time(&date_time);
- if (date_time.date.year < 2020) {
- date_time.date.year = 2020;
- watch_set_date_time(date_time);
- }
-
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);
@@ -64,19 +57,19 @@ void app_setup() {
watch_enable_display();
- watch_register_tick_callback(cb_tick);
+ watch_rtc_register_tick_callback(cb_tick);
}
/**
* Nothing to do here.
*/
-void app_prepare_for_sleep() {
+void app_prepare_for_standby() {
}
/**
* @todo restore the BME280's calibration values from backup memory
*/
-void app_wake_from_sleep() {
+void app_wake_from_standby() {
}
/**
@@ -191,9 +184,8 @@ float read_humidity(int32_t t_fine) {
}
void log_data() {
- struct calendar_date_time date_time;
- watch_get_date_time(&date_time);
- uint8_t hour = date_time.time.hour;
+ 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++) {
@@ -205,12 +197,11 @@ void log_data() {
}
void do_clock_mode() {
- struct calendar_date_time date_time;
+ 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_get_date_time(&date_time);
- watch_display_string((char *)months[date_time.date.month - 1], 0);
- sprintf(buf, "%2d%2d%02d%02d", date_time.date.day, date_time.time.hour, date_time.time.min, date_time.time.sec);
+ 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();
}
@@ -271,28 +262,27 @@ void prefs_mode_handle_secondary_button() {
}
void do_set_time_mode() {
- struct calendar_date_time date_time;
+ watch_date_time date_time = watch_rtc_get_date_time();
- watch_get_date_time(&date_time);
watch_display_string(" ", 0);
switch (application_state.page) {
case 0: // hour
- sprintf(buf, "ST t%2d", date_time.time.hour);
+ sprintf(buf, "ST t%2d", date_time.unit.hour);
break;
case 1: // minute
- sprintf(buf, "ST t %02d", date_time.time.min);
+ sprintf(buf, "ST t %02d", date_time.unit.minute);
break;
case 2: // second
- sprintf(buf, "ST t %02d", date_time.time.sec);
+ sprintf(buf, "ST t %02d", date_time.unit.second);
break;
case 3: // year
- sprintf(buf, "ST d%2d", date_time.date.year - 2000);
+ sprintf(buf, "ST d%2d", date_time.unit.year + 20);
break;
case 4: // month
- sprintf(buf, "ST d %02d", date_time.date.month);
+ sprintf(buf, "ST d %02d", date_time.unit.month);
break;
case 5: // day
- sprintf(buf, "ST d %02d", date_time.date.day);
+ sprintf(buf, "ST d %02d", date_time.unit.day);
break;
}
watch_display_string(buf, 0);
@@ -305,37 +295,36 @@ void set_time_mode_handle_primary_button() {
}
void set_time_mode_handle_secondary_button() {
- struct calendar_date_time date_time;
- watch_get_date_time(&date_time);
+ 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.time.hour = (date_time.time.hour + 1) % 24;
+ date_time.unit.hour = (date_time.unit.hour + 1) % 24;
break;
case 1: // minute
- date_time.time.min = (date_time.time.min + 1) % 60;
+ date_time.unit.minute = (date_time.unit.minute + 1) % 60;
break;
case 2: // second
- date_time.time.sec = 0;
+ date_time.unit.second = 0;
break;
case 3: // year
// only allow 2021-2030. fix this sometime next decade
- date_time.date.year = ((date_time.date.year % 10) + 1) + 2020;
+ date_time.unit.year = ((date_time.unit.year % 10) + 1);
break;
case 4: // month
- date_time.date.month = ((date_time.date.month + 1) % 12);
+ date_time.unit.month = ((date_time.unit.month + 1) % 12);
break;
case 5: // day
- date_time.date.day = date_time.date.day + 1;
+ 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.date.day > days_in_month[date_time.date.month - 1]) {
- date_time.date.day = 1;
+ if (date_time.unit.day > days_in_month[date_time.unit.month - 1]) {
+ date_time.unit.day = 1;
}
break;
}
- watch_set_date_time(date_time);
+ watch_rtc_set_date_time(date_time);
}
void cb_mode_pressed() {
@@ -377,9 +366,8 @@ void cb_alarm_pressed() {
void cb_tick() {
// TODO: use alarm interrupt to trigger data acquisition.
- struct calendar_date_time date_time;
- watch_get_date_time(&date_time);
- if (date_time.time.min == 0 && date_time.time.sec == 0) {
+ watch_date_time date_time = watch_rtc_get_date_time();
+ if (date_time.unit.minute == 0 && date_time.unit.second == 0) {
log_data();
}
diff --git a/apps/Sensor Watch Buzzer Demo/app.c b/apps/Sensor Watch Buzzer Demo/app.c
index 74777fcc..58158dff 100644
--- a/apps/Sensor Watch Buzzer Demo/app.c
+++ b/apps/Sensor Watch Buzzer Demo/app.c
@@ -17,7 +17,7 @@ void app_init() {
memset(&application_state, 0, sizeof(application_state));
}
-void app_wake_from_deep_sleep() {
+void app_wake_from_backup() {
}
void app_setup() {
@@ -28,11 +28,11 @@ void app_setup() {
watch_enable_buzzer();
}
-void app_prepare_for_sleep() {
+void app_prepare_for_standby() {
watch_display_string(" rains ", 2);
}
-void app_wake_from_sleep() {
+void app_wake_from_standby() {
}
bool app_loop() {
diff --git a/apps/Sensor Watch Starter Project/app.c b/apps/Sensor Watch Starter Project/app.c
index d2ddd6a6..fa887bae 100644
--- a/apps/Sensor Watch Starter Project/app.c
+++ b/apps/Sensor Watch Starter Project/app.c
@@ -20,8 +20,9 @@ typedef struct ApplicationState {
ApplicationMode mode;
LightColor color;
bool light_on;
+ bool beep;
uint8_t wake_count;
- bool enter_deep_sleep;
+ bool enter_sleep_mode;
} ApplicationState;
ApplicationState application_state;
@@ -50,20 +51,14 @@ void app_init() {
}
/**
- * @brief the app_wake_from_deep_sleep function is only called if your app is waking from
+ * @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_deep_sleep() {
- // retrieve our application state from the backup registers
- application_state.mode = (ApplicationMode)watch_get_backup_data(0);
- application_state.color = (LightColor)watch_get_backup_data(1);
- application_state.wake_count = (uint8_t)watch_get_backup_data(2) + 1;
-
- // wait a moment for the user's finger to be off the button
- delay_ms(250);
+void app_wake_from_backup() {
+ // This app does not support BACKUP mode.
}
/**
@@ -74,12 +69,16 @@ void app_wake_from_deep_sleep() {
* 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 ultra-low power BACKUP sleep mode, this function will
- * be called again when it wakes from that deep sleep state. In this state, the RTC will
- * still be configured with the correct date and time.
+ * @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.
@@ -98,27 +97,31 @@ void app_setup() {
}
/**
- * @brief the app_prepare_for_sleep function is called before the watch goes into the
- * STANDBY sleep 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).
+ * @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_sleep() {
+void app_prepare_for_standby() {
}
/**
- * @brief the app_wake_from_sleep function is called after the watch wakes from the
- * STANDBY sleep mode.
+ * @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_sleep() {
+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 STANDBY sleep mode.
+ * @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) {
@@ -151,22 +154,21 @@ bool app_loop() {
break;
}
- if (application_state.enter_deep_sleep) {
- application_state.enter_deep_sleep = false;
-
- // stash our application state in the backup registers
- watch_store_backup_data((uint32_t)application_state.mode, 0);
- watch_store_backup_data((uint32_t)application_state.color, 1);
- watch_store_backup_data((uint32_t)application_state.wake_count, 2);
-
- // turn off the LED
- watch_set_led_off();
-
+ 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(NULL);
+ 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;
@@ -188,8 +190,9 @@ void cb_light_pressed() {
void cb_mode_pressed() {
application_state.mode = (application_state.mode + 1) % 2;
+ application_state.beep = true;
}
void cb_alarm_pressed() {
- application_state.enter_deep_sleep = true;
+ application_state.enter_sleep_mode = true;
}
diff --git a/apps/beats-time/app.c b/apps/beats-time/app.c
new file mode 100644
index 00000000..5c68a3e8
--- /dev/null
+++ b/apps/beats-time/app.c
@@ -0,0 +1,282 @@
+#include <stdio.h>
+#include <string.h>
+#include <math.h>
+#include "watch.h"
+
+const uint8_t UTC_OFFSET = 4; // set to your current UTC offset to see correct beats time
+const uint8_t BEAT_REFRESH_FREQUENCY = 8;
+
+typedef enum ApplicationMode {
+ MODE_CLOCK = 0, // Displays month, day and current time.
+ MODE_BEATS,
+ MODE_SET, // (ST) Set time and date
+ NUM_MODES // Last item in the enum, it's the number of cases.
+} ApplicationMode;
+
+
+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.
+ uint8_t last_second; // lets us see when the second changed, for subsecond timing
+ uint8_t subsecond; // a value from 0 to (BEAT_REFRESH_FREQUENCY - 1) indicating the fractional second
+} ApplicationState;
+
+void do_clock_mode();
+void do_beats_mode();
+void do_set_time_mode();
+void set_time_mode_handle_primary_button();
+void set_time_mode_handle_secondary_button();
+
+float clock2beats(uint16_t, uint16_t, uint16_t, int16_t);
+
+void cb_light_pressed();
+void cb_mode_pressed();
+void cb_alarm_pressed();
+void cb_tick();
+void cb_fast_tick();
+
+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();
+ watch_enable_display();
+
+ watch_rtc_register_tick_callback(cb_tick);
+}
+
+void app_prepare_for_standby() {
+}
+
+void app_wake_from_standby() {
+}
+
+void update_tick_frequency() {
+ watch_rtc_disable_all_periodic_callbacks();
+ if (application_state.mode == MODE_BEATS) {
+ watch_rtc_register_periodic_callback(cb_fast_tick, BEAT_REFRESH_FREQUENCY);
+ } else {
+ watch_rtc_register_tick_callback(cb_tick);
+ }
+}
+
+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);
+ update_tick_frequency();
+ 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;
+ update_tick_frequency();
+ }
+
+ // 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_BEATS:
+ do_beats_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;
+}
+
+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_beats_mode() {
+ watch_clear_colon();
+
+ watch_date_time date_time = watch_rtc_get_date_time();
+ float beats = clock2beats(date_time.unit.hour, date_time.unit.minute, date_time.unit.second, UTC_OFFSET);
+ sprintf(buf, "bt %6.0f", beats * 100);
+
+ watch_display_string(buf, 0);
+}
+
+float clock2beats(uint16_t hours, uint16_t minutes, uint16_t seconds, int16_t utc_offset) {
+ float beats = seconds + ((float)application_state.subsecond / (float)BEAT_REFRESH_FREQUENCY);
+ beats += 60 * minutes;
+ beats += (float)hours * 60 * 60;
+ beats += (utc_offset + 1) * 60 * 60; // offset from utc + 1 since beats in in UTC+1
+
+ beats /= 86.4; // convert to beats
+ while(beats > 1000) beats -= 1000; // beats %= 1000 but for a float
+
+ return beats;
+}
+
+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_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_SET:
+ set_time_mode_handle_primary_button();
+ break;
+ default:
+ break;
+ }
+}
+
+void cb_tick() {
+ if (application_state.light_ticks > 0) {
+ application_state.light_ticks--;
+ }
+ if (application_state.mode_ticks > 0) {
+ application_state.mode_ticks--;
+ }
+}
+
+void cb_fast_tick() {
+ watch_date_time date_time = watch_rtc_get_date_time();
+ if (date_time.unit.second != application_state.last_second) {
+ application_state.last_second = date_time.unit.second;
+ application_state.subsecond = 0;
+ } else {
+ application_state.subsecond++;
+ }
+}
diff --git a/apps/beats-time/make/.gitignore b/apps/beats-time/make/.gitignore
new file mode 100755
index 00000000..3722ac63
--- /dev/null
+++ b/apps/beats-time/make/.gitignore
@@ -0,0 +1 @@
+build/
diff --git a/apps/beats-time/make/Makefile b/apps/beats-time/make/Makefile
new file mode 100755
index 00000000..112d8e50
--- /dev/null
+++ b/apps/beats-time/make/Makefile
@@ -0,0 +1,24 @@
+# Leave this line at the top of the file; it has all the watch library sources and includes.
+TOP = ../../..
+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; it has all the targets for making your project.
+include $(TOP)/rules.mk