Skip to content

Commit b837aad

Browse files
committed
samples: Added tone_doremi sample
Add a tone_doremi sample to demonstrate how to sounding with tone API Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
1 parent 24e41a6 commit b837aad

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed

cores/arduino/zephyrCommon.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,14 @@ void tone_expiry_cb(struct k_timer *timer) {
262262
pin_size_t pin = pt->pin;
263263

264264
if (pt->count == 0 && !pt->infinity) {
265-
if (pin >= 0) {
265+
if (pin != pin_size_t(-1)) {
266266
gpio_pin_set_dt(&arduino_pins[pin], 0);
267267
}
268268

269269
k_timer_stop(timer);
270270
pt->pin = pin_size_t(-1);
271271
} else {
272-
if (pin >= 0) {
272+
if (pin != pin_size_t(-1)) {
273273
gpio_pin_toggle_dt(&arduino_pins[pin]);
274274
}
275275

samples/tone_doremi/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
# get value of NORMALIZED_BOARD_TARGET early
6+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE} COMPONENTS yaml boards)
7+
8+
if (EXISTS ${CMAKE_CURRENT_LIST_DIR}/../../variants/${NORMALIZED_BOARD_TARGET}/${NORMALIZED_BOARD_TARGET}_${BOARD_REVISION}.overlay)
9+
set(DTC_OVERLAY_FILE ${CMAKE_CURRENT_LIST_DIR}/../../variants/${NORMALIZED_BOARD_TARGET}/${NORMALIZED_BOARD_TARGET}_${BOARD_REVISION}.overlay)
10+
elseif (EXISTS ${CMAKE_CURRENT_LIST_DIR}/../../variants/${NORMALIZED_BOARD_TARGET}/${NORMALIZED_BOARD_TARGET}.overlay)
11+
set(DTC_OVERLAY_FILE ${CMAKE_CURRENT_LIST_DIR}/../../variants/${NORMALIZED_BOARD_TARGET}/${NORMALIZED_BOARD_TARGET}.overlay)
12+
endif()
13+
14+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
15+
project(tone_doremi)
16+
17+
target_sources(app PRIVATE src/app.cpp)
18+
19+
zephyr_compile_options(-Wno-unused-variable -Wno-comment)

samples/tone_doremi/README.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.. _tone_doremi:
2+
3+
Tone DoReMi
4+
###########
5+
6+
Overview
7+
********
8+
9+
Use tone to play the scale.
10+
Play Do, Re, Mi on D4 pin, then Fa, So, La on D5 pin.
11+
12+
Building and Running
13+
********************
14+
15+
Build and flash tone_doremi sample as follows,
16+
17+
.. code-block:: sh
18+
19+
$> west build -p -b arduino_nano_33_ble samples/tone_doremi/
20+
21+
$> west flash --bossac=/home/$USER/.arduino15/packages/arduino/tools/bossac/1.9.1-arduino2/bossac

samples/tone_doremi/prj.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_ARDUINO_API=y
2+
CONFIG_ARDUINO_MAX_TONES=2

samples/tone_doremi/src/app.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* Copyright (c) 2026 TOKITA Hiroshi
5+
*/
6+
7+
#include <Arduino.h>
8+
#include "zephyrSerial.h"
9+
10+
#define NOTE_C4 262
11+
#define NOTE_D4 294
12+
#define NOTE_E4 330
13+
#define NOTE_F4 349
14+
#define NOTE_G4 392
15+
#define NOTE_A4 440
16+
17+
void setup() {
18+
Serial.begin(115200);
19+
}
20+
21+
void loop() {
22+
Serial.println("Do@D4");
23+
tone(D4, NOTE_C4, 1000);
24+
delay(2000);
25+
Serial.println("Re@D4");
26+
tone(D4, NOTE_D4, 1000);
27+
delay(2000);
28+
Serial.println("Mi@D4 - Infinity");
29+
tone(D4, NOTE_E4, 0);
30+
delay(2000);
31+
Serial.println("Fa@D5");
32+
tone(D5, NOTE_F4, 1000);
33+
delay(2000);
34+
Serial.println("So@D5");
35+
tone(D5, NOTE_G4, 1000);
36+
delay(2000);
37+
Serial.println("La@D5 - Infinity");
38+
tone(D5, NOTE_A4, 0);
39+
40+
while(true) {
41+
delay(1000);
42+
}
43+
}
44+

0 commit comments

Comments
 (0)