Add music
This commit is contained in:
		
							
								
								
									
										2
									
								
								TODO.md
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								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
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										25
									
								
								libs/core/control.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								libs/core/control.cpp
									
									
									
									
									
										Normal 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
									
									
								
							
							
						
						
									
										3
									
								
								libs/core/dal.d.ts
									
									
									
									
										vendored
									
									
								
							@@ -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,
 | 
			
		||||
 
 | 
			
		||||
@@ -9,8 +9,6 @@
 | 
			
		||||
#include <assert.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
#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));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,7 @@
 | 
			
		||||
        "pxt.h",
 | 
			
		||||
        "pxtcore.h",
 | 
			
		||||
        "linux.cpp",
 | 
			
		||||
        "control.cpp",
 | 
			
		||||
        "buttons.cpp",
 | 
			
		||||
        "screen.cpp",
 | 
			
		||||
        "screen.ts",
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										18
									
								
								libs/core/shims.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										18
									
								
								libs/core/shims.d.ts
									
									
									
									
										vendored
									
									
								
							@@ -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 {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,8 @@
 | 
			
		||||
    ],
 | 
			
		||||
    "dependencies": {
 | 
			
		||||
        "base": "file:../base",
 | 
			
		||||
        "core": "file:../core"
 | 
			
		||||
        "core": "file:../core",
 | 
			
		||||
        "music": "file:../music"
 | 
			
		||||
    },
 | 
			
		||||
    "public": true
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										52
									
								
								libs/music/music.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								libs/music/music.cpp
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										1
									
								
								libs/music/pins.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										21
									
								
								libs/music/pxt.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								libs/music/pxt.json
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										28
									
								
								libs/music/shims.d.ts
									
									
									
									
										vendored
									
									
										Normal 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.
 | 
			
		||||
@@ -9,6 +9,7 @@
 | 
			
		||||
    "bundleddirs": [
 | 
			
		||||
        "libs/base",
 | 
			
		||||
        "libs/core",
 | 
			
		||||
        "libs/music",
 | 
			
		||||
        "libs/ev3"
 | 
			
		||||
    ],
 | 
			
		||||
    "simulator": {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user