Bouilhol Raphael / Mbed 2 deprecated DailyCo

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers speaker.cpp Source File

speaker.cpp

00001 /*
00002 Authors : BOUILLHOL Raphael / HENG Marie-Claire / KO Roqyun / MOUTOU Killian
00003 Description : Play notes with speaker module.
00004 */
00005 #include "speaker.h"
00006 
00007 Speaker::Speaker(PinName pin) : d_out(pin)
00008 {
00009     note_table[DO] = 955;
00010     note_table[RE] = 852;
00011     note_table[MI] = 758;
00012     note_table[FA] = 716;
00013     note_table[SOL] = 638;
00014     note_table[LA] = 568;
00015     note_table[SI] = 506;
00016     note_table[HIGH_DO] = 477;
00017 }
00018 Speaker::~Speaker()
00019 {
00020 }
00021 void Speaker::play_s(int note, int duration)
00022 {
00023     play_us(note, duration * 1000000);
00024 }
00025 void Speaker::play_ms(int note, int duration)
00026 {
00027     // Play 
00028     play_us(note, duration * 1000);
00029 }
00030 void Speaker::play_us(int note, int duration)
00031 {
00032     // Play a note.
00033     // Generate a rectangular wave of specific frequency.
00034     // The rectangular wave repeats as many as 'duration' divided by its period => duration.
00035     for(int i = 0; i < duration/(note_table[note] * 2); i++) {
00036         d_out = 1; // Output at 1
00037         wait_us(note_table[note]); // 200 ms
00038         d_out = 0; // Output at 1
00039         wait_us(note_table[note]); // 1 sec
00040     }
00041     wait_ms(50); // 1 sec
00042 }