Skip to content

Commit

Permalink
add support for radio groups (microsoft#399)
Browse files Browse the repository at this point in the history
* add groupID to radio

* add groupId

* bump up to new version
  • Loading branch information
tballmsft authored May 16, 2017
1 parent cec694a commit 72e9543
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@
"semantic-ui-less": "^2.2.4"
},
"dependencies": {
"pxt-core": "0.12.71"
"pxt-core": "0.12.72"
}
}
2 changes: 1 addition & 1 deletion sim/dalboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ namespace pxsim {
break;
case "radiopacket":
let packet = <SimulatorRadioPacketMessage>msg;
this.radioState.recievePacket(packet);
this.radioState.receivePacket(packet);
break;
}
}
Expand Down
37 changes: 23 additions & 14 deletions sim/state/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ namespace pxsim {
rssi: -1,
serial: 0,
time: 0,
payload: { type: -1 }
payload: { type: -1, groupId: 0 }
};
}
}

export class RadioBus {
// uint8_t radioDefaultGroup = MICROBIT_RADIO_DEFAULT_GROUP;
groupId = 0; // todo
power = 0;
serial = 0;
transmitSerialNumber = false;
Expand All @@ -59,10 +58,6 @@ namespace pxsim {
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) {
this.power = Math.max(0, Math.min(7, power));
}
Expand All @@ -71,26 +66,37 @@ namespace pxsim {
this.transmitSerialNumber = !!sn;
}

broadcast(msg: number) {
broadcast(msg: number, groupId: number) {
Runtime.postMessage(<SimulatorEventBusMessage>{
type: "eventbus",
id: DAL.MES_BROADCAST_GENERAL_ID,
eventid: msg,
power: this.power,
group: this.groupId
group: groupId
})
}
}

export class RadioState {
bus: RadioBus;
groupId: number;

constructor(runtime: Runtime) {
this.bus = new RadioBus(runtime);
this.groupId = 0;
}

public recievePacket(packet: SimulatorRadioPacketMessage) {
this.bus.datagram.queue(packet)
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)
}
}
}
Expand All @@ -103,15 +109,15 @@ namespace pxsim.radio {
}

export function broadcastMessage(msg: number): void {
board().radioState.bus.broadcast(msg);
board().radioState.broadcast(msg);
}

export function onBroadcastMessageReceived(msg: number, handler: RefAction): void {
pxtcore.registerWithDal(DAL.MES_BROADCAST_GENERAL_ID, msg, handler);
}

export function setGroup(id: number): void {
board().radioState.bus.setGroup(id);
board().radioState.setGroup(id);
}

export function setTransmitPower(power: number): void {
Expand All @@ -125,15 +131,17 @@ namespace pxsim.radio {
export function sendNumber(value: number): void {
board().radioState.bus.datagram.send({
type: PacketPayloadType.NUMBER,
numberData: value
groupId: board().radioState.groupId,
numberData: value,
});
}

export function sendString(msg: string): void {
msg = msg.substr(0, 19);
board().radioState.bus.datagram.send({
type: PacketPayloadType.STRING,
stringData: msg
groupId: board().radioState.groupId,
stringData: msg,
});
}

Expand All @@ -153,6 +161,7 @@ namespace pxsim.radio {
msg.push()
board().radioState.bus.datagram.send({
type: PacketPayloadType.VALUE,
groupId: board().radioState.groupId,
stringData: name,
numberData: value
});
Expand Down

0 comments on commit 72e9543

Please sign in to comment.