exposing serial number block.
This commit is contained in:
parent
cdb23fffb6
commit
db0c5d9f9f
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,6 +234,13 @@ namespace pxsim.control {
|
|||||||
: '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