From a30099447f050ec759145405d24322f1a803356d Mon Sep 17 00:00:00 2001 From: Mirko Zeibig Date: Tue, 29 Sep 2020 18:09:13 +0200 Subject: [PATCH] initial commit --- .gitignore | 5 +++ .vscode/extensions.json | 7 ++++ README.md | 2 ++ boards/bbcmicrobit.json | 44 ++++++++++++++++++++++++ boards/bbcmicrobit_b.json | 34 ++++++++++++++++++ include/README | 39 +++++++++++++++++++++ lib/README | 46 +++++++++++++++++++++++++ mbed_app.json | 7 ++++ platformio.ini | 15 ++++++++ src/main.cpp | 72 +++++++++++++++++++++++++++++++++++++++ test/README | 11 ++++++ 11 files changed, 282 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 boards/bbcmicrobit.json create mode 100644 boards/bbcmicrobit_b.json create mode 100644 include/README create mode 100644 lib/README create mode 100644 mbed_app.json create mode 100644 platformio.ini create mode 100644 src/main.cpp create mode 100644 test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..e80666b --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} diff --git a/README.md b/README.md index 241b907..e86017d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # micro_beacons +Quellcode zum Vortrag: https://media.ccc.de/v/ds20-11329-micro_beacons + diff --git a/boards/bbcmicrobit.json b/boards/bbcmicrobit.json new file mode 100644 index 0000000..721f973 --- /dev/null +++ b/boards/bbcmicrobit.json @@ -0,0 +1,44 @@ +{ + "build": { + "arduino":{ + "ldscript": "nrf51_xxaa.ld" + }, + "core": "nRF5", + "cpu": "cortex-m0", + "extra_flags": "-DARDUINO_BBC_MICROBIT -DUSE_LFRC", + "f_cpu": "16000000L", + "mcu": "nrf51822", + "variant": "BBCmicrobit", + "zephyr": { + "variant": "bbc_microbit" + } + }, + "connectivity": [ + "bluetooth" + ], + "debug": { + "onboard_tools": [ + "cmsis-dap" + ], + "svd_path": "nrf51.svd", + "jlink_device": "nRF51822_xxAA" + }, + "frameworks": [ + "arduino", + "mbed", + "zephyr" + ], + "name": "BBC micro:bit", + "upload": { + "maximum_ram_size": 16384, + "maximum_size": 262144, + "protocol": "cmsis-dap", + "protocols": [ + "cmsis-dap", + "mbed", + "jlink" + ] + }, + "url": "https://developer.mbed.org/platforms/Microbit/", + "vendor": "BBC" +} diff --git a/boards/bbcmicrobit_b.json b/boards/bbcmicrobit_b.json new file mode 100644 index 0000000..ce911e7 --- /dev/null +++ b/boards/bbcmicrobit_b.json @@ -0,0 +1,34 @@ +{ + "build": { + "cpu": "cortex-m0", + "f_cpu": "16000000L", + "mcu": "nrf51822" + }, + "connectivity": [ + "bluetooth" + ], + "debug": { + "onboard_tools": [ + "cmsis-dap" + ], + "svd_path": "nrf51.svd", + "jlink_device": "nRF51822_xxAA" + }, + "frameworks": [ + "mbed" + ], + "name": "BBC micro:bit B(S130)", + "upload": { + "maximum_ram_size": 16384, + "maximum_size": 262144, + "protocol": "cmsis-dap", + "protocols": [ + "cmsis-dap", + "mbed", + "jlink" + ] + }, + "url": "https://developer.mbed.org/platforms/Microbit/", + "vendor": "BBC" +} + diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/mbed_app.json b/mbed_app.json new file mode 100644 index 0000000..8b1769c --- /dev/null +++ b/mbed_app.json @@ -0,0 +1,7 @@ +{ + "target_overrides": { + "*": { + "target.features_add": ["BLE"] + } + } +} diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..f05a9df --- /dev/null +++ b/platformio.ini @@ -0,0 +1,15 @@ +[env:bbcmicrobit] +platform = nordicnrf51 +board = bbcmicrobit_b +framework = mbed + +lib_deps = + microbit + +build_flags = -DNRF51_S130 + -std=c++11 + -I$PLATFORMIO_HOME_DIR/packages/framework-mbed@5.51105.190312/features/FEATURE_BLE +build_unflags = -std=gnu++98 +upload_protocol = mbed + +monitor_speed = 115200 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..08d1db2 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,72 @@ +#include "MicroBit.h" +#include "ble/DiscoveredCharacteristic.h" +#include "ble/DiscoveredService.h" + +uint8_t counter; +MicroBit uBit; + +void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) +{ + + if (params->advertisingDataLen == 31) + { + //len=31 + //data=02011A03036FFD17166FFD86BFAF5ACE16AB0C809B47092C07E20DF6017412 + // ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ + // | | | | | | | | | | +-- Associated Encrypted Metadata + // | | | | | | | | | +-- Rolling Proximity Identifier + // | | | | | | | | +------ Service - 0xFD6F + // | | | | | | | +-------- Type - 0x16 + // | | | | | | +---------- Length - 0x17 + // | | | | | +-------------- Service - 0xFD6F + if (params->advertisingData[0] == 0x02 && + params->advertisingData[1] == 0x01 && + params->advertisingData[2] == 0x1a && + params->advertisingData[3] == 0x03 && + params->advertisingData[4] == 0x03 && + params->advertisingData[5] == 0x6f && + params->advertisingData[6] == 0xfd + ) { + int x; + int y; + x = (counter) % 5; + y = (counter) / 5; + uBit.display.image.setPixelValue(x, y, 0); + counter++; + counter = counter % 25; + x = counter % 5; + y = counter / 5; + char id[33]; + int8_t rssi = params->rssi; + uBit.display.image.setPixelValue(x, y, (uint8_t)rssi); + BLEProtocol::AddressBytes_t peer; + memcpy(peer, params->peerAddr, BLEProtocol::ADDR_LEN); + + for (int i = 0; i < 16; i++) + { + sprintf(&id[i * 2], "%02X", params->advertisingData[i + 11]); + } + uBit.serial.printf("id=%s - %i (%02x:%02x:%02x:%02x:%02x:%02x)\r\n", + id, (uint8_t)rssi, peer[5], peer[4], peer[3], peer[2], peer[1], peer[0]); + } + } +} + +int main() +{ + counter = 0; + uBit.display.setDisplayMode(DISPLAY_MODE_GREYSCALE); + scheduler_init(uBit.messageBus); + uBit.serial.printf("Scanner.....\r\n"); + + uBit.ble = new BLEDevice(); + uBit.ble->init(); + uBit.ble->gap().setScanParams(500, 400); + uBit.ble->gap().startScan(advertisementCallback); + + while (true) { + uBit.ble->waitForEvent(); + } + return 0; +} + diff --git a/test/README b/test/README new file mode 100644 index 0000000..df5066e --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html