Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/helpers/nrf52/SerialBLEInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,13 @@ void SerialBLEInterface::begin(const char* prefix, char* name, uint32_t pin_code

char charpin[20];
snprintf(charpin, sizeof(charpin), "%lu", (unsigned long)pin_code);

// If we want to control BLE LED ourselves, uncomment this:
// Bluefruit.autoConnLed(false);

#if defined(RAK_BOARD)
Bluefruit.autoConnLed(false);
#ifdef LED_BLUE
digitalWrite(LED_BLUE, 1 - LED_STATE_ON);
#endif
#endif
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.begin();

Expand Down Expand Up @@ -252,6 +256,8 @@ void SerialBLEInterface::enable() {
_isEnabled = true;
clearBuffers();
_last_health_check = millis();
_last_led_blink = 0;
_led_state = false;

Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.start(0);
Expand All @@ -265,6 +271,10 @@ void SerialBLEInterface::disconnect() {

void SerialBLEInterface::disable() {
_isEnabled = false;
_led_state = false;
#if defined(RAK_BOARD) && defined(LED_BLUE) && LED_BLUE >= 0
digitalWrite(LED_BLUE, 1 - LED_STATE_ON);
#endif
BLE_DEBUG_PRINTLN("SerialBLEInterface: disable");

Bluefruit.Advertising.restartOnDisconnect(false);
Expand Down Expand Up @@ -296,6 +306,20 @@ size_t SerialBLEInterface::writeFrame(const uint8_t src[], size_t len) {
}

size_t SerialBLEInterface::checkRecvFrame(uint8_t dest[]) {
#if defined(RAK_BOARD) && defined(LED_BLUE) && LED_BLUE >= 0
if (_isEnabled && _conn_handle == BLE_CONN_HANDLE_INVALID) {
unsigned long t = millis();
if (t - _last_led_blink >= 500) {
_last_led_blink = t;
_led_state = !_led_state;
digitalWrite(LED_BLUE, _led_state ? LED_STATE_ON : (1 - LED_STATE_ON));
}
} else if (_led_state) {
_led_state = false;
digitalWrite(LED_BLUE, 1 - LED_STATE_ON);
}
#endif

if (send_queue_len > 0) {
if (!isConnected()) {
BLE_DEBUG_PRINTLN("writeBytes: connection invalid, clearing send queue");
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/nrf52/SerialBLEInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class SerialBLEInterface : public BaseSerialInterface {
uint16_t _conn_handle;
unsigned long _last_health_check;
unsigned long _last_retry_attempt;
unsigned long _last_led_blink;
bool _led_state;

struct Frame {
uint8_t len;
Expand Down Expand Up @@ -49,6 +51,8 @@ class SerialBLEInterface : public BaseSerialInterface {
_conn_handle = BLE_CONN_HANDLE_INVALID;
_last_health_check = 0;
_last_retry_attempt = 0;
_last_led_blink = 0;
_led_state = false;
send_queue_len = 0;
recv_queue_len = 0;
}
Expand Down