2016-04-07 21:52:02 +02:00
|
|
|
#include "pxt.h"
|
2016-04-03 02:34:06 +02:00
|
|
|
|
2016-04-07 21:52:02 +02:00
|
|
|
using namespace pxt;
|
2016-04-03 02:34:06 +02:00
|
|
|
|
2016-05-11 06:13:16 +02:00
|
|
|
#define MAX_FIELD_NAME_LENGTH 12
|
2018-10-29 18:42:12 +01:00
|
|
|
#define MAX_FIELD_DOUBLE_NAME_LENGTH 8
|
2016-10-24 21:55:44 +02:00
|
|
|
#define MAX_PAYLOAD_LENGTH 20
|
|
|
|
#define PACKET_PREFIX_LENGTH 9
|
|
|
|
#define VALUE_PACKET_NAME_LEN_OFFSET 13
|
2018-10-29 18:42:12 +01:00
|
|
|
#define DOUBLE_VALUE_PACKET_NAME_LEN_OFFSET 17
|
2016-10-24 21:55:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Packet Spec:
|
|
|
|
// | 0 | 1 ... 4 | 5 ... 8 | 9 ... 28
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
// | packet type | system time | serial number | payload
|
|
|
|
//
|
|
|
|
// Serial number defaults to 0 unless enabled by user
|
|
|
|
|
|
|
|
// payload: number (9 ... 12)
|
|
|
|
#define PACKET_TYPE_NUMBER 0
|
|
|
|
|
|
|
|
// payload: number (9 ... 12), name length (13), name (14 ... 26)
|
|
|
|
#define PACKET_TYPE_VALUE 1
|
|
|
|
|
|
|
|
// payload: string length (9), string (10 ... 28)
|
|
|
|
#define PACKET_TYPE_STRING 2
|
2016-05-10 07:55:37 +02:00
|
|
|
|
2018-01-30 19:11:12 +01:00
|
|
|
// payload: buffer length (9), buffer (10 ... 28)
|
|
|
|
#define PACKET_TYPE_BUFFER 3
|
|
|
|
|
2018-10-29 18:42:12 +01:00
|
|
|
// payload: number (9 ... 16)
|
|
|
|
#define PACKET_TYPE_DOUBLE 4
|
|
|
|
|
|
|
|
// payload: number (9 ... 16), name length (17), name (18 ... 26)
|
|
|
|
#define PACKET_TYPE_DOUBLE_VALUE 5
|
|
|
|
|
2018-10-02 18:32:08 +02:00
|
|
|
//% color=#E3008C weight=96 icon="\uf012"
|
2016-04-03 02:34:06 +02:00
|
|
|
namespace radio {
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// Radio
|
2016-10-11 22:48:25 +02:00
|
|
|
// -------------------------------------------------------------------------
|
2016-04-03 02:34:06 +02:00
|
|
|
bool radioEnabled = false;
|
2016-05-11 06:13:16 +02:00
|
|
|
bool transmitSerialNumber = false;
|
2016-10-11 22:48:25 +02:00
|
|
|
|
2016-04-03 02:34:06 +02:00
|
|
|
PacketBuffer packet;
|
2016-10-11 22:48:25 +02:00
|
|
|
|
2016-10-24 21:55:44 +02:00
|
|
|
uint8_t type;
|
|
|
|
uint32_t time;
|
|
|
|
uint32_t serial;
|
2018-10-29 18:42:12 +01:00
|
|
|
int ivalue;
|
|
|
|
double dvalue;
|
2018-05-30 00:55:58 +02:00
|
|
|
String msg; // may be NULL before first packet
|
|
|
|
Buffer bufMsg; // may be NULL before first packet
|
2016-10-24 21:55:44 +02:00
|
|
|
|
2016-04-03 02:34:06 +02:00
|
|
|
int radioEnable() {
|
|
|
|
int r = uBit.radio.enable();
|
2016-04-16 07:41:30 +02:00
|
|
|
if (r != MICROBIT_OK) {
|
|
|
|
uBit.panic(43);
|
|
|
|
return r;
|
|
|
|
}
|
2016-04-03 02:34:06 +02:00
|
|
|
if (!radioEnabled) {
|
2016-04-07 21:52:02 +02:00
|
|
|
uBit.radio.setGroup(pxt::programHash());
|
2018-09-24 19:16:03 +02:00
|
|
|
uBit.radio.setTransmitPower(6); // start with high power by default
|
2016-04-03 02:34:06 +02:00
|
|
|
radioEnabled = true;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2018-10-08 18:46:23 +02:00
|
|
|
/**
|
|
|
|
* Sends an event over radio to neigboring devices
|
|
|
|
*/
|
|
|
|
//% blockId=radioRaiseEvent block="radio raise event|from source %src=control_event_source_id|with value %value=control_event_value_id"
|
|
|
|
//% blockExternalInputs=1
|
|
|
|
//% advanced=true
|
|
|
|
//% weight=1
|
|
|
|
//% help=radio/raise-event
|
|
|
|
void raiseEvent(int src, int value) {
|
2016-04-03 02:34:06 +02:00
|
|
|
if (radioEnable() != MICROBIT_OK) return;
|
2018-10-08 18:46:23 +02:00
|
|
|
uBit.radio.event.eventReceived(MicroBitEvent(src, value, CREATE_ONLY));
|
2016-04-03 02:34:06 +02:00
|
|
|
}
|
|
|
|
|
2016-10-24 21:55:44 +02:00
|
|
|
void setPacketPrefix(uint8_t* buf, int type) {
|
|
|
|
// prefix: type (0), time (1..4), serial (5..8)
|
|
|
|
uint32_t t = system_timer_current_time();
|
|
|
|
uint32_t sn = transmitSerialNumber ? microbit_serial_number() : 0;
|
|
|
|
|
|
|
|
buf[0] = (uint8_t) type;
|
|
|
|
memcpy(buf + 1, &t, 4);
|
|
|
|
memcpy(buf + 5, &sn, 4);
|
|
|
|
}
|
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
uint8_t copyStringValue(uint8_t* buf, String data, uint8_t maxLength) {
|
2019-02-22 00:41:32 +01:00
|
|
|
uint8_t len = min_(maxLength, data->getUTF8Size());
|
2016-10-24 21:55:44 +02:00
|
|
|
|
|
|
|
// One byte for length of the string
|
|
|
|
buf[0] = len;
|
|
|
|
|
|
|
|
if (len > 0) {
|
2019-02-22 00:41:32 +01:00
|
|
|
memcpy(buf + 1, data->getUTF8Data(), len);
|
2016-10-24 21:55:44 +02:00
|
|
|
}
|
|
|
|
return len + 1;
|
|
|
|
}
|
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
String getStringValue(uint8_t* buf, uint8_t maxLength) {
|
2016-10-24 21:55:44 +02:00
|
|
|
// First byte is the string length
|
2018-05-30 00:55:58 +02:00
|
|
|
uint8_t len = min_(maxLength, buf[0]);
|
|
|
|
return mkString((char*)buf + 1, len);
|
2016-10-24 21:55:44 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
uint8_t copyBufferValue(uint8_t* buf, Buffer data, uint8_t maxLength) {
|
|
|
|
uint8_t len = min_(maxLength, data->length);
|
2018-01-30 19:11:12 +01:00
|
|
|
|
|
|
|
// One byte for length of the buffer
|
|
|
|
buf[0] = len;
|
|
|
|
if (len > 0) {
|
2018-05-30 00:55:58 +02:00
|
|
|
memcpy(buf + 1, data->data, len);
|
2018-01-30 19:11:12 +01:00
|
|
|
}
|
|
|
|
return len + 1;
|
2018-08-02 22:30:02 +02:00
|
|
|
}
|
2018-01-30 19:11:12 +01:00
|
|
|
|
2018-05-30 00:55:58 +02:00
|
|
|
Buffer getBufferValue(uint8_t* buf, uint8_t maxLength) {
|
2018-01-30 19:11:12 +01:00
|
|
|
// First byte is the buffer length
|
2018-05-30 00:55:58 +02:00
|
|
|
uint8_t len = min_(maxLength, buf[0]);
|
|
|
|
// skip first byte
|
|
|
|
return mkBuffer(buf + 1, len);
|
2018-01-30 19:11:12 +01:00
|
|
|
}
|
|
|
|
|
2018-10-29 18:42:12 +01:00
|
|
|
void writePacketAsJSON(uint8_t tp, int iv, double dv, int s, int t, String m, Buffer b) {
|
2016-10-26 01:38:01 +02:00
|
|
|
// 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);
|
2018-01-30 19:11:12 +01:00
|
|
|
if ((tp == PACKET_TYPE_STRING || tp == PACKET_TYPE_VALUE) && NULL != m) {
|
2016-10-26 01:38:01 +02:00
|
|
|
uBit.serial.send(",\"n\":\"");
|
2019-02-22 00:41:32 +01:00
|
|
|
uBit.serial.send((uint8_t*)m->getUTF8Data(), m->getUTF8Size());
|
2018-01-30 19:11:12 +01:00
|
|
|
uBit.serial.send("\"");
|
|
|
|
}
|
|
|
|
if (tp == PACKET_TYPE_BUFFER && NULL != b) {
|
|
|
|
uBit.serial.send(",\"b\":\"");
|
|
|
|
// TODO: proper base64 encoding
|
2018-05-30 00:55:58 +02:00
|
|
|
uBit.serial.send(b->data, b->length);
|
2016-10-26 01:38:01 +02:00
|
|
|
uBit.serial.send("\"");
|
|
|
|
}
|
|
|
|
if (tp == PACKET_TYPE_NUMBER || tp == PACKET_TYPE_VALUE) {
|
|
|
|
uBit.serial.send(",\"v\":");
|
2018-10-29 18:42:12 +01:00
|
|
|
uBit.serial.send(iv);
|
|
|
|
} else if (tp == PACKET_TYPE_DOUBLE || tp == PACKET_TYPE_DOUBLE_VALUE) {
|
|
|
|
uBit.serial.send(",\"v\":");
|
|
|
|
TNumber td = fromDouble(dv);
|
|
|
|
String sd = numops::toString(td);
|
2019-02-22 00:41:32 +01:00
|
|
|
uBit.serial.send((uint8_t*)sd->getUTF8Data(), sd->getUTF8Size());
|
2018-10-29 18:42:12 +01:00
|
|
|
decrRC(sd);
|
2016-10-26 01:38:01 +02:00
|
|
|
}
|
|
|
|
uBit.serial.send("}\r\n");
|
|
|
|
}
|
|
|
|
|
2016-10-24 21:55:44 +02:00
|
|
|
/**
|
|
|
|
* Takes a packet from the micro:bit radio queue.
|
|
|
|
* @param writeToSerial if true, write the received packet to serial without updating the global packet;
|
|
|
|
if false, update the global packet instead
|
|
|
|
*/
|
|
|
|
void receivePacket(bool writeToSerial) {
|
|
|
|
PacketBuffer p = uBit.radio.datagram.recv();
|
|
|
|
|
|
|
|
uint8_t* buf = p.getBytes();
|
|
|
|
uint8_t tp;
|
|
|
|
int t;
|
|
|
|
int s;
|
2018-10-29 18:42:12 +01:00
|
|
|
int iv = 0;
|
|
|
|
double dv = 0;
|
2018-05-30 00:55:58 +02:00
|
|
|
String m = NULL;
|
|
|
|
Buffer b = NULL;
|
2016-10-24 21:55:44 +02:00
|
|
|
|
|
|
|
memcpy(&tp, buf, 1);
|
|
|
|
memcpy(&t, buf + 1, 4);
|
|
|
|
memcpy(&s, buf + 5, 4);
|
|
|
|
|
2018-10-29 18:42:12 +01:00
|
|
|
switch(tp) {
|
|
|
|
case PACKET_TYPE_STRING:
|
|
|
|
m = getStringValue(buf + PACKET_PREFIX_LENGTH, MAX_PAYLOAD_LENGTH - 1);
|
|
|
|
break;
|
|
|
|
case PACKET_TYPE_BUFFER:
|
|
|
|
b = getBufferValue(buf + PACKET_PREFIX_LENGTH, MAX_PAYLOAD_LENGTH - 1);
|
|
|
|
break;
|
|
|
|
case PACKET_TYPE_DOUBLE:
|
|
|
|
case PACKET_TYPE_DOUBLE_VALUE:
|
|
|
|
memcpy(&dv, buf + PACKET_PREFIX_LENGTH, sizeof(double));
|
|
|
|
if (tp == PACKET_TYPE_DOUBLE_VALUE) {
|
|
|
|
m = getStringValue(buf + DOUBLE_VALUE_PACKET_NAME_LEN_OFFSET, MAX_FIELD_DOUBLE_NAME_LENGTH);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PACKET_TYPE_NUMBER:
|
|
|
|
case PACKET_TYPE_VALUE:
|
|
|
|
memcpy(&iv, buf + PACKET_PREFIX_LENGTH, sizeof(int));
|
|
|
|
if (tp == PACKET_TYPE_VALUE) {
|
|
|
|
m = getStringValue(buf + VALUE_PACKET_NAME_LEN_OFFSET, MAX_FIELD_NAME_LENGTH);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: // unknown packet
|
|
|
|
return;
|
2016-10-24 21:55:44 +02:00
|
|
|
}
|
|
|
|
|
2018-01-30 19:11:12 +01:00
|
|
|
if (NULL == m)
|
2018-05-30 00:55:58 +02:00
|
|
|
m = mkString("", 0);
|
2018-01-30 19:11:12 +01:00
|
|
|
if (NULL == b)
|
2018-05-30 00:55:58 +02:00
|
|
|
b = mkBuffer(NULL, 0);
|
2018-01-30 19:11:12 +01:00
|
|
|
|
2016-10-24 21:55:44 +02:00
|
|
|
if (!writeToSerial) {
|
|
|
|
// Refresh global packet
|
|
|
|
packet = p;
|
|
|
|
type = tp;
|
|
|
|
time = t;
|
|
|
|
serial = s;
|
2018-10-29 18:42:12 +01:00
|
|
|
ivalue = iv;
|
|
|
|
dvalue = dv;
|
2018-05-30 00:55:58 +02:00
|
|
|
decrRC(msg);
|
|
|
|
decrRC(bufMsg);
|
2016-10-24 21:55:44 +02:00
|
|
|
msg = m;
|
2018-01-30 19:11:12 +01:00
|
|
|
bufMsg = b;
|
2016-10-24 21:55:44 +02:00
|
|
|
}
|
|
|
|
else {
|
2018-10-29 18:42:12 +01:00
|
|
|
writePacketAsJSON(tp, iv, dv, s, t, m, b);
|
2018-05-30 00:55:58 +02:00
|
|
|
decrRC(m);
|
|
|
|
decrRC(b);
|
2016-10-24 21:55:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 02:34:06 +02:00
|
|
|
/**
|
2016-05-11 06:13:16 +02:00
|
|
|
* Broadcasts a number over radio to any connected micro:bit in the group.
|
2016-10-11 22:48:25 +02:00
|
|
|
*/
|
2016-05-11 06:13:16 +02:00
|
|
|
//% help=radio/send-number
|
|
|
|
//% weight=60
|
2016-08-02 01:02:06 +02:00
|
|
|
//% blockId=radio_datagram_send block="radio send number %value" blockGap=8
|
2018-10-29 18:42:12 +01:00
|
|
|
void sendNumber(TNumber value) {
|
2016-10-11 22:48:25 +02:00
|
|
|
if (radioEnable() != MICROBIT_OK) return;
|
2016-10-24 21:55:44 +02:00
|
|
|
|
2018-10-29 18:42:12 +01:00
|
|
|
int iv = toInt(value);
|
|
|
|
double dv = toDouble(value);
|
|
|
|
if (iv == dv) {
|
|
|
|
uint8_t length = PACKET_PREFIX_LENGTH + sizeof(int);
|
|
|
|
uint8_t buf[length];
|
|
|
|
memset(buf, 0, length);
|
|
|
|
setPacketPrefix(buf, PACKET_TYPE_NUMBER);
|
|
|
|
memcpy(buf + PACKET_PREFIX_LENGTH, &iv, sizeof(int));
|
|
|
|
uBit.radio.datagram.send(buf, length);
|
|
|
|
} else {
|
|
|
|
uint8_t length = PACKET_PREFIX_LENGTH + sizeof(double);
|
|
|
|
uint8_t buf[length];
|
|
|
|
memset(buf, 0, length);
|
|
|
|
setPacketPrefix(buf, PACKET_TYPE_DOUBLE);
|
|
|
|
memcpy(buf + PACKET_PREFIX_LENGTH, &dv, sizeof(double));
|
|
|
|
uBit.radio.datagram.send(buf, length);
|
|
|
|
}
|
2016-04-03 02:34:06 +02:00
|
|
|
}
|
2016-10-11 22:48:25 +02:00
|
|
|
|
2016-05-10 07:55:37 +02:00
|
|
|
/**
|
|
|
|
* Broadcasts a name / value pair along with the device serial number
|
2016-08-02 01:02:06 +02:00
|
|
|
* and running time to any connected micro:bit in the group.
|
2016-10-27 23:48:22 +02:00
|
|
|
* @param name the field name (max 12 characters), eg: "name"
|
2016-05-11 06:13:16 +02:00
|
|
|
* @param value the numberic value
|
2016-05-10 07:55:37 +02:00
|
|
|
*/
|
2016-05-11 01:42:18 +02:00
|
|
|
//% help=radio/send-value
|
2016-05-11 06:13:16 +02:00
|
|
|
//% weight=59
|
2016-08-02 01:02:06 +02:00
|
|
|
//% blockId=radio_datagram_send_value block="radio send|value %name|= %value" blockGap=8
|
2018-10-29 18:42:12 +01:00
|
|
|
void sendValue(String name, TNumber value) {
|
2016-05-10 07:55:37 +02:00
|
|
|
if (radioEnable() != MICROBIT_OK) return;
|
|
|
|
|
|
|
|
uint8_t buf[32];
|
2016-07-21 10:54:53 +02:00
|
|
|
memset(buf, 0, 32);
|
2016-10-24 21:55:44 +02:00
|
|
|
|
2018-10-29 18:42:12 +01:00
|
|
|
int iv = toInt(value);
|
|
|
|
double dv = toDouble(value);
|
|
|
|
if (iv == dv) {
|
|
|
|
setPacketPrefix(buf, PACKET_TYPE_VALUE);
|
|
|
|
memcpy(buf + PACKET_PREFIX_LENGTH, &iv, sizeof(int));
|
2016-10-24 21:55:44 +02:00
|
|
|
|
2018-10-29 18:42:12 +01:00
|
|
|
int stringLen = copyStringValue(buf + VALUE_PACKET_NAME_LEN_OFFSET, name, MAX_FIELD_DOUBLE_NAME_LENGTH);
|
|
|
|
uBit.radio.datagram.send(buf, VALUE_PACKET_NAME_LEN_OFFSET + stringLen);
|
|
|
|
} else {
|
|
|
|
setPacketPrefix(buf, PACKET_TYPE_DOUBLE_VALUE);
|
|
|
|
memcpy(buf + PACKET_PREFIX_LENGTH, &dv, sizeof(double));
|
2016-10-24 21:55:44 +02:00
|
|
|
|
2018-10-29 18:42:12 +01:00
|
|
|
int stringLen = copyStringValue(buf + DOUBLE_VALUE_PACKET_NAME_LEN_OFFSET, name, MAX_FIELD_NAME_LENGTH);
|
|
|
|
uBit.radio.datagram.send(buf, DOUBLE_VALUE_PACKET_NAME_LEN_OFFSET + stringLen);
|
|
|
|
}
|
2016-05-10 07:55:37 +02:00
|
|
|
}
|
2016-10-11 22:48:25 +02:00
|
|
|
|
2016-05-12 21:35:40 +02:00
|
|
|
/**
|
2016-10-24 21:55:44 +02:00
|
|
|
* Broadcasts a string along with the device serial number
|
|
|
|
* and running time to any connected micro:bit in the group.
|
2016-10-11 22:48:25 +02:00
|
|
|
*/
|
2016-05-12 21:35:40 +02:00
|
|
|
//% help=radio/send-string
|
|
|
|
//% weight=58
|
2016-08-02 01:02:06 +02:00
|
|
|
//% blockId=radio_datagram_send_string block="radio send string %msg"
|
2018-08-02 22:30:02 +02:00
|
|
|
//% msg.shadowOptions.toString=true
|
2018-05-30 00:55:58 +02:00
|
|
|
void sendString(String msg) {
|
2018-01-30 19:11:12 +01:00
|
|
|
if (radioEnable() != MICROBIT_OK || NULL == msg) return;
|
2016-10-11 22:48:25 +02:00
|
|
|
|
2016-10-24 21:55:44 +02:00
|
|
|
uint8_t buf[32];
|
|
|
|
memset(buf, 0, 32);
|
|
|
|
|
|
|
|
setPacketPrefix(buf, PACKET_TYPE_STRING);
|
|
|
|
int stringLen = copyStringValue(buf + PACKET_PREFIX_LENGTH, msg, MAX_PAYLOAD_LENGTH - 1);
|
2016-10-11 22:48:25 +02:00
|
|
|
|
2016-10-24 21:55:44 +02:00
|
|
|
uBit.radio.datagram.send(buf, PACKET_PREFIX_LENGTH + stringLen);
|
2016-05-12 21:35:40 +02:00
|
|
|
}
|
2016-10-11 22:48:25 +02:00
|
|
|
|
2018-01-30 19:11:12 +01:00
|
|
|
/**
|
|
|
|
* Broadcasts a buffer (up to 19 bytes long) along with the device serial number
|
|
|
|
* and running time to any connected micro:bit in the group.
|
|
|
|
*/
|
|
|
|
//% help=radio/send-buffer
|
|
|
|
//% weight=57
|
|
|
|
//% advanced=true
|
|
|
|
void sendBuffer(Buffer msg) {
|
|
|
|
if (radioEnable() != MICROBIT_OK || NULL == msg) return;
|
|
|
|
|
|
|
|
uint8_t buf[32];
|
|
|
|
memset(buf, 0, 32);
|
|
|
|
|
|
|
|
setPacketPrefix(buf, PACKET_TYPE_BUFFER);
|
|
|
|
int bufLen = copyBufferValue(buf + PACKET_PREFIX_LENGTH, msg, MAX_PAYLOAD_LENGTH - 1);
|
|
|
|
|
|
|
|
uBit.radio.datagram.send(buf, PACKET_PREFIX_LENGTH + bufLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-10 07:55:37 +02:00
|
|
|
/**
|
2016-10-24 21:55:44 +02:00
|
|
|
* Reads the next packet from the radio queue and and writes it to serial
|
|
|
|
* as JSON.
|
2016-05-10 07:55:37 +02:00
|
|
|
*/
|
2016-05-11 06:13:16 +02:00
|
|
|
//% help=radio/write-value-to-serial
|
|
|
|
//% weight=3
|
2016-08-02 01:02:06 +02:00
|
|
|
//% blockId=radio_write_value_serial block="radio write value to serial"
|
2016-10-26 01:38:01 +02:00
|
|
|
//% deprecated=true
|
2016-05-11 06:13:16 +02:00
|
|
|
void writeValueToSerial() {
|
2016-05-10 07:55:37 +02:00
|
|
|
if (radioEnable() != MICROBIT_OK) return;
|
2016-10-24 21:55:44 +02:00
|
|
|
receivePacket(true);
|
2016-05-10 07:55:37 +02:00
|
|
|
}
|
2016-04-03 02:34:06 +02:00
|
|
|
|
2016-10-26 01:38:01 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
2018-10-29 18:42:12 +01:00
|
|
|
writePacketAsJSON(type, ivalue, dvalue, (int) serial, (int) time, msg, bufMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
TNumber readNumber() {
|
|
|
|
if (type == PACKET_TYPE_NUMBER || type == PACKET_TYPE_VALUE)
|
|
|
|
return fromInt(ivalue);
|
|
|
|
else if (type == PACKET_TYPE_DOUBLE || type == PACKET_TYPE_DOUBLE_VALUE)
|
|
|
|
return fromDouble(dvalue);
|
|
|
|
else
|
|
|
|
return fromInt(0);
|
2016-10-26 01:38:01 +02:00
|
|
|
}
|
|
|
|
|
2016-04-03 02:34:06 +02:00
|
|
|
/**
|
2016-10-24 21:55:44 +02:00
|
|
|
* 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.
|
2016-04-03 02:34:06 +02:00
|
|
|
*/
|
|
|
|
//% help=radio/receive-number
|
|
|
|
//% weight=46
|
2016-08-02 01:02:06 +02:00
|
|
|
//% blockId=radio_datagram_receive block="radio receive number" blockGap=8
|
2016-10-25 01:30:21 +02:00
|
|
|
//% deprecated=true
|
2018-10-29 18:42:12 +01:00
|
|
|
TNumber receiveNumber()
|
2016-04-03 02:34:06 +02:00
|
|
|
{
|
|
|
|
if (radioEnable() != MICROBIT_OK) return 0;
|
2016-10-24 21:55:44 +02:00
|
|
|
receivePacket(false);
|
2018-10-29 18:42:12 +01:00
|
|
|
return readNumber();
|
2016-04-03 02:34:06 +02:00
|
|
|
}
|
2016-08-09 01:54:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2016-10-25 01:30:21 +02:00
|
|
|
//% deprecated=true
|
2016-08-09 01:54:43 +02:00
|
|
|
void onDataReceived(Action body) {
|
|
|
|
if (radioEnable() != MICROBIT_OK) return;
|
2016-10-11 22:48:25 +02:00
|
|
|
registerWithDal(MICROBIT_ID_RADIO, MICROBIT_RADIO_EVT_DATAGRAM, body);
|
2016-10-24 21:55:44 +02:00
|
|
|
// make sure the receive buffer has a free spot
|
2018-10-29 18:42:12 +01:00
|
|
|
receivePacket(false);
|
2016-08-09 01:54:43 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 22:48:25 +02:00
|
|
|
|
2016-05-12 21:35:40 +02:00
|
|
|
/**
|
2016-10-24 21:55:44 +02:00
|
|
|
* Reads the next packet from the radio queue and returns the packet's string
|
|
|
|
* payload or the empty string if the packet did not contain a string.
|
|
|
|
*/
|
2016-08-02 01:02:06 +02:00
|
|
|
//% blockId=radio_datagram_receive_string block="radio receive string" blockGap=8
|
2016-05-12 21:35:40 +02:00
|
|
|
//% weight=44
|
|
|
|
//% help=radio/receive-string
|
2016-10-25 01:30:21 +02:00
|
|
|
//% deprecated=true
|
2018-05-30 00:55:58 +02:00
|
|
|
String receiveString() {
|
|
|
|
if (radioEnable() != MICROBIT_OK) return mkString("", 0);
|
2016-10-24 21:55:44 +02:00
|
|
|
receivePacket(false);
|
|
|
|
return msg;
|
2016-05-12 21:35:40 +02:00
|
|
|
}
|
2016-10-11 22:48:25 +02:00
|
|
|
|
2016-04-03 02:34:06 +02:00
|
|
|
/**
|
2016-10-24 21:55:44 +02:00
|
|
|
* Gets the received signal strength indicator (RSSI) from the last packet taken
|
|
|
|
* from the radio queue (via ``receiveNumber``, ``receiveString``, etc). Not supported in simulator.
|
2016-04-03 02:34:06 +02:00
|
|
|
* namespace=radio
|
|
|
|
*/
|
|
|
|
//% help=radio/received-signal-strength
|
|
|
|
//% weight=40
|
2016-08-02 01:02:06 +02:00
|
|
|
//% blockId=radio_datagram_rssi block="radio received signal strength"
|
2016-10-25 01:30:21 +02:00
|
|
|
//% deprecated=true
|
2016-04-03 02:34:06 +02:00
|
|
|
int receivedSignalStrength() {
|
|
|
|
if (radioEnable() != MICROBIT_OK) return 0;
|
|
|
|
return packet.getRSSI();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the group id for radio communications. A micro:bit can only listen to one group ID at any time.
|
2017-03-04 08:39:42 +01:00
|
|
|
* @param id the group id between ``0`` and ``255``, eg: 1
|
2016-04-03 02:34:06 +02:00
|
|
|
*/
|
|
|
|
//% help=radio/set-group
|
2017-03-01 23:57:47 +01:00
|
|
|
//% weight=10 blockGap=8
|
2016-08-02 01:02:06 +02:00
|
|
|
//% blockId=radio_set_group block="radio set group %ID"
|
2017-03-04 08:39:42 +01:00
|
|
|
//% id.min=0 id.max=255
|
2016-04-03 02:34:06 +02:00
|
|
|
void setGroup(int id) {
|
|
|
|
if (radioEnable() != MICROBIT_OK) return;
|
|
|
|
uBit.radio.setGroup(id);
|
|
|
|
}
|
2016-10-11 22:48:25 +02:00
|
|
|
|
2016-04-03 02:34:06 +02:00
|
|
|
/**
|
|
|
|
* Change the output power level of the transmitter to the given value.
|
2016-06-19 14:15:13 +02:00
|
|
|
* @param power a value in the range 0..7, where 0 is the lowest power and 7 is the highest. eg: 7
|
2016-04-03 02:34:06 +02:00
|
|
|
*/
|
|
|
|
//% help=radio/set-transmit-power
|
2016-08-02 01:02:06 +02:00
|
|
|
//% weight=9 blockGap=8
|
|
|
|
//% blockId=radio_set_transmit_power block="radio set transmit power %power"
|
2017-03-04 08:39:42 +01:00
|
|
|
//% power.min=0 power.max=7
|
2016-10-11 22:48:25 +02:00
|
|
|
//% advanced=true
|
2016-04-03 02:34:06 +02:00
|
|
|
void setTransmitPower(int power) {
|
|
|
|
if (radioEnable() != MICROBIT_OK) return;
|
|
|
|
uBit.radio.setTransmitPower(power);
|
|
|
|
}
|
2016-10-11 22:48:25 +02:00
|
|
|
|
2016-05-11 06:13:16 +02:00
|
|
|
/**
|
|
|
|
* Set the radio to transmit the serial number in each message.
|
2016-10-12 04:36:00 +02:00
|
|
|
* @param transmit value indicating if the serial number is transmitted, eg: true
|
2016-05-11 06:13:16 +02:00
|
|
|
*/
|
|
|
|
//% help=radio/set-transmit-serial-number
|
2016-08-02 01:02:06 +02:00
|
|
|
//% weight=8 blockGap=8
|
|
|
|
//% blockId=radio_set_transmit_serial_number block="radio set transmit serial number %transmit"
|
2016-10-11 22:48:25 +02:00
|
|
|
//% advanced=true
|
2016-05-11 06:13:16 +02:00
|
|
|
void setTransmitSerialNumber(bool transmit) {
|
2016-08-09 01:54:43 +02:00
|
|
|
if (radioEnable() != MICROBIT_OK) return;
|
2016-05-11 06:13:16 +02:00
|
|
|
transmitSerialNumber = transmit;
|
|
|
|
}
|
2016-10-24 21:55:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number payload from the last packet taken from the radio queue
|
|
|
|
* (via ``receiveNumber``, ``receiveString``, etc) or 0 if that packet did not
|
|
|
|
* contain a number.
|
|
|
|
*/
|
|
|
|
//% help=radio/received-number
|
2018-10-29 18:42:12 +01:00
|
|
|
TNumber receivedNumber() {
|
2016-10-24 21:55:44 +02:00
|
|
|
if (radioEnable() != MICROBIT_OK) return 0;
|
2018-10-29 18:42:12 +01:00
|
|
|
return readNumber();
|
2016-10-24 21:55:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the serial number of the sender micro:bit from the last packet taken
|
|
|
|
* from the radio queue (via ``receiveNumber``, ``receiveString``, etc) or 0 if
|
|
|
|
* that packet did not send a serial number.
|
|
|
|
*/
|
|
|
|
//% help=radio/received-serial
|
|
|
|
uint32_t receivedSerial() {
|
|
|
|
if (radioEnable() != MICROBIT_OK) return 0;
|
|
|
|
return serial;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the string payload from the last packet taken from the radio queue
|
|
|
|
* (via ``receiveNumber``, ``receiveString``, etc) or the empty string if that
|
|
|
|
* packet did not contain a string.
|
|
|
|
*/
|
|
|
|
//% help=radio/received-string
|
2018-05-30 00:55:58 +02:00
|
|
|
String receivedString() {
|
|
|
|
if (radioEnable() != MICROBIT_OK || NULL == msg) return mkString("", 0);
|
|
|
|
incrRC(msg);
|
2016-10-24 21:55:44 +02:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2018-01-30 19:11:12 +01:00
|
|
|
/**
|
|
|
|
* Returns the buffer payload from the last packet taken from the radio queue
|
|
|
|
* (via ``receiveNumber``, ``receiveString``, etc) or the empty string if that
|
|
|
|
* packet did not contain a string.
|
|
|
|
*/
|
|
|
|
//% help=radio/received-buffer
|
|
|
|
Buffer receivedBuffer() {
|
2018-05-30 00:55:58 +02:00
|
|
|
if (radioEnable() != MICROBIT_OK || NULL == bufMsg) return mkBuffer(NULL, 0);
|
|
|
|
incrRC(bufMsg);
|
2018-01-30 19:11:12 +01:00
|
|
|
return bufMsg;
|
|
|
|
}
|
|
|
|
|
2016-10-24 21:55:44 +02:00
|
|
|
/**
|
|
|
|
* Returns the system time of the sender micro:bit at the moment when it sent the
|
|
|
|
* last packet taken from the radio queue (via ``receiveNumber``,
|
|
|
|
* ``receiveString``, etc).
|
|
|
|
*/
|
|
|
|
//% help=radio/received-time
|
|
|
|
uint32_t receivedTime() {
|
|
|
|
if (radioEnable() != MICROBIT_OK) return 0;
|
|
|
|
return time;
|
|
|
|
}
|
2016-04-03 02:34:06 +02:00
|
|
|
}
|