Jurica Resetar / Mbed OS acd52832_Indoor_Posit_Central

Dependencies:   aconno_SEGGER_RTT

Files at this revision

API Documentation at this revision

Comitter:
jurica238814
Date:
Tue Mar 06 09:52:46 2018 +0000
Child:
1:2bdc506d8baa
Commit message:
Publish commit.

Changed in this revision

SEGGER_RTT.lib Show annotated file Show diff for this revision Revisions of this file
aconno_ble/aconno_ble.cpp Show annotated file Show diff for this revision Revisions of this file
aconno_ble/aconno_ble.h Show annotated file Show diff for this revision Revisions of this file
aconno_bsp/aconno_bsp.h Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
source/main.cpp Show annotated file Show diff for this revision Revisions of this file
source/main.h Show annotated file Show diff for this revision Revisions of this file
tasks/tasks.cpp Show annotated file Show diff for this revision Revisions of this file
tasks/tasks.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SEGGER_RTT.lib	Tue Mar 06 09:52:46 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/jurica238814/code/aconno_SEGGER_RTT/#e61e7fc7cfe1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aconno_ble/aconno_ble.cpp	Tue Mar 06 09:52:46 2018 +0000
@@ -0,0 +1,74 @@
+/*
+ *   Made by Jurica Resetar @ aconno
+ *   More info @ aconno.de
+ *
+ */
+
+#include "aconno_ble.h"
+
+/**
+* Callback triggered when the ble initialization process has finished
+*/
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
+    BLE&        ble   = params->ble;
+    ble_error_t error = params->error;
+
+    if (error != BLE_ERROR_NONE) {
+        return;
+    }
+
+    /* Ensure that it is the default instance of BLE */
+    if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
+        return;
+    }
+    
+    /* setup advertising */
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE_b);
+    ble.gap().setAdvertisingInterval(ADV_INTERVAL_MS);
+}
+
+/* advertisement callback is called every time a new ble device is discovered */
+void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){
+    uint8_t payloadSize;
+    const uint8_t *payload;
+    uint8_t dataSize=0, dataType=0, tempIndex=0;
+    static uint8_t tryToConnect = 0;
+    
+    payloadSize = params->advertisingDataLen;
+    payload = params->advertisingData;
+    dataSize = *(payload);
+    dataType = *(payload+1);
+    
+    
+    do{
+        if(dataType == DEVICE_NAME_ID){
+            printf("Device name:\t");
+            for(int i=tempIndex; i<tempIndex+dataSize-1; i++){
+                printf("%c", (char)*(payload+tempIndex+i+2));
+                /*
+                if(*(MeriddaDeviceName+i) == *(payload+tempIndex+i+2))
+                {
+                    tryToConnect = 1;
+                    SEND("Meridda device discovered\r\n");
+                }
+                */
+            }
+            tryToConnect = 1;
+            printf("\r\n");
+            printf("RSSI: %d\r\n", params->rssi);
+        }
+        else{
+            tempIndex = tempIndex + dataSize + 1;
+            dataSize = *(payload+tempIndex);
+            dataType = *(payload+tempIndex+1);
+        }
+    }while(tempIndex<payloadSize && !tryToConnect);
+    
+    /*
+    if(tryToConnect){
+        SEND("Trying to establise connection...\r\n");
+        BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
+    }
+    */
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aconno_ble/aconno_ble.h	Tue Mar 06 09:52:46 2018 +0000
@@ -0,0 +1,36 @@
+/*
+*  Made by Jurica Resetar @ aconno
+*  More info @ aconno.de
+*  jurica_resetar@yahoo.com
+*/
+
+#ifndef ACONNO_BLE_H
+#define ACONNO_BLE_H
+
+#include "mbed.h"
+#include "ble/BLE.h"
+#include "GapAdvertisingData.h"
+
+#define MSD_SIZE_b                  (10)
+#define ADV_INTERVAL_MS             (100)
+#define ADVERTISING_DURATION_S      (1)
+#define BLE_SLEEP_DURATION_S        (1)
+#define PRINT_ON_RTT                (1)
+#define DEVICE_NAME_ID              (0x09)
+
+#if PRINT_ON_RTT
+    #include "SEGGER_RTT.h"
+    #define printf(...)                      SEGGER_RTT_printf(0, __VA_ARGS__)
+#else
+    #define printf(...)
+#endif
+
+/* Global variables and constants */
+const char DEVICE_NAME[] = "aconno beacon";
+const char MSD[MSD_SIZE_b] = {0x59, 0x00, 0x9C, 0x23, 0x9D, 0x82, 0x1B, 0xD4, 0x11, 0xE8};
+
+/* Function declarations */
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params);
+void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params);
+
+#endif //ACONNO_BLE_H
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aconno_bsp/aconno_bsp.h	Tue Mar 06 09:52:46 2018 +0000
@@ -0,0 +1,24 @@
+/*
+ *
+ */
+ 
+#ifndef ACONNO_BSP_H
+#define ACONNO_BSP_H
+
+#define PRINT_ON_RTT    (1)
+#define DEBUG_LED       (1)
+
+#if PRINT_ON_RTT
+    #include "SEGGER_RTT.h"
+    #define printf(...)                      SEGGER_RTT_printf(0, __VA_ARGS__)
+#else
+    #define printf(...)
+#endif
+
+#if DEBUG_LED
+    DigitalOut advLed(p22);
+    DigitalOut scanLed(p23);
+    DigitalOut periodicLed(p24);
+#endif
+
+#endif  // ACONNO_BSP_H
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Tue Mar 06 09:52:46 2018 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#91e6db1ea251ffcc973001ed90477f42fdca5751
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/source/main.cpp	Tue Mar 06 09:52:46 2018 +0000
@@ -0,0 +1,33 @@
+/*
+ *  
+ *
+ */
+ 
+#include "main.h"
+
+int main()
+{
+    printf("Main program started.\r\n");
+    
+    BLE &ble = BLE::Instance();
+    ble.init(bleInitComplete);
+    printf("BLE initialised.\r\n");
+    
+    #if DEBUG_LED
+        // Turn the leds off
+        advLed = 1;
+        scanLed = 1;
+        periodicLed = 1;
+    #endif
+       
+    //tBleStartAdvertising.start(callback(bleStartAdvertising, &ble));
+    tBleStartScanning.start(callback(bleStartScanning, &ble));
+    tPeriodicCallback.start(callback(periodicCallback, &ble));
+    
+    while(true)
+    {
+        // Do nothing
+        
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/source/main.h	Tue Mar 06 09:52:46 2018 +0000
@@ -0,0 +1,35 @@
+/*
+ *
+ *
+ */
+
+#ifndef MAIN_H
+#define MAIN_H
+
+#include "mbed.h"
+#include "aconno_ble.h"
+#include "tasks.h"
+#include "ble/BLE.h"
+#include "GapAdvertisingData.h"
+
+#define PRINT_ON_RTT    (1)
+#define DEBUG_LED       (1)
+
+#if PRINT_ON_RTT
+    #include "SEGGER_RTT.h"
+    #define printf(...)                      SEGGER_RTT_printf(0, __VA_ARGS__)
+#else
+    #define printf(...)
+#endif
+
+#if DEBUG_LED
+    DigitalOut advLed(p22);
+    DigitalOut scanLed(p23);
+    DigitalOut periodicLed(p24);
+#endif
+
+Thread tBleStartAdvertising;
+Thread tBleStartScanning;
+Thread tPeriodicCallback;
+
+#endif // MAIN_H
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/tasks.cpp	Tue Mar 06 09:52:46 2018 +0000
@@ -0,0 +1,58 @@
+/*
+ *
+ *
+ */
+
+#include "tasks.h"
+#include "aconno_ble.h"
+
+#if DEBUG_LED
+    extern DigitalOut advLed;
+    extern DigitalOut scanLed;
+    extern DigitalOut periodicLed;
+#endif
+
+void periodicCallback(BLE *ble)
+{
+    while(true)
+    {
+        ble->waitForEvent();
+        #if DEBUG_LED
+            periodicLed = !periodicLed;
+        #endif
+        wait(PERIODIC_CALLBACK_S);
+    }       
+}
+
+void bleStartAdvertising(BLE *ble)
+{
+    while(true)
+    {
+        ble->gap().startAdvertising();
+        printf("Advertisement started.\r\n");
+        #if DEBUG_LED
+            advLed = 0;
+        #endif
+        wait(ADVERTISING_DURATION_S);
+        wait_ms(1000);
+        ble->gap().stopAdvertising();
+        printf("Advertisement stopped.\r\n");
+        #if DEBUG_LED
+            advLed = 1;
+        #endif
+        wait(BLE_SLEEP_DURATION_S);
+        wait_ms(1000);
+    }
+}
+
+void bleStartScanning(BLE *ble)
+{
+    while(true)
+    {
+        ble->gap().setScanParams(1500, 400);
+        ble->gap().startScan(advertisementCallback);
+        Thread::signal_wait(0x00023456);
+        //Thread::signal_clr(ACC_INT_SIG);
+        //bleT.signal_set(DISABLE_BLE_SIG);
+    }    
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/tasks.h	Tue Mar 06 09:52:46 2018 +0000
@@ -0,0 +1,31 @@
+/*
+ *
+ *
+ */
+ 
+#ifndef TASKS_H
+#define TASKS_H
+
+#include "mbed.h"
+#include "aconno_ble.h"
+#include "ble/BLE.h"
+#include "GapAdvertisingData.h"
+#include "ble/DiscoveredCharacteristic.h"
+#include "ble/DiscoveredService.h"
+
+#define DEBUG_LED               (1)
+#define PERIODIC_CALLBACK_S     (1)
+#define PRINT_ON_RTT            (1)
+
+#if PRINT_ON_RTT
+    #include "SEGGER_RTT.h"
+    #define printf(...)                      SEGGER_RTT_printf(0, __VA_ARGS__)
+#else
+    #define printf(...)
+#endif
+
+void bleStartAdvertising(BLE *ble);
+void bleStartScanning(BLE *ble);
+void periodicCallback(BLE *ble);
+
+#endif // TASKS_H
\ No newline at end of file