add sound support using the on-board speaker

This commit is contained in:
Matthias L. Jugel 2016-10-26 20:52:40 +02:00
parent d6d64f3edb
commit a4ba24d6ef
5 changed files with 339 additions and 42 deletions

View File

@ -1,51 +1,327 @@
basic.setLedColor(Colors.Blue) // Für Elise...
basic.pause(500)
basic.setLedColor(Colors.Red) // play e4
basic.pause(500) music.playTone(330, 300);
basic.setLedColor(Colors.Green) basic.pause(350);
basic.pause(500) // play d4#
basic.setLedColor(Colors.Violet) music.playTone(311, 300);
basic.pause(500) basic.pause(350);
// play e4
music.playTone(330, 300);
basic.pause(350);
// play d4#
music.playTone(311, 300);
basic.pause(350);
// play e4
music.playTone(330, 300);
basic.pause(350);
// play b3
music.playTone(247, 300);
basic.pause(400);
// play d4
music.playTone(294, 300);
basic.pause(400);
// play c4
music.playTone(262, 300);
basic.pause(400);
// play a3
music.playTone(220, 900);
basic.pause(1000);
// play d3
music.playTone(147, 300);
basic.pause(350);
//play f3
music.playTone(175, 300);
basic.pause(400);
//play a3
music.playTone(220, 300);
basic.pause(400);
// play b3
music.playTone(247, 900);
basic.pause(1000);
// play f3
music.playTone(175, 300);
basic.pause(400);
// play a3#
music.playTone(233, 300);
basic.pause(400);
// play b3
music.playTone(247, 300);
basic.pause(400);
// play c4
music.playTone(262, 900);
basic.pause(1000);
basic.pause(300);
// play e4
music.playTone(330, 300);
basic.pause(400);
// play d4#
music.playTone(311, 300);
basic.pause(400);
// play e4
music.playTone(330, 300);
basic.pause(400);
// play d4#
music.playTone(311, 300);
basic.pause(400);
// play e4
music.playTone(330, 300);
basic.pause(400);
// play b3
music.playTone(247, 300);
basic.pause(400);
// play d4
music.playTone(294, 300);
basic.pause(400);
// play c4
music.playTone(263, 300);
basic.pause(400);
// play a3
music.playTone(220, 900);
basic.pause(1000);
// play d3
music.playTone(147, 300);
basic.pause(400);
// play f3
music.playTone(175, 300);
basic.pause(400);
// play a3
music.playTone(220, 300);
basic.pause(400);
// play b3
music.playTone(247, 900);
basic.pause(1000);
// play f3
music.playTone(175, 300);
basic.pause(400);
// play c4
music.playTone(262, 300);
basic.pause(400);
// play b3
music.playTone(247, 300);
basic.pause(400);
// play a3
music.playTone(220, 900);
basic.pause(1000);
// play b3
music.playTone(247, 300);
basic.pause(400);
// play c4
music.playTone(262, 300);
basic.pause(400);
// play d4
music.playTone(294, 300);
basic.pause(400);
// play e4
music.playTone(330, 900);
basic.pause(1000);
// play g3
music.playTone(196, 300);
basic.pause(400);
// play f4
music.playTone(349, 300);
basic.pause(400);
//play e4
music.playTone(330, 300);
basic.pause(400);
// play d4
music.playTone(294, 900);
basic.pause(1000);
// play e3
music.playTone(165, 300);
basic.pause(400);
// play e4
music.playTone(330, 300);
basic.pause(400);
// play d4
music.playTone(294, 300);
basic.pause(400);
// play c4
music.playTone(262, 900);
basic.pause(1000);
// play d3
music.playTone(147, 300);
basic.pause(400);
// play d4
music.playTone(294, 300);
basic.pause(400);
// play c4
music.playTone(262, 300);
basic.pause(400);
// play b3
music.playTone(247, 900);
basic.pause(1000);
basic.pause(400);
// play e4
music.playTone(330, 300);
basic.pause(400);
// play d4#
music.playTone(311, 300);
basic.pause(350);
// play e4
music.playTone(330, 300);
basic.pause(350);
// play d4#
music.playTone(311, 300);
basic.pause(350);
// play e4
music.playTone(330, 300);
basic.pause(350);
// play b3
music.playTone(247, 300);
basic.pause(400);
// play d4
music.playTone(294, 300);
basic.pause(400);
// play c4
music.playTone(262, 300);
basic.pause(400);
// play a3
music.playTone(220, 900);
basic.pause(1000);
// play d3
music.playTone(147, 300);
basic.pause(350);
//play f3
music.playTone(175, 300);
basic.pause(400);
//play a3
music.playTone(220, 300);
basic.pause(400);
// play b3
music.playTone(247, 900);
basic.pause(1000);
// play f3
music.playTone(175, 300);
basic.pause(400);
// play a3
music.playTone(233, 300);
basic.pause(400);
// play b3
music.playTone(247, 300);
basic.pause(400);
// play c4
music.playTone(262, 900);
basic.pause(1000);
basic.pause(300);
// play e4
music.playTone(330, 300);
basic.pause(400);
// play d4#
music.playTone(311, 300);
basic.pause(400);
// play e4
music.playTone(330, 300);
basic.pause(400);
// play d4#
music.playTone(311, 300);
basic.pause(400);
// play e4
music.playTone(330, 300);
basic.pause(400);
// play b3
music.playTone(247, 300);
basic.pause(400);
// play d4
music.playTone(294, 300);
basic.pause(400);
// play c4
music.playTone(262, 300);
basic.pause(400);
// play a3
music.playTone(220, 900);
basic.pause(1000);
// play d3
music.playTone(147, 300);
basic.pause(400);
// play f3
music.playTone(175, 300);
basic.pause(400);
// play a3
music.playTone(220, 300);
basic.pause(400);
// play b3
music.playTone(247, 900);
basic.pause(1000);
// play f3
music.playTone(175, 300);
basic.pause(400);
// play c4
music.playTone(262, 300);
basic.pause(400);
// play b3
music.playTone(247, 300);
basic.pause(400);
// play a3
music.playTone(220, 900);
basic.pause(1000);
// and again slightly different
var notes = [
Note.E5, Note.Eb5, Note.E5, Note.Eb5, Note.E5, Note.B5, Note.D5, Note.C5, Note.A5,
Note.C5, Note.E5, Note.A5, Note.B5, Note.E5, Note.A5, Note.B5, Note.C5,
Note.E5, Note.Eb5, Note.E5, Note.Eb5, Note.E5, Note.B5, Note.D5, Note.C5, Note.A5,
Note.C5, Note.E5, Note.A5, Note.B5, Note.E5, Note.C5, Note.B5, Note.A5,
Note.B5, Note.C5, Note.D5, Note.E5, Note.G, Note.F, Note.E5, Note.D5, Note.E5, Note.E5, Note.D5, Note.C5, Note.E5, Note.D5, Note.C5, Note.E5,
Note.E5, Note.Eb5, Note.E5, Note.Eb5, Note.E5, Note.E5, Note.D5, Note.C5, Note.A5,
Note.C5, Note.E5, Note.A5, Note.B5, Note.E5, Note.A5, Note.B5, Note.C5,
Note.E5, Note.Eb5, Note.E5, Note.Eb5, Note.E5, Note.B5, Note.D5, Note.C5, Note.A5,
Note.C5, Note.E5, Note.A5, Note.B5, Note.E5, Note.C5, Note.B5, Note.A5
];
for (let note of notes) {
music.playTone(note, 100);
}
basic.setLedColor(Colors.Blue);
basic.pause(500);
basic.setLedColor(Colors.Red);
basic.pause(500);
basic.setLedColor(Colors.Green);
basic.pause(500);
basic.setLedColor(Colors.Violet);
basic.pause(500);
basic.setLedColor(0); basic.setLedColor(0);
input.onGesture(Gesture.Shake, () => { input.onGesture(Gesture.Shake, () => {
basic.showString("S") basic.showString("S");
}) });
input.onGesture(Gesture.LogoUp, () => { input.onGesture(Gesture.LogoUp, () => {
basic.showString("U") basic.showString("U");
}) });
input.onGesture(Gesture.LogoDown, () => { input.onGesture(Gesture.LogoDown, () => {
basic.showString("D") basic.showString("D");
}) });
input.onGesture(Gesture.ScreenUp, () => { input.onGesture(Gesture.ScreenUp, () => {
basic.showString("+") basic.showString("+");
}) });
input.onGesture(Gesture.TiltRight, () => { input.onGesture(Gesture.TiltRight, () => {
basic.showString("R") basic.showString("R");
}) });
input.onGesture(Gesture.FreeFall, () => { input.onGesture(Gesture.FreeFall, () => {
basic.showString("F") basic.showString("F");
}) });
input.onGesture(Gesture.ScreenDown, () => { input.onGesture(Gesture.ScreenDown, () => {
basic.showString("-") basic.showString("-");
}) });
input.onGesture(Gesture.TiltLeft, () => { input.onGesture(Gesture.TiltLeft, () => {
basic.showString("L") basic.showString("L");
}) });
input.onGesture(Gesture.ThreeG, () => { input.onGesture(Gesture.ThreeG, () => {
basic.showString("3") basic.showString("3");
}) });
input.onGesture(Gesture.SixG, () => { input.onGesture(Gesture.SixG, () => {
basic.showString("6") basic.showString("6");
}) });
input.onPinPressed(TouchPin.P0, () => { input.onPinPressed(TouchPin.P0, () => {
basic.showNumber(0) basic.showNumber(0);
}) });
input.onPinPressed(TouchPin.P1, () => { input.onPinPressed(TouchPin.P1, () => {
basic.showNumber(1) basic.showNumber(1);
}) });
input.onPinPressed(TouchPin.P2, () => { input.onPinPressed(TouchPin.P2, () => {
basic.showNumber(2) basic.showNumber(2);
}) });
input.onPinPressed(TouchPin.P3, () => { input.onPinPressed(TouchPin.P3, () => {
basic.showNumber(3) basic.showNumber(3);
}) });

3
libs/core/dal.d.ts vendored
View File

@ -153,6 +153,9 @@ declare const enum DAL {
CALLIOPE_ID_IO_P14 = 38, CALLIOPE_ID_IO_P14 = 38,
CALLIOPE_ID_IO_P15 = 39, CALLIOPE_ID_IO_P15 = 39,
CALLIOPE_ID_IO_P22 = 40, CALLIOPE_ID_IO_P22 = 40,
CALLIOPE_ID_IO_P28 = 41,
CALLIOPE_ID_IO_P29 = 42,
CALLIOPE_ID_IO_P30 = 43,
MICROBIT_ID_MESSAGE_BUS_LISTENER = 1021, MICROBIT_ID_MESSAGE_BUS_LISTENER = 1021,
MICROBIT_ID_NOTIFY_ONE = 1022, MICROBIT_ID_NOTIFY_ONE = 1022,
MICROBIT_ID_NOTIFY = 1023, MICROBIT_ID_NOTIFY = 1023,

View File

@ -264,7 +264,9 @@ declare namespace motors {
//P16 = MICROBIT_ID_IO_P16, //P16 = MICROBIT_ID_IO_P16,
P19 = 24, // MICROBIT_ID_IO_P19 P19 = 24, // MICROBIT_ID_IO_P19
P20 = 25, // MICROBIT_ID_IO_P20 P20 = 25, // MICROBIT_ID_IO_P20
P22 = 40, // CALLIOPE_ID_IO_P22 P28 = 41, // CALLIOPE_ID_IO_P28
P29 = 42, // CALLIOPE_ID_IO_P29
P30 = 43, // CALLIOPE_ID_IO_P30
} }
@ -275,6 +277,8 @@ declare namespace motors {
P3 = 10, // MICROBIT_ID_IO_P3 P3 = 10, // MICROBIT_ID_IO_P3
P4 = 11, // MICROBIT_ID_IO_P4 P4 = 11, // MICROBIT_ID_IO_P4
P10 = 17, // MICROBIT_ID_IO_P10 P10 = 17, // MICROBIT_ID_IO_P10
P29 = 42, // CALLIOPE_ID_IO_P29
P30 = 43, // CALLIOPE_ID_IO_P30
} }

View File

@ -130,7 +130,7 @@ namespace music {
let beatsPerMinute: number = 120; let beatsPerMinute: number = 120;
/** /**
* Plays a tone through pin ``P0`` for the given duration. * Plays a tone through ``speaker`` for the given duration.
* @param frequency pitch of the tone to play in Hertz (Hz) * @param frequency pitch of the tone to play in Hertz (Hz)
* @param ms tone duration in milliseconds (ms) * @param ms tone duration in milliseconds (ms)
*/ */
@ -138,20 +138,26 @@ namespace music {
//% 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"
export function playTone(frequency: number, ms: number): void { export function playTone(frequency: number, ms: number): void {
pins.analogSetPitchPin(AnalogPin.P0); // TODO check timing
pins.digitalWritePin(DigitalPin.P28, 1); // switch on the motor driver
pins.analogSetPitchPin(AnalogPin.P29);
pins.analogPitch(frequency, ms); pins.analogPitch(frequency, ms);
pins.digitalWritePin(DigitalPin.P28, 0); // switch off the motor driver
} }
/** /**
* Plays a tone through pin ``P0``. * Plays a tone through ``speaker``.
* @param frequency pitch of the tone to play in Hertz (Hz) * @param frequency pitch of the tone to play in Hertz (Hz)
*/ */
//% help=music/ring-tone weight=80 //% help=music/ring-tone weight=80
//% blockId=device_ring block="ring tone (Hz)|%note=device_note" icon="\uf025" blockGap=8 //% blockId=device_ring block="ring tone (Hz)|%note=device_note" icon="\uf025" blockGap=8
//% parts="speaker" //% parts="speaker"
export function ringTone(frequency: number): void { export function ringTone(frequency: number): void {
pins.analogSetPitchPin(AnalogPin.P0); // TODO check timing
pins.digitalWritePin(DigitalPin.P28, 1); // switch on the motor driver
pins.analogSetPitchPin(AnalogPin.P29);
pins.analogPitch(frequency, 0); pins.analogPitch(frequency, 0);
pins.digitalWritePin(DigitalPin.P28, 0); // switch off the motor driver
} }
/** /**
@ -192,7 +198,7 @@ namespace music {
let beat = 60000 / beatsPerMinute; let beat = 60000 / beatsPerMinute;
if (fraction == BeatFraction.Whole) return beat; if (fraction == BeatFraction.Whole) return beat;
else if (fraction == BeatFraction.Half) return beat / 2; else if (fraction == BeatFraction.Half) return beat / 2;
else if (fraction == BeatFraction.Quarter) return beat / 4 else if (fraction == BeatFraction.Quarter) return beat / 4;
else if (fraction == BeatFraction.Eighth) return beat / 8; else if (fraction == BeatFraction.Eighth) return beat / 8;
else return beat / 16; else return beat / 16;
} }

View File

@ -20,6 +20,9 @@ enum class DigitalPin {
//P16 = MICROBIT_ID_IO_P16, //P16 = MICROBIT_ID_IO_P16,
P19 = MICROBIT_ID_IO_P19, P19 = MICROBIT_ID_IO_P19,
P20 = MICROBIT_ID_IO_P20, P20 = MICROBIT_ID_IO_P20,
P28 = CALLIOPE_ID_IO_P28,
P29 = CALLIOPE_ID_IO_P29,
P30 = CALLIOPE_ID_IO_P30
}; };
enum class AnalogPin { enum class AnalogPin {
@ -29,6 +32,8 @@ enum class AnalogPin {
P3 = MICROBIT_ID_IO_P3, P3 = MICROBIT_ID_IO_P3,
P4 = MICROBIT_ID_IO_P4, P4 = MICROBIT_ID_IO_P4,
P10 = MICROBIT_ID_IO_P10, P10 = MICROBIT_ID_IO_P10,
P29 = CALLIOPE_ID_IO_P29,
P30 = CALLIOPE_ID_IO_P30
}; };
enum class PulseValue { enum class PulseValue {
@ -74,6 +79,9 @@ MicroBitPin *getPin(int id) {
case CALLIOPE_ID_IO_P14: return &uBit.io.CAL_P14; case CALLIOPE_ID_IO_P14: return &uBit.io.CAL_P14;
case CALLIOPE_ID_IO_P15: return &uBit.io.CAL_P15; case CALLIOPE_ID_IO_P15: return &uBit.io.CAL_P15;
case CALLIOPE_ID_IO_P22: return &uBit.io.CAL_P22; case CALLIOPE_ID_IO_P22: return &uBit.io.CAL_P22;
case CALLIOPE_ID_IO_P28: return &uBit.io.CAL_P28;
case CALLIOPE_ID_IO_P29: return &uBit.io.CAL_P29;
case CALLIOPE_ID_IO_P30: return &uBit.io.CAL_P30;
default: return NULL; default: return NULL;
} }
} }