pxt-calliope/libs/radio/radio.cpp

111 lines
3.5 KiB
C++
Raw Normal View History

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
//% color=#E3008C weight=96 icon="\uf012"
2016-04-03 02:34:06 +02:00
namespace radio {
bool radioEnabled = false;
PacketBuffer packet;
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());
uBit.radio.setTransmitPower(6); // start with high power by default
2016-04-03 02:34:06 +02:00
radioEnabled = true;
}
return r;
}
/**
* 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;
uBit.radio.event.eventReceived(MicroBitEvent(src, value, CREATE_ONLY));
2016-04-03 02:34:06 +02:00
}
/**
* Takes the next packet from the radio queue and returns its contents in a Buffer
*/
//% 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());
}
/**
* Sends a raw packet through the radio
*/
//% advanced=true
void sendRawPacket(Buffer msg) {
if (radioEnable() != MICROBIT_OK || NULL == msg) return;
uBit.radio.datagram.send(msg->data, msg->length);
2016-04-03 02:34:06 +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
//% deprecated=true
void onDataReceived(Action body) {
if (radioEnable() != MICROBIT_OK) return;
registerWithDal(MICROBIT_ID_RADIO, MICROBIT_RADIO_EVT_DATAGRAM, body);
readRawPacket();
}
2016-04-03 02:34:06 +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"
//% deprecated=true
2016-04-03 02:34:06 +02:00
int receivedSignalStrength() {
if (radioEnable() != MICROBIT_OK || packet == NULL) return 0;
2016-04-03 02:34:06 +02:00
return packet.getRSSI();
}
/**
* Sets the group id for radio communications. A micro:bit can only listen to one group ID at any time.
* @param id the group id between ``0`` and ``255``, eg: 1
2016-04-03 02:34:06 +02:00
*/
//% help=radio/set-group
//% weight=10 blockGap=8
2016-08-02 01:02:06 +02:00
//% blockId=radio_set_group block="radio set group %ID"
//% 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-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"
//% power.min=0 power.max=7
//% advanced=true
2016-04-03 02:34:06 +02:00
void setTransmitPower(int power) {
if (radioEnable() != MICROBIT_OK) return;
uBit.radio.setTransmitPower(power);
}
}