Button initiated config service

Dependencies:   BLE_API_EddystoneConfigService_2 mbed nRF51822

Fork of BLE_EddystoneBeaconConfigService_3 by URIBeacon

Files at this revision

API Documentation at this revision

Comitter:
scottjenson
Date:
Sun Oct 11 22:46:57 2015 +0000
Parent:
66:e2522ba2de62
Child:
68:f7f7ec038494
Commit message:
This is my version of the config image. Works on nRF USB dongle, now trying to get it to work on minewtech board

Changed in this revision

BLE_API.lib Show annotated file Show diff for this revision Revisions of this file
EddystoneConfigService.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
nrfConfigParamsPersistence.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/BLE_API.lib	Thu Sep 24 00:19:02 2015 +0000
+++ b/BLE_API.lib	Sun Oct 11 22:46:57 2015 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/teams/URIBeacon/code/BLE_API_EddystoneConfigService_2/#1e6e77b0b3a3
+https://developer.mbed.org/teams/URIBeacon/code/BLE_API_EddystoneConfigService_2/#e499d62e02f9
--- a/EddystoneConfigService.h	Thu Sep 24 00:19:02 2015 +0000
+++ b/EddystoneConfigService.h	Sun Oct 11 22:46:57 2015 +0000
@@ -90,6 +90,7 @@
     struct Params_t {
         // Config Data
         bool          isConfigured;  // Flag for configuration being complete, True = configured, false = not configured. Reset at instantiation, used for external callbacks. 
+        bool          deepSleep;     // Flag set so device boots into deep sleep, button press turns on beacon
         uint8_t       lockedState;
         Lock_t        lock;
         uint8_t       flags;
@@ -241,7 +242,7 @@
         if (defaultUriDataLength > URI_DATA_MAX) {
             return;
         }
-        memcpy(defaultUriData,uriIn,URI_DATA_MAX);
+        //memcpy(defaultUriData,uriIn,URI_DATA_MAX);
         //defaultUriDataLength = strlen(uriIn);
         INFO("\t default URI = %s : %d", uriIn, defaultUriDataLength);
 //              defaultUriData = (UriData_t *)uriIn;
@@ -390,6 +391,7 @@
         INFO("Resetting to defaults");
         // General
         params.lockedState      = false;
+        params.deepSleep        = true; // boot into deepSleep
         memset(params.lock, 0, sizeof(Lock_t));
         params.flags            = 0x10;
         memcpy(params.advPowerLevels, defaultAdvPowerLevels, sizeof(PowerLevels_t));
--- a/main.cpp	Thu Sep 24 00:19:02 2015 +0000
+++ b/main.cpp	Sun Oct 11 22:46:57 2015 +0000
@@ -19,11 +19,58 @@
 #include "EddystoneConfigService.h"
 
 #include "ConfigParamsPersistence.h"
+//#include "BLEDevice.h"
+#include "DeviceInformationService.h"
 
 BLE ble;
 EddystoneConfigService *EddystoneBeaconConfig;
 EddystoneConfigService::Params_t params;
 
+Serial pc(USBTX,USBRX);   // uncomment to use printf(...)
+
+#define TINYBLE false
+#define SEEEDARCH true
+
+#if TINYBLE
+int ledOn = 0;
+int ledOff = 1;
+
+InterruptIn button1(P0_17);
+DigitalOut ledRed(P0_22);
+DigitalOut ledGreen(P0_21);
+DigitalOut ledBlue(P0_23);
+#endif
+
+#if SEEEDARCH
+int ledOn = 1;
+int ledOff = 0;
+
+InterruptIn button1(P0_17);
+InterruptIn mysw1(P0_18);
+DigitalOut ledRed(P0_12);
+DigitalOut ledGreen(P0_15);
+DigitalOut ledBlue(P0_16);
+#endif
+
+// Make sure to turn off before and after animation
+// to separate it from the LED which may already be on
+void ledRebootAnimation(void) {
+    ledRed = ledOff;
+    wait(0.1);
+    ledRed = ledOn;
+    wait(0.1);
+    ledRed = ledOff;
+    wait(0.1);
+    ledRed = ledOn;
+    wait(0.1);
+    ledRed = ledOff;
+    wait(0.1);
+    ledRed = ledOn;
+    wait(0.1);
+    ledRed = ledOff; 
+    wait(0.1);
+}
+
 /**
  * URIBeaconConfig service can operate in two modes: a configuration mode which
  * allows a user to update settings over a connection; and normal URIBeacon mode
@@ -33,7 +80,7 @@
  *
  * The following help with this switch.
  */
-static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 30;  // Duration after power-on that config service is available.
+static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 10;  // Duration after power-on that config service is available.
 
 Ticker configAdvertisementTimeout;
 
@@ -42,6 +89,7 @@
  */
 void timeout(void)
 {
+    ledRed = ledOff;
     Gap::GapState_t state;
     state = ble.getGapState();
     if (!state.connected) { /* don't switch if we're in a connected state. */
@@ -55,6 +103,7 @@
  */
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
 {
+    ledRed = ledOff;
     if (true == params.isConfigured){
         // end advertising, the beacon is configured
         timeout();
@@ -64,12 +113,32 @@
         ble.gap().startAdvertising();
     }
 }
+int reboot = false; // global used later to reboot the beacon
+int dirty = false;  // global used to mark params is dirty and needs to be saved
+
+void handle_button1() {
+    printf("handle_button1\n\r");
+    ble.gap().stopAdvertising();
+    params.deepSleep = !params.deepSleep;
+    dirty = true;
+    }
+
+extern void pstorageSetup();
 
 int main(void)
 {
+    printf("main\n\r");
+    ledRed = ledOff;
+    ledGreen = ledOff;
+    ledBlue = ledOff;
     ble.init();
     ble.gap().onDisconnection(disconnectionCallback);
 
+    // Initialize button1 and pull up
+    // set interrupt callback for button1    
+    button1.fall(&handle_button1);
+    button1.mode(PullUp);
+
     /*
      * Load parameters from (platform specific) persistent storage. Parameters
      * can be set to non-default values while the URIBeacon is in configuration
@@ -79,28 +148,41 @@
      */
     bool fetchedFromPersistentStorage = loadURIBeaconConfigParams(&params);
     
+    // only setup beacon if device is NOT in deepSleep
     // Set UID and TLM frame data
-    EddystoneConfigService::UIDNamespaceID_t uidNamespaceID = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; // 10Byte Namespace UUID
-    EddystoneConfigService::UIDInstanceID_t  uidInstanceID = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; // 6Byte Instance ID
-    uint8_t tlmVersion = 0x00;
+    //EddystoneConfigService::UIDNamespaceID_t uidNamespaceID = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; // 10Byte Namespace UUID
+    //EddystoneConfigService::UIDInstanceID_t  uidInstanceID = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; // 6Byte Instance ID
+    //uint8_t tlmVersion = 0x00;
     
     /* Initialize a EddystoneBeaconConfig service providing config params, default URI, and power levels. */
     static EddystoneConfigService::PowerLevels_t defaultAdvPowerLevels = {-47, -33, -21, -13}; // Values for ADV packets related to firmware levels, calibrated based on measured values at 1m
     static EddystoneConfigService::PowerLevels_t radioPowerLevels =      {-30, -16, -4, 4};    // Values for radio power levels, provided by manufacturer.
     EddystoneBeaconConfig = new EddystoneConfigService(ble, params, defaultAdvPowerLevels, radioPowerLevels);
     EddystoneBeaconConfig->setDefaultURIFrameData("http://mbed.org",2);
-    EddystoneBeaconConfig->setDefaultUIDFrameData(&uidNamespaceID, &uidInstanceID,5);
-    EddystoneBeaconConfig->setDefaultTLMFrameData(tlmVersion,10);
+    //EddystoneBeaconConfig->setDefaultUIDFrameData(&uidNamespaceID, &uidInstanceID,5);
+    //EddystoneBeaconConfig->setDefaultTLMFrameData(tlmVersion,10);
     EddystoneBeaconConfig->start(!fetchedFromPersistentStorage);
     
     if (!EddystoneBeaconConfig->initSuccessfully()) {
         error("failed to accommodate URI");
     }
-    configAdvertisementTimeout.attach(timeout, CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS);
-
-    ble.gap().startAdvertising(); /* Set the whole thing in motion. After this call a GAP central can scan the EddystoneBeaconConfig
-                                   * service. This can then be switched to the normal URIBeacon functionality after a timeout. */
+    if (!params.deepSleep) {
+        printf("deepSleep is false\n\r");
+        ledRed = ledOn;
+        configAdvertisementTimeout.attach(timeout, CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS);
+        ble.gap().startAdvertising(); /* Set the whole thing in motion. After this call a GAP central can scan the EddystoneBeaconConfig
+                                       * service. This can then be switched to the normal URIBeacon functionality after a timeout. */
+        void pstorageSetup();
+    }
+    else {
+        printf("deepSleep is true\n\r");
+        ledRed = ledOff;
+    }
+   
     while (true) {
         ble.waitForEvent();
+        if (dirty) {
+            saveURIBeaconConfigParams(&params);
+        }
     }
 }
--- a/nrfConfigParamsPersistence.cpp	Thu Sep 24 00:19:02 2015 +0000
+++ b/nrfConfigParamsPersistence.cpp	Sun Oct 11 22:46:57 2015 +0000
@@ -41,6 +41,10 @@
 static PersistentParams_t persistentParams;
 
 static pstorage_handle_t pstorageHandle;
+extern int reboot;
+extern int dirty;
+extern void ledRebootAnimation();
+
 
 /**
  * Dummy callback handler needed by Nordic's pstorage module. This is called
@@ -52,23 +56,30 @@
                                          uint8_t           *p_data,
                                          uint32_t           data_len)
 {
+    if (dirty) {
+        ledRebootAnimation();
+        NVIC_SystemReset();
+    }
     /* APP_ERROR_CHECK(result); */
 }
 
+// ScottJ Routine Rohit asked to setup to reset storage callback after going into advertising mode
+void pstorageSetup() {
+    pstorage_init();
+    static pstorage_module_param_t pstorageParams = {
+        .cb          = pstorageNotificationCallback,
+        .block_size  = sizeof(PersistentParams_t),
+        .block_count = 1
+    };
+    pstorage_register(&pstorageParams, &pstorageHandle);
+}
 /* Platform-specific implementation for persistence on the nRF5x. Based on the
  * pstorage module provided by the Nordic SDK. */
 bool loadURIBeaconConfigParams(EddystoneConfigService::Params_t *paramsP)
 {
     static bool pstorageInitied = false;
     if (!pstorageInitied) {
-        pstorage_init();
-
-        static pstorage_module_param_t pstorageParams = {
-            .cb          = pstorageNotificationCallback,
-            .block_size  = sizeof(PersistentParams_t),
-            .block_count = 1
-        };
-        pstorage_register(&pstorageParams, &pstorageHandle);
+        pstorageSetup();
         pstorageInitied = true;
     }
 
@@ -87,7 +98,7 @@
  * pstorage module provided by the Nordic SDK. */
 void saveURIBeaconConfigParams(const EddystoneConfigService::Params_t *paramsP)
 {
-    memcpy(&persistentParams.params, paramsP, sizeof(EddystoneConfigService::Params_t));
+   memcpy(&persistentParams.params, paramsP, sizeof(EddystoneConfigService::Params_t));
     if (persistentParams.persistenceSignature != PersistentParams_t::MAGIC) {
         persistentParams.persistenceSignature = PersistentParams_t::MAGIC;
         pstorage_store(&pstorageHandle,