Add music

This commit is contained in:
Michal Moskal 2017-07-07 11:44:34 +01:00
parent 43b167957b
commit 97b6c7dc4b
13 changed files with 166 additions and 5 deletions

View File

@ -1,4 +1,4 @@
* [ ] try unlink ELF file before uploading * [x] try unlink ELF file before uploading - didn't work
* [ ] implement serialPoll * [ ] implement serialPoll
* [ ] try some motors * [ ] try some motors

25
libs/core/control.cpp Normal file
View File

@ -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();
}
}

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

@ -647,6 +647,9 @@ declare const enum DAL {
// built/dockermake/node_modules/ev3api-bin/include/uart.h // built/dockermake/node_modules/ev3api-bin/include/uart.h
// built/dockermake/pxtapp/pxt.h // built/dockermake/pxtapp/pxt.h
ID_BUTTON_BASE = 100, ID_BUTTON_BASE = 100,
DEVICE_EVT_ANY = 0,
DEVICE_ID_NOTIFY = 10000,
DEVICE_ID_NOTIFY_ONE = 10001,
// built/dockermake/pxtapp/pxtbase.h // built/dockermake/pxtapp/pxtbase.h
PXT_REF_TAG_STRING = 1, PXT_REF_TAG_STRING = 1,
PXT_REF_TAG_BUFFER = 2, PXT_REF_TAG_BUFFER = 2,

View File

@ -9,8 +9,6 @@
#include <assert.h> #include <assert.h>
#include <unistd.h> #include <unistd.h>
#define DEVICE_EVT_ANY 0
void *operator new(size_t size) { void *operator new(size_t size) {
return malloc(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) { void raiseEvent(int id, int event) {
auto e = mkEvent(id, event); auto e = mkEvent(id, event);
pthread_mutex_lock(&eventMutex); pthread_mutex_lock(&eventMutex);
@ -293,5 +299,4 @@ void dmesg(const char *format, ...) {
fflush(dmesgFile); fflush(dmesgFile);
fdatasync(fileno(dmesgFile)); fdatasync(fileno(dmesgFile));
} }
} }

View File

@ -7,6 +7,7 @@
namespace pxt { namespace pxt {
void raiseEvent(int id, int event); void raiseEvent(int id, int event);
int allocateNotifyEvent();
void sleep_core_us(uint64_t us); void sleep_core_us(uint64_t us);
class Button; class Button;
@ -15,4 +16,8 @@ typedef Button *Button_;
extern "C" void target_init(); extern "C" void target_init();
} }
#define DEVICE_EVT_ANY 0
#define DEVICE_ID_NOTIFY 10000
#define DEVICE_ID_NOTIFY_ONE 10001
#endif #endif

View File

@ -7,6 +7,7 @@
"pxt.h", "pxt.h",
"pxtcore.h", "pxtcore.h",
"linux.cpp", "linux.cpp",
"control.cpp",
"buttons.cpp", "buttons.cpp",
"screen.cpp", "screen.cpp",
"screen.ts", "screen.ts",

18
libs/core/shims.d.ts vendored
View File

@ -1,4 +1,22 @@
// Auto-generated. Do not edit. // 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 { declare namespace input {
/** /**

View File

@ -7,7 +7,8 @@
], ],
"dependencies": { "dependencies": {
"base": "file:../base", "base": "file:../base",
"core": "file:../core" "core": "file:../core",
"music": "file:../music"
}, },
"public": true "public": true
} }

52
libs/music/music.cpp Normal file
View File

@ -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);
}
}

1
libs/music/pins.cpp Normal file
View File

@ -0,0 +1 @@

21
libs/music/pxt.json Normal file
View File

@ -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"
}
}

28
libs/music/shims.d.ts vendored Normal file
View File

@ -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.

View File

@ -9,6 +9,7 @@
"bundleddirs": [ "bundleddirs": [
"libs/base", "libs/base",
"libs/core", "libs/core",
"libs/music",
"libs/ev3" "libs/ev3"
], ],
"simulator": { "simulator": {