Skip to content

Commit

Permalink
timers: Add alarm buzzer and vibration patterns when timers expire.
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkra committed Oct 13, 2024
1 parent 302e50a commit e05855d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/boards/native_posix.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion app/src/applications/timer/timer_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
}
Expand Down
47 changes: 45 additions & 2 deletions app/src/drivers/zsw_buzzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <zephyr/drivers/regulator.h>
#include <zephyr/logging/log.h>

#if CONFIG_ZSWATCH_PCB_REV >= 5
#if (CONFIG_ZSWATCH_PCB_REV >= 5 || CONFIG_BOARD_NATIVE_POSIX)

LOG_MODULE_REGISTER(zsw_buzzer, LOG_LEVEL_WRN);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions app/src/drivers/zsw_buzzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/*
Expand Down
25 changes: 25 additions & 0 deletions app/src/drivers/zsw_vibration_motor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions app/src/drivers/zsw_vibration_motor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/*
Expand Down

0 comments on commit e05855d

Please sign in to comment.