better music support

This commit is contained in:
Peli de Halleux
2016-03-14 08:32:02 -07:00
parent 53c3b22c1c
commit 4cd222ec82
4 changed files with 36 additions and 8 deletions

View File

@ -322,6 +322,8 @@ namespace ks.rt.micro_bit {
export function setAnalogPeriodUs(pin: Pin, micros:number) {
pin.mode = PinMode.Analog | PinMode.Output;
pin.period = micros;
board().updateView();
}
export function servoWritePin(pin: Pin, value: number) {
@ -385,16 +387,33 @@ namespace ks.rt.micro_bit {
}
export function enablePitch(pin: Pin) {
pin.mode = PinMode.Analog | PinMode.Output;
pin.mode = PinMode.Analog | PinMode.Output | PinMode.Pitch;
}
export function pitch(frequency: number, ms: number) {
// update analog output
let pin = board().pins.filter(pin => !!pin && (pin.mode & PinMode.Pitch) != 0)[0] || board().pins[0];
if (frequency <= 0) {
pin.value = 0;
pin.period = 0;
} else {
pin.value = 512;
pin.period = 1000000/frequency;
}
board().updateView();
let cb = getResume();
AudioContextManager.tone(frequency, 1);
setTimeout(() => {
if (ms > 0) AudioContextManager.stop();
cb()
}, ms);
if (ms <= 0) cb();
else {
setTimeout(() => {
AudioContextManager.stop();
pin.value = 0;
pin.period = 0;
board().updateView();
cb()
}, ms);
}
}

View File

@ -219,7 +219,7 @@ namespace ks.rt.micro_bit {
let v = '';
if (pin.mode & PinMode.Analog) {
v = Math.floor(100 - (pin.value || 0) / 1023 * 100) + '%';
if(text) text.textContent = (pin.value || 0) + "";
if(text) text.textContent = (pin.period ? "~" : "") + (pin.value || 0) + "";
}
else if (pin.mode & PinMode.Digital) {
v = pin.value > 0 ? '0%' : '100%';

View File

@ -14,13 +14,15 @@ namespace ks.rt.micro_bit {
Analog = 0x0002,
Input = 0x0004,
Output = 0x0008,
Touch = 0x0010
Touch = 0x0010,
Pitch = 0x0020
}
export class Pin {
constructor(public id: number) { }
touched = false;
value = 0;
period = 0;
mode = PinMode.Unused;
isTouched(): boolean {