fixing radio stack (#2461)

* fixing radio stack

* updated shims
This commit is contained in:
Peli de Halleux 2019-10-05 06:24:15 -07:00 committed by GitHub
parent 7433fd8beb
commit 36b8e5d661
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 38 deletions

View File

@ -41,14 +41,18 @@ namespace radio {
//% help=radio/received-packet //% help=radio/received-packet
Buffer readRawPacket() { Buffer readRawPacket() {
if (radioEnable() != MICROBIT_OK) return mkBuffer(NULL, 0); if (radioEnable() != MICROBIT_OK) return mkBuffer(NULL, 0);
packet = uBit.radio.datagram.recv(); uint8_t buf[32];
return mkBuffer(packet.getBytes(), packet.length()); 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 * Sends a raw packet through the radio
*/ */
//% advanced=true //% advanced=true
//% async
void sendRawPacket(Buffer msg) { void sendRawPacket(Buffer msg) {
if (radioEnable() != MICROBIT_OK || NULL == msg) return; if (radioEnable() != MICROBIT_OK || NULL == msg) return;
uBit.radio.datagram.send(msg->data, msg->length); uBit.radio.datagram.send(msg->data, msg->length);

View File

@ -16,10 +16,6 @@ enum RadioPacketProperty {
*/ */
//% color=#E3008C weight=96 icon="\uf012" //% color=#E3008C weight=96 icon="\uf012"
namespace radio { 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_FIELD_DOUBLE_NAME_LENGTH = 8;
const MAX_PAYLOAD_LENGTH = 20; const MAX_PAYLOAD_LENGTH = 20;
@ -51,30 +47,43 @@ namespace radio {
let initialized = false; let initialized = false;
export let lastPacket: RadioPacket; 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() { function init() {
if (initialized) return; if (initialized) return;
initialized = true; initialized = true;
onDataReceived(() => { onDataReceived(() => {
lastPacket = RadioPacket.getPacket(readRawPacket()); let buffer: Buffer = readRawPacket();
lastPacket.signal = receivedSignalStrength(); while (buffer && buffer.length) {
lastPacket = RadioPacket.getPacket(buffer);
lastPacket.signal = receivedSignalStrength();
switch (lastPacket.packetType) {
case PACKET_TYPE_NUMBER:
case PACKET_TYPE_DOUBLE:
if (onReceivedNumberHandler)
onReceivedNumberHandler(lastPacket.numberPayload);
break;
case PACKET_TYPE_VALUE:
case PACKET_TYPE_DOUBLE_VALUE:
if (onReceivedValueHandler)
onReceivedValueHandler(lastPacket.stringPayload, lastPacket.numberPayload);
break;
case PACKET_TYPE_BUFFER:
if(onReceivedBufferHandler)
onReceivedBufferHandler(lastPacket.bufferPayload);
break;
case PACKET_TYPE_STRING:
if (onReceivedStringHandler)
onReceivedStringHandler(lastPacket.stringPayload);
break;
}
switch (lastPacket.packetType) { // read next packet if any
case PACKET_TYPE_NUMBER: buffer = readRawPacket();
case PACKET_TYPE_DOUBLE:
control.raiseEvent(DAL.MICROBIT_ID_RADIO, MAKECODE_RADIO_EVT_NUMBER);
break;
case PACKET_TYPE_VALUE:
case PACKET_TYPE_DOUBLE_VALUE:
control.raiseEvent(DAL.MICROBIT_ID_RADIO, MAKECODE_RADIO_EVT_VALUE);
break;
case PACKET_TYPE_BUFFER:
control.raiseEvent(DAL.MICROBIT_ID_RADIO, MAKECODE_RADIO_EVT_BUFFER);
break;
case PACKET_TYPE_STRING:
control.raiseEvent(DAL.MICROBIT_ID_RADIO, MAKECODE_RADIO_EVT_STRING);
break;
} }
}) })
} }
@ -87,9 +96,7 @@ namespace radio {
//% useLoc="radio.onDataPacketReceived" draggableParameters=reporter //% useLoc="radio.onDataPacketReceived" draggableParameters=reporter
export function onReceivedNumber(cb: (receivedNumber: number) => void) { export function onReceivedNumber(cb: (receivedNumber: number) => void) {
init(); init();
control.onEvent(DAL.MICROBIT_ID_RADIO, MAKECODE_RADIO_EVT_NUMBER, () => { onReceivedNumberHandler = cb;
cb(lastPacket.numberPayload);
});
} }
/** /**
@ -100,9 +107,7 @@ namespace radio {
//% useLoc="radio.onDataPacketReceived" draggableParameters=reporter //% useLoc="radio.onDataPacketReceived" draggableParameters=reporter
export function onReceivedValue(cb: (name: string, value: number) => void) { export function onReceivedValue(cb: (name: string, value: number) => void) {
init(); init();
control.onEvent(DAL.MICROBIT_ID_RADIO, MAKECODE_RADIO_EVT_VALUE, () => { onReceivedValueHandler = cb;
cb(lastPacket.stringPayload, lastPacket.numberPayload);
});
} }
/** /**
@ -113,9 +118,7 @@ namespace radio {
//% useLoc="radio.onDataPacketReceived" draggableParameters=reporter //% useLoc="radio.onDataPacketReceived" draggableParameters=reporter
export function onReceivedString(cb: (receivedString: string) => void) { export function onReceivedString(cb: (receivedString: string) => void) {
init(); init();
control.onEvent(DAL.MICROBIT_ID_RADIO, MAKECODE_RADIO_EVT_STRING, () => { onReceivedStringHandler = cb;
cb(lastPacket.stringPayload);
});
} }
/** /**
@ -126,9 +129,7 @@ namespace radio {
//% useLoc="radio.onDataPacketReceived" draggableParameters=reporter //% useLoc="radio.onDataPacketReceived" draggableParameters=reporter
export function onReceivedBuffer(cb: (receivedBuffer: Buffer) => void) { export function onReceivedBuffer(cb: (receivedBuffer: Buffer) => void) {
init(); init();
control.onEvent(DAL.MICROBIT_ID_RADIO, MAKECODE_RADIO_EVT_BUFFER, () => { onReceivedBufferHandler = cb;
cb(lastPacket.bufferPayload);
});
} }
/** /**
@ -140,7 +141,7 @@ namespace radio {
//% blockId=radio_received_packet block="received packet %type=radio_packet_property" blockGap=16 //% blockId=radio_received_packet block="received packet %type=radio_packet_property" blockGap=16
export function receivedPacket(type: number) { export function receivedPacket(type: number) {
if (lastPacket) { if (lastPacket) {
switch(type) { switch (type) {
case RadioPacketProperty.Time: return lastPacket.time; case RadioPacketProperty.Time: return lastPacket.time;
case RadioPacketProperty.SerialNumber: return lastPacket.serial; case RadioPacketProperty.SerialNumber: return lastPacket.serial;
case RadioPacketProperty.SignalStrength: return lastPacket.signal; case RadioPacketProperty.SignalStrength: return lastPacket.signal;

View File

@ -24,7 +24,8 @@ declare namespace radio {
/** /**
* Sends a raw packet through the radio * Sends a raw packet through the radio
*/ */
//% advanced=true shim=radio::sendRawPacket //% advanced=true
//% async shim=radio::sendRawPacket
function sendRawPacket(msg: Buffer): void; function sendRawPacket(msg: Buffer): void;
/** /**

View File

@ -48,7 +48,7 @@ namespace pxsim {
rssi: -1, rssi: -1,
serial: 0, serial: 0,
time: 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) { export function sendRawPacket(buf: RefBuffer) {
let cb = getResume();
board().radioState.datagram.send({ board().radioState.datagram.send({
type: 0, type: 0,
groupId: board().radioState.groupId, groupId: board().radioState.groupId,
bufferData: buf.data bufferData: buf.data
}); });
setTimeout(cb, 1);
} }
export function readRawPacket() { export function readRawPacket() {