broadcast project (#918)

* broadcast project

* revert chaanges
This commit is contained in:
Peli de Halleux 2019-09-24 21:44:59 -07:00 committed by GitHub
parent fc5ecd9f10
commit 83aeb24a98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 1 deletions

View File

@ -0,0 +1,66 @@
/**
* Message broadcasting
*/
//% weight=70
//% color="#58AB41"
namespace broadcast {
const broadcastEventId = control.allocateNotifyEvent();
const broadcastDoneEventId = control.allocateNotifyEvent();
function normalizeId(id: number) {
// upper ids are reserved for answer
return ((id + 1) | 0) & 0xffff;
}
/**
* An enum shim
*/
//% shim=ENUM_GET
//% blockId=msg_enum_shim
//% block="$arg"
//% enumName="Messages"
//% enumMemberName="message"
//% enumPromptHint="e.g. Move, Turn, ..."
//% enumInitialMembers="message1"
//% blockHidden=1
//% enumIsHash=1
export function __messageShim(arg: number) {
// This function should do nothing, but must take in a single
// argument of type number and return a number value.
return arg;
}
/**
* Register code to run when a message is received
*/
//% block="on %id=msg_enum_shim|received"
//% blockId=broadcastonreceived draggableParameters
export function onMessageReceived(message: number, body: () => void) {
const messageid = normalizeId(message);
control.onEvent(broadcastEventId, messageid, function () {
body();
control.raiseEvent(broadcastDoneEventId, messageid);
})
}
/**
* Sends a message to activate code
*/
//% block="send %id=msg_enum_shim"
//% blockId=broadcastsend draggableParameters
export function sendMessage(message: number) {
const messageid = normalizeId(message);
control.raiseEvent(broadcastEventId, messageid);
}
/**
* Sends a message and pauses until the handler to finishes.
*/
//% block="send %id=msg_enum_shim| and pause"
//% blockId=broadcastsendpause
export function sendMessageAndPause(message: number) {
const messageid = normalizeId(message);
control.raiseEvent(broadcastEventId, messageid);
control.waitForEvent(broadcastDoneEventId, messageid);
}
}

11
libs/broadcast/pxt.json Normal file
View File

@ -0,0 +1,11 @@
{
"name": "broadcast",
"description": "Broadcasting messages - beta",
"files": [
"broadcast.ts"
],
"public": true,
"dependencies": {
"core": "file:../core"
}
}

View File

@ -17,7 +17,8 @@
"libs/gyro-sensor", "libs/gyro-sensor",
"libs/screen", "libs/screen",
"libs/ev3", "libs/ev3",
"libs/storage" "libs/storage",
"libs/broadcast"
], ],
"simulator": { "simulator": {
"autoRun": true, "autoRun": true,