pxt-calliope/libs/radio/radio.ts

53 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

2016-04-06 01:52:50 +02:00
/**
* Communicate data using radio packets
*/
2017-01-20 05:55:31 +01:00
//% color=#E3008C weight=34 icon="\uf012"
2016-03-10 23:01:04 +01:00
namespace radio {
export class Packet {
/**
* The number payload if a number was sent in this packet (via ``sendNumber()`` or ``sendValue()``)
* or 0 if this packet did not contain a number.
*/
public receivedNumber: number;
/**
* The string payload if a string was sent in this packet (via ``sendString()`` or ``sendValue()``)
* or the empty string if this packet did not contain a string.
*/
public receivedString: string;
/**
* The system time of the sender of the packet at the time the packet was sent.
*/
public time: number;
/**
* The serial number of the sender of the packet or 0 if the sender did not sent their serial number.
*/
public serial: number;
/**
* The received signal strength indicator (RSSI) of the packet.
*/
public signal: number;
}
/**
* Registers code to run when the radio receives a packet. Also takes the
* received packet from the radio queue.
*/
//% help=radio/on-data-packet-received
2016-11-14 19:25:26 +01:00
//% mutate=objectdestructuring
//% mutateText=Packet
//% mutateDefaults="receivedNumber;receivedString:name,receivedNumber:value;receivedString"
//% blockId=radio_on_packet block="on radio received" blockGap=8
export function onDataPacketReceived(cb: (packet: Packet) => void) {
onDataReceived(() => {
receiveNumber();
const packet = new Packet();
packet.receivedNumber = receivedNumber();
packet.time = receivedTime();
packet.serial = receivedSerial();
packet.receivedString = receivedString();
packet.signal = receivedSignalStrength();
cb(packet)
});
}
2016-03-10 23:01:04 +01:00
}