Button initiated config service

Dependencies:   BLE_API_EddystoneConfigService_2 mbed nRF51822

Fork of BLE_EddystoneBeaconConfigService_3 by URIBeacon

main.cpp

Committer:
scottjenson
Date:
2015-10-11
Revision:
67:345bc62c748d
Parent:
63:2478203aa555
Child:
68:f7f7ec038494

File content as of revision 67:345bc62c748d:

/* mbed Microcontroller Library
 * Copyright (c) 2006-2013 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 "mbed.h"
#include "BLE.h"
#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
 * which involves advertising a URI. Constructing an object from URIBeaconConfig
 * service sets up advertisements for the configuration mode. It is then up to
 * the application to switch to URIBeacon mode based on some timeout.
 *
 * The following help with this switch.
 */
static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 10;  // Duration after power-on that config service is available.

Ticker configAdvertisementTimeout;

/**
 * Stop advertising the Config Service after a delay; and switch to a non-connectable advertising mode only beacon.
 */
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. */
        configAdvertisementTimeout.detach(); /* disable the callback from the timeout Ticker. */
        EddystoneBeaconConfig->setupEddystoneAdvertisements();        
    }
}

/**
 * Callback triggered upon a disconnection event.
 */
void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
    ledRed = ledOff;
    if (true == params.isConfigured){
        // end advertising, the beacon is configured
        timeout();
    }
    else{
        // eddystone is not configured, continue advertising    
        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
     * mode (within the first 60 seconds of power-up). Thereafter, parameters
     * get copied out to persistent storage before switching to normal URIBeacon
     * operation.
     */
    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;
    
    /* 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->start(!fetchedFromPersistentStorage);
    
    if (!EddystoneBeaconConfig->initSuccessfully()) {
        error("failed to accommodate URI");
    }
    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);
        }
    }
}