add support for radio groups (#399)

* add groupID to radio

* add groupId

* bump up to new version
This commit is contained in:
Tom Ball 2017-05-16 16:19:52 -07:00 committed by GitHub
parent cec694a113
commit 72e95438eb
3 changed files with 25 additions and 16 deletions

View File

@ -38,6 +38,6 @@
"semantic-ui-less": "^2.2.4" "semantic-ui-less": "^2.2.4"
}, },
"dependencies": { "dependencies": {
"pxt-core": "0.12.71" "pxt-core": "0.12.72"
} }
} }

View File

@ -97,7 +97,7 @@ namespace pxsim {
break; break;
case "radiopacket": case "radiopacket":
let packet = <SimulatorRadioPacketMessage>msg; let packet = <SimulatorRadioPacketMessage>msg;
this.radioState.recievePacket(packet); this.radioState.receivePacket(packet);
break; break;
} }
} }

View File

@ -41,14 +41,13 @@ namespace pxsim {
rssi: -1, rssi: -1,
serial: 0, serial: 0,
time: 0, time: 0,
payload: { type: -1 } payload: { type: -1, groupId: 0 }
}; };
} }
} }
export class RadioBus { export class RadioBus {
// uint8_t radioDefaultGroup = MICROBIT_RADIO_DEFAULT_GROUP; // uint8_t radioDefaultGroup = MICROBIT_RADIO_DEFAULT_GROUP;
groupId = 0; // todo
power = 0; power = 0;
serial = 0; serial = 0;
transmitSerialNumber = false; transmitSerialNumber = false;
@ -59,10 +58,6 @@ namespace pxsim {
this.serial = Math.floor(Math.random() * Math.pow(2, 32)) - Math.pow(2, 31); // 32 bit signed integer this.serial = Math.floor(Math.random() * Math.pow(2, 32)) - Math.pow(2, 31); // 32 bit signed integer
} }
setGroup(id: number) {
this.groupId = id & 0xff; // byte only
}
setTransmitPower(power: number) { setTransmitPower(power: number) {
this.power = Math.max(0, Math.min(7, power)); this.power = Math.max(0, Math.min(7, power));
} }
@ -71,25 +66,36 @@ namespace pxsim {
this.transmitSerialNumber = !!sn; this.transmitSerialNumber = !!sn;
} }
broadcast(msg: number) { broadcast(msg: number, groupId: number) {
Runtime.postMessage(<SimulatorEventBusMessage>{ Runtime.postMessage(<SimulatorEventBusMessage>{
type: "eventbus", type: "eventbus",
id: DAL.MES_BROADCAST_GENERAL_ID, id: DAL.MES_BROADCAST_GENERAL_ID,
eventid: msg, eventid: msg,
power: this.power, power: this.power,
group: this.groupId group: groupId
}) })
} }
} }
export class RadioState { export class RadioState {
bus: RadioBus; bus: RadioBus;
groupId: number;
constructor(runtime: Runtime) { constructor(runtime: Runtime) {
this.bus = new RadioBus(runtime); this.bus = new RadioBus(runtime);
this.groupId = 0;
} }
public recievePacket(packet: SimulatorRadioPacketMessage) { 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) this.bus.datagram.queue(packet)
} }
} }
@ -103,7 +109,7 @@ namespace pxsim.radio {
} }
export function broadcastMessage(msg: number): void { export function broadcastMessage(msg: number): void {
board().radioState.bus.broadcast(msg); board().radioState.broadcast(msg);
} }
export function onBroadcastMessageReceived(msg: number, handler: RefAction): void { export function onBroadcastMessageReceived(msg: number, handler: RefAction): void {
@ -111,7 +117,7 @@ namespace pxsim.radio {
} }
export function setGroup(id: number): void { export function setGroup(id: number): void {
board().radioState.bus.setGroup(id); board().radioState.setGroup(id);
} }
export function setTransmitPower(power: number): void { export function setTransmitPower(power: number): void {
@ -125,7 +131,8 @@ namespace pxsim.radio {
export function sendNumber(value: number): void { export function sendNumber(value: number): void {
board().radioState.bus.datagram.send({ board().radioState.bus.datagram.send({
type: PacketPayloadType.NUMBER, type: PacketPayloadType.NUMBER,
numberData: value groupId: board().radioState.groupId,
numberData: value,
}); });
} }
@ -133,7 +140,8 @@ namespace pxsim.radio {
msg = msg.substr(0, 19); msg = msg.substr(0, 19);
board().radioState.bus.datagram.send({ board().radioState.bus.datagram.send({
type: PacketPayloadType.STRING, type: PacketPayloadType.STRING,
stringData: msg groupId: board().radioState.groupId,
stringData: msg,
}); });
} }
@ -153,6 +161,7 @@ namespace pxsim.radio {
msg.push() msg.push()
board().radioState.bus.datagram.send({ board().radioState.bus.datagram.send({
type: PacketPayloadType.VALUE, type: PacketPayloadType.VALUE,
groupId: board().radioState.groupId,
stringData: name, stringData: name,
numberData: value numberData: value
}); });