Merge branch 'master' of https://github.com/Microsoft/pxt-microbit
This commit is contained in:
commit
ea724038dc
@ -159,7 +159,7 @@ namespace control {
|
|||||||
/**
|
/**
|
||||||
* Gets a friendly name for the device derived from the its serial number
|
* Gets a friendly name for the device derived from the its serial number
|
||||||
*/
|
*/
|
||||||
//% blockId="control_device_name" block="device name" weight=10
|
//% blockId="control_device_name" block="device name" weight=10 blockGap=8
|
||||||
StringData* deviceName() {
|
StringData* deviceName() {
|
||||||
return ManagedString(microbit_friendly_name()).leakData();
|
return ManagedString(microbit_friendly_name()).leakData();
|
||||||
}
|
}
|
||||||
@ -167,7 +167,7 @@ namespace control {
|
|||||||
/**
|
/**
|
||||||
* Derive a unique, consistent serial number of this device from internal data.
|
* Derive a unique, consistent serial number of this device from internal data.
|
||||||
*/
|
*/
|
||||||
//%
|
//% blockId="control_device_serial_number" block="device serial number" weight=9
|
||||||
int deviceSerialNumber() {
|
int deviceSerialNumber() {
|
||||||
return microbit_serial_number();
|
return microbit_serial_number();
|
||||||
}
|
}
|
||||||
|
51
libs/microbit/messages.ts
Normal file
51
libs/microbit/messages.ts
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
namespace messages {
|
||||||
|
var streamid: string;
|
||||||
|
|
||||||
|
export function setStreamId(id: string) {
|
||||||
|
streamid = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new message that includes the board serial number and the stream id if any
|
||||||
|
*/
|
||||||
|
export function createMessage() : Message {
|
||||||
|
let m = new Message();
|
||||||
|
m.addNumber('board', control.deviceSerialNumber());
|
||||||
|
if (streamid != null && streamid.length > 0)
|
||||||
|
m.addString('stream', streamid);
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A message containig custom data
|
||||||
|
*/
|
||||||
|
export class Message {
|
||||||
|
private buffer:string = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a string field to the message
|
||||||
|
*/
|
||||||
|
//%
|
||||||
|
public addString(name:string, value:string) {
|
||||||
|
if (this.buffer.length > 0) this.buffer += ',';
|
||||||
|
this.buffer += name + ':"' + value + '"';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a number field to the message
|
||||||
|
*/
|
||||||
|
//%
|
||||||
|
public addNumber(name:string, value: number) {
|
||||||
|
if (this.buffer.length > 0) this.buffer += ',';
|
||||||
|
this.buffer += name + ':' + value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the message to a JSON payload
|
||||||
|
*/
|
||||||
|
//%
|
||||||
|
public toJSON() : string {
|
||||||
|
return '{' + this.buffer + '}';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
4
libs/microbit/shims.d.ts
vendored
4
libs/microbit/shims.d.ts
vendored
@ -346,13 +346,13 @@ declare namespace control {
|
|||||||
/**
|
/**
|
||||||
* Gets a friendly name for the device derived from the its serial number
|
* Gets a friendly name for the device derived from the its serial number
|
||||||
*/
|
*/
|
||||||
//% blockId="control_device_name" block="device name" weight=10 shim=control::deviceName
|
//% blockId="control_device_name" block="device name" weight=10 blockGap=8 shim=control::deviceName
|
||||||
function deviceName(): string;
|
function deviceName(): string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Derive a unique, consistent serial number of this device from internal data.
|
* Derive a unique, consistent serial number of this device from internal data.
|
||||||
*/
|
*/
|
||||||
//% shim=control::deviceSerialNumber
|
//% blockId="control_device_serial_number" block="device serial number" weight=9 shim=control::deviceSerialNumber
|
||||||
function deviceSerialNumber(): number;
|
function deviceSerialNumber(): number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@
|
|||||||
},
|
},
|
||||||
"runtime": {
|
"runtime": {
|
||||||
"mathBlocks": true,
|
"mathBlocks": true,
|
||||||
"loopBlocks": true,
|
"loopsBlocks": true,
|
||||||
"logicBlocks": true,
|
"logicBlocks": true,
|
||||||
"variablesBlocks": true
|
"variablesBlocks": true
|
||||||
},
|
},
|
||||||
|
@ -233,6 +233,13 @@ namespace pxsim.control {
|
|||||||
? b.id.slice(0, 4)
|
? b.id.slice(0, 4)
|
||||||
: 'abcd';
|
: 'abcd';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function deviceSerialNumber(): number {
|
||||||
|
let b = board();
|
||||||
|
return parseInt(b && b.id
|
||||||
|
? b.id.slice(1)
|
||||||
|
: '42');
|
||||||
|
}
|
||||||
|
|
||||||
export function onEvent(id: number, evid: number, handler: RefAction) {
|
export function onEvent(id: number, evid: number, handler: RefAction) {
|
||||||
pxt.registerWithDal(id, evid, handler)
|
pxt.registerWithDal(id, evid, handler)
|
||||||
|
@ -511,7 +511,7 @@ namespace pxsim {
|
|||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super()
|
super()
|
||||||
this.id = "b" + Math.random().toString().slice(1);
|
this.id = "b" +Math_.random(2147483647);
|
||||||
this.animationQ = new AnimationQueue(runtime);
|
this.animationQ = new AnimationQueue(runtime);
|
||||||
this.bus = new EventBus(runtime);
|
this.bus = new EventBus(runtime);
|
||||||
this.radio = new RadioBus(runtime);
|
this.radio = new RadioBus(runtime);
|
||||||
|
Loading…
Reference in New Issue
Block a user