Updating radio JSON writing API (#290)
This commit is contained in:
committed by
Peli de Halleux
parent
7d912110f4
commit
f191d9033d
@ -100,6 +100,25 @@ namespace radio {
|
||||
return ManagedString().leakData();
|
||||
}
|
||||
|
||||
void writePacketAsJSON(uint8_t tp, int v, int s, int t, StringData* m) {
|
||||
// Convert the packet to JSON and send over serial
|
||||
uBit.serial.send("{");
|
||||
uBit.serial.send("\"t\":");
|
||||
uBit.serial.send(t);
|
||||
uBit.serial.send(",\"s\":");
|
||||
uBit.serial.send(s);
|
||||
if (tp == PACKET_TYPE_STRING || tp == PACKET_TYPE_VALUE) {
|
||||
uBit.serial.send(",\"n\":\"");
|
||||
uBit.serial.send(m);
|
||||
uBit.serial.send("\"");
|
||||
}
|
||||
if (tp == PACKET_TYPE_NUMBER || tp == PACKET_TYPE_VALUE) {
|
||||
uBit.serial.send(",\"v\":");
|
||||
uBit.serial.send(v);
|
||||
}
|
||||
uBit.serial.send("}\r\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a packet from the micro:bit radio queue.
|
||||
* @param writeToSerial if true, write the received packet to serial without updating the global packet;
|
||||
@ -144,22 +163,7 @@ namespace radio {
|
||||
msg = m;
|
||||
}
|
||||
else {
|
||||
// Convert the packet to JSON and send over serial
|
||||
uBit.serial.send("{");
|
||||
uBit.serial.send("\"t\":");
|
||||
uBit.serial.send(t);
|
||||
uBit.serial.send(",\"s\":");
|
||||
uBit.serial.send(s);
|
||||
if (tp == PACKET_TYPE_STRING || tp == PACKET_TYPE_VALUE) {
|
||||
uBit.serial.send(",\"n\":\"");
|
||||
uBit.serial.send(m);
|
||||
uBit.serial.send("\"");
|
||||
}
|
||||
if (tp == PACKET_TYPE_NUMBER || tp == PACKET_TYPE_VALUE) {
|
||||
uBit.serial.send(",\"v\":");
|
||||
uBit.serial.send(v);
|
||||
}
|
||||
uBit.serial.send("}\r\n");
|
||||
writePacketAsJSON(tp, v, s, t, m);
|
||||
}
|
||||
}
|
||||
|
||||
@ -231,12 +235,25 @@ namespace radio {
|
||||
//% help=radio/write-value-to-serial
|
||||
//% weight=3
|
||||
//% blockId=radio_write_value_serial block="radio write value to serial"
|
||||
//% advanced=true
|
||||
//% deprecated=true
|
||||
void writeValueToSerial() {
|
||||
if (radioEnable() != MICROBIT_OK) return;
|
||||
receivePacket(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the last received packet to serial as JSON. This should be called
|
||||
* within an ``onDataPacketReceived`` callback.
|
||||
*/
|
||||
//% help=radio/write-received-packet-to-serial
|
||||
//% weight=3
|
||||
//% blockId=radio_write_packet_serial block="radio write received packet to serial"
|
||||
//% advanced=true
|
||||
void writeReceivedPacketToSerial() {
|
||||
if (radioEnable() != MICROBIT_OK) return;
|
||||
writePacketAsJSON(type, value, (int) serial, (int) time, msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the next packet from the radio queue and returns the packet's number
|
||||
* payload or 0 if the packet did not contain a number.
|
||||
|
12
libs/radio/shims.d.ts
vendored
12
libs/radio/shims.d.ts
vendored
@ -40,9 +40,19 @@ declare namespace radio {
|
||||
//% help=radio/write-value-to-serial
|
||||
//% weight=3
|
||||
//% blockId=radio_write_value_serial block="radio write value to serial"
|
||||
//% advanced=true shim=radio::writeValueToSerial
|
||||
//% deprecated=true shim=radio::writeValueToSerial
|
||||
function writeValueToSerial(): void;
|
||||
|
||||
/**
|
||||
* Writes the last received packet to serial as JSON. This should be called
|
||||
* within an ``onDataPacketReceived`` callback.
|
||||
*/
|
||||
//% help=radio/write-received-packet-to-serial
|
||||
//% weight=3
|
||||
//% blockId=radio_write_packet_serial block="radio write received packet to serial"
|
||||
//% advanced=true shim=radio::writeReceivedPacketToSerial
|
||||
function writeReceivedPacketToSerial(): void;
|
||||
|
||||
/**
|
||||
* Reads the next packet from the radio queue and returns the packet's number
|
||||
* payload or 0 if the packet did not contain a number.
|
||||
|
Reference in New Issue
Block a user