ADT7410 sample

Dependencies:   ADT7410 BLE_API mbed nRF51822

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 TICKER_TIME 200000                  //200000[us] = 200[ms]
00044 #define DIGITAL_OUT_PIN P0_9
00045 //#define ANALOG_IN_PIN   P0_4
00046 
00047 
00048 //------------------------------------------------------------
00049 //Object generation
00050 //------------------------------------------------------------ 
00051 BLE ble;
00052 DigitalOut      LED_SET(DIGITAL_OUT_PIN);
00053 //AnalogIn        ANALOG(ANALOG_IN_PIN);
00054 
00055 //I2C Pin setting P0_4=SDA, P0_5=SCL
00056 ADT7410 temp(P0_4, P0_5, 0x90, 10000);
00057 
00058 //------------------------------------------------------------
00059 //Service & Characteristic Setting
00060 //------------------------------------------------------------ 
00061 //Service UUID
00062 static const uint8_t base_uuid[] = { 0x71, 0x3D, 0x00, 0x00, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ;
00063 
00064 //Characteristic UUID
00065 static const uint8_t tx_uuid[]   = { 0x71, 0x3D, 0x00, 0x03, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ;
00066 static const uint8_t rx_uuid[]   = { 0x71, 0x3D, 0x00, 0x02, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ;
00067 
00068 //Characteristic Value
00069 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
00070 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
00071 
00072 //Characteristic Property Setting etc
00073 GattCharacteristic  txCharacteristic (tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00074 GattCharacteristic  rxCharacteristic (rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00075 GattCharacteristic *myChars[] = {&txCharacteristic, &rxCharacteristic};
00076 
00077 //Service Setting
00078 GattService         myService(base_uuid, myChars, sizeof(myChars) / sizeof(GattCharacteristic *));
00079 
00080 
00081 //======================================================================
00082 //onDisconnection
00083 //======================================================================
00084 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00085 {
00086     ble.startAdvertising();
00087 }
00088 
00089 
00090 //======================================================================
00091 //onDataWritten
00092 //======================================================================
00093 void WrittenHandler(const GattWriteCallbackParams *Handler)
00094 {   
00095     uint8_t buf[TXRX_BUF_LEN];
00096     uint16_t bytesRead;
00097     
00098     if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 
00099     {
00100         ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
00101         memset(txPayload, 0, TXRX_BUF_LEN);
00102         memcpy(txPayload, buf, TXRX_BUF_LEN); 
00103        
00104         if(buf[0] == 1)
00105             LED_SET = 1;
00106         else
00107             LED_SET = 0;
00108     }
00109 }
00110 
00111 
00112 //======================================================================
00113 //onTimeout
00114 //======================================================================
00115 
00116 /*
00117 void m_status_check_handle(void)
00118 {   
00119     uint8_t buf[2];
00120     //Read Analog port
00121     float s = ANALOG;
00122     uint16_t value = s*1024; 
00123     buf[0] = (value >> 8);
00124     buf[1] = value;
00125 
00126 }
00127 */
00128 
00129 //======================================================================
00130 //convert reverse UUID
00131 //======================================================================
00132 void reverseUUID(const uint8_t* src, uint8_t* dst)
00133 {
00134     int i;
00135     
00136     for(i=0;i<16;i++)
00137         dst[i] = src[15 - i];
00138 }
00139 
00140 
00141 //======================================================================
00142 //main
00143 //======================================================================
00144 int main(void)
00145 {
00146     uint8_t base_uuid_rev[16];
00147 
00148     //Timer Setting [us]
00149     Ticker ticker;
00150     //ticker.attach_us(m_status_check_handle, TICKER_TIME);
00151     
00152     //BLE init
00153     ble.init();
00154     
00155     //EventListener
00156     ble.onDisconnection(disconnectionCallback);
00157     ble.onDataWritten(WrittenHandler);  
00158 
00159     //------------------------------------------------------------
00160     //setup advertising 
00161     //------------------------------------------------------------
00162     //Classic BT not support
00163     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00164     
00165     //Connectable to Central
00166     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00167     
00168     //Local Name
00169     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
00170                                     (const uint8_t *)DEVICE_LOCAL_NAME, sizeof(DEVICE_LOCAL_NAME) - 1);
00171     
00172     //GAP AdvertisingData                                
00173     reverseUUID(base_uuid, base_uuid_rev);  
00174     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00175                                     (uint8_t *)base_uuid_rev, sizeof(base_uuid));
00176                                     
00177     //Advertising Interval 
00178     ble.setAdvertisingInterval(ADVERTISING_INTERVAL);
00179 
00180     //Add Service
00181     ble.addService(myService);
00182     
00183     //Start Advertising
00184     ble.startAdvertising(); 
00185     
00186     
00187     uint8_t buf[2];
00188     float tempadt;
00189     
00190     // reset sensor to default values
00191     temp.reset();
00192 
00193     // reduce sample rate to save power
00194     temp.setConfig(ONE_SPS_MODE);   
00195 
00196     
00197     //------------------------------------------------------------
00198     //Loop
00199     //------------------------------------------------------------
00200     while(1)
00201     {
00202     
00203         // get temperature every two seconds
00204         tempadt = temp.getTemp();
00205     
00206         int16_t value0 = tempadt;                   //Get integer value
00207         int16_t value1 = (tempadt - value0)*100;    //Get decimal value
00208         buf[0] = value0;
00209         buf[1] = value1;
00210     
00211         //Send out
00212         ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 2);
00213         wait(0.5);
00214         
00215         ble.waitForEvent(); 
00216     }
00217 }