ADXL362 with BLE Nano

Dependencies:   ADXL362_v3 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 "ADXL362.h"
00036  
00037 
00038 //------------------------------------------------------------
00039 //Definition
00040 //------------------------------------------------------------ 
00041 #define TXRX_BUF_LEN 20                     //max 20[byte]
00042 #define DEVICE_LOCAL_NAME "ADXL362"         //Change Name
00043 #define ADVERTISING_INTERVAL 160            //16 * 0.625[ms] = 100[ms]
00044 #define TICKER_TIME 200000                  //200000[us] = 200[ms]
00045 #define DIGITAL_OUT_PIN P0_9
00046 //#define ANALOG_IN_PIN   P0_4
00047 
00048 //Set SPI Pin
00049 #define CS P0_10
00050 #define MOSI P0_9
00051 #define MISO P0_11
00052 #define SCK P0_8
00053 
00054 
00055 //------------------------------------------------------------
00056 //Object generation
00057 //------------------------------------------------------------ 
00058 BLE ble;
00059 DigitalOut      LED_SET(DIGITAL_OUT_PIN);
00060 //AnalogIn        ANALOG(ANALOG_IN_PIN);
00061 
00062 //SPI pin setting and change header file.(see ADXL362.h line185)
00063 ADXL362 adxl362(CS, MOSI, MISO, SCK);
00064 
00065 //------------------------------------------------------------
00066 //Service & Characteristic Setting
00067 //------------------------------------------------------------ 
00068 //Service UUID
00069 static const uint8_t base_uuid[] = { 0x71, 0x3D, 0x00, 0x00, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ;
00070 
00071 //Characteristic UUID
00072 static const uint8_t tx_uuid[]   = { 0x71, 0x3D, 0x00, 0x03, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ;
00073 static const uint8_t rx_uuid[]   = { 0x71, 0x3D, 0x00, 0x02, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ;
00074 
00075 //Characteristic Value
00076 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
00077 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
00078 
00079 //Characteristic Property Setting etc
00080 GattCharacteristic  txCharacteristic (tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00081 GattCharacteristic  rxCharacteristic (rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00082 GattCharacteristic *myChars[] = {&txCharacteristic, &rxCharacteristic};
00083 
00084 //Service Setting
00085 GattService         myService(base_uuid, myChars, sizeof(myChars) / sizeof(GattCharacteristic *));
00086 
00087 
00088 //======================================================================
00089 //onDisconnection
00090 //======================================================================
00091 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00092 {
00093     ble.startAdvertising();
00094 }
00095 
00096 
00097 //======================================================================
00098 //onDataWritten
00099 //======================================================================
00100 void WrittenHandler(const GattWriteCallbackParams *Handler)
00101 {   
00102     uint8_t buf[TXRX_BUF_LEN];
00103     uint16_t bytesRead;
00104     
00105     if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 
00106     {
00107         ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
00108         memset(txPayload, 0, TXRX_BUF_LEN);
00109         memcpy(txPayload, buf, TXRX_BUF_LEN); 
00110        
00111         if(buf[0] == 1)
00112             LED_SET = 1;
00113         else
00114             LED_SET = 0;
00115     }
00116 }
00117 
00118 
00119 //======================================================================
00120 //onTimeout
00121 //======================================================================
00122 
00123 
00124 void m_status_check_handle(void)
00125 {   
00126         //uint8_t x,y,z;
00127         uint8_t x[1];
00128         x[0] = adxl362.scanx_u8();
00129         //y=adxl362.scany_u8();
00130         //z=adxl362.scanz_u8();
00131         //printf("x = %x y = %x z = %x\r\n",x,y,z);
00132         //wait_ms(10);
00133     
00134         //Send out
00135         ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), x, 1); 
00136 }
00137 
00138 
00139 //======================================================================
00140 //convert reverse UUID
00141 //======================================================================
00142 void reverseUUID(const uint8_t* src, uint8_t* dst)
00143 {
00144     int i;
00145     
00146     for(i=0;i<16;i++)
00147         dst[i] = src[15 - i];
00148 }
00149 
00150 
00151 //======================================================================
00152 //main
00153 //======================================================================
00154 int main(void)
00155 {
00156     //ADXL362 Reset
00157     adxl362.reset();
00158     wait_ms(600);      // we need to wait at least 500ms after ADXL362 reset
00159     adxl362.set_mode(ADXL362::MEASUREMENT);
00160    
00161     uint8_t base_uuid_rev[16];
00162 
00163     //Timer Setting [us]
00164     Ticker ticker;
00165     ticker.attach_us(m_status_check_handle, TICKER_TIME);
00166     
00167     //BLE init
00168     ble.init();
00169     
00170     //EventListener
00171     ble.onDisconnection(disconnectionCallback);
00172     ble.onDataWritten(WrittenHandler);  
00173 
00174     //------------------------------------------------------------
00175     //setup advertising 
00176     //------------------------------------------------------------
00177     //Classic BT not support
00178     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00179     
00180     //Connectable to Central
00181     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00182     
00183     //Local Name
00184     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
00185                                     (const uint8_t *)DEVICE_LOCAL_NAME, sizeof(DEVICE_LOCAL_NAME) - 1);
00186     
00187     //GAP AdvertisingData                                
00188     reverseUUID(base_uuid, base_uuid_rev);  
00189     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00190                                     (uint8_t *)base_uuid_rev, sizeof(base_uuid));
00191                                     
00192     //Advertising Interval 
00193     ble.setAdvertisingInterval(ADVERTISING_INTERVAL);
00194 
00195     //Add Service
00196     ble.addService(myService);
00197     
00198     //Start Advertising
00199     ble.startAdvertising(); 
00200     
00201     //------------------------------------------------------------
00202     //Loop
00203     //------------------------------------------------------------
00204     while(1)
00205     {
00206         ble.waitForEvent(); 
00207     }
00208 }