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

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_P15 = 39,
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_NOTIFY_ONE = 1022,
MICROBIT_ID_NOTIFY = 1023,

View File

@ -264,7 +264,9 @@ declare namespace motors {
//P16 = MICROBIT_ID_IO_P16,
P19 = 24, // MICROBIT_ID_IO_P19
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
P4 = 11, // MICROBIT_ID_IO_P4
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;
/**
* 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 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
//% parts="speaker"
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.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)
*/
//% help=music/ring-tone weight=80
//% blockId=device_ring block="ring tone (Hz)|%note=device_note" icon="\uf025" blockGap=8
//% parts="speaker"
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.digitalWritePin(DigitalPin.P28, 0); // switch off the motor driver
}
/**
@ -192,7 +198,7 @@ namespace music {
let beat = 60000 / beatsPerMinute;
if (fraction == BeatFraction.Whole) return beat;
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 return beat / 16;
}

View File

@ -20,6 +20,9 @@ enum class DigitalPin {
//P16 = MICROBIT_ID_IO_P16,
P19 = MICROBIT_ID_IO_P19,
P20 = MICROBIT_ID_IO_P20,
P28 = CALLIOPE_ID_IO_P28,
P29 = CALLIOPE_ID_IO_P29,
P30 = CALLIOPE_ID_IO_P30
};
enum class AnalogPin {
@ -29,6 +32,8 @@ enum class AnalogPin {
P3 = MICROBIT_ID_IO_P3,
P4 = MICROBIT_ID_IO_P4,
P10 = MICROBIT_ID_IO_P10,
P29 = CALLIOPE_ID_IO_P29,
P30 = CALLIOPE_ID_IO_P30
};
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_P15: return &uBit.io.CAL_P15;
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;
}
}