Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

Have a piezzo buzzer, and want to play music ?

let Timer = {set: ffi('int mgos_set_timer(int,int,void(*)(userdata),userdata)')};
let PWM = {set: ffi('int mgos_pwm_set(int, int, int)')};

//Music is an array with [note frequency,duration].
let music=([[523,100],[0,100],[587,100],[0,100],[698,100],[0,100],[587,100],[0,100],[880,275],[0,300],[880,300],[0,300],[784,700],[0,700],
[523,100],[0,100],[587,100],[0,100],[698,100],[0,100],[587,100],[0,100],[784,275],[0,300],[784,300],[0,300],[698,300],[0,300],[659,100],
[0,100],[587,300],[0,300],[523,100],[0,100],[587,100],[0,100],[698,100],[0,100],[587,100],[0,100],[698,400],[0,400],[784,200],[0,200],
[659,300],[0,300],[587,100],[0,100],[523,475],[0,500],[523,200],[0,200],[784,400],[0,400],[698,800],[0,800]]);

let play = function(data) {
PWM.set(data.pin,music[data.count][0],0.7);
if (data.count+1 < music.length) {
Timer.set(music[data.count][1]/2, 0, play, data);
data.count++;
}
};
play({ pin: 5, count: 0});

Comments

Sign In or Register to comment.