exposing serial number block.

This commit is contained in:
Peli de Halleux 2016-04-28 10:46:03 -07:00
parent cdb23fffb6
commit db0c5d9f9f
5 changed files with 63 additions and 5 deletions

View File

@ -159,7 +159,7 @@ namespace control {
/**
* 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() {
return ManagedString(microbit_friendly_name()).leakData();
}
@ -167,7 +167,7 @@ namespace control {
/**
* 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() {
return microbit_serial_number();
}

51
libs/microbit/messages.ts Normal file
View 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 + '}';
}
}
}

View File

@ -346,13 +346,13 @@ declare namespace control {
/**
* 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;
/**
* 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;
}

View File

@ -234,6 +234,13 @@ namespace pxsim.control {
: '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) {
pxt.registerWithDal(id, evid, handler)
}

View File

@ -511,7 +511,7 @@ namespace pxsim {
constructor() {
super()
this.id = "b" + Math.random().toString().slice(1);
this.id = "b" +Math_.random(2147483647);
this.animationQ = new AnimationQueue(runtime);
this.bus = new EventBus(runtime);
this.radio = new RadioBus(runtime);