buzzer: C implementation; C++ wraps C

Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
Jon Trulson
2016-10-28 16:47:01 -06:00
parent f623414c04
commit 7b9fbd8738
16 changed files with 461 additions and 154 deletions

View File

@ -31,18 +31,21 @@
int
main(int argc, char **argv) {
//! [Interesting]
int chord[] = { DO, RE, MI, FA, SOL, LA, SI, DO, SI };
int chord[] = { BUZZER_DO, BUZZER_RE, BUZZER_MI,
BUZZER_FA, BUZZER_SOL, BUZZER_LA,
BUZZER_SI };
// create Buzzer instance
upm::Buzzer* sound = new upm::Buzzer(5);
// print sensor name
std::cout << sound->name() << std::endl;
// play sound (DO, RE, MI, etc...), pausing for 0.1 seconds between notes
for (int chord_ind = 0; chord_ind < 7; chord_ind++) {
// play each note for one second
std::cout << sound->playSound(chord[chord_ind], 1000000) << std::endl;
usleep(100000);
// play each sound (DO, RE, MI, etc...) for .5 seconds, pausing
// for 0.1 seconds between notes
for (int chord_ind = 0; chord_ind < 7; chord_ind++)
{
std::cout << sound->playSound(chord[chord_ind], 500000) << std::endl;
upm_delay_ms(100);
}
//! [Interesting]

View File

@ -131,6 +131,7 @@ add_example (md)
add_example (linefinder)
add_example (uln200xa)
add_example (mma7660)
add_example (buzzer)
# Custom examples
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)

View File

@ -1,6 +1,6 @@
/*
* Author: Stefan Andritoiu <stefan.andritoiu@intel.com>
* Copyright (c) 2015 Intel Corporation.
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -22,36 +22,47 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
public class Buzzer_soundSample {
public static void main(String[] args) throws InterruptedException {
//! [Interesting]
int chord[] = {
upm_buzzer.javaupm_buzzer.DO,
upm_buzzer.javaupm_buzzer.RE,
upm_buzzer.javaupm_buzzer.MI,
upm_buzzer.javaupm_buzzer.FA,
upm_buzzer.javaupm_buzzer.SOL,
upm_buzzer.javaupm_buzzer.LA,
upm_buzzer.javaupm_buzzer.SI
};
// Instantiate a buzzer on digital pin D5
upm_buzzer.Buzzer sound = new upm_buzzer.Buzzer(5);
// print sensor name
System.out.println(sound.name());
for (int i = 0; i < chord.length; i++) {
// play each note for one second
int note = sound.playSound( chord[i], 1000000);
System.out.println(note);
Thread.sleep(100);
}
//! [Interesting]
sound.stopSound();
}
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <upm_utilities.h>
#include <buzzer.h>
int
main(int argc, char **argv)
{
//! [Interesting]
int chord[] = { BUZZER_DO, BUZZER_RE, BUZZER_MI,
BUZZER_FA, BUZZER_SOL, BUZZER_LA,
BUZZER_SI };
// create Buzzer context, using PWM pin 5
buzzer_context sound = buzzer_init(5);
if (!sound)
{
printf("buzzer_init() failed\n");
return 1;
}
printf("Playing...\n");
// play each sound (DO, RE, MI, etc...) for .5 seconds, pausing
// for 0.1 seconds between notes
for (int chord_ind = 0; chord_ind < 7; chord_ind++)
{
buzzer_play_sound(sound, chord[chord_ind], 500000);
printf("%d\n", chord[chord_ind]);
upm_delay_ms(100);
}
printf("Exiting...\n");
buzzer_close(sound);
//! [Interesting]
return 0;
}

View File

@ -22,28 +22,29 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import upm_buzzer.Buzzer;
public class BuzzerSample {
public static void main(String[] args) throws InterruptedException {
// ! [Interesting]
int chord[] = {
upm_buzzer.javaupm_buzzer.DO,
upm_buzzer.javaupm_buzzer.RE,
upm_buzzer.javaupm_buzzer.MI,
upm_buzzer.javaupm_buzzer.FA,
upm_buzzer.javaupm_buzzer.SOL,
upm_buzzer.javaupm_buzzer.LA,
upm_buzzer.javaupm_buzzer.SI};
upm_buzzer.javaupm_buzzer.BUZZER_DO,
upm_buzzer.javaupm_buzzer.BUZZER_RE,
upm_buzzer.javaupm_buzzer.BUZZER_MI,
upm_buzzer.javaupm_buzzer.BUZZER_FA,
upm_buzzer.javaupm_buzzer.BUZZER_SOL,
upm_buzzer.javaupm_buzzer.BUZZER_LA,
upm_buzzer.javaupm_buzzer.BUZZER_SI};
// Instantiate a buzzer on digital pin D5
upm_buzzer.Buzzer sound = new upm_buzzer.Buzzer(5);
Buzzer sound = new Buzzer(5);
// print sensor name
System.out.println(sound.name());
for (int i = 0; i < chord.length; i++) {
// play each note for one second
int note = sound.playSound(chord[i], 1000000);
// play each note for one half second
int note = sound.playSound(chord[i], 500000);
System.out.println(note);
Thread.sleep(100);
@ -52,4 +53,4 @@ public class BuzzerSample {
sound.stopSound();
}
}
}

View File

@ -25,7 +25,6 @@ add_example(Apds9002 apds9002)
add_example(BISS0001Sample biss0001)
add_example(BMPX8XSample bmpx8x)
add_example(BuzzerSample buzzer)
add_example(Buzzer_soundSample buzzer)
add_example(CJQ4435Sample cjq4435)
add_example(DS1307Sample ds1307)
add_example(ENC03RSample enc03r)

View File

@ -26,15 +26,13 @@ var upmBuzzer = require("jsupm_buzzer");
// Initialize on GPIO 5
var myBuzzer = new upmBuzzer.Buzzer(5);
var chords = [];
chords.push(upmBuzzer.DO);
chords.push(upmBuzzer.RE);
chords.push(upmBuzzer.MI);
chords.push(upmBuzzer.FA);
chords.push(upmBuzzer.SOL);
chords.push(upmBuzzer.LA);
chords.push(upmBuzzer.SI);
chords.push(upmBuzzer.DO);
chords.push(upmBuzzer.SI);
chords.push(upmBuzzer.BUZZER_DO);
chords.push(upmBuzzer.BUZZER_RE);
chords.push(upmBuzzer.BUZZER_MI);
chords.push(upmBuzzer.BUZZER_FA);
chords.push(upmBuzzer.BUZZER_SOL);
chords.push(upmBuzzer.BUZZER_LA);
chords.push(upmBuzzer.BUZZER_SI);
var chordIndex = 0;
// Print sensor name
@ -44,8 +42,8 @@ function melody()
{
if (chords.length != 0)
{
//Play sound for one second
console.log( myBuzzer.playSound(chords[chordIndex], 1000000) );
//Play sound for one half second
console.log( myBuzzer.playSound(chords[chordIndex], 500000) );
chordIndex++;
//Reset the sound to start from the beginning.
if (chordIndex > chords.length - 1)

View File

@ -28,17 +28,17 @@ def main():
# Create the buzzer object using GPIO pin 5
buzzer = upmBuzzer.Buzzer(5)
chords = [upmBuzzer.DO, upmBuzzer.RE, upmBuzzer.MI, upmBuzzer.FA,
upmBuzzer.SOL, upmBuzzer.LA, upmBuzzer.SI, upmBuzzer.DO,
upmBuzzer.SI];
chords = [upmBuzzer.BUZZER_DO, upmBuzzer.BUZZER_RE, upmBuzzer.BUZZER_MI,
upmBuzzer.BUZZER_FA, upmBuzzer.BUZZER_SOL, upmBuzzer.BUZZER_LA,
upmBuzzer.BUZZER_SI];
# Print sensor name
print(buzzer.name())
# Play sound (DO, RE, MI, etc.), pausing for 0.1 seconds between notes
for chord_ind in range (0,7):
# play each note for one second
print(buzzer.playSound(chords[chord_ind], 1000000))
# play each note for a half second
print(buzzer.playSound(chords[chord_ind], 500000))
time.sleep(0.1)
print("exiting application")