call receiveNumber in onDataReceived to flush queue if needed (startup race)
fixed simulator issue when event onDataReceived gets raised
This commit is contained in:
parent
de5def8dde
commit
7481b9c24c
@ -134,16 +134,6 @@ namespace radio {
|
|||||||
uBit.serial.send("}\r\n");
|
uBit.serial.send("}\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Registers code to run when a packet is received over radio.
|
|
||||||
*/
|
|
||||||
//% help=radio/on-data-received
|
|
||||||
//% weight=50
|
|
||||||
//% blockId=radio_datagram_received_event block="radio on data received" blockGap=8
|
|
||||||
void onDataReceived(Action body) {
|
|
||||||
if (radioEnable() != MICROBIT_OK) return;
|
|
||||||
registerWithDal(MICROBIT_ID_RADIO, MICROBIT_RADIO_EVT_DATAGRAM, body);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a number at a given index, between ``0`` and ``3``, from the packet received by ``receive number``. Not supported in simulator.
|
* Reads a number at a given index, between ``0`` and ``3``, from the packet received by ``receive number``. Not supported in simulator.
|
||||||
@ -162,7 +152,7 @@ namespace radio {
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads the next packet as a number from the radio queue.
|
* Reads the next packet as a number from the radio queue.
|
||||||
*/
|
*/
|
||||||
@ -175,6 +165,20 @@ namespace radio {
|
|||||||
packet = uBit.radio.datagram.recv();
|
packet = uBit.radio.datagram.recv();
|
||||||
return receivedNumberAt(0);
|
return receivedNumberAt(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers code to run when a packet is received over radio.
|
||||||
|
*/
|
||||||
|
//% help=radio/on-data-received
|
||||||
|
//% weight=50
|
||||||
|
//% blockId=radio_datagram_received_event block="radio on data received" blockGap=8
|
||||||
|
void onDataReceived(Action body) {
|
||||||
|
if (radioEnable() != MICROBIT_OK) return;
|
||||||
|
registerWithDal(MICROBIT_ID_RADIO, MICROBIT_RADIO_EVT_DATAGRAM, body);
|
||||||
|
// make the the receive buffer has a free spot
|
||||||
|
receiveNumber();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads the next packet as a string and returns it.
|
* Reads the next packet as a string and returns it.
|
||||||
@ -231,6 +235,7 @@ namespace radio {
|
|||||||
//% weight=8 blockGap=8
|
//% weight=8 blockGap=8
|
||||||
//% blockId=radio_set_transmit_serial_number block="radio set transmit serial number %transmit"
|
//% blockId=radio_set_transmit_serial_number block="radio set transmit serial number %transmit"
|
||||||
void setTransmitSerialNumber(bool transmit) {
|
void setTransmitSerialNumber(bool transmit) {
|
||||||
|
if (radioEnable() != MICROBIT_OK) return;
|
||||||
transmitSerialNumber = transmit;
|
transmitSerialNumber = transmit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
16
libs/microbit-radio/shims.d.ts
vendored
16
libs/microbit-radio/shims.d.ts
vendored
@ -41,14 +41,6 @@ declare namespace radio {
|
|||||||
//% blockId=radio_write_value_serial block="radio write value to serial" shim=radio::writeValueToSerial
|
//% blockId=radio_write_value_serial block="radio write value to serial" shim=radio::writeValueToSerial
|
||||||
function writeValueToSerial(): void;
|
function writeValueToSerial(): void;
|
||||||
|
|
||||||
/**
|
|
||||||
* Registers code to run when a packet is received over radio.
|
|
||||||
*/
|
|
||||||
//% help=radio/on-data-received
|
|
||||||
//% weight=50
|
|
||||||
//% blockId=radio_datagram_received_event block="radio on data received" blockGap=8 shim=radio::onDataReceived
|
|
||||||
function onDataReceived(body: () => void): void;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a number at a given index, between ``0`` and ``3``, from the packet received by ``receive number``. Not supported in simulator.
|
* Reads a number at a given index, between ``0`` and ``3``, from the packet received by ``receive number``. Not supported in simulator.
|
||||||
* @param index index of the number to read from 0 to 3. 1 eg
|
* @param index index of the number to read from 0 to 3. 1 eg
|
||||||
@ -66,6 +58,14 @@ declare namespace radio {
|
|||||||
//% blockId=radio_datagram_receive block="radio receive number" blockGap=8 shim=radio::receiveNumber
|
//% blockId=radio_datagram_receive block="radio receive number" blockGap=8 shim=radio::receiveNumber
|
||||||
function receiveNumber(): number;
|
function receiveNumber(): number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers code to run when a packet is received over radio.
|
||||||
|
*/
|
||||||
|
//% help=radio/on-data-received
|
||||||
|
//% weight=50
|
||||||
|
//% blockId=radio_datagram_received_event block="radio on data received" blockGap=8 shim=radio::onDataReceived
|
||||||
|
function onDataReceived(body: () => void): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads the next packet as a string and returns it.
|
* Reads the next packet as a string and returns it.
|
||||||
*/
|
*/
|
||||||
|
@ -517,6 +517,7 @@ namespace pxsim.radio {
|
|||||||
|
|
||||||
export function onDataReceived(handler: RefAction): void {
|
export function onDataReceived(handler: RefAction): void {
|
||||||
pxt.registerWithDal(DAL.MICROBIT_ID_RADIO, DAL.MICROBIT_RADIO_EVT_DATAGRAM, handler);
|
pxt.registerWithDal(DAL.MICROBIT_ID_RADIO, DAL.MICROBIT_RADIO_EVT_DATAGRAM, handler);
|
||||||
|
radio.receiveNumber();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,10 +72,9 @@ namespace pxsim {
|
|||||||
}
|
}
|
||||||
|
|
||||||
queue(packet: PacketBuffer) {
|
queue(packet: PacketBuffer) {
|
||||||
if (this.datagram.length < 5) {
|
if (this.datagram.length < 4)
|
||||||
this.datagram.push(packet);
|
this.datagram.push(packet);
|
||||||
(<Board>runtime.board).bus.queue(DAL.MICROBIT_ID_RADIO, DAL.MICROBIT_RADIO_EVT_DATAGRAM);
|
(<Board>runtime.board).bus.queue(DAL.MICROBIT_ID_RADIO, DAL.MICROBIT_RADIO_EVT_DATAGRAM);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
send(buffer: number[] | string) {
|
send(buffer: number[] | string) {
|
||||||
|
Loading…
Reference in New Issue
Block a user