Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
support for reading 4 numbers from radio buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Feb 10, 2016
1 parent 88da53e commit cb9ddb7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
12 changes: 12 additions & 0 deletions generated/metainfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,18 @@
"args": 1,
"full": "bitvm::bitvm_micro_bit::createReadOnlyImage"
},
{
"proto": "int micro_bit::datagramGetNumber (int index); ",
"name": "micro_bit::datagramGetNumber",
"type": "F",
"args": 1
},
{
"proto": "int micro_bit::datagramGetRSSI (); ",
"name": "micro_bit::datagramGetRSSI",
"type": "F",
"args": 0
},
{
"proto": "int micro_bit::datagramReceiveNumber (); ",
"name": "micro_bit::datagramReceiveNumber",
Expand Down
2 changes: 2 additions & 0 deletions generated/pointers.inc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
(uint32_t)(void*)::bitvm::bitvm_micro_bit::createImage, // F1 over {shim:micro_bit::createImage}
(uint32_t)(void*)::bitvm::bitvm_micro_bit::createImageFromString, // F1 over {shim:micro_bit::createImageFromString}
(uint32_t)(void*)::bitvm::bitvm_micro_bit::createReadOnlyImage, // F1 over {shim:micro_bit::createReadOnlyImage}
(uint32_t)(void*)::touch_develop::micro_bit::datagramGetNumber, // F1 {shim:micro_bit::datagramGetNumber}
(uint32_t)(void*)::touch_develop::micro_bit::datagramGetRSSI, // F0 {shim:micro_bit::datagramGetRSSI}
(uint32_t)(void*)::touch_develop::micro_bit::datagramReceiveNumber, // F0 {shim:micro_bit::datagramReceiveNumber}
(uint32_t)(void*)::touch_develop::micro_bit::datagramSendNumber, // P1 {shim:micro_bit::datagramSendNumber}
(uint32_t)(void*)::touch_develop::micro_bit::datagramSendNumbers, // P4 {shim:micro_bit::datagramSendNumbers}
Expand Down
2 changes: 2 additions & 0 deletions microbit-touchdevelop/MicroBitTouchDevelop.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,8 @@ namespace touch_develop {
void datagramSendNumber(int value);
void datagramSendNumbers(int value0, int value1, int value2, int value3);
int datagramReceiveNumber();
int datagramGetNumber(int index);
int datagramGetRSSI();
void onDatagramReceived(function<void()> f);

namespace devices {
Expand Down
38 changes: 29 additions & 9 deletions source/MicroBitTouchDevelop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,17 @@ namespace touch_develop {
// -------------------------------------------------------------------------
uint8_t radioDefaultGroup = MICROBIT_RADIO_DEFAULT_GROUP;
bool radioEnabled = false;
int datagramBuf[4];
int datagramRSSI = -1;

int radioEnable() {
int r = uBit.radio.enable();
if (r != MICROBIT_OK) return r;
if (!radioEnabled) {
if (radioDefaultGroup != MICROBIT_RADIO_DEFAULT_GROUP)
if (radioDefaultGroup != MICROBIT_RADIO_DEFAULT_GROUP) {
uBit.radio.setGroup(radioDefaultGroup);
}
memset(datagramBuf, 4, 0);
radioEnabled = true;
}
return r;
Expand All @@ -256,12 +260,8 @@ namespace touch_develop {
registerWithDal(MES_BROADCAST_GENERAL_ID, message, f);
}

static int datagramBuf[4];
void datagramSendNumber(int value) {
if (radioEnable() != MICROBIT_OK) return;

datagramBuf[0] = value;
uBit.radio.datagram.send((uint8_t*)datagramBuf, 4);
datagramSendNumbers(value, 0, 0, 0);
}

void datagramSendNumbers(int value0, int value1, int value2, int value3) {
Expand All @@ -271,16 +271,36 @@ namespace touch_develop {
datagramBuf[1] = value1;
datagramBuf[2] = value2;
datagramBuf[3] = value3;
uBit.radio.datagram.send((uint8_t*)datagramBuf, 16);
uBit.radio.datagram.send((uint8_t*)datagramBuf, 16);
memset(datagramBuf, 4, 0);
}

int datagramReceiveNumber() {
if (radioEnable() != MICROBIT_OK) return 0;

datagramBuf[0] = 0;
uBit.radio.datagram.recv((uint8_t*)datagramBuf, 4);
memset(datagramBuf, 4, 0);

PacketBuffer packet = uBit.radio.datagram.recv();
uint8_t* buf = (uint8_t*)datagramBuf;
int n = min(16, packet.length());
for(int i = 0;i<n;++i)
buf[i] = packet[i];
datagramRSSI = packet.getRSSI();

return datagramBuf[0];
}

int datagramGetNumber(int index) {
if (radioEnable() != MICROBIT_OK) return 0;
if (index < 0 || index >= 4) return 0;
return datagramBuf[index];
}

int datagramGetRSSI() {
if (radioEnable() != MICROBIT_OK) return 0;

return datagramRSSI;
}

void onDatagramReceived(function<void()> f) {
if (radioEnable() != MICROBIT_OK) return;
Expand Down

0 comments on commit cb9ddb7

Please sign in to comment.