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
|
|
|
}
|
|
|
|
|
2019-12-02 05:58:26 +01: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-12-12 19:55:00 +01:00
|
|
|
const b = board();
|
2016-08-30 20:51:32 +02:00
|
|
|
Runtime.postMessage(<SimulatorRadioPacketMessage>{
|
|
|
|
type: "radiopacket",
|
2019-12-02 05:58:26 +01:00
|
|
|
broadcast: true,
|
|
|
|
rssi: -42, // -42 is the strongest signal
|
|
|
|
serial: b.radioState.transmitSerialNumber ? pxsim.control.deviceSerialNumber() : 0,
|
2017-12-12 19:55:00 +01: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,
|
2019-12-02 05:58:26 +01:00
|
|
|
payload: { type: -1, groupId: 0, bufferData: new Uint8Array(0) }
|
2016-10-24 21:55:44 +02:00
|
|
|
};
|
|
|
|
}
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
export class RadioState {
|
2016-08-30 20:51:32 +02:00
|
|
|
power = 0;
|
|
|
|
transmitSerialNumber = false;
|
|
|
|
datagram: RadioDatagram;
|
2019-12-02 05:58:26 +01:00
|
|
|
groupId: number;
|
|
|
|
band: number;
|
2016-08-30 20:51:32 +02:00
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
constructor(runtime: Runtime) {
|
2016-08-30 20:51:32 +02:00
|
|
|
this.datagram = new RadioDatagram(runtime);
|
2019-12-02 05:58:26 +01:00
|
|
|
this.power = 6; // default value
|
|
|
|
this.groupId = 0;
|
|
|
|
this.band = 7; // https://github.com/lancaster-university/microbit-dal/blob/master/inc/core/MicroBitConfig.h#L320
|
|
|
|
}
|
|
|
|
|
|
|
|
public setGroup(id: number) {
|
|
|
|
this.groupId = id & 0xff; // byte only
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setTransmitPower(power: number) {
|
2019-12-02 05:58:26 +01:00
|
|
|
power = power | 0;
|
2016-08-30 20:51:32 +02:00
|
|
|
this.power = Math.max(0, Math.min(7, power));
|
|
|
|
}
|
|
|
|
|
|
|
|
setTransmitSerialNumber(sn: boolean) {
|
|
|
|
this.transmitSerialNumber = !!sn;
|
|
|
|
}
|
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
setFrequencyBand(band: number) {
|
|
|
|
band = band | 0;
|
|
|
|
if (band < 0 || band > 83) return;
|
|
|
|
this.band = band;
|
|
|
|
}
|
|
|
|
|
|
|
|
raiseEvent(id: number, eventid: number) {
|
2016-08-30 20:51:32 +02:00
|
|
|
Runtime.postMessage(<SimulatorEventBusMessage>{
|
|
|
|
type: "eventbus",
|
2019-12-02 05:58:26 +01:00
|
|
|
broadcast: true,
|
|
|
|
id,
|
|
|
|
eventid,
|
2016-08-30 20:51:32 +02:00
|
|
|
power: this.power,
|
2019-12-02 05:58:26 +01:00
|
|
|
group: this.groupId
|
2016-08-30 20:51:32 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
receivePacket(packet: SimulatorRadioPacketMessage) {
|
2017-12-12 19:55:00 +01:00
|
|
|
if (this.groupId == packet.payload.groupId)
|
2019-12-02 05:58:26 +01:00
|
|
|
this.datagram.queue(packet)
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace pxsim.radio {
|
2019-12-02 05:58:26 +01: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-12-12 19:55:00 +01:00
|
|
|
board().radioState.setGroup(id);
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setTransmitPower(power: number): void {
|
2019-12-02 05:58:26 +01:00
|
|
|
board().radioState.setTransmitPower(power);
|
2016-10-26 01:38:01 +02:00
|
|
|
}
|
2016-10-24 21:55:44 +02:00
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
export function setFrequencyBand(band: number) {
|
|
|
|
board().radioState.setFrequencyBand(band);
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
export function sendRawPacket(buf: RefBuffer) {
|
|
|
|
let cb = getResume();
|
|
|
|
board().radioState.datagram.send({
|
|
|
|
type: 0,
|
2017-12-12 19:55:00 +01:00
|
|
|
groupId: board().radioState.groupId,
|
2019-12-02 05:58:26 +01:00
|
|
|
bufferData: buf.data
|
2016-10-24 21:55:44 +02:00
|
|
|
});
|
2019-12-02 05:58:26 +01:00
|
|
|
setTimeout(cb, 1);
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
export function readRawPacket() {
|
|
|
|
const packet = board().radioState.datagram.recv();
|
|
|
|
return new RefBuffer(packet.payload.bufferData)
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function receivedSignalStrength(): number {
|
2019-12-02 05:58:26 +01: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);
|
2019-12-02 05:58:26 +01:00
|
|
|
readRawPacket();
|
2016-10-26 01:38:01 +02:00
|
|
|
}
|
2016-08-30 20:51:32 +02:00
|
|
|
}
|