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
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2016-08-30 20:51:32 +02:00
|
|
|
Runtime.postMessage(<SimulatorRadioPacketMessage>{
|
|
|
|
type: "radiopacket",
|
2016-10-24 21:55:44 +02:00
|
|
|
rssi: 0, // Not yet supported
|
|
|
|
serial: board().radioState.bus.transmitSerialNumber ? board().radioState.bus.serial : 0,
|
|
|
|
time: 0, // Not yet supported
|
|
|
|
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,
|
|
|
|
payload: { type: -1 }
|
|
|
|
};
|
|
|
|
}
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class RadioBus {
|
|
|
|
// uint8_t radioDefaultGroup = MICROBIT_RADIO_DEFAULT_GROUP;
|
|
|
|
groupId = 0; // todo
|
|
|
|
power = 0;
|
2016-10-24 21:55:44 +02:00
|
|
|
serial = 0;
|
2016-08-30 20:51:32 +02:00
|
|
|
transmitSerialNumber = false;
|
|
|
|
datagram: RadioDatagram;
|
|
|
|
|
|
|
|
constructor(private runtime: Runtime) {
|
|
|
|
this.datagram = new RadioDatagram(runtime);
|
2016-10-24 21:55:44 +02:00
|
|
|
this.serial = Math.floor(Math.random() * Math.pow(2, 32)) - Math.pow(2, 31); // 32 bit signed integer
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setGroup(id: number) {
|
|
|
|
this.groupId = id & 0xff; // byte only
|
|
|
|
}
|
|
|
|
|
|
|
|
setTransmitPower(power: number) {
|
|
|
|
this.power = Math.max(0, Math.min(7, power));
|
|
|
|
}
|
|
|
|
|
|
|
|
setTransmitSerialNumber(sn: boolean) {
|
|
|
|
this.transmitSerialNumber = !!sn;
|
|
|
|
}
|
|
|
|
|
|
|
|
broadcast(msg: number) {
|
|
|
|
Runtime.postMessage(<SimulatorEventBusMessage>{
|
|
|
|
type: "eventbus",
|
|
|
|
id: DAL.MES_BROADCAST_GENERAL_ID,
|
|
|
|
eventid: msg,
|
|
|
|
power: this.power,
|
|
|
|
group: this.groupId
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class RadioState {
|
|
|
|
bus: RadioBus;
|
|
|
|
|
|
|
|
constructor(runtime: Runtime) {
|
|
|
|
this.bus = new RadioBus(runtime);
|
|
|
|
}
|
|
|
|
|
|
|
|
public recievePacket(packet: SimulatorRadioPacketMessage) {
|
2016-10-24 21:55:44 +02:00
|
|
|
this.bus.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,
|
|
|
|
STRING = 2
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:51:32 +02:00
|
|
|
export function broadcastMessage(msg: number): void {
|
|
|
|
board().radioState.bus.broadcast(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function onBroadcastMessageReceived(msg: number, handler: RefAction): void {
|
2016-09-01 14:06:03 +02:00
|
|
|
pxtcore.registerWithDal(DAL.MES_BROADCAST_GENERAL_ID, msg, handler);
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setGroup(id: number): void {
|
|
|
|
board().radioState.bus.setGroup(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setTransmitPower(power: number): void {
|
|
|
|
board().radioState.bus.setTransmitPower(power);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setTransmitSerialNumber(transmit: boolean): void {
|
|
|
|
board().radioState.bus.setTransmitSerialNumber(transmit);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function sendNumber(value: number): void {
|
2016-10-24 21:55:44 +02:00
|
|
|
board().radioState.bus.datagram.send({
|
|
|
|
type: PacketPayloadType.NUMBER,
|
|
|
|
numberData: value
|
|
|
|
});
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function sendString(msg: string): void {
|
2016-10-24 21:55:44 +02:00
|
|
|
msg = msg.substr(0, 19);
|
|
|
|
board().radioState.bus.datagram.send({
|
|
|
|
type: PacketPayloadType.STRING,
|
|
|
|
stringData: msg
|
|
|
|
});
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function writeValueToSerial(): void {
|
|
|
|
let b = board();
|
2016-10-24 21:55:44 +02:00
|
|
|
let p = b.radioState.bus.datagram.recv();
|
|
|
|
|
|
|
|
switch(p.payload.type) {
|
|
|
|
case PacketPayloadType.NUMBER:
|
|
|
|
b.writeSerial(`{"t":${p.time},"s":${p.serial},"v":${p.payload.numberData}}`)
|
|
|
|
break;
|
|
|
|
case PacketPayloadType.VALUE:
|
|
|
|
b.writeSerial(`{"t":${p.time},"s":${p.serial},"n":"${p.payload.stringData}","v":${p.payload.numberData}}`)
|
|
|
|
break;
|
|
|
|
case PacketPayloadType.STRING:
|
|
|
|
b.writeSerial(`{"t":${p.time},"s":${p.serial},"n":"${p.payload.stringData}"}`)
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
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()
|
|
|
|
board().radioState.bus.datagram.send({
|
|
|
|
type: PacketPayloadType.VALUE,
|
|
|
|
stringData: name,
|
|
|
|
numberData: value
|
|
|
|
});
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function receiveNumber(): number {
|
2016-10-24 21:55:44 +02:00
|
|
|
const packet = board().radioState.bus.datagram.recv();
|
|
|
|
return receivedNumber();
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function receiveString(): string {
|
2016-10-24 21:55:44 +02:00
|
|
|
const packet = board().radioState.bus.datagram.recv();
|
|
|
|
return receivedString();
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function receivedSignalStrength(): number {
|
|
|
|
return board().radioState.bus.datagram.lastReceived.rssi;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return board().radioState.bus.datagram.lastReceived.payload.numberData || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function receivedSerial(): number {
|
|
|
|
return board().radioState.bus.datagram.lastReceived.serial;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function receivedString(): string {
|
|
|
|
return board().radioState.bus.datagram.lastReceived.payload.stringData || "";
|
|
|
|
}
|
|
|
|
|
|
|
|
export function receivedTime(): number {
|
|
|
|
return board().radioState.bus.datagram.lastReceived.time;
|
|
|
|
}
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|