From 40fe1b46163778ead2b9126609833adf5660b43f Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Thu, 27 Oct 2016 13:47:39 -0700 Subject: [PATCH] fix simulator support for music --- libs/core/music.cpp | 2 +- libs/core/shims.d.ts | 2 +- sim/dalboard.ts | 2 ++ sim/state/music.ts | 29 +++++++++++++++++++++++++++++ sim/visuals/microbit.ts | 10 ++++++++++ 5 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 sim/state/music.ts diff --git a/libs/core/music.cpp b/libs/core/music.cpp index 544c4089..0bde4657 100644 --- a/libs/core/music.cpp +++ b/libs/core/music.cpp @@ -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); diff --git a/libs/core/shims.d.ts b/libs/core/shims.d.ts index affa3b2d..46185bfb 100644 --- a/libs/core/shims.d.ts +++ b/libs/core/shims.d.ts @@ -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; /** diff --git a/sim/dalboard.ts b/sim/dalboard.ts index 962ad55e..6c880ba4 100644 --- a/sim/dalboard.ts +++ b/sim/dalboard.ts @@ -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(); diff --git a/sim/state/music.ts b/sim/state/music.ts new file mode 100644 index 00000000..cb040e4c --- /dev/null +++ b/sim/state/music.ts @@ -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); + } + } +} \ No newline at end of file diff --git a/sim/visuals/microbit.ts b/sim/visuals/microbit.ts index cbb658a3..0b53b417 100644 --- a/sim/visuals/microbit.ts +++ b/sim/visuals/microbit.ts @@ -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) {