parent
7433fd8beb
commit
36b8e5d661
@ -41,14 +41,18 @@ namespace radio {
|
||||
//% help=radio/received-packet
|
||||
Buffer readRawPacket() {
|
||||
if (radioEnable() != MICROBIT_OK) return mkBuffer(NULL, 0);
|
||||
packet = uBit.radio.datagram.recv();
|
||||
return mkBuffer(packet.getBytes(), packet.length());
|
||||
uint8_t buf[32];
|
||||
int size = uBit.radio.datagram.recv(buf, sizeof(buf));
|
||||
if (size <= 0)
|
||||
return mkBuffer(NULL, 0);
|
||||
return mkBuffer(buf, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a raw packet through the radio
|
||||
*/
|
||||
//% advanced=true
|
||||
//% async
|
||||
void sendRawPacket(Buffer msg) {
|
||||
if (radioEnable() != MICROBIT_OK || NULL == msg) return;
|
||||
uBit.radio.datagram.send(msg->data, msg->length);
|
||||
|
@ -16,10 +16,6 @@ enum RadioPacketProperty {
|
||||
*/
|
||||
//% color=#E3008C weight=96 icon="\uf012"
|
||||
namespace radio {
|
||||
export const MAKECODE_RADIO_EVT_NUMBER = 10;
|
||||
export const MAKECODE_RADIO_EVT_STRING = 11;
|
||||
export const MAKECODE_RADIO_EVT_BUFFER = 12;
|
||||
export const MAKECODE_RADIO_EVT_VALUE = 13;
|
||||
|
||||
const MAX_FIELD_DOUBLE_NAME_LENGTH = 8;
|
||||
const MAX_PAYLOAD_LENGTH = 20;
|
||||
@ -51,31 +47,44 @@ namespace radio {
|
||||
let initialized = false;
|
||||
|
||||
export let lastPacket: RadioPacket;
|
||||
let onReceivedNumberHandler: (receivedNumber: number) => void;
|
||||
let onReceivedValueHandler: (name: string, value: number) => void;
|
||||
let onReceivedStringHandler: (receivedString: string) => void;
|
||||
let onReceivedBufferHandler: (receivedBuffer: Buffer) => void;
|
||||
|
||||
function init() {
|
||||
if (initialized) return;
|
||||
initialized = true;
|
||||
|
||||
onDataReceived(() => {
|
||||
lastPacket = RadioPacket.getPacket(readRawPacket());
|
||||
let buffer: Buffer = readRawPacket();
|
||||
while (buffer && buffer.length) {
|
||||
lastPacket = RadioPacket.getPacket(buffer);
|
||||
lastPacket.signal = receivedSignalStrength();
|
||||
|
||||
switch (lastPacket.packetType) {
|
||||
case PACKET_TYPE_NUMBER:
|
||||
case PACKET_TYPE_DOUBLE:
|
||||
control.raiseEvent(DAL.MICROBIT_ID_RADIO, MAKECODE_RADIO_EVT_NUMBER);
|
||||
if (onReceivedNumberHandler)
|
||||
onReceivedNumberHandler(lastPacket.numberPayload);
|
||||
break;
|
||||
case PACKET_TYPE_VALUE:
|
||||
case PACKET_TYPE_DOUBLE_VALUE:
|
||||
control.raiseEvent(DAL.MICROBIT_ID_RADIO, MAKECODE_RADIO_EVT_VALUE);
|
||||
if (onReceivedValueHandler)
|
||||
onReceivedValueHandler(lastPacket.stringPayload, lastPacket.numberPayload);
|
||||
break;
|
||||
case PACKET_TYPE_BUFFER:
|
||||
control.raiseEvent(DAL.MICROBIT_ID_RADIO, MAKECODE_RADIO_EVT_BUFFER);
|
||||
if(onReceivedBufferHandler)
|
||||
onReceivedBufferHandler(lastPacket.bufferPayload);
|
||||
break;
|
||||
case PACKET_TYPE_STRING:
|
||||
control.raiseEvent(DAL.MICROBIT_ID_RADIO, MAKECODE_RADIO_EVT_STRING);
|
||||
if (onReceivedStringHandler)
|
||||
onReceivedStringHandler(lastPacket.stringPayload);
|
||||
break;
|
||||
}
|
||||
|
||||
// read next packet if any
|
||||
buffer = readRawPacket();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -87,9 +96,7 @@ namespace radio {
|
||||
//% useLoc="radio.onDataPacketReceived" draggableParameters=reporter
|
||||
export function onReceivedNumber(cb: (receivedNumber: number) => void) {
|
||||
init();
|
||||
control.onEvent(DAL.MICROBIT_ID_RADIO, MAKECODE_RADIO_EVT_NUMBER, () => {
|
||||
cb(lastPacket.numberPayload);
|
||||
});
|
||||
onReceivedNumberHandler = cb;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -100,9 +107,7 @@ namespace radio {
|
||||
//% useLoc="radio.onDataPacketReceived" draggableParameters=reporter
|
||||
export function onReceivedValue(cb: (name: string, value: number) => void) {
|
||||
init();
|
||||
control.onEvent(DAL.MICROBIT_ID_RADIO, MAKECODE_RADIO_EVT_VALUE, () => {
|
||||
cb(lastPacket.stringPayload, lastPacket.numberPayload);
|
||||
});
|
||||
onReceivedValueHandler = cb;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -113,9 +118,7 @@ namespace radio {
|
||||
//% useLoc="radio.onDataPacketReceived" draggableParameters=reporter
|
||||
export function onReceivedString(cb: (receivedString: string) => void) {
|
||||
init();
|
||||
control.onEvent(DAL.MICROBIT_ID_RADIO, MAKECODE_RADIO_EVT_STRING, () => {
|
||||
cb(lastPacket.stringPayload);
|
||||
});
|
||||
onReceivedStringHandler = cb;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -126,9 +129,7 @@ namespace radio {
|
||||
//% useLoc="radio.onDataPacketReceived" draggableParameters=reporter
|
||||
export function onReceivedBuffer(cb: (receivedBuffer: Buffer) => void) {
|
||||
init();
|
||||
control.onEvent(DAL.MICROBIT_ID_RADIO, MAKECODE_RADIO_EVT_BUFFER, () => {
|
||||
cb(lastPacket.bufferPayload);
|
||||
});
|
||||
onReceivedBufferHandler = cb;
|
||||
}
|
||||
|
||||
/**
|
||||
|
3
libs/radio/shims.d.ts
vendored
3
libs/radio/shims.d.ts
vendored
@ -24,7 +24,8 @@ declare namespace radio {
|
||||
/**
|
||||
* Sends a raw packet through the radio
|
||||
*/
|
||||
//% advanced=true shim=radio::sendRawPacket
|
||||
//% advanced=true
|
||||
//% async shim=radio::sendRawPacket
|
||||
function sendRawPacket(msg: Buffer): void;
|
||||
|
||||
/**
|
||||
|
@ -48,7 +48,7 @@ namespace pxsim {
|
||||
rssi: -1,
|
||||
serial: 0,
|
||||
time: 0,
|
||||
payload: { type: -1, groupId: 0 }
|
||||
payload: { type: -1, groupId: 0, bufferData: new Uint8Array(0) }
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -109,11 +109,13 @@ namespace pxsim.radio {
|
||||
}
|
||||
|
||||
export function sendRawPacket(buf: RefBuffer) {
|
||||
let cb = getResume();
|
||||
board().radioState.datagram.send({
|
||||
type: 0,
|
||||
groupId: board().radioState.groupId,
|
||||
bufferData: buf.data
|
||||
});
|
||||
setTimeout(cb, 1);
|
||||
}
|
||||
|
||||
export function readRawPacket() {
|
||||
|
Loading…
Reference in New Issue
Block a user