Part One of my Project Course. Implementation of simple I/O and a custom defined protocol over UDP/IP.
Dependencies: C12832 LM75B mbed EthernetInterface mbed-rtos
Diff: buzzer_music.cpp
- Revision:
- 1:b5c534165dfe
- Parent:
- 0:88d3b9015f7c
- Child:
- 2:6bfe732ba6bc
--- a/buzzer_music.cpp Mon Feb 26 11:25:59 2018 +0000 +++ b/buzzer_music.cpp Wed Mar 14 07:59:21 2018 +0000 @@ -1,5 +1,5 @@ #include "buzzer_music.h" -#include "master.h" +#include <string.h> namespace ProjectOne{ @@ -7,21 +7,35 @@ const int BuzzerMusic::lengths[] = {480,480,480,360,120,480,360,120,960,480,480,480,360,120,480,360,120}; const int BuzzerMusic::delays[] = {100,100,100,75,100,100,75,100,100,100,100,100,75,100,100,75,100}; - //PwmOut speaker(NC); - BuzzerMusic::BuzzerMusic(PinName speakerPin) : speaker(speakerPin){ } - void BuzzerMusic::playMusic(int x, int y){ - for(int i = x; i < y; i ++){ - speaker = 0.5; - speaker.period(1.0/frequencies[i]); - wait((float)lengths[i]/1000); - speaker = 0.0; - wait((float)delays[i]/1000); + void BuzzerMusic::playMusic(std::string indexes){ + char *command_info[2]; + int i = 0; + char *p = strtok ((char *)indexes.c_str(), "-"); + while (p != NULL) + { + command_info[i++] = p; + p = strtok (NULL, "-"); } - printf("Played Music"); + int x = atoi(command_info[0]); + int y = atoi(command_info[1]); + + if(x >= 0 && y <= 17){ + for(int i = x; i < y; i ++){ + speaker = 0.5; + speaker.period(1.0/frequencies[i]); + wait((float)lengths[i]/1000); + speaker = 0.0; + wait((float)delays[i]/1000); + } + printf("Played Music\r\n"); + }else{ + printf("Indexes not valid!\r\n"); + } + } }