Skip to content

Commit 72aab44

Browse files
committed
Modify library to use pico/tone.h instead
1 parent 8a83502 commit 72aab44

File tree

1 file changed

+50
-69
lines changed

1 file changed

+50
-69
lines changed

tone/christmas_melody/christmas_melody.c

Lines changed: 50 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
#include "pico/stdlib.h"
8-
#include "hardware/pwm.h"
8+
#include "pico/tone.h"
99

1010
/** Example code to generate tones with Buzzer using PWM with pico-sdk.
1111
*
@@ -40,16 +40,9 @@
4040

4141
uint slice_num; // Variable to save the PWM slice number
4242

43-
void play_tone(uint gpio, uint16_t freq, float duration);
44-
4543
int main() {
46-
// Configure BUZZER_PIN as PWM
47-
gpio_set_function(BUZZER_PIN, GPIO_FUNC_PWM);
48-
// Update slice_num with the slice number of BUZZER_PIN
49-
slice_num = pwm_gpio_to_slice_num(BUZZER_PIN);
50-
// Get default configuration for PWM slice and initialize pwm
51-
pwm_config config = pwm_get_default_config();
52-
pwm_init(slice_num, &config, true);
44+
// Configure tone generation on BUZZER_PIN
45+
tone_init(BUZZER_PIN);
5346
while (1) {
5447
/**
5548
* .:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:.
@@ -67,109 +60,97 @@ int main() {
6760
* .:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:.
6861
*/
6962
// 1
70-
play_tone(BUZZER_PIN, NOTE_G4, 1.5f);
71-
play_tone(BUZZER_PIN, NOTE_A4, .5f);
72-
play_tone(BUZZER_PIN, NOTE_G4, 1.f);
63+
tone(BUZZER_PIN, NOTE_G4, 1.5f);
64+
tone(BUZZER_PIN, NOTE_A4, .5f);
65+
tone(BUZZER_PIN, NOTE_G4, 1.f);
7366

7467
// 2
75-
play_tone(BUZZER_PIN, NOTE_E4, 3.f);
68+
tone(BUZZER_PIN, NOTE_E4, 3.f);
7669

7770
// 3
78-
play_tone(BUZZER_PIN, NOTE_G4, 1.5f);
79-
play_tone(BUZZER_PIN, NOTE_A4, .5f);
80-
play_tone(BUZZER_PIN, NOTE_G4, 1.f);
71+
tone(BUZZER_PIN, NOTE_G4, 1.5f);
72+
tone(BUZZER_PIN, NOTE_A4, .5f);
73+
tone(BUZZER_PIN, NOTE_G4, 1.f);
8174

8275
// 4
83-
play_tone(BUZZER_PIN, NOTE_E4, 3.f);
76+
tone(BUZZER_PIN, NOTE_E4, 3.f);
8477

8578
// 5
86-
play_tone(BUZZER_PIN, NOTE_D5, 2.f);
87-
play_tone(BUZZER_PIN, NOTE_D5, 1.f);
79+
tone(BUZZER_PIN, NOTE_D5, 2.f);
80+
tone(BUZZER_PIN, NOTE_D5, 1.f);
8881

8982
// 6
90-
play_tone(BUZZER_PIN, NOTE_B4, 3.f);
83+
tone(BUZZER_PIN, NOTE_B4, 3.f);
9184

9285
// 7
93-
play_tone(BUZZER_PIN, NOTE_C5, 2.f);
94-
play_tone(BUZZER_PIN, NOTE_C5, 1.f);
86+
tone(BUZZER_PIN, NOTE_C5, 2.f);
87+
tone(BUZZER_PIN, NOTE_C5, 1.f);
9588

9689
// 8
97-
play_tone(BUZZER_PIN, NOTE_G4, 3.f);
90+
tone(BUZZER_PIN, NOTE_G4, 3.f);
9891

9992
// 9
100-
play_tone(BUZZER_PIN, NOTE_A4, 2.f);
101-
play_tone(BUZZER_PIN, NOTE_A4, 1.f);
93+
tone(BUZZER_PIN, NOTE_A4, 2.f);
94+
tone(BUZZER_PIN, NOTE_A4, 1.f);
10295

10396
// 10
104-
play_tone(BUZZER_PIN, NOTE_C5, 1.5f);
105-
play_tone(BUZZER_PIN, NOTE_B4, .5f);
106-
play_tone(BUZZER_PIN, NOTE_A4, 1.f);
97+
tone(BUZZER_PIN, NOTE_C5, 1.5f);
98+
tone(BUZZER_PIN, NOTE_B4, .5f);
99+
tone(BUZZER_PIN, NOTE_A4, 1.f);
107100

108101
// 11
109-
play_tone(BUZZER_PIN, NOTE_G4, 1.5f);
110-
play_tone(BUZZER_PIN, NOTE_A4, .5f);
111-
play_tone(BUZZER_PIN, NOTE_G4, 1.f);
102+
tone(BUZZER_PIN, NOTE_G4, 1.5f);
103+
tone(BUZZER_PIN, NOTE_A4, .5f);
104+
tone(BUZZER_PIN, NOTE_G4, 1.f);
112105

113106
// 12
114-
play_tone(BUZZER_PIN, NOTE_E4, 3.f);
107+
tone(BUZZER_PIN, NOTE_E4, 3.f);
115108

116109
// 13
117-
play_tone(BUZZER_PIN, NOTE_A4, 2.f);
118-
play_tone(BUZZER_PIN, NOTE_A4, 1.f);
110+
tone(BUZZER_PIN, NOTE_A4, 2.f);
111+
tone(BUZZER_PIN, NOTE_A4, 1.f);
119112

120113
// 14
121-
play_tone(BUZZER_PIN, NOTE_C5, 1.5f);
122-
play_tone(BUZZER_PIN, NOTE_B4, .5f);
123-
play_tone(BUZZER_PIN, NOTE_A4, 1.f);
114+
tone(BUZZER_PIN, NOTE_C5, 1.5f);
115+
tone(BUZZER_PIN, NOTE_B4, .5f);
116+
tone(BUZZER_PIN, NOTE_A4, 1.f);
124117

125118
// 15
126-
play_tone(BUZZER_PIN, NOTE_G4, 1.5f);
127-
play_tone(BUZZER_PIN, NOTE_A4, .5f);
128-
play_tone(BUZZER_PIN, NOTE_G4, 1.f);
119+
tone(BUZZER_PIN, NOTE_G4, 1.5f);
120+
tone(BUZZER_PIN, NOTE_A4, .5f);
121+
tone(BUZZER_PIN, NOTE_G4, 1.f);
129122

130123
// 16
131-
play_tone(BUZZER_PIN, NOTE_E4, 3.f);
124+
tone(BUZZER_PIN, NOTE_E4, 3.f);
132125

133126
// 17
134-
play_tone(BUZZER_PIN, NOTE_D5, 2.f);
135-
play_tone(BUZZER_PIN, NOTE_D5, 1.f);
127+
tone(BUZZER_PIN, NOTE_D5, 2.f);
128+
tone(BUZZER_PIN, NOTE_D5, 1.f);
136129

137130
// 18
138-
play_tone(BUZZER_PIN, NOTE_F5, 1.5f);
139-
play_tone(BUZZER_PIN, NOTE_D5, .5f);
140-
play_tone(BUZZER_PIN, NOTE_B4, 1.f);
131+
tone(BUZZER_PIN, NOTE_F5, 1.5f);
132+
tone(BUZZER_PIN, NOTE_D5, .5f);
133+
tone(BUZZER_PIN, NOTE_B4, 1.f);
141134

142135
// 19
143-
play_tone(BUZZER_PIN, NOTE_C5, 3.f);
136+
tone(BUZZER_PIN, NOTE_C5, 3.f);
144137

145138
// 20
146-
play_tone(BUZZER_PIN, NOTE_E5, 3.f);
139+
tone(BUZZER_PIN, NOTE_E5, 3.f);
147140

148141
// 21
149-
play_tone(BUZZER_PIN, NOTE_C5, 1.5f);
150-
play_tone(BUZZER_PIN, NOTE_G4, .5f);
151-
play_tone(BUZZER_PIN, NOTE_E4, 1.f);
142+
tone(BUZZER_PIN, NOTE_C5, 1.5f);
143+
tone(BUZZER_PIN, NOTE_G4, .5f);
144+
tone(BUZZER_PIN, NOTE_E4, 1.f);
152145

153146
// 22
154-
play_tone(BUZZER_PIN, NOTE_G4, 1.5f);
155-
play_tone(BUZZER_PIN, NOTE_F4, .5f);
156-
play_tone(BUZZER_PIN, NOTE_D4, 1.f);
147+
tone(BUZZER_PIN, NOTE_G4, 1.5f);
148+
tone(BUZZER_PIN, NOTE_F4, .5f);
149+
tone(BUZZER_PIN, NOTE_D4, 1.f);
157150

158151
// 23 & 24
159-
play_tone(BUZZER_PIN, NOTE_C4, 5.f);
160-
// play_tone(BUZZER_PIN, <Silencio>, 2.f);
152+
tone(BUZZER_PIN, NOTE_C4, 5.f);
153+
// tone(BUZZER_PIN, <Silencio>, 2.f);
161154
// Fin
162155
}
163156
}
164-
165-
void play_tone(uint gpio, uint16_t freq, float duration) {
166-
// Calculate and configure new clock divider according to the frequency
167-
float clkdiv = (1.f / freq) * 2000.f;
168-
pwm_set_clkdiv(slice_num, clkdiv);
169-
// Configure duty to 50% ((2**16-1)/2)
170-
pwm_set_gpio_level(BUZZER_PIN, 32768U);
171-
sleep_ms((uint32_t) SPEED * duration);
172-
// Make silence after each note to distinguish them better.
173-
pwm_set_gpio_level(BUZZER_PIN, 0);
174-
sleep_ms(SILENCE);
175-
}

0 commit comments

Comments
 (0)