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

Revision:
35:c64495aac4d1
Parent:
34:f6d4a699a1ea
Child:
36:5508506dda71
--- a/main.cpp	Thu Nov 26 16:36:04 2015 +0000
+++ b/main.cpp	Wed Feb 17 22:39:00 2016 +0000
@@ -14,38 +14,101 @@
  * 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/BLE.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;
 
-void onBleInitError(BLE::InitializationCompleteCallbackContext* initContext)
-{
-    /* Initialization error handling goes here... */
+//  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;
 }
 
-/*
- * Update TLM frame battery voltage value.
- */
-uint16_t tlmBatteryVoltageCallback(uint16_t prevBatteryVoltage)
-{
-    prevBatteryVoltage++;
-    return prevBatteryVoltage;
-}
-
-/*
- * Update TLM frame beacon temperature value.
- */
-uint16_t tlmBeaconTemperatureCallback(uint16_t prevBeaconTemperature)
-{
-    prevBeaconTemperature++;
-    return prevBeaconTemperature;
-}
-
-void bleInitComplete(BLE::InitializationCompleteCallbackContext* initContext)
-{
+void bleInitComplete(BLE::InitializationCompleteCallbackContext* initContext){
     BLE         &ble  = initContext->ble;
     ble_error_t error = initContext->error;
 
@@ -53,42 +116,85 @@
         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};
+    // 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 providing config params, default URI, and power levels. */
-    static const PowerLevels_t defaultAdvPowerLevels = {-47, -33, -21, -13}; // Values for ADV packets related to firmware levels, calibrated based on measured values at 1m
-    static const PowerLevels_t radioPowerLevels      = {-30, -16, -4, 4};    // Values for radio power levels, provided by manufacturer.
+    // 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/
 
-    /* Set everything to defaults */
-    eddyServicePtr = new EddystoneService(ble, defaultAdvPowerLevels, radioPowerLevels);
-
-    /* Set default URL, UID and TLM frame data if not initialized through the config service */
-    eddyServicePtr->setURLData("http://mbed.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);
-
-    /* Set battery voltage and temperature callbacks */
-    eddyServicePtr->onTLMBatteryVoltageUpdate(tlmBatteryVoltageCallback);
-    eddyServicePtr->onTLMBeaconTemperatureUpdate(tlmBeaconTemperatureCallback);
-
-    /* Start Eddystone in config mode */
-    eddyServicePtr->startBeaconService(5, 5, 5);
+    eddyServicePtr->startBeaconService(5, 5, 5);    // Start Eddystone in config mode
 }
 
-int main(void)
-{
+#if (CHK_ACC == 0)
+int main(void){
+    uint8_t old_dice;
+
+    CHG_LED = 1;
     BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
     ble.init(bleInitComplete);
-
-    /* SpinWait for initialization to complete. This is necessary because the
-     * BLE object is used in the main loop below. */
-    while (ble.hasInitialized()  == false) { /* spin loop */ }
+    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) {
-        ble.waitForEvent(); /* this will return upon any system event (such as an interrupt or a ticker wakeup) */
+        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)