fix simulator support for music
This commit is contained in:
parent
8fca50e907
commit
40fe1b4616
@ -8,7 +8,7 @@ namespace music {
|
||||
*/
|
||||
//% help=music/play-tone weight=90
|
||||
//% blockId=device_play_note block="play|tone %note=device_note|for %duration=device_beat" icon="\uf025" blockGap=8
|
||||
//% parts="speaker"
|
||||
//% parts="speaker" async
|
||||
void playTone(int freqency, int ms) {
|
||||
uBit.soundmotor.Sound_On(freqency);
|
||||
if(ms > 0) uBit.sleep(ms);
|
||||
|
2
libs/core/shims.d.ts
vendored
2
libs/core/shims.d.ts
vendored
@ -533,7 +533,7 @@ declare namespace music {
|
||||
*/
|
||||
//% help=music/play-tone weight=90
|
||||
//% blockId=device_play_note block="play|tone %note=device_note|for %duration=device_beat" icon="\uf025" blockGap=8
|
||||
//% parts="speaker" shim=music::playTone
|
||||
//% parts="speaker" async shim=music::playTone
|
||||
function playTone(freqency: number, ms: number): void;
|
||||
|
||||
/**
|
||||
|
@ -14,6 +14,7 @@ namespace pxsim {
|
||||
radioState: RadioState;
|
||||
neopixelState: NeoPixelState;
|
||||
rgbLedState: number;
|
||||
speakerState: SpeakerState;
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
@ -59,6 +60,7 @@ namespace pxsim {
|
||||
this.builtinParts["lightsensor"] = this.lightSensorState = new LightSensorState();
|
||||
this.builtinParts["compass"] = this.compassState = new CompassState();
|
||||
this.builtinParts["neopixel"] = this.neopixelState = new NeoPixelState();
|
||||
this.builtinParts["speaker"] = this.speakerState = new SpeakerState();
|
||||
|
||||
this.builtinVisuals["buttonpair"] = () => new visuals.ButtonPairView();
|
||||
this.builtinVisuals["ledmatrix"] = () => new visuals.LedMatrixView();
|
||||
|
29
sim/state/music.ts
Normal file
29
sim/state/music.ts
Normal file
@ -0,0 +1,29 @@
|
||||
namespace pxsim {
|
||||
export class SpeakerState {
|
||||
frequency: number;
|
||||
ms: number;
|
||||
}
|
||||
|
||||
}
|
||||
namespace pxsim.music {
|
||||
|
||||
export function playTone(frequency: number, ms: number) {
|
||||
const b = board();
|
||||
b.speakerState.frequency = frequency;
|
||||
b.speakerState.ms = ms;
|
||||
|
||||
runtime.queueDisplayUpdate();
|
||||
let cb = getResume();
|
||||
AudioContextManager.tone(frequency, 1);
|
||||
if (ms <= 0) cb();
|
||||
else {
|
||||
setTimeout(() => {
|
||||
AudioContextManager.stop();
|
||||
b.speakerState.frequency = 0;
|
||||
b.speakerState.ms = 0;
|
||||
runtime.queueDisplayUpdate();
|
||||
cb()
|
||||
}, ms);
|
||||
}
|
||||
}
|
||||
}
|
@ -1349,6 +1349,7 @@ namespace pxsim.visuals {
|
||||
this.updateButtonAB();
|
||||
this.updateGestures();
|
||||
this.updateRgbLed();
|
||||
this.updateSpeaker();
|
||||
|
||||
if (!runtime || runtime.dead) svg.addClass(this.element, "grayscale");
|
||||
else svg.removeClass(this.element, "grayscale");
|
||||
@ -1371,6 +1372,15 @@ namespace pxsim.visuals {
|
||||
}
|
||||
}
|
||||
|
||||
private updateSpeaker() {
|
||||
let state = this.board;
|
||||
if (state.speakerState.frequency) {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private updateGestures() {
|
||||
let state = this.board;
|
||||
if (state.accelerometerState.useShake && !this.shakeButton) {
|
||||
|
Loading…
Reference in New Issue
Block a user