fix simulator support for music

This commit is contained in:
Peli de Halleux 2016-10-27 13:47:39 -07:00
parent 8fca50e907
commit 40fe1b4616
5 changed files with 43 additions and 2 deletions

View File

@ -8,7 +8,7 @@ namespace music {
*/ */
//% help=music/play-tone weight=90 //% help=music/play-tone weight=90
//% blockId=device_play_note block="play|tone %note=device_note|for %duration=device_beat" icon="\uf025" blockGap=8 //% 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) { void playTone(int freqency, int ms) {
uBit.soundmotor.Sound_On(freqency); uBit.soundmotor.Sound_On(freqency);
if(ms > 0) uBit.sleep(ms); if(ms > 0) uBit.sleep(ms);

View File

@ -533,7 +533,7 @@ declare namespace music {
*/ */
//% help=music/play-tone weight=90 //% help=music/play-tone weight=90
//% blockId=device_play_note block="play|tone %note=device_note|for %duration=device_beat" icon="\uf025" blockGap=8 //% 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; function playTone(freqency: number, ms: number): void;
/** /**

View File

@ -14,6 +14,7 @@ namespace pxsim {
radioState: RadioState; radioState: RadioState;
neopixelState: NeoPixelState; neopixelState: NeoPixelState;
rgbLedState: number; rgbLedState: number;
speakerState: SpeakerState;
constructor() { constructor() {
super() super()
@ -59,6 +60,7 @@ namespace pxsim {
this.builtinParts["lightsensor"] = this.lightSensorState = new LightSensorState(); this.builtinParts["lightsensor"] = this.lightSensorState = new LightSensorState();
this.builtinParts["compass"] = this.compassState = new CompassState(); this.builtinParts["compass"] = this.compassState = new CompassState();
this.builtinParts["neopixel"] = this.neopixelState = new NeoPixelState(); this.builtinParts["neopixel"] = this.neopixelState = new NeoPixelState();
this.builtinParts["speaker"] = this.speakerState = new SpeakerState();
this.builtinVisuals["buttonpair"] = () => new visuals.ButtonPairView(); this.builtinVisuals["buttonpair"] = () => new visuals.ButtonPairView();
this.builtinVisuals["ledmatrix"] = () => new visuals.LedMatrixView(); this.builtinVisuals["ledmatrix"] = () => new visuals.LedMatrixView();

29
sim/state/music.ts Normal file
View 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);
}
}
}

View File

@ -1349,6 +1349,7 @@ namespace pxsim.visuals {
this.updateButtonAB(); this.updateButtonAB();
this.updateGestures(); this.updateGestures();
this.updateRgbLed(); this.updateRgbLed();
this.updateSpeaker();
if (!runtime || runtime.dead) svg.addClass(this.element, "grayscale"); if (!runtime || runtime.dead) svg.addClass(this.element, "grayscale");
else svg.removeClass(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() { private updateGestures() {
let state = this.board; let state = this.board;
if (state.accelerometerState.useShake && !this.shakeButton) { if (state.accelerometerState.useShake && !this.shakeButton) {