From e05855d7a8d8a325c9265c2c5fee855256f1f3cd Mon Sep 17 00:00:00 2001 From: Jakob Krantz Date: Sat, 12 Oct 2024 22:49:12 +0200 Subject: [PATCH] timers: Add alarm buzzer and vibration patterns when timers expire. --- app/boards/native_posix.conf | 2 +- app/src/applications/timer/timer_app.c | 4 ++- app/src/drivers/zsw_buzzer.c | 47 ++++++++++++++++++++++++-- app/src/drivers/zsw_buzzer.h | 1 + app/src/drivers/zsw_vibration_motor.c | 25 ++++++++++++++ app/src/drivers/zsw_vibration_motor.h | 1 + 6 files changed, 76 insertions(+), 4 deletions(-) diff --git a/app/boards/native_posix.conf b/app/boards/native_posix.conf index 95e2484d..41e28231 100644 --- a/app/boards/native_posix.conf +++ b/app/boards/native_posix.conf @@ -18,6 +18,7 @@ CONFIG_MAX30101_MULTI_LED_MODE=n CONFIG_APDS9306=n CONFIG_PINCTRL=n CONFIG_TEST_RANDOM_GENERATOR=y +CONFIG_REGULATOR=y CONFIG_GPIO=y CONFIG_INPUT=y @@ -33,7 +34,6 @@ CONFIG_SPI=n CONFIG_GC9A01=n CONFIG_INPUT_MODIFIED_CST816S=n CONFIG_INPUT_CST816S_INTERRUPT=n -CONFIG_REGULATOR=n CONFIG_REGULATOR_FIXED=n CONFIG_SDL_DISPLAY_DEFAULT_PIXEL_FORMAT_RGB_565=y diff --git a/app/src/applications/timer/timer_app.c b/app/src/applications/timer/timer_app.c index 43d8a041..47332165 100644 --- a/app/src/applications/timer/timer_app.c +++ b/app/src/applications/timer/timer_app.c @@ -9,6 +9,7 @@ #include "timer_ui.h" #include "zsw_alarm.h" #include "drivers/zsw_vibration_motor.h" +#include "drivers/zsw_buzzer.h" #include "events/zsw_periodic_event.h" #include "ui/popup/zsw_popup_window.h" #include "zsw_clock.h" @@ -83,7 +84,8 @@ static void alarm_triggered_cb(void *user_data) static char buf[50]; snprintf(buf, sizeof(buf), "Timer %d triggered", timer_id); - zsw_vibration_run_pattern(ZSW_VIBRATION_PATTERN_NOTIFICATION); + zsw_vibration_run_pattern(ZSW_VIBRATION_PATTERN_ALARM); + zsw_buzzer_run_melody(ZSW_BUZZER_PATTERN_ALARM); // TODO: Make a different popup for timer, and make it vibrate until timer is dismissed zsw_popup_show("Timer", buf, NULL, 10, false); } diff --git a/app/src/drivers/zsw_buzzer.c b/app/src/drivers/zsw_buzzer.c index 979cd6e8..b9156886 100644 --- a/app/src/drivers/zsw_buzzer.c +++ b/app/src/drivers/zsw_buzzer.c @@ -12,7 +12,7 @@ #include #include -#if CONFIG_ZSWATCH_PCB_REV >= 5 +#if (CONFIG_ZSWATCH_PCB_REV >= 5 || CONFIG_BOARD_NATIVE_POSIX) LOG_MODULE_REGISTER(zsw_buzzer, LOG_LEVEL_WRN); @@ -117,7 +117,46 @@ struct note_duration_t beep_song[] = { {.note = G5, .duration = 150}, }; -static const struct pwm_dt_spec buzzer_dt = PWM_DT_SPEC_GET(DT_ALIAS(buzzer_pwm)); +static struct note_duration_t alarm_melody[] = { + {.note = C6, .duration = eigth}, + {.note = REST, .duration = sixteenth}, + {.note = C6, .duration = eigth}, + {.note = REST, .duration = sixteenth}, + {.note = C6, .duration = eigth}, + {.note = REST, .duration = sixteenth}, + {.note = G5, .duration = eigth}, + {.note = REST, .duration = sixteenth}, + {.note = G5, .duration = eigth}, + {.note = REST, .duration = sixteenth}, + {.note = G5, .duration = eigth}, + {.note = REST, .duration = sixteenth}, + {.note = E5, .duration = eigth}, + {.note = REST, .duration = sixteenth}, + {.note = E5, .duration = eigth}, + {.note = REST, .duration = sixteenth}, + {.note = E5, .duration = eigth}, + {.note = REST, .duration = sixteenth}, + {.note = A5, .duration = eigth}, + {.note = REST, .duration = sixteenth}, + {.note = A5, .duration = eigth}, + {.note = REST, .duration = sixteenth}, + {.note = A5, .duration = eigth}, + {.note = REST, .duration = sixteenth}, + {.note = F5, .duration = eigth}, + {.note = REST, .duration = sixteenth}, + {.note = F5, .duration = eigth}, + {.note = REST, .duration = sixteenth}, + {.note = F5, .duration = eigth}, + {.note = REST, .duration = sixteenth}, + {.note = D5, .duration = eigth}, + {.note = REST, .duration = sixteenth}, + {.note = D5, .duration = eigth}, + {.note = REST, .duration = sixteenth}, + {.note = D5, .duration = eigth}, + {.note = REST, .duration = sixteenth}, +}; + +static const struct pwm_dt_spec buzzer_dt = PWM_DT_SPEC_GET_OR(DT_ALIAS(buzzer_pwm), {}); static const struct device *const reg_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(regulator_buzzer)); K_TIMER_DEFINE(buzzer_timer, pattern_timer_timeout, NULL); @@ -153,6 +192,10 @@ int zsw_buzzer_run_melody(zsw_buzzer_melody_t melody) active_pattern = mario_song; active_pattern_len = ARRAY_SIZE(mario_song); break; + case ZSW_BUZZER_PATTERN_ALARM: + active_pattern = alarm_melody; + active_pattern_len = ARRAY_SIZE(alarm_melody); + break; default: __ASSERT(false, "Invalid vibration pattern"); return -EINVAL; diff --git a/app/src/drivers/zsw_buzzer.h b/app/src/drivers/zsw_buzzer.h index 6cd06e60..78f0262a 100644 --- a/app/src/drivers/zsw_buzzer.h +++ b/app/src/drivers/zsw_buzzer.h @@ -22,6 +22,7 @@ typedef enum zsw_buzzer_melody_t { ZSW_BUZZER_PATTERN_BEEP, ZSW_BUZZER_PATTERN_MARIO, + ZSW_BUZZER_PATTERN_ALARM, } zsw_buzzer_melody_t; /* diff --git a/app/src/drivers/zsw_vibration_motor.c b/app/src/drivers/zsw_vibration_motor.c index c7c0bbc7..f20ff3db 100644 --- a/app/src/drivers/zsw_vibration_motor.c +++ b/app/src/drivers/zsw_vibration_motor.c @@ -54,6 +54,27 @@ static vib_motor_state_t not_pattern[] = { {.enabled = false, .percent = 0, .delay = 0}, }; +static vib_motor_state_t alarm_pattern[] = { + {.enabled = true, .percent = 100, .delay = 200}, + {.enabled = false, .percent = 0, .delay = 100}, + {.enabled = true, .percent = 100, .delay = 200}, + {.enabled = false, .percent = 0, .delay = 100}, + {.enabled = true, .percent = 100, .delay = 200}, + {.enabled = false, .percent = 0, .delay = 300}, + {.enabled = true, .percent = 100, .delay = 100}, + {.enabled = false, .percent = 0, .delay = 100}, + {.enabled = true, .percent = 100, .delay = 100}, + {.enabled = false, .percent = 0, .delay = 100}, + {.enabled = true, .percent = 100, .delay = 100}, + {.enabled = false, .percent = 0, .delay = 0}, + {.enabled = true, .percent = 100, .delay = 100}, + {.enabled = false, .percent = 0, .delay = 100}, + {.enabled = true, .percent = 100, .delay = 100}, + {.enabled = false, .percent = 0, .delay = 100}, + {.enabled = true, .percent = 100, .delay = 100}, + {.enabled = false, .percent = 0, .delay = 0}, +}; + static vib_motor_state_t *active_pattern; static uint8_t active_pattern_index; static uint8_t active_pattern_len; @@ -87,6 +108,10 @@ int zsw_vibration_run_pattern(zsw_vibration_pattern_t pattern) active_pattern = not_pattern; active_pattern_len = ARRAY_SIZE(not_pattern); break; + case ZSW_VIBRATION_PATTERN_ALARM: + active_pattern = alarm_pattern; + active_pattern_len = ARRAY_SIZE(alarm_pattern); + break; default: __ASSERT(false, "Invalid vibration pattern"); return -EINVAL; diff --git a/app/src/drivers/zsw_vibration_motor.h b/app/src/drivers/zsw_vibration_motor.h index 1af48f6f..ddfe4bda 100644 --- a/app/src/drivers/zsw_vibration_motor.h +++ b/app/src/drivers/zsw_vibration_motor.h @@ -22,6 +22,7 @@ typedef enum zsw_vibration_pattern { ZSW_VIBRATION_PATTERN_CLICK, ZSW_VIBRATION_PATTERN_NOTIFICATION, + ZSW_VIBRATION_PATTERN_ALARM, } zsw_vibration_pattern_t; /*