Skip to content

Commit c77a146

Browse files
authored
Merge pull request #2 from jcarranz97/example/with-pico_tone
Modify example to use pico/tone.h library
2 parents 279961d + 5137025 commit c77a146

File tree

8 files changed

+169
-189
lines changed

8 files changed

+169
-189
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ add_subdirectory(timer)
5656
add_subdirectory(uart)
5757
add_subdirectory(usb)
5858
add_subdirectory(watchdog)
59+
add_subdirectory(tone)

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ App|Description
244244
[hello_pwm](pwm/hello_pwm) | Minimal example of driving PWM output on GPIOs.
245245
[led_fade](pwm/led_fade) | Fade an LED between low and high brightness. An interrupt handler updates the PWM slice's output level each time the counter wraps.
246246
[measure_duty_cycle](pwm/measure_duty_cycle) | Drives a PWM output at a range of duty cycles, and uses another PWM slice in input mode to measure the duty cycle.
247-
[pico_tone](pwm/pico_tone) | Play a christmas melody :notes::christmas_tree: by generating square waves with PWM and a Buzzer :speaker:.
248247

249248
### Reset
250249

@@ -369,3 +368,9 @@ At the time of writing, there is only one dual example available:
369368
App|Description
370369
---|---
371370
[hello_watchdog](watchdog/hello_watchdog) | Set the watchdog timer, and let it expire. Detect the reboot, and halt.
371+
372+
### Tone
373+
374+
App|Description
375+
---|---
376+
[christmas_melody](tone/christmas_melody) | Play a christmas melody :notes::christmas_tree: with a Buzzer :speaker:.

pwm/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ if (NOT PICO_NO_HARDWARE)
22
add_subdirectory(hello_pwm)
33
add_subdirectory(led_fade)
44
add_subdirectory(measure_duty_cycle)
5-
add_subdirectory(pico_tone)
65
endif ()

pwm/pico_tone/CMakeLists.txt

Lines changed: 0 additions & 12 deletions
This file was deleted.

pwm/pico_tone/pico_tone.c

Lines changed: 0 additions & 175 deletions
This file was deleted.

tone/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if (NOT PICO_NO_HARDWARE)
2+
add_subdirectory(christmas_melody)
3+
endif ()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
add_executable(christmas_melody
2+
christmas_melody.c
3+
)
4+
5+
# Add pico_stdlib library which aggregates commonly used features
6+
target_link_libraries(christmas_melody pico_stdlib pico_tone)
7+
8+
# Create map/bin/hex/uf2 file in addition to ELF.
9+
pico_add_extra_outputs(christmas_melody)
10+
11+
# add url via pico_set_program_url
12+
example_auto_set_url(christmas_melody)
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/**
2+
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include "pico/stdlib.h"
8+
#include "pico/tone.h"
9+
10+
/** Example code to generate tones with Buzzer using pico-sdk.
11+
*
12+
* The melody is the Christmas song "Noche de Paz" (Silent Night).
13+
*
14+
* .:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:.
15+
* . .
16+
* . * Song: Noche de Paz (Silent Night) .
17+
* . /.\ Instrument: Buzzer .
18+
* . /..'\ Interpreter: Raspberry Pi Pico .
19+
* . /'.'\ _ .
20+
* . /.''.'\ _[ ]_ .
21+
* . /.'.'.\ Merry Christmas! (") .
22+
* . /'.''.'.\ Feliz Navidad! `__( : )--' .
23+
* . ^^^[_]^^^ ( : ) .
24+
* . ""`-...-'"" .
25+
* . .
26+
* .:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:.
27+
*
28+
*/
29+
#define BUZZER_PIN 3
30+
31+
// Notes' frequencies used for the melody in Hz.
32+
#define NOTE_C4 262
33+
#define NOTE_D4 294
34+
#define NOTE_E4 330
35+
#define NOTE_F4 349
36+
#define NOTE_G4 392
37+
#define NOTE_A4 440
38+
#define NOTE_B4 494
39+
#define NOTE_C5 523
40+
#define NOTE_D5 587
41+
#define NOTE_E5 659
42+
#define NOTE_F5 698
43+
44+
#define SPEED 400U // Speed of each note in ms
45+
#define SILENCE 10U // 10 ms of silence after each note
46+
47+
uint slice_num; // Variable to save the PWM slice number
48+
49+
int main() {
50+
// Configure tone generation on BUZZER_PIN
51+
tone_init(BUZZER_PIN);
52+
while (1) {
53+
// 1
54+
tone(BUZZER_PIN, NOTE_G4, 1.5f);
55+
tone(BUZZER_PIN, NOTE_A4, .5f);
56+
tone(BUZZER_PIN, NOTE_G4, 1.f);
57+
58+
// 2
59+
tone(BUZZER_PIN, NOTE_E4, 3.f);
60+
61+
// 3
62+
tone(BUZZER_PIN, NOTE_G4, 1.5f);
63+
tone(BUZZER_PIN, NOTE_A4, .5f);
64+
tone(BUZZER_PIN, NOTE_G4, 1.f);
65+
66+
// 4
67+
tone(BUZZER_PIN, NOTE_E4, 3.f);
68+
69+
// 5
70+
tone(BUZZER_PIN, NOTE_D5, 2.f);
71+
tone(BUZZER_PIN, NOTE_D5, 1.f);
72+
73+
// 6
74+
tone(BUZZER_PIN, NOTE_B4, 3.f);
75+
76+
// 7
77+
tone(BUZZER_PIN, NOTE_C5, 2.f);
78+
tone(BUZZER_PIN, NOTE_C5, 1.f);
79+
80+
// 8
81+
tone(BUZZER_PIN, NOTE_G4, 3.f);
82+
83+
// 9
84+
tone(BUZZER_PIN, NOTE_A4, 2.f);
85+
tone(BUZZER_PIN, NOTE_A4, 1.f);
86+
87+
// 10
88+
tone(BUZZER_PIN, NOTE_C5, 1.5f);
89+
tone(BUZZER_PIN, NOTE_B4, .5f);
90+
tone(BUZZER_PIN, NOTE_A4, 1.f);
91+
92+
// 11
93+
tone(BUZZER_PIN, NOTE_G4, 1.5f);
94+
tone(BUZZER_PIN, NOTE_A4, .5f);
95+
tone(BUZZER_PIN, NOTE_G4, 1.f);
96+
97+
// 12
98+
tone(BUZZER_PIN, NOTE_E4, 3.f);
99+
100+
// 13
101+
tone(BUZZER_PIN, NOTE_A4, 2.f);
102+
tone(BUZZER_PIN, NOTE_A4, 1.f);
103+
104+
// 14
105+
tone(BUZZER_PIN, NOTE_C5, 1.5f);
106+
tone(BUZZER_PIN, NOTE_B4, .5f);
107+
tone(BUZZER_PIN, NOTE_A4, 1.f);
108+
109+
// 15
110+
tone(BUZZER_PIN, NOTE_G4, 1.5f);
111+
tone(BUZZER_PIN, NOTE_A4, .5f);
112+
tone(BUZZER_PIN, NOTE_G4, 1.f);
113+
114+
// 16
115+
tone(BUZZER_PIN, NOTE_E4, 3.f);
116+
117+
// 17
118+
tone(BUZZER_PIN, NOTE_D5, 2.f);
119+
tone(BUZZER_PIN, NOTE_D5, 1.f);
120+
121+
// 18
122+
tone(BUZZER_PIN, NOTE_F5, 1.5f);
123+
tone(BUZZER_PIN, NOTE_D5, .5f);
124+
tone(BUZZER_PIN, NOTE_B4, 1.f);
125+
126+
// 19
127+
tone(BUZZER_PIN, NOTE_C5, 3.f);
128+
129+
// 20
130+
tone(BUZZER_PIN, NOTE_E5, 3.f);
131+
132+
// 21
133+
tone(BUZZER_PIN, NOTE_C5, 1.5f);
134+
tone(BUZZER_PIN, NOTE_G4, .5f);
135+
tone(BUZZER_PIN, NOTE_E4, 1.f);
136+
137+
// 22
138+
tone(BUZZER_PIN, NOTE_G4, 1.5f);
139+
tone(BUZZER_PIN, NOTE_F4, .5f);
140+
tone(BUZZER_PIN, NOTE_D4, 1.f);
141+
142+
// 23 & 24
143+
tone(BUZZER_PIN, NOTE_C4, 5.f);
144+
// tone(BUZZER_PIN, <Silencio>, 2.f);
145+
// Fin
146+
}
147+
}

0 commit comments

Comments
 (0)