Run on TY51822r3 board with ACC sensor (LIS3DH or BMC050)

Dependencies:   BLE_API LIS3DH mbed nRF51822 BMC050 nRF51_LowPwr nRF51_Vdd

Fork of BLE_EddystoneBeacon_Service by Bluetooth Low Energy

main.cpp

Committer:
kenjiArai
Date:
2016-02-17
Revision:
35:c64495aac4d1
Parent:
34:f6d4a699a1ea
Child:
36:5508506dda71

File content as of revision 35:c64495aac4d1:

/* 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.
 */

/*
 *  /////// Works well on Switch Science mbed TY51822r3 ///////
 *  Modified by Kenji Arai
 *      http://www.page.sannet.ne.jp/kenjia/index.html
 *      http://mbed.org/users/kenjiArai/
 *
 *      Started:  Feburary  1st, 2016
 *      Revised:  Feburary 18th, 2016
 *
 *  Original:
 *      nRF51822_SimpleControls
 *      https://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_EddystoneBeacon_Service/
 *  Tested Controller Device:
 *      iPhone6 Physical Web (PhyWeb) By Viet Hoa Dinh
 *      https://itunes.apple.com/us/app/physical-web/id927653608?mt=8
 */

/*
 *  STEP1
 *      If you just got the board, plese set following difinition and not connect all of pins.
#define NOT_USE_ACC             1
#define CHK_ACC                 0
#define NEED_CONSOLE_OUTPUT     0 
 *  STEP2
 *      If you equiped the LIS3DH Acc sensor, please check ACC data using folloing definition.
#define NOT_USE_ACC             0
#define CHK_ACC                 1
#define NEED_CONSOLE_OUTPUT     1
 *  STEP3
 *      This is a final setup without no debug information.
#define NOT_USE_ACC             0
#define CHK_ACC                 0
#define NEED_CONSOLE_OUTPUT     0 
 *
 */

//  Include ---------------------------------------------------------------------------------------
#include "mbed.h"
#include "BLE.h"
#include "EddystoneService.h"
#include "LIS3DH.h"

//  Definition ------------------------------------------------------------------------------------
#define NOT_USE_ACC             0
#define CHK_ACC                 0
#define NEED_CONSOLE_OUTPUT     0 /* Set this if you need debug messages on the console;
                                   * it will have an impact on code-size and power consumption. */

#if NEED_CONSOLE_OUTPUT
#define DEBUG(...) { printf(__VA_ARGS__); }
#else
#define DEBUG(...) /* nothing */
#endif /* #if NEED_CONSOLE_OUTPUT */

//  Object ----------------------------------------------------------------------------------------
DigitalOut  CHG_LED(LED1);
I2C         i2c(I2C_SDA0, I2C_SCL0); // SDA, SCL
LIS3DH      acc(i2c,LIS3DH_G_CHIP_ADDR,LIS3DH_DR_NR_LP_50HZ, LIS3DH_FS_2G);

//  RAM -------------------------------------------------------------------------------------------
EddystoneService *eddyServicePtr;

//  ROM / Constant data ---------------------------------------------------------------------------

//  Function prototypes ---------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
//  Control Program
//-------------------------------------------------------------------------------------------------
#if (NOT_USE_ACC == 0)
int8_t check_dice(void){
    float fa[3];    // Acc  0:X, 1:Y, 2:Z

    acc.read_data(fa);
    DEBUG("acc:%4.3f\r\n", fa[0]);
    if (fa[0] > 6.0f){      return 2;}
    if (fa[0] < -6.0f){     return 5;}
    if (fa[1] > 6.0f){      return 4;}
    if (fa[1] < -6.0f){     return 3;}
    if (fa[2] > 6.0f){      return 1;}
    if (fa[2] < -6.0f){     return 6;}
    return 0;
}
#else
int8_t check_dice(void){
    return 1;
}
#endif  // (NOT_USE_ACC == 0)

void onBleInitError(BLE::InitializationCompleteCallbackContext* initContext){
    // Initialization error handling goes here...
    (void) initContext;
}

void bleInitComplete(BLE::InitializationCompleteCallbackContext* initContext){
    BLE         &ble  = initContext->ble;
    ble_error_t error = initContext->error;

    if (error != BLE_ERROR_NONE) {
        onBleInitError(initContext);
        return;
    }
    // Set UID and TLM frame data
    const UIDNamespaceID_t uidNamespaceID =
             {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
    const UIDInstanceID_t  uidInstanceID  =
             {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
    uint8_t tlmVersion = 0x00;

    // Initialize a EddystoneBeaconConfig service
    // Values for ADV packets related to firmware levels, calibrated based on measured values at 1m
//    static const PowerLevels_t defaultAdvPowerLevels = {-47, -33, -21, -13};
    static const PowerLevels_t defaultAdvPowerLevels = {-87, -73, -61, -53};
    // Values for radio power levels, provided by manufacturer.
//    static const PowerLevels_t radioPowerLevels      = {-30, -16, -4, 4};
    static const PowerLevels_t radioPowerLevels      = {-60, -46, -34, -26};   
    eddyServicePtr = new EddystoneService(ble, defaultAdvPowerLevels, radioPowerLevels);
    // created short cut web addres by http://bitly.oshiire.org/

    switch (check_dice()){
        case 1:
            eddyServicePtr->setURLData("http://bit.ly/1oJh91B");    // Switch sience(mbed)
            break;
        case 2:
            eddyServicePtr->setURLData("http://bit.ly/1oJhP7g");    // switch sience(HP)
            break;
        case 3:
            eddyServicePtr->setURLData("http://bit.ly/1VvuCVr");    // Taiyo Yuden BLE
            break;
        case 4:
            eddyServicePtr->setURLData("http://bit.ly/1Vvtp0l");    // Taiyo Yuden
            break;
        case 5:
            eddyServicePtr->setURLData("http://bit.ly/1Vvt51J");    // JH1PJL(mbed)
            break;
        case 6:
            eddyServicePtr->setURLData("http://bit.ly/1VvteT0");    // JH1PJL(HP)
            break;
        case 0:
        default:
            eddyServicePtr->setURLData("http://mbed.org");
            break;
    }
    eddyServicePtr->setUIDData(&uidNamespaceID, &uidInstanceID);
    eddyServicePtr->setTLMData(tlmVersion);
    eddyServicePtr->startBeaconService(5, 5, 5);    // Start Eddystone in config mode
}

#if (CHK_ACC == 0)
int main(void){
    uint8_t old_dice;

    CHG_LED = 1;
    BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
    ble.init(bleInitComplete);
    while (ble.hasInitialized() == false){;}
    old_dice = check_dice();    // set initial value
    wait(0.5);
    CHG_LED = 0;
    while (true) {
        ble.waitForEvent();
        if (old_dice != check_dice()){
            SCB->AIRCR = 0x05fa0004;    // System RESET!![
            // Not come here (Just in case)
            deepsleep();
            while(true){ wait(100);}
        } 
    }
}
#else
#if (NOY_USE_ACC == 1)
#error "Please set CHK_ACC = 0)
#else
int main(void){
    float fa[3];
    
    while (true) {
        acc.read_data(fa);
        printf("acc:x=%4.2f,y=%4.2f,z=%4.2f\r\n",fa[0],fa[1],fa[2]);
        wait(0.5);
    }
}
#endif  // (NOY_USE_ACC == 1)
#endif  // (CHK_ACC == 0)