From 97b6c7dc4bb6dbbad3589e0b59874f44e866f89a Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Fri, 7 Jul 2017 11:44:34 +0100 Subject: [PATCH] Add music --- TODO.md | 2 +- libs/core/control.cpp | 25 +++++++++++++++++++++ libs/core/dal.d.ts | 3 +++ libs/core/linux.cpp | 11 ++++++--- libs/core/pxt.h | 5 +++++ libs/core/pxt.json | 1 + libs/core/shims.d.ts | 18 +++++++++++++++ libs/ev3/pxt.json | 3 ++- libs/music/music.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++ libs/music/pins.cpp | 1 + libs/music/pxt.json | 21 +++++++++++++++++ libs/music/shims.d.ts | 28 +++++++++++++++++++++++ pxtarget.json | 1 + 13 files changed, 166 insertions(+), 5 deletions(-) create mode 100644 libs/core/control.cpp create mode 100644 libs/music/music.cpp create mode 100644 libs/music/pins.cpp create mode 100644 libs/music/pxt.json create mode 100644 libs/music/shims.d.ts diff --git a/TODO.md b/TODO.md index bce6ceff..99d264d6 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,4 @@ -* [ ] try unlink ELF file before uploading +* [x] try unlink ELF file before uploading - didn't work * [ ] implement serialPoll * [ ] try some motors diff --git a/libs/core/control.cpp b/libs/core/control.cpp new file mode 100644 index 00000000..00cbb611 --- /dev/null +++ b/libs/core/control.cpp @@ -0,0 +1,25 @@ +#include "pxt.h" + +namespace control { + +/** + * Announce that an event happened to registered handlers. + * @param src ID of the Component that generated the event + * @param value Component specific code indicating the cause of the event. + * @param mode optional definition of how the event should be processed after construction. + */ +//% weight=21 blockGap=12 blockId="control_raise_event" +//% block="raise event|from %src|with value value" blockExternalInputs=1 +void raiseEvent(int src, int value) { + pxt::raiseEvent(src, value); +} + +/** +* Allocates the next user notification event +*/ +//% help=control/allocate-notify-event +int allocateNotifyEvent() { + return pxt::allocateNotifyEvent(); +} + +} diff --git a/libs/core/dal.d.ts b/libs/core/dal.d.ts index 326fca7e..2f3f4c37 100644 --- a/libs/core/dal.d.ts +++ b/libs/core/dal.d.ts @@ -647,6 +647,9 @@ declare const enum DAL { // built/dockermake/node_modules/ev3api-bin/include/uart.h // built/dockermake/pxtapp/pxt.h ID_BUTTON_BASE = 100, + DEVICE_EVT_ANY = 0, + DEVICE_ID_NOTIFY = 10000, + DEVICE_ID_NOTIFY_ONE = 10001, // built/dockermake/pxtapp/pxtbase.h PXT_REF_TAG_STRING = 1, PXT_REF_TAG_BUFFER = 2, diff --git a/libs/core/linux.cpp b/libs/core/linux.cpp index 374cf16b..bf6005c4 100644 --- a/libs/core/linux.cpp +++ b/libs/core/linux.cpp @@ -9,8 +9,6 @@ #include #include -#define DEVICE_EVT_ANY 0 - void *operator new(size_t size) { return malloc(size); } @@ -237,6 +235,14 @@ static void *evtDispatcher(void *dummy) { } } +int allocateNotifyEvent() { + static volatile int notifyId; + pthread_mutex_lock(&eventMutex); + int res = ++notifyId; + pthread_mutex_unlock(&eventMutex); + return res; +} + void raiseEvent(int id, int event) { auto e = mkEvent(id, event); pthread_mutex_lock(&eventMutex); @@ -293,5 +299,4 @@ void dmesg(const char *format, ...) { fflush(dmesgFile); fdatasync(fileno(dmesgFile)); } - } diff --git a/libs/core/pxt.h b/libs/core/pxt.h index d938e920..74560f0b 100644 --- a/libs/core/pxt.h +++ b/libs/core/pxt.h @@ -7,6 +7,7 @@ namespace pxt { void raiseEvent(int id, int event); +int allocateNotifyEvent(); void sleep_core_us(uint64_t us); class Button; @@ -15,4 +16,8 @@ typedef Button *Button_; extern "C" void target_init(); } +#define DEVICE_EVT_ANY 0 +#define DEVICE_ID_NOTIFY 10000 +#define DEVICE_ID_NOTIFY_ONE 10001 + #endif diff --git a/libs/core/pxt.json b/libs/core/pxt.json index e2f34ecc..1f6d1c17 100644 --- a/libs/core/pxt.json +++ b/libs/core/pxt.json @@ -7,6 +7,7 @@ "pxt.h", "pxtcore.h", "linux.cpp", + "control.cpp", "buttons.cpp", "screen.cpp", "screen.ts", diff --git a/libs/core/shims.d.ts b/libs/core/shims.d.ts index d27bc840..956aa617 100644 --- a/libs/core/shims.d.ts +++ b/libs/core/shims.d.ts @@ -1,4 +1,22 @@ // Auto-generated. Do not edit. +declare namespace control { + + /** + * Announce that an event happened to registered handlers. + * @param src ID of the Component that generated the event + * @param value Component specific code indicating the cause of the event. + * @param mode optional definition of how the event should be processed after construction. + */ + //% weight=21 blockGap=12 blockId="control_raise_event" + //% block="raise event|from %src|with value value" blockExternalInputs=1 shim=control::raiseEvent + function raiseEvent(src: int32, value: int32): void; + + /** + * Allocates the next user notification event + */ + //% help=control/allocate-notify-event shim=control::allocateNotifyEvent + function allocateNotifyEvent(): int32; +} declare namespace input { /** diff --git a/libs/ev3/pxt.json b/libs/ev3/pxt.json index 069669c9..f9f04342 100644 --- a/libs/ev3/pxt.json +++ b/libs/ev3/pxt.json @@ -7,7 +7,8 @@ ], "dependencies": { "base": "file:../base", - "core": "file:../core" + "core": "file:../core", + "music": "file:../music" }, "public": true } diff --git a/libs/music/music.cpp b/libs/music/music.cpp new file mode 100644 index 00000000..7f0459e5 --- /dev/null +++ b/libs/music/music.cpp @@ -0,0 +1,52 @@ +#include "pxt.h" +#include "ev3.h" + +#define NOTE_PAUSE 20 + +namespace music { + +byte currVolume = 100; + +/** +* Set the output volume of the sound synthesizer. +* @param volume the volume 0...256, eg: 128 +*/ +//% weight=96 +//% blockId=synth_set_volume block="set volume %volume" +//% parts="speaker" blockGap=8 +//% volume.min=0 volume.max=256 +//% help=music/set-volume +//% weight=1 +void setVolume(int volume) { + currVolume = max(0, min(100, volume * 100 / 256)); +} + +/** +* Play a tone through the speaker for some amount of time. +* @param frequency pitch of the tone to play in Hertz (Hz) +* @param ms tone duration in milliseconds (ms) +*/ +//% help=music/play-tone weight=90 +//% blockId=music_play_note block="play tone|at %note=device_note|for %duration=device_beat" +//% parts="headphone" async blockGap=8 +//% blockNamespace=music +void playTone(int frequency, int ms) { + if (frequency <= 0) { + StopSound(); + if (ms >= 0) + sleep_ms(ms); + } else { + if (ms > 0) { + int d = max(1, ms - NOTE_PAUSE); // allow for short rest + int r = max(1, ms - d); + PlayToneEx(frequency, d, currVolume); + sleep_ms(d + r); + } else { + // ring + PlayToneEx(frequency, 60000, currVolume); + } + } + sleep_ms(1); +} + +} \ No newline at end of file diff --git a/libs/music/pins.cpp b/libs/music/pins.cpp new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/libs/music/pins.cpp @@ -0,0 +1 @@ + diff --git a/libs/music/pxt.json b/libs/music/pxt.json new file mode 100644 index 00000000..7e580311 --- /dev/null +++ b/libs/music/pxt.json @@ -0,0 +1,21 @@ +{ + "name": "music", + "description": "The music library", + "files": [ + "README.md", + "music.cpp", + "enums.d.ts", + "shims.d.ts", + "melodies.ts", + "music.ts", + "ns.ts" + ], + "testFiles": [ + "test.ts" + ], + "public": true, + "additionalFilePath": "../../node_modules/pxt-common-packages/libs/music", + "dependencies": { + "core": "file:../core" + } +} \ No newline at end of file diff --git a/libs/music/shims.d.ts b/libs/music/shims.d.ts new file mode 100644 index 00000000..3389bfc2 --- /dev/null +++ b/libs/music/shims.d.ts @@ -0,0 +1,28 @@ +// Auto-generated. Do not edit. +declare namespace music { + + /** + * Set the output volume of the sound synthesizer. + * @param volume the volume 0...256, eg: 128 + */ + //% weight=96 + //% blockId=synth_set_volume block="set volume %volume" + //% parts="speaker" blockGap=8 + //% volume.min=0 volume.max=256 + //% help=music/set-volume + //% weight=1 shim=music::setVolume + function setVolume(volume: int32): void; + + /** + * Play a tone through the speaker for some amount of time. + * @param frequency pitch of the tone to play in Hertz (Hz) + * @param ms tone duration in milliseconds (ms) + */ + //% help=music/play-tone weight=90 + //% blockId=music_play_note block="play tone|at %note=device_note|for %duration=device_beat" + //% parts="headphone" async blockGap=8 + //% blockNamespace=music shim=music::playTone + function playTone(frequency: int32, ms: int32): void; +} + +// Auto-generated. Do not edit. Really. diff --git a/pxtarget.json b/pxtarget.json index 67c953ff..e1bb6a69 100644 --- a/pxtarget.json +++ b/pxtarget.json @@ -9,6 +9,7 @@ "bundleddirs": [ "libs/base", "libs/core", + "libs/music", "libs/ev3" ], "simulator": {