Li Ruofan 201199450

Dependencies:   mbed Gamepad Joystick

bgm/bgm.cpp

Committer:
DannyLee
Date:
2020-05-16
Revision:
8:b4a2954dd74f
Parent:
6:cbd9e1f26a10

File content as of revision 8:b4a2954dd74f:

#include "bgm.h"

// construct & destruct/
Bgm::Bgm()
    :
    _timeout(new Timeout()),
    _buzzer(new PwmOut(PTC10))
{}

Bgm::~Bgm()
{
    
}

void Bgm::tone(float frequency, float duration)
{
    _buzzer->period(1.0f/frequency);
    _buzzer->write(0.5);  // 50% duty cycle - square wave
    _timeout->attach(callback(this, &Bgm::tone_off), duration );
}
// The number "1,2,3,4,5,6,7" means each number note in the notation

// Excerpted from famous Chinese and English songs
void Bgm::welcome(){
    tone(784.0f,0.4f);
    wait(0.4f);
    tone(784.0f,0.1f);
    wait(0.1f);
    tone(880.0f,0.4f);
    wait(0.4f);
    tone(587.0f,0.8f);
    wait(0.8f);
    tone(523.0f,0.4f);
    wait(0.4f);
    tone(523.0f,0.1f);
    wait(0.1f);
    tone(440.0f,0.4f);
    wait(0.4f);
    tone(587.0f,0.8f);
    wait(0.8f);
    tone(784.0f,0.4f);
    wait(0.4f);
    tone(784.0f,0.4f);
    wait(0.4f);
    tone(880.0f,0.1f);
    wait(0.1f);
    tone(1046.0f,0.4f);
    wait(0.4f);
    tone(880.0f,0.2f);
    wait(0.2f);
    tone(784.0f,0.2f);
    wait(0.2f);
    tone(523.0f,0.4f);
    wait(0.4f);
    tone(523.0f,0.1f);
    wait(0.1f);
    tone(440.0f,0.1f);
    wait(0.1f);
    tone(587.0f,0.8f);
    wait(0.8f);
    
}

void Bgm::battlefield(){
    tone(659.0f,0.3f);
    wait(0.3f);
    tone(659.0f,0.2f);
    wait(0.2f);
    tone(587.0f,0.3f);
    wait(0.3f);
    tone(523.0f,0.2f);
    wait(0.2f);
    tone(523.0f,0.3f);
    wait(0.3f);
    tone(659.0f,0.2f);
    wait(0.2f);
    tone(784.0f,0.3f);
    wait(0.3f);
    tone(1046.0f,0.3f);
    wait(0.3f);
    tone(1046.0f,0.2f);
    wait(0.2f);
    tone(1046.0f,0.2f);
    wait(0.2f);
    tone(880.0f,0.5f);
    wait(0.5f);
    tone(784.0f,0.3f);
    wait(0.3f);
    tone(784.0f,0.2f);
    wait(0.2f);
    tone(880.0f,0.3f);
    wait(0.3f);
    tone(784.0f,0.3f);
    wait(0.3f);
    tone(659.0f,0.2f);
    wait(0.2f);
    tone(523.0f,0.3f);
    wait(0.3f);
    tone(587.0f,0.3f);
    wait(0.3f);
    tone(587.0f,0.2f);
    wait(0.2f);
    tone(659.0f,0.3f);
    wait(0.3f);
    tone(587.0f,0.2f);
    wait(0.2f);
    
}

void Bgm::died(){
    tone(784.0f,0.2f);
    wait(0.2f);
    tone(880.0f,0.3f);
    wait(0.3f);
    tone(784.0f,0.2f);
    wait(0.2f);
    tone(698.0f,0.3f);
    wait(0.3f);
    tone(659.0f,0.2f);
    wait(0.2f);
    tone(698.0f,0.3f);
    wait(0.3f);
    tone(784.0f,0.3f);
    wait(0.3f);
    tone(587.0f,0.3f);
    wait(0.3f);
    tone(784.0f,0.2f);
    wait(0.2f);
    tone(784.0f,0.3f);
    wait(0.3f);
    tone(659.0f,0.2f);
    wait(0.2f);
    tone(523.0f,0.3f);
    wait(0.3f);
    
}

void Bgm::tone_off()
{
    // called after timeout
    _buzzer->write(0.0);
}