pxt-calliope/libs/radio/radio.ts

115 lines
4.3 KiB
TypeScript
Raw Normal View History

2016-04-06 01:52:50 +02:00
/**
* Communicate data using radio packets
*/
2017-01-06 14:43:50 +01:00
//% color=#E3008C weight=96 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 buffer payload if a buffer was sent in this packet
* or the empty buffer
*/
public receivedBuffer: Buffer;
/**
* 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 deprecated=true
//% 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.receivedBuffer = receivedBuffer();
packet.signal = receivedSignalStrength();
cb(packet)
});
}
/**
* Registers code to run when the radio receives a packet. Also takes the
* received packet from the radio queue.
*/
//% help=radio/on-radio-received-number
//% blockId=radio_on_number block="on radio received number" blockGap=8
export function onReceivedNumber(cb: (num: number, time?: number, serial?: number, signal?: number) => void) {
onDataReceived(() => {
receiveNumber();
const packet = new Packet();
packet.receivedNumber = receivedNumber();
packet.time = receivedTime();
packet.serial = receivedSerial();
packet.signal = receivedSignalStrength();
cb(packet.receivedNumber, packet.time, packet.serial, packet.signal);
});
}
/**
* Registers code to run when the radio receives a packet. Also takes the
* received packet from the radio queue.
*/
//% help=radio/on-radio-received-string
//% blockId=radio_on_string block="on radio received string" blockGap=8
export function onReceivedString(cb: (received: string, time?: number, serial?: number, signal?: number) => void) {
onDataReceived(() => {
receiveNumber();
const packet = new Packet();
packet.time = receivedTime();
packet.serial = receivedSerial();
packet.signal = receivedSignalStrength();
packet.receivedString = receivedString();
cb(packet.receivedString, packet.time, packet.serial, packet.signal);
});
}
/**
* Registers code to run when the radio receives a packet. Also takes the
* received packet from the radio queue.
*/
//% help=radio/on-radio-received-value
//% blockId=radio_on_value block="on radio received" blockGap=8
export function onReceivedValue(cb: (packet: Packet) => void) {
onDataReceived(() => {
receiveNumber();
const packet = new Packet();
packet.receivedNumber = receivedNumber();
packet.time = receivedTime();
packet.serial = receivedSerial();
packet.signal = receivedSignalStrength();
packet.receivedString = receivedString();
cb(packet)
});
}
2016-03-10 23:01:04 +01:00
}