2016-04-06 01:52:50 +02:00
|
|
|
/**
|
|
|
|
* Communicate data using radio packets
|
|
|
|
*/
|
2018-10-02 18:32:08 +02:00
|
|
|
//% color=#E3008C weight=96 icon="\uf012"
|
2016-03-10 23:01:04 +01:00
|
|
|
namespace radio {
|
2016-10-24 21:55:44 +02:00
|
|
|
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.
|
|
|
|
*/
|
2016-10-26 01:39:13 +02:00
|
|
|
public receivedString: string;
|
2018-01-30 19:11:12 +01:00
|
|
|
/**
|
|
|
|
* The buffer payload if a buffer was sent in this packet
|
|
|
|
* or the empty buffer
|
|
|
|
*/
|
|
|
|
public receivedBuffer: Buffer;
|
2016-10-24 21:55:44 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2018-05-14 22:18:19 +02:00
|
|
|
export enum PacketProperty {
|
|
|
|
//% blockIdentity=radio._packetProperty
|
|
|
|
//% block="time"
|
|
|
|
Time,
|
|
|
|
//% block="serial number"
|
|
|
|
//% blockIdentity=radio._packetProperty
|
|
|
|
SerialNumber,
|
|
|
|
//% blockIdentity=radio._packetProperty
|
|
|
|
//% block="signal strength"
|
|
|
|
SignalStrength
|
|
|
|
}
|
|
|
|
|
2016-10-24 21:55:44 +02:00
|
|
|
/**
|
|
|
|
* Registers code to run when the radio receives a packet. Also takes the
|
|
|
|
* received packet from the radio queue.
|
|
|
|
*/
|
2018-05-14 22:50:39 +02:00
|
|
|
//% help=radio/on-data-packet-received blockHandlerKey="radioreceived" deprecated=true
|
2016-11-11 17:16:27 +01:00
|
|
|
//% mutate=objectdestructuring
|
2016-10-24 21:55:44 +02:00
|
|
|
//% mutateText=Packet
|
2016-10-27 23:48:22 +02:00
|
|
|
//% mutateDefaults="receivedNumber;receivedString:name,receivedNumber:value;receivedString"
|
2016-10-24 21:55:44 +02:00
|
|
|
//% 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();
|
2016-10-26 01:39:13 +02:00
|
|
|
packet.receivedString = receivedString();
|
2018-01-30 19:11:12 +01:00
|
|
|
packet.receivedBuffer = receivedBuffer();
|
2016-10-24 21:55:44 +02:00
|
|
|
packet.signal = receivedSignalStrength();
|
2018-05-14 22:18:19 +02:00
|
|
|
lastPacket = packet;
|
2016-10-24 21:55:44 +02:00
|
|
|
cb(packet)
|
|
|
|
});
|
|
|
|
}
|
2018-05-09 19:56:48 +02:00
|
|
|
|
|
|
|
/**
|
2018-05-14 22:18:19 +02:00
|
|
|
* Registers code to run when the radio receives a number.
|
2018-05-09 19:56:48 +02:00
|
|
|
*/
|
2018-07-09 20:17:32 +02:00
|
|
|
//% help=radio/on-received-number blockHandlerKey="radioreceived"
|
2018-09-27 01:27:32 +02:00
|
|
|
//% blockId=radio_on_number block="on radio received" blockGap=16
|
2018-08-27 22:24:10 +02:00
|
|
|
//% useLoc="radio.onDataPacketReceived"
|
2018-05-14 22:18:19 +02:00
|
|
|
export function onReceivedNumber(cb: (receivedNumber: number) => void) {
|
2018-05-09 19:56:48 +02:00
|
|
|
onDataReceived(() => {
|
|
|
|
receiveNumber();
|
|
|
|
const packet = new Packet();
|
2018-05-14 22:18:19 +02:00
|
|
|
packet.time = receivedTime();
|
|
|
|
packet.serial = receivedSerial();
|
|
|
|
packet.signal = receivedSignalStrength();
|
2018-05-09 19:56:48 +02:00
|
|
|
packet.receivedNumber = receivedNumber();
|
2018-05-14 22:18:19 +02:00
|
|
|
lastPacket = packet;
|
|
|
|
cb(packet.receivedNumber);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers code to run when the radio receives a key value pair.
|
|
|
|
*/
|
2018-07-09 20:17:32 +02:00
|
|
|
//% help=radio/on-received-value blockHandlerKey="radioreceived"
|
2018-09-27 01:27:32 +02:00
|
|
|
//% blockId=radio_on_value block="on radio received" blockGap=16
|
2018-08-27 22:24:10 +02:00
|
|
|
//% useLoc="radio.onDataPacketReceived"
|
2018-05-14 22:18:19 +02:00
|
|
|
export function onReceivedValue(cb: (name: string, value: number) => void) {
|
|
|
|
onDataReceived(() => {
|
|
|
|
receiveNumber();
|
|
|
|
const packet = new Packet();
|
2018-05-09 19:56:48 +02:00
|
|
|
packet.time = receivedTime();
|
|
|
|
packet.serial = receivedSerial();
|
|
|
|
packet.signal = receivedSignalStrength();
|
2018-05-14 22:18:19 +02:00
|
|
|
packet.receivedNumber = receivedNumber();
|
|
|
|
packet.receivedString = receivedString();
|
|
|
|
lastPacket = packet;
|
|
|
|
cb(packet.receivedString, packet.receivedNumber)
|
2018-05-09 19:56:48 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-14 22:18:19 +02:00
|
|
|
* Registers code to run when the radio receives a string.
|
2018-05-09 19:56:48 +02:00
|
|
|
*/
|
2018-07-09 20:17:32 +02:00
|
|
|
//% help=radio/on-received-string blockHandlerKey="radioreceived"
|
2018-09-27 01:27:32 +02:00
|
|
|
//% blockId=radio_on_string block="on radio received" blockGap=16
|
2018-08-27 22:24:10 +02:00
|
|
|
//% useLoc="radio.onDataPacketReceived"
|
2018-05-14 22:18:19 +02:00
|
|
|
export function onReceivedString(cb: (receivedString: string) => void) {
|
2018-05-09 19:56:48 +02:00
|
|
|
onDataReceived(() => {
|
|
|
|
receiveNumber();
|
|
|
|
const packet = new Packet();
|
|
|
|
packet.time = receivedTime();
|
|
|
|
packet.serial = receivedSerial();
|
|
|
|
packet.signal = receivedSignalStrength();
|
|
|
|
packet.receivedString = receivedString();
|
2018-05-14 22:18:19 +02:00
|
|
|
lastPacket = packet;
|
|
|
|
cb(packet.receivedString);
|
2018-05-09 19:56:48 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-14 22:18:19 +02:00
|
|
|
* Registers code to run when the radio receives a buffer.
|
2018-05-09 19:56:48 +02:00
|
|
|
*/
|
2018-07-09 20:17:32 +02:00
|
|
|
//% help=radio/on-received-buffer blockHandlerKey="radioreceived" blockHidden=1
|
2018-09-27 01:27:32 +02:00
|
|
|
//% blockId=radio_on_buffer block="on radio received" blockGap=16
|
2018-08-27 22:24:10 +02:00
|
|
|
//% useLoc="radio.onDataPacketReceived"
|
2018-05-14 22:18:19 +02:00
|
|
|
export function onReceivedBuffer(cb: (buffer: Buffer) => void) {
|
2018-05-09 19:56:48 +02:00
|
|
|
onDataReceived(() => {
|
|
|
|
receiveNumber();
|
|
|
|
const packet = new Packet();
|
|
|
|
packet.time = receivedTime();
|
|
|
|
packet.serial = receivedSerial();
|
|
|
|
packet.signal = receivedSignalStrength();
|
2018-05-14 22:18:19 +02:00
|
|
|
packet.receivedBuffer = receivedBuffer();
|
|
|
|
lastPacket = packet;
|
|
|
|
cb(packet.receivedBuffer)
|
2018-05-09 19:56:48 +02:00
|
|
|
});
|
2018-05-14 22:18:19 +02:00
|
|
|
}
|
2018-05-09 19:56:48 +02:00
|
|
|
|
2018-05-14 22:18:19 +02:00
|
|
|
let lastPacket: Packet;
|
|
|
|
/**
|
|
|
|
* Returns properties of the last radio packet received.
|
|
|
|
* @param type the type of property to retrieve from the last packet
|
|
|
|
*/
|
|
|
|
//% help=radio/get-received-packet-property advanced=true
|
2018-09-27 01:27:32 +02:00
|
|
|
//% blockId=radio_received_packet_property block="received packet %type=radio_packet_property" blockGap=16
|
2018-05-14 22:18:19 +02:00
|
|
|
export function getReceivedPacketProperty(type: number) {
|
|
|
|
if (lastPacket) {
|
|
|
|
switch(type) {
|
|
|
|
case PacketProperty.Time: return lastPacket.time;
|
|
|
|
case PacketProperty.SerialNumber: return lastPacket.serial;
|
|
|
|
case PacketProperty.SignalStrength: return lastPacket.signal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a packet property.
|
|
|
|
* @param type the packet property type, eg: PacketProperty.time
|
|
|
|
*/
|
|
|
|
//% blockId=radio_packet_property block="%note"
|
|
|
|
//% shim=TD_ID blockHidden=1
|
|
|
|
export function _packetProperty(type: PacketProperty): number {
|
|
|
|
return type;
|
2018-05-09 19:56:48 +02:00
|
|
|
}
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|