2016-08-30 20:51:32 +02:00
|
|
|
namespace pxsim {
|
|
|
|
export interface PacketBuffer {
|
2016-10-24 21:55:44 +02:00
|
|
|
payload: SimulatorRadioPacketPayload;
|
|
|
|
rssi: number;
|
|
|
|
serial: number;
|
|
|
|
time: number;
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 19:33:05 +02:00
|
|
|
// Extends interface in pxt-core
|
|
|
|
export interface SimulatorRadioPacketPayload {
|
|
|
|
bufferData?: Uint8Array;
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:51:32 +02:00
|
|
|
export class RadioDatagram {
|
|
|
|
datagram: PacketBuffer[] = [];
|
2016-10-24 21:55:44 +02:00
|
|
|
lastReceived: PacketBuffer = RadioDatagram.defaultPacket();
|
2016-08-30 20:51:32 +02:00
|
|
|
|
|
|
|
constructor(private runtime: Runtime) {
|
|
|
|
}
|
|
|
|
|
|
|
|
queue(packet: PacketBuffer) {
|
|
|
|
if (this.datagram.length < 4) {
|
|
|
|
this.datagram.push(packet);
|
|
|
|
}
|
|
|
|
(<DalBoard>runtime.board).bus.queue(DAL.MICROBIT_ID_RADIO, DAL.MICROBIT_RADIO_EVT_DATAGRAM);
|
|
|
|
}
|
|
|
|
|
2016-10-24 21:55:44 +02:00
|
|
|
send(payload: SimulatorRadioPacketPayload) {
|
2017-05-25 16:10:02 +02:00
|
|
|
const b = board();
|
2016-08-30 20:51:32 +02:00
|
|
|
Runtime.postMessage(<SimulatorRadioPacketMessage>{
|
|
|
|
type: "radiopacket",
|
2018-08-02 19:33:05 +02:00
|
|
|
rssi: -42, // -42 is the strongest signal
|
2018-10-08 18:46:23 +02:00
|
|
|
serial: b.radioState.transmitSerialNumber ? pxsim.control.deviceSerialNumber() : 0,
|
2017-05-25 16:10:02 +02:00
|
|
|
time: new Date().getTime(),
|
2016-10-24 21:55:44 +02:00
|
|
|
payload
|
2016-08-30 20:51:32 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
recv(): PacketBuffer {
|
|
|
|
let r = this.datagram.shift();
|
2016-10-24 21:55:44 +02:00
|
|
|
if (!r) r = RadioDatagram.defaultPacket();
|
2016-08-30 20:51:32 +02:00
|
|
|
return this.lastReceived = r;
|
|
|
|
}
|
2016-10-24 21:55:44 +02:00
|
|
|
|
|
|
|
private static defaultPacket(): PacketBuffer {
|
|
|
|
return {
|
|
|
|
rssi: -1,
|
|
|
|
serial: 0,
|
|
|
|
time: 0,
|
2017-05-17 01:19:52 +02:00
|
|
|
payload: { type: -1, groupId: 0 }
|
2016-10-24 21:55:44 +02:00
|
|
|
};
|
|
|
|
}
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
2018-10-08 18:46:23 +02:00
|
|
|
export class RadioState {
|
2016-08-30 20:51:32 +02:00
|
|
|
power = 0;
|
|
|
|
transmitSerialNumber = false;
|
|
|
|
datagram: RadioDatagram;
|
2018-10-08 18:46:23 +02:00
|
|
|
groupId: number;
|
2016-08-30 20:51:32 +02:00
|
|
|
|
2018-10-08 18:46:23 +02:00
|
|
|
constructor(runtime: Runtime) {
|
2016-08-30 20:51:32 +02:00
|
|
|
this.datagram = new RadioDatagram(runtime);
|
2018-09-24 19:16:03 +02:00
|
|
|
this.power = 6; // default value
|
2018-10-08 18:46:23 +02:00
|
|
|
this.groupId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public setGroup(id: number) {
|
|
|
|
this.groupId = id & 0xff; // byte only
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setTransmitPower(power: number) {
|
|
|
|
this.power = Math.max(0, Math.min(7, power));
|
|
|
|
}
|
|
|
|
|
|
|
|
setTransmitSerialNumber(sn: boolean) {
|
|
|
|
this.transmitSerialNumber = !!sn;
|
|
|
|
}
|
|
|
|
|
2018-10-08 18:46:23 +02:00
|
|
|
raiseEvent(id: number, eventid: number) {
|
2016-08-30 20:51:32 +02:00
|
|
|
Runtime.postMessage(<SimulatorEventBusMessage>{
|
2018-10-08 18:46:23 +02:00
|
|
|
type: "eventbus",
|
2019-02-22 00:41:32 +01:00
|
|
|
broadcast: true,
|
2018-10-08 18:46:23 +02:00
|
|
|
id,
|
|
|
|
eventid,
|
2016-08-30 20:51:32 +02:00
|
|
|
power: this.power,
|
2018-10-08 18:46:23 +02:00
|
|
|
group: this.groupId
|
2016-08-30 20:51:32 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-08 18:46:23 +02:00
|
|
|
receivePacket(packet: SimulatorRadioPacketMessage) {
|
2017-05-17 01:19:52 +02:00
|
|
|
if (this.groupId == packet.payload.groupId)
|
2018-10-08 18:46:23 +02:00
|
|
|
this.datagram.queue(packet)
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace pxsim.radio {
|
2016-10-24 21:55:44 +02:00
|
|
|
enum PacketPayloadType {
|
|
|
|
NUMBER = 0,
|
|
|
|
VALUE = 1,
|
2018-01-30 19:11:12 +01:00
|
|
|
STRING = 2,
|
|
|
|
BUFFER = 3
|
2016-10-24 21:55:44 +02:00
|
|
|
}
|
|
|
|
|
2018-10-08 18:46:23 +02:00
|
|
|
export function raiseEvent(id: number, eventid: number): void {
|
|
|
|
board().radioState.raiseEvent(id, eventid);
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setGroup(id: number): void {
|
2017-05-17 01:19:52 +02:00
|
|
|
board().radioState.setGroup(id);
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setTransmitPower(power: number): void {
|
2018-10-08 18:46:23 +02:00
|
|
|
board().radioState.setTransmitPower(power);
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setTransmitSerialNumber(transmit: boolean): void {
|
2018-10-08 18:46:23 +02:00
|
|
|
board().radioState.setTransmitSerialNumber(transmit);
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function sendNumber(value: number): void {
|
2018-10-08 18:46:23 +02:00
|
|
|
board().radioState.datagram.send({
|
2016-10-24 21:55:44 +02:00
|
|
|
type: PacketPayloadType.NUMBER,
|
2017-05-17 01:19:52 +02:00
|
|
|
groupId: board().radioState.groupId,
|
|
|
|
numberData: value,
|
2016-10-24 21:55:44 +02:00
|
|
|
});
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function sendString(msg: string): void {
|
2018-01-30 19:11:12 +01:00
|
|
|
if (msg === undefined) return;
|
|
|
|
|
2016-10-24 21:55:44 +02:00
|
|
|
msg = msg.substr(0, 19);
|
2018-10-08 18:46:23 +02:00
|
|
|
board().radioState.datagram.send({
|
2016-10-24 21:55:44 +02:00
|
|
|
type: PacketPayloadType.STRING,
|
2017-05-17 01:19:52 +02:00
|
|
|
groupId: board().radioState.groupId,
|
|
|
|
stringData: msg,
|
2016-10-24 21:55:44 +02:00
|
|
|
});
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
2018-01-30 19:11:12 +01:00
|
|
|
export function sendBuffer(buf: RefBuffer): void {
|
|
|
|
if (!buf) return;
|
2018-08-02 19:33:05 +02:00
|
|
|
|
2018-01-30 19:11:12 +01:00
|
|
|
const data = buf.data.slice(0, 18);
|
2018-10-08 18:46:23 +02:00
|
|
|
board().radioState.datagram.send({
|
2018-01-30 19:11:12 +01:00
|
|
|
type: PacketPayloadType.STRING,
|
|
|
|
groupId: board().radioState.groupId,
|
2018-08-02 19:33:05 +02:00
|
|
|
bufferData: data
|
2018-01-30 19:11:12 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:51:32 +02:00
|
|
|
export function writeValueToSerial(): void {
|
2016-10-26 01:38:01 +02:00
|
|
|
const b = board();
|
2018-10-08 18:46:23 +02:00
|
|
|
writePacketToSerial(b, b.radioState.datagram.recv())
|
2016-10-26 01:38:01 +02:00
|
|
|
}
|
2016-10-24 21:55:44 +02:00
|
|
|
|
2016-10-26 01:38:01 +02:00
|
|
|
export function writeReceivedPacketToSerial(): void {
|
|
|
|
const b = board();
|
2018-10-08 18:46:23 +02:00
|
|
|
writePacketToSerial(b, b.radioState.datagram.lastReceived);
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function sendValue(name: string, value: number) {
|
2016-10-24 21:55:44 +02:00
|
|
|
name = name.substr(0, 12);
|
|
|
|
const msg: number[] = [];
|
|
|
|
msg.push()
|
2018-10-08 18:46:23 +02:00
|
|
|
board().radioState.datagram.send({
|
2016-10-24 21:55:44 +02:00
|
|
|
type: PacketPayloadType.VALUE,
|
2017-05-17 01:19:52 +02:00
|
|
|
groupId: board().radioState.groupId,
|
2016-10-24 21:55:44 +02:00
|
|
|
stringData: name,
|
|
|
|
numberData: value
|
|
|
|
});
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function receiveNumber(): number {
|
2018-10-08 18:46:23 +02:00
|
|
|
const packet = board().radioState.datagram.recv();
|
2016-10-24 21:55:44 +02:00
|
|
|
return receivedNumber();
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function receiveString(): string {
|
2018-10-08 18:46:23 +02:00
|
|
|
const packet = board().radioState.datagram.recv();
|
2016-10-24 21:55:44 +02:00
|
|
|
return receivedString();
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function receivedSignalStrength(): number {
|
2018-10-08 18:46:23 +02:00
|
|
|
return board().radioState.datagram.lastReceived.rssi;
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function onDataReceived(handler: RefAction): void {
|
2016-09-01 14:06:03 +02:00
|
|
|
pxtcore.registerWithDal(DAL.MICROBIT_ID_RADIO, DAL.MICROBIT_RADIO_EVT_DATAGRAM, handler);
|
2016-08-30 20:51:32 +02:00
|
|
|
radio.receiveNumber();
|
|
|
|
}
|
2016-10-24 21:55:44 +02:00
|
|
|
|
|
|
|
export function receivedNumber(): number {
|
2018-10-08 18:46:23 +02:00
|
|
|
return board().radioState.datagram.lastReceived.payload.numberData || 0;
|
2016-10-24 21:55:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function receivedSerial(): number {
|
2018-10-08 18:46:23 +02:00
|
|
|
return board().radioState.datagram.lastReceived.serial;
|
2016-10-24 21:55:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function receivedString(): string {
|
2018-10-08 18:46:23 +02:00
|
|
|
return initString(board().radioState.datagram.lastReceived.payload.stringData || "");
|
2016-10-24 21:55:44 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 19:33:05 +02:00
|
|
|
export function receivedBuffer(): RefBuffer {
|
2018-10-08 18:46:23 +02:00
|
|
|
return new RefBuffer(board().radioState.datagram.lastReceived.payload.bufferData || new Uint8Array(0))
|
2018-08-02 19:33:05 +02:00
|
|
|
}
|
2018-01-30 19:11:12 +01:00
|
|
|
|
2016-10-24 21:55:44 +02:00
|
|
|
export function receivedTime(): number {
|
2018-10-08 18:46:23 +02:00
|
|
|
return board().radioState.datagram.lastReceived.time;
|
2016-10-24 21:55:44 +02:00
|
|
|
}
|
2016-10-26 01:38:01 +02:00
|
|
|
|
|
|
|
function writePacketToSerial(b: DalBoard, p: PacketBuffer) {
|
2018-01-30 19:11:12 +01:00
|
|
|
switch (p.payload.type) {
|
2016-10-26 01:38:01 +02:00
|
|
|
case PacketPayloadType.NUMBER:
|
|
|
|
b.writeSerial(`{"t":${p.time},"s":${p.serial},"v":${p.payload.numberData}}\r\n`)
|
|
|
|
break;
|
|
|
|
case PacketPayloadType.VALUE:
|
|
|
|
b.writeSerial(`{"t":${p.time},"s":${p.serial},"n":"${p.payload.stringData}","v":${p.payload.numberData}}\r\n`)
|
|
|
|
break;
|
|
|
|
case PacketPayloadType.STRING:
|
|
|
|
b.writeSerial(`{"t":${p.time},"s":${p.serial},"n":"${p.payload.stringData}"}\r\n`)
|
|
|
|
break;
|
2018-04-21 19:25:43 +02:00
|
|
|
// TODO: (microbit master)
|
|
|
|
// case PacketPayloadType.BUFFER:
|
|
|
|
// const buf = new Uint8Array(p.payload.bufferData.buffer);
|
|
|
|
// let res = "";
|
|
|
|
// for (let i = 0; i < buf.length; ++i)
|
|
|
|
// res += String.fromCharCode(buf[i]);
|
|
|
|
// b.writeSerial(`{"t":${p.time},"s":${p.serial},"b":"${res}"}\r\n`)
|
2016-10-26 01:38:01 +02:00
|
|
|
default:
|
2018-01-30 19:11:12 +01:00
|
|
|
// unknown type
|
|
|
|
break;
|
2016-10-26 01:38:01 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|