pxt-calliope/sim/state/radio.ts

250 lines
7.6 KiB
TypeScript
Raw Normal View History

2016-08-30 20:51:32 +02:00
namespace pxsim {
export interface PacketBuffer {
payload: SimulatorRadioPacketPayload;
rssi: number;
serial: number;
time: number;
2016-08-30 20:51:32 +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[] = [];
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);
}
send(payload: SimulatorRadioPacketPayload) {
const b = board();
2016-08-30 20:51:32 +02:00
Runtime.postMessage(<SimulatorRadioPacketMessage>{
type: "radiopacket",
rssi: -42, // -42 is the strongest signal
serial: b.radioState.bus.transmitSerialNumber ? pxsim.control.deviceSerialNumber() : 0,
time: new Date().getTime(),
payload
2016-08-30 20:51:32 +02:00
})
}
recv(): PacketBuffer {
let r = this.datagram.shift();
if (!r) r = RadioDatagram.defaultPacket();
2016-08-30 20:51:32 +02:00
return this.lastReceived = r;
}
private static defaultPacket(): PacketBuffer {
return {
rssi: -1,
serial: 0,
time: 0,
payload: { type: -1, groupId: 0 }
};
}
2016-08-30 20:51:32 +02:00
}
export class RadioBus {
// uint8_t radioDefaultGroup = MICROBIT_RADIO_DEFAULT_GROUP;
power = 0;
transmitSerialNumber = false;
datagram: RadioDatagram;
constructor(private runtime: Runtime) {
this.datagram = new RadioDatagram(runtime);
}
setTransmitPower(power: number) {
this.power = Math.max(0, Math.min(7, power));
}
setTransmitSerialNumber(sn: boolean) {
this.transmitSerialNumber = !!sn;
}
broadcast(msg: number, groupId: number) {
2016-08-30 20:51:32 +02:00
Runtime.postMessage(<SimulatorEventBusMessage>{
2018-05-24 20:15:45 +02:00
type: "event",
2016-08-30 20:51:32 +02:00
id: DAL.MES_BROADCAST_GENERAL_ID,
eventid: msg,
power: this.power,
group: groupId
2016-08-30 20:51:32 +02:00
})
}
}
export class RadioState {
bus: RadioBus;
groupId: number;
2016-08-30 20:51:32 +02:00
constructor(runtime: Runtime) {
this.bus = new RadioBus(runtime);
this.groupId = 0;
2016-08-30 20:51:32 +02:00
}
public setGroup(id: number) {
this.groupId = id & 0xff; // byte only
}
public broadcast(msg: number) {
this.bus.broadcast(msg, this.groupId)
}
public receivePacket(packet: SimulatorRadioPacketMessage) {
if (this.groupId == packet.payload.groupId)
this.bus.datagram.queue(packet)
2016-08-30 20:51:32 +02:00
}
}
}
namespace pxsim.radio {
enum PacketPayloadType {
NUMBER = 0,
VALUE = 1,
STRING = 2,
BUFFER = 3
}
2016-08-30 20:51:32 +02:00
export function broadcastMessage(msg: number): void {
board().radioState.broadcast(msg);
2016-08-30 20:51:32 +02:00
}
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.setGroup(id);
2016-08-30 20:51:32 +02:00
}
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 {
board().radioState.bus.datagram.send({
type: PacketPayloadType.NUMBER,
groupId: board().radioState.groupId,
numberData: value,
});
2016-08-30 20:51:32 +02:00
}
export function sendString(msg: string): void {
if (msg === undefined) return;
msg = msg.substr(0, 19);
board().radioState.bus.datagram.send({
type: PacketPayloadType.STRING,
groupId: board().radioState.groupId,
stringData: msg,
});
2016-08-30 20:51:32 +02:00
}
export function sendBuffer(buf: RefBuffer): void {
if (!buf) return;
const data = buf.data.slice(0, 18);
board().radioState.bus.datagram.send({
type: PacketPayloadType.STRING,
groupId: board().radioState.groupId,
bufferData: data
});
}
2016-08-30 20:51:32 +02:00
export function writeValueToSerial(): void {
2016-10-26 01:38:01 +02:00
const b = board();
writePacketToSerial(b, b.radioState.bus.datagram.recv())
}
2016-10-26 01:38:01 +02:00
export function writeReceivedPacketToSerial(): void {
const b = board();
writePacketToSerial(b, b.radioState.bus.datagram.lastReceived);
2016-08-30 20:51:32 +02:00
}
export function sendValue(name: string, value: number) {
name = name.substr(0, 12);
const msg: number[] = [];
msg.push()
board().radioState.bus.datagram.send({
type: PacketPayloadType.VALUE,
groupId: board().radioState.groupId,
stringData: name,
numberData: value
});
2016-08-30 20:51:32 +02:00
}
export function receiveNumber(): number {
const packet = board().radioState.bus.datagram.recv();
return receivedNumber();
2016-08-30 20:51:32 +02:00
}
export function receiveString(): string {
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();
}
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 initString(board().radioState.bus.datagram.lastReceived.payload.stringData || "");
}
export function receivedBuffer(): RefBuffer {
return new RefBuffer(board().radioState.bus.datagram.lastReceived.payload.bufferData || new Uint8Array(0))
}
export function receivedTime(): number {
return board().radioState.bus.datagram.lastReceived.time;
}
2016-10-26 01:38:01 +02:00
function writePacketToSerial(b: DalBoard, p: PacketBuffer) {
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;
// 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:
// unknown type
break;
2016-10-26 01:38:01 +02:00
}
}
2016-08-30 20:51:32 +02:00
}