Skip to content

Commit

Permalink
Make it more idiot-proof
Browse files Browse the repository at this point in the history
Comms to NINA are now automatically started when needed.

The example should be a bit clearer now.
  • Loading branch information
joonazan committed Mar 13, 2020
1 parent 660567f commit 4917681
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 45 deletions.
28 changes: 12 additions & 16 deletions examples/multiplication/multiplication.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,28 @@

#include <FastBLE.h>

void setup() {
}
void setup() {}

BLE::Output<uint64_t> out;
BLETypes::Output<uint64_t> out;

/*
* Callback that is later attached to one input Characteristic of the Service
*/
// Input characteristics accept writes with a length corresponding to the callback's argument
// In this case four bytes
void cb(int32_t x) {
out.write((int64_t)x << 8);
}

void loop() {
/*
* FastBLE object's constructors may only be called after the Arduino library has initialized itself.
* That makes them inconvenient to initialize in setup, so I've hijacked loop instead.
*/
BLE ble;
out = BLE::Output<uint64_t>(UUID_128(0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12, 0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59));
// Outputs and inputs may only be added after the Arduino libraries have started up i.e. not in global scope
out = BLE.add_output<uint64_t>(UUID_128(0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12, 0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59));

auto in = BLE.make_input(UUID_128(0x2a, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12, 0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59), cb);

BLE::Input<int32_t> in(UUID_128(0x2a, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12, 0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59), cb);
BLE::IInput* inputs[] = { &in };
// I do initialization in loop because this array must live as long as the program
BLETypes::IInput* inputs[] = { &in };

ble.start(UUID_128(0x2c, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12, 0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0xED, 0xFE), inputs);
BLE.start(UUID_128(0x2c, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12, 0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0xED, 0xFE), inputs);

while(true) {
ble.poll();
BLE.poll();
}
}
64 changes: 41 additions & 23 deletions src/FastBLE.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,16 @@

#define UUID_128(bytes...) (ble_uuid_any_t){ .u128 = { u: {type: BLE_UUID_TYPE_128}, value: { bytes } } }

extern uint16_t output_count;
extern uint16_t input_count;
class BLEClass;

class BLE {
public:
namespace BLETypes {
template <typename T>
class Output {
uint16_t _n;
Output(uint16_t x) : _n(x) {}
friend BLEClass;
public:
Output() {}

Output(ble_uuid_any_t id) {
_n = output_count++;

SetupMessage msg
{
type: BLE_CREATE_OUTPUT,
uuid: id,
length: sizeof(T),
};
Serial2.write((uint8_t*)&msg, sizeof(SetupMessage));
};

void write(T obj) {
Serial2.write((uint8_t*)&_n, 2);
Serial2.write((uint8_t*)&obj, sizeof(T));
Expand All @@ -37,7 +24,7 @@ class BLE {
class IInput {
virtual void receive();
virtual void setup();
friend BLE;
friend BLEClass;
};

template <typename T>
Expand All @@ -60,14 +47,45 @@ class BLE {
Serial2.readBytes((uint8_t*)&x, sizeof(T));
_cb(x);
}
public:
Input(ble_uuid_any_t id, void (*cb)(T)) : _cb(cb), _uuid(id) { input_count++; };

Input(ble_uuid_any_t id, void (*cb)(T)) : _cb(cb), _uuid(id) {};
friend BLEClass;
};
}

BLE();
void start(ble_uuid_any_t, IInput**);
class BLEClass {
public:
template <typename T>
BLETypes::Output<T> add_output(ble_uuid_any_t id) {
init_if_not_initialized();

SetupMessage msg
{
type: BLE_CREATE_OUTPUT,
uuid: id,
length: sizeof(T),
};
Serial2.write((uint8_t*)&msg, sizeof(SetupMessage));

return { _output_count++ };
}

template <typename T>
BLETypes::Input<T> make_input(ble_uuid_any_t id, void (*cb)(T)) {
_input_count++;
return { id, cb };
};

void start(ble_uuid_any_t, BLETypes::IInput**);
void poll();

private:
IInput** _inputs;
bool _initialized = false;
void init_if_not_initialized();

uint16_t _output_count = 0;
uint16_t _input_count = 0;
BLETypes::IInput** _inputs;
};

extern BLEClass BLE;
18 changes: 12 additions & 6 deletions src/FastBle.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#include "FastBLE.h"

uint16_t output_count = 0;
uint16_t input_count = 0;
BLEClass BLE;

void BLEClass::init_if_not_initialized() {
if (_initialized) {
return;
}
_initialized = true;

BLE::BLE() {
// reset Nina
pinMode(NINA_RESETN, OUTPUT);
digitalWrite(NINA_RESETN, HIGH);
Expand All @@ -15,10 +19,12 @@ BLE::BLE() {
while(!Serial2);
}

void BLE::start(ble_uuid_any_t svc_id, IInput** inputs) {
void BLEClass::start(ble_uuid_any_t svc_id, BLETypes::IInput** inputs) {
init_if_not_initialized();

_inputs = inputs;

for (uint16_t i = 0; i < input_count; i++) {
for (uint16_t i = 0; i < _input_count; i++) {
inputs[i]->setup();
}

Expand All @@ -30,7 +36,7 @@ void BLE::start(ble_uuid_any_t svc_id, IInput** inputs) {
Serial2.write((uint8_t*)&msg, sizeof(SetupMessage));
}

void BLE::poll() {
void BLEClass::poll() {
if (Serial2.available() >= 2) {
uint16_t input_no;
Serial2.readBytes((uint8_t*)&input_no, 2);
Expand Down

0 comments on commit 4917681

Please sign in to comment.