Created
February 3, 2020 02:18
-
-
Save pipitone/6dfb549d1daf345194c08ed182e3ef89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* mbed Microcontroller Library | |
* Copyright (c) 2006-2015 ARM Limited | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
#include <events/mbed_events.h> | |
#include <mbed.h> | |
#include "ble/BLE.h" | |
#include "ble/gap/Gap.h" | |
#include "ble/gap/AdvertisingDataParser.h" | |
#include "platform/CircularBuffer.h" | |
#define BUF_SIZE 20 | |
static EventQueue event_queue(/* event count */ 20 * EVENTS_EVENT_SIZE); | |
class LEDBlinkerDemo : ble::Gap::EventHandler { | |
public: | |
LEDBlinkerDemo(BLE &ble, events::EventQueue &event_queue) : | |
_ble(ble), | |
_event_queue(event_queue), | |
_alive_led(LED1, 1), | |
_actuated_led(LED2, 0) { } | |
~LEDBlinkerDemo() { } | |
void start() { | |
_ble.gap().setEventHandler(this); | |
_ble.init(this, &LEDBlinkerDemo::on_init_complete); | |
_event_queue.call_every(10000, this, &LEDBlinkerDemo::send_recent_beacons); | |
_event_queue.dispatch_forever(); | |
} | |
private: | |
/** Callback triggered when the ble initialization process has finished */ | |
void on_init_complete(BLE::InitializationCompleteCallbackContext *params) { | |
if (params->error != BLE_ERROR_NONE) { | |
printf("Ble initialization failed."); | |
return; | |
} | |
//print_mac_address(); | |
ble::ScanParameters scan_params; | |
scan_params.set1mPhyConfiguration(ble::scan_interval_t(5000), ble::scan_window_t(1000), false); | |
_ble.gap().setScanParameters(scan_params); | |
_ble.gap().startScan(); | |
} | |
void send_recent_beacons() { | |
_alive_led = !_alive_led; | |
beacon data; | |
printf("\n\n-----------------------------\n"); | |
printf ("Beacons: \n"); | |
int i = 1; | |
while (!buf.empty()) { | |
buf.pop(data); | |
printf("%02d. ", i); | |
print_address(data.addr.data()); | |
printf(" rssi: %d \n", data.rssi); | |
i++; | |
} | |
printf("-----------------------------\n\n"); | |
} | |
private: | |
/** print device address to the terminal */ | |
void print_address(const uint8_t *addr) | |
{ | |
printf("%02x:%02x:%02x:%02x:%02x:%02x", | |
addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]); | |
} | |
void onAdvertisingReport(const ble::AdvertisingReportEvent &event) { | |
//if (event.getRssi() < -40) continue; | |
_actuated_led = false; | |
print_address(event.getPeerAddress().data()); | |
printf(" rssi: %d\r\n", event.getRssi()); | |
beacon b {event.getPeerAddress(), event.getRssi()}; | |
buf.push(b); | |
_actuated_led = true; | |
} | |
private: | |
struct beacon { | |
ble::address_t addr; | |
ble::rssi_t rssi; | |
}; | |
BLE &_ble; | |
events::EventQueue &_event_queue; | |
DigitalOut _alive_led; | |
DigitalOut _actuated_led; | |
CircularBuffer<beacon, BUF_SIZE> buf; | |
}; | |
/** Schedule processing of events from the BLE middleware in the event queue. */ | |
void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) { | |
event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents)); | |
} | |
int main() | |
{ | |
BLE &ble = BLE::Instance(); | |
ble.onEventsToProcess(schedule_ble_events); | |
LEDBlinkerDemo demo(ble, event_queue); | |
demo.start(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install instructions for Ubuntu 19.10: