KPE buzzer

mail@pastecode.io avatar
unknown
c_cpp
a year ago
1.7 kB
13
Indexable
#include "mbed.h"
#define C 261.63
#define C_S 277.18
#define D 293.66
#define D_S 311.13
#define E 329.63
#define F 349.23
#define F_S 369.99
#define G 392
#define G_S 415.3
#define A 440
#define A_S 466.16
#define B 493.88
#define c 261
#define d 294
#define e 329
#define f 349
#define g 391
#define gS 415
#define a 440
#define aS 455
#define b 466
#define cH 523
#define cSH 554
#define dH 587
#define dSH 622
#define eH 659
#define fH 698
#define fSH 740
#define gH 784
#define gSH 830
#define aH 880
class Buzzer {
public:
Buzzer(PinName pin) : _pin(pin) {}

void beep(int freq, float time) {
    _pin = freq? 0.5:0;
    _pin.period(freq? 1.0/freq :1);
    wait(time);
}
private:
    PwmOut _pin;
};
Buzzer buzzer(PA_4);
int main() {

    short freq[] = {B,B,C,D,D,C,B,A,G,G,A,B,B,A,A,B,B,C,D,D,C,B,A,G,G,A,B,A,G,G,A,B,G,A,B,C,B,G,A,B,C,B,A,G,A,B,B,C,D,D,C,B,A,G,G,A,B,A,G,G}; //frekvence
    double time[] = {0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,1,1,0}; //cas
    short imperial[] = {a,a,a,f,cH,a,f,cH,a,eH,eH,eH,fH,cH,gS,f,cH,a,aH,a,a,aH,gSH,gH,fSH,fH,fSH,0,aS,dSH,dH,cSH,cH,b,cH,0,f,gS,f,a,cH,a,cH,eH,aH,a,a,aH,gSH,gH,fSH,fH,fSH,0,aS,dSH,dH,cSH,cH,b,cH,0,f,gS,f,cH,a,f,c,a,0};
    double time2[] = {0.5,0.5,0.5,0.35,0.15,0.5,0.35,0.15,1,0.5,0.5,0.5,0.35,0.15,0.5,0.35,0.15,1,0.5,0.35,0.15,0.5,0.25,0.25,0.125,0.125,0.25,0.25,0.25,0.5,0.25,0.25,0.125,0.125,0.25,0.25,0.125,0.5,0.375,0.125,0.5,0.375,0.125,1,0.5,0.35,0.15,0.5,0.25,0.25,0.125,0.125,0.25,0.25,0.25,0.5,0.25,0.25,0.125,0.125,0.25,0.25,0.25,0.5,0.375,0.125,0.5,0.375,0.125,1,1};

    for(int i = 0; i < sizeof(imperial)/sizeof(short); i++)
    {
        buzzer.beep(imperial[i], time2[i]);
    }

}