Skip to content

Commit 80ece1d

Browse files
authored
Merge pull request #135 from tmobile/tmo-CFSPDK-1162-Provide-Ability-to-Call-Back-at-the-End-of-a-Tone-Sequence
Implement tone player
2 parents 1865cb7 + 9ba5de2 commit 80ece1d

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed

samples/tmo_shell/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ target_sources(app PRIVATE src/tmo_bq24250.c)
3232
target_sources(app PRIVATE src/tmo_battery_ctrl.c)
3333
target_sources(app PRIVATE src/tmo_sntp.c)
3434
target_sources(app PRIVATE src/tmo_modem.c)
35+
target_sources(app PRIVATE src/tmo_tone_player.c)
3536
target_sources_ifdef(CONFIG_WIFI app PRIVATE src/tmo_wifi.c)
3637
target_sources_ifdef(CONFIG_BT_SMP app PRIVATE src/tmo_smp.c)
3738
target_sources_ifdef(CONFIG_BT_PERIPHERAL app PRIVATE src/tmo_ble_demo.c)

samples/tmo_shell/src/tmo_shell.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ typedef int sec_tag_t;
7474
#include "tmo_pm_sys.h"
7575
#endif
7676

77+
#include "tmo_tone_player.h"
78+
7779
const struct device *ext_flash_dev = NULL;
7880
const struct device *gecko_flash_dev = NULL;
7981

@@ -2463,6 +2465,19 @@ static int mountfs()
24632465
return rc;
24642466
}
24652467

2468+
int cmd_play_tone_file(const struct shell *shell, size_t argc, char **argv)
2469+
{
2470+
if (argc == 2) {
2471+
if (!play_tone_bin(argv[1], NULL)) {
2472+
shell_fprintf(shell, SHELL_VT100_COLOR_GREEN, "Now playing %s\n", argv[1]);
2473+
}
2474+
} else {
2475+
shell_error(shell, "Usage: tmo buzzer player <filename>");
2476+
return -EINVAL;
2477+
}
2478+
return 0;
2479+
}
2480+
24662481
int cmd_play_tone(const struct shell *shell, size_t argc, char **argv)
24672482
{
24682483
if (argc != 3) {
@@ -2548,6 +2563,7 @@ int cmd_tmo_buzzer_vol(const struct shell *shell, int argc, char**argv)
25482563
SHELL_STATIC_SUBCMD_SET_CREATE(tmo_buzzer_sub,
25492564
SHELL_CMD(jingle, NULL, "Play TMO jingle", tmo_play_jingle),
25502565
SHELL_CMD(ramp, NULL, "Play ramp tune", tmo_play_ramp),
2566+
SHELL_CMD(player, NULL, "Play a tone file", cmd_play_tone_file),
25512567
SHELL_CMD(tone, NULL, "Play a tone for a time", cmd_play_tone),
25522568
SHELL_CMD(vol, NULL, "Set buzzer volume", cmd_tmo_buzzer_vol),
25532569
SHELL_SUBCMD_SET_END
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright (c) 2023 T-Mobile USA, Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/fs/fs.h>
9+
#include <zephyr/logging/log.h>
10+
#include <zephyr/sys/byteorder.h>
11+
#include "tmo_buzzer.h"
12+
13+
LOG_MODULE_REGISTER(tmo_tone_player, LOG_LEVEL_DBG);
14+
15+
struct note_s {
16+
uint16_t freq;
17+
uint16_t dur_ms;
18+
};
19+
20+
static struct note_s next_note;
21+
static struct fs_file_t tone_bin;
22+
static bool timer_running;
23+
static void (*song_finished_callback)(void);
24+
25+
static void set_tone(uint16_t freq)
26+
{
27+
pwm_set_frequency(buzzer, 0, freq);
28+
}
29+
30+
static void next_note_f(struct k_timer *timer_id)
31+
{
32+
set_tone(next_note.freq);
33+
34+
if (next_note.dur_ms) {
35+
k_timer_start(timer_id, K_MSEC(next_note.dur_ms), K_FOREVER);
36+
} else {
37+
timer_running = false;
38+
if (song_finished_callback)
39+
song_finished_callback();
40+
song_finished_callback = NULL;
41+
return;
42+
}
43+
44+
if (fs_read(&tone_bin, &next_note, sizeof(next_note)) < 4) {
45+
fs_close(&tone_bin);
46+
memset(&next_note, 0, sizeof(next_note));
47+
return;
48+
}
49+
50+
next_note.freq = sys_be16_to_cpu(next_note.freq);
51+
next_note.dur_ms = sys_be16_to_cpu(next_note.dur_ms);
52+
}
53+
54+
K_TIMER_DEFINE(next_tone, next_note_f, NULL);
55+
56+
int play_tone_bin(char* filename, void (*finished_callback)(void))
57+
{
58+
if (timer_running) {
59+
k_timer_stop(&next_tone);
60+
fs_close(&tone_bin);
61+
}
62+
63+
song_finished_callback = finished_callback;
64+
65+
fs_file_t_init(&tone_bin);
66+
67+
if (fs_open(&tone_bin, filename, FS_O_READ)) {
68+
LOG_ERR("Failed to open tone file");
69+
return -EIO;
70+
}
71+
72+
if (fs_read(&tone_bin, &next_note, sizeof(next_note)) < 4) {
73+
LOG_ERR("Failed to read tone file");
74+
fs_close(&tone_bin);
75+
return -EIO;
76+
}
77+
78+
next_note.freq = sys_be16_to_cpu(next_note.freq);
79+
next_note.dur_ms = sys_be16_to_cpu(next_note.dur_ms);
80+
timer_running = true;
81+
next_note_f(&next_tone);
82+
83+
return 0;
84+
}
85+
86+
int play_single_note(uint16_t freq, uint16_t duration, void (*finished_callback)(void))
87+
{
88+
if (timer_running) {
89+
return -EBUSY;
90+
}
91+
fs_close(&tone_bin);
92+
next_note.freq = 0;
93+
next_note.dur_ms = 0;
94+
95+
song_finished_callback = finished_callback;
96+
97+
set_tone(next_note.freq);
98+
99+
k_timer_start(&next_tone, K_MSEC(duration), K_FOREVER);
100+
101+
return 0;
102+
}
103+
104+
int cancel_playback(void)
105+
{
106+
if (timer_running) {
107+
set_tone(0);
108+
k_timer_stop(&next_tone);
109+
fs_close(&tone_bin);
110+
} else {
111+
return -EALREADY;
112+
}
113+
return 0;
114+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2023 T-Mobile USA, Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef TMO_TONE_PLAYER_H
8+
#define TMO_TONE_PLAYER_H
9+
10+
/**
11+
* @brief Play a tone binary file
12+
*
13+
* @param filename Filename of the tone binary
14+
* @param finished_callback Callback function for tone sequence finish
15+
* @return 0 on sucess, negative error on failure
16+
*/
17+
int play_tone_bin(char* filename, void (*finished_callback)(void));
18+
19+
/**
20+
* @brief Play a single note
21+
*
22+
* @param freq Frequency of the note
23+
* @param duration Duration in milliseconds
24+
* @param finished_callback Callback function for note finish
25+
* @return 0 on sucess, negative error on failure
26+
*/
27+
int play_single_note(uint16_t freq, uint16_t duration, void (*finished_callback)(void));
28+
29+
/**
30+
* @brief Cancel playback of current tone sequence
31+
*
32+
* @return 0 on sucess, negative error on failure
33+
*/
34+
int cancel_playback(void);
35+
36+
#endif /* TMO_TONE_PLAYER_H */

0 commit comments

Comments
 (0)