Bert Gereels / Mbed 2 deprecated ProjectOne

Dependencies:   C12832 LM75B mbed EthernetInterface mbed-rtos

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers buzzer_music.cpp Source File

buzzer_music.cpp

00001 #include "buzzer_music.h"
00002 #include <string.h>
00003 
00004 namespace ProjectOne{
00005     
00006     const int BuzzerMusic::frequencies[] = {110,110,110,98,130,110,98,130,110,165,165,165,175,130,104,98,130};
00007     const int BuzzerMusic::lengths[] = {480,480,480,360,120,480,360,120,960,480,480,480,360,120,480,360,120};
00008     const int BuzzerMusic::delays[] = {100,100,100,75,100,100,75,100,100,100,100,100,75,100,100,75,100};
00009     
00010     BuzzerMusic::BuzzerMusic(PinName speakerPin) : speaker(speakerPin){
00011         
00012     }
00013 
00014     void BuzzerMusic::playMusic(std::string indexes){
00015         char *command_info[2];
00016         int i = 0;
00017         char *p = strtok ((char *)indexes.c_str(), "-");
00018         while (p != NULL)
00019         {
00020             command_info[i++] = p;
00021             p = strtok (NULL, "-");
00022         }
00023         int x = atoi(command_info[0]);
00024         int y = atoi(command_info[1]);
00025 
00026         if(x >= 1 && y <= 17){
00027             for(int i = x; i < y; i ++){
00028                 speaker = 0.5;
00029                 speaker.period(1.0/frequencies[i]);
00030                 wait((float)lengths[i]/1000);
00031                 speaker = 0.0;
00032                 wait((float)delays[i]/1000);
00033             }
00034             printf("Played Music\r\n");
00035         }else{
00036             printf("Indexes not valid!\r\n");
00037         }
00038         
00039     }
00040 
00041 }
00042