Jun ADI / Mbed OS BLEnanov2_adt7410_180831

Dependencies:   ADT7410

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 
00003 Copyright (c) 2012-2014 RedBearLab
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00006 and associated documentation files (the "Software"), to deal in the Software without restriction, 
00007 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
00008 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
00009 subject to the following conditions:
00010 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00011 
00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
00013 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
00014 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
00015 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
00016 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 
00018 */
00019 
00020 /*
00021  *    The application works with the BlueJelly.js
00022  *
00023  *    http://jellyware.jp/ 
00024  *
00025  */
00026 
00027 //======================================================================
00028 //Grobal
00029 //====================================================================== 
00030 //------------------------------------------------------------
00031 //Include Header Files
00032 //------------------------------------------------------------ 
00033 #include "mbed.h"
00034 #include "ble/BLE.h"
00035 #include "ADT7410.h"
00036 
00037 //------------------------------------------------------------
00038 //Definition
00039 //------------------------------------------------------------ 
00040 #define TXRX_BUF_LEN 20                     //max 20[byte]
00041 #define DEVICE_LOCAL_NAME "ADT7410 Temperature"  //Change Device name   
00042 #define ADVERTISING_INTERVAL 160            //160 * 0.625[ms] = 100[ms]
00043 #define DIGITAL_OUT_PIN P0_29
00044 
00045 //------------------------------------------------------------
00046 //Object generation
00047 //------------------------------------------------------------ 
00048 BLE blenano;
00049 DigitalOut      LED_SET(DIGITAL_OUT_PIN);
00050 
00051 //I2C Pin setting P0_4=SDA, P0_5=SCL
00052 ADT7410 temp(P0_5, P0_4, 0x90, 10000);
00053 
00054 
00055 //------------------------------------------------------------
00056 //Service & Characteristic Setting
00057 //------------------------------------------------------------ 
00058 //Service UUID
00059 static const uint8_t base_uuid[] = { 0x71, 0x3D, 0x00, 0x00, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ;
00060 
00061 //Characteristic UUID
00062 static const uint8_t tx_uuid[]   = { 0x71, 0x3D, 0x00, 0x03, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ;
00063 static const uint8_t rx_uuid[]   = { 0x71, 0x3D, 0x00, 0x02, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ;
00064 
00065 //Characteristic Value
00066 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
00067 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
00068 
00069 //Characteristic Property Setting etc
00070 GattCharacteristic  txCharacteristic (tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00071 GattCharacteristic  rxCharacteristic (rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00072 GattCharacteristic *myChars[] = {&txCharacteristic, &rxCharacteristic};
00073 
00074 //Service Setting
00075 GattService         myService(base_uuid, myChars, sizeof(myChars) / sizeof(GattCharacteristic *));
00076 
00077 
00078 //======================================================================
00079 //onDisconnection
00080 //======================================================================
00081 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00082 {
00083     blenano.startAdvertising();
00084 }
00085 
00086 
00087 //======================================================================
00088 //convert reverse UUID
00089 //======================================================================
00090 void reverseUUID(const uint8_t* src, uint8_t* dst)
00091 {
00092     int i;
00093 
00094     for(i=0;i<16;i++)
00095         dst[i] = src[15 - i];
00096 }
00097 
00098 
00099 //======================================================================
00100 //main
00101 //======================================================================
00102 int main(void)
00103 {
00104     uint8_t base_uuid_rev[16];
00105 
00106     //BLE init
00107     blenano.init();
00108 
00109     //EventListener
00110     blenano.onDisconnection(disconnectionCallback);
00111     //blenano.onDataWritten(WrittenHandler);  
00112 
00113     //------------------------------------------------------------
00114     //setup advertising 
00115     //------------------------------------------------------------
00116     //Classic BT not support
00117     blenano.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00118 
00119     //Connectable to Central
00120     blenano.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00121 
00122     //Local Name
00123     blenano.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
00124                                     (const uint8_t *)DEVICE_LOCAL_NAME, sizeof(DEVICE_LOCAL_NAME) - 1);
00125 
00126     //GAP AdvertisingData                                
00127     reverseUUID(base_uuid, base_uuid_rev);  
00128     blenano.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00129                                     (uint8_t *)base_uuid_rev, sizeof(base_uuid));
00130 
00131     //Advertising Interval 
00132     blenano.setAdvertisingInterval(ADVERTISING_INTERVAL);
00133 
00134     //Add Service
00135     blenano.addService(myService);
00136 
00137     //Start Advertising
00138     blenano.startAdvertising(); 
00139 
00140     uint8_t buf[2];
00141     float tempadt;
00142 
00143     // reset sensor to default values
00144     temp.reset();
00145 
00146     // reduce sample rate to save power
00147     temp.setConfig(ONE_SPS_MODE);   
00148 
00149     //------------------------------------------------------------
00150     //Loop
00151     //------------------------------------------------------------
00152     while(1)
00153     {
00154         // get temperature every 10 seconds
00155         tempadt = temp.getTemp();
00156 
00157         int16_t value0 = tempadt;                   //Get integer value
00158         int16_t value1 = (tempadt - value0)*100;    //Get decimal value
00159         buf[0] = value0;
00160         buf[1] = value1;
00161 
00162         //Send out
00163         blenano.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 2);
00164 
00165         blenano.waitForEvent(); 
00166 
00167         wait(1);
00168     }
00169 }