Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ADXL362_v3 BLE_API mbed nRF51822
Fork of BLENano_SimpleTemplate_adxl362_170813 by
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 #include "ble/services/UARTService.h" 00038 00039 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; 00040 * it will have an impact on code-size and power consumption. */ 00041 00042 #if NEED_CONSOLE_OUTPUT 00043 #define DEBUG(...) { printf(__VA_ARGS__); } 00044 #else 00045 #define DEBUG(...) /* nothing */ 00046 #endif /* #if NEED_CONSOLE_OUTPUT */ 00047 00048 //------------------------------------------------------------ 00049 //Definition 00050 //------------------------------------------------------------ 00051 #define TXRX_BUF_LEN 20 //max 20[byte] 00052 #define DEVICE_LOCAL_NAME "ADXL362" //Change Name 00053 #define ADVERTISING_INTERVAL 160 //16 * 0.625[ms] = 100[ms] 00054 #define TICKER_TIME 200000 //200000[us] = 200[ms] 00055 #define DIGITAL_OUT_PIN P0_9 00056 //#define ANALOG_IN_PIN P0_4 00057 00058 //Set SPI Pin 00059 #define CS P0_8 00060 #define MOSI P0_5 00061 #define MISO P0_6 00062 #define SCK P0_7 00063 00064 00065 //------------------------------------------------------------ 00066 //Object generation 00067 //------------------------------------------------------------ 00068 BLE ble; 00069 DigitalOut LED_SET(DIGITAL_OUT_PIN); 00070 //AnalogIn ANALOG(ANALOG_IN_PIN); 00071 UARTService *uartServicePtr; 00072 00073 //SPI pin setting and change header file.(see ADXL362.h line185) 00074 ADXL362 adxl362(CS, MOSI, MISO, SCK); 00075 00076 //------------------------------------------------------------ 00077 //Service & Characteristic Setting 00078 //------------------------------------------------------------ 00079 //Service UUID 00080 static const uint8_t base_uuid[] = { 0x71, 0x3D, 0x00, 0x00, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ; 00081 00082 //Characteristic UUID 00083 static const uint8_t tx_uuid[] = { 0x71, 0x3D, 0x00, 0x03, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ; 00084 static const uint8_t rx_uuid[] = { 0x71, 0x3D, 0x00, 0x02, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ; 00085 00086 //Characteristic Value 00087 uint8_t txPayload[TXRX_BUF_LEN] = {0,}; 00088 uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; 00089 00090 //Characteristic Property Setting etc 00091 GattCharacteristic txCharacteristic (tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); 00092 GattCharacteristic rxCharacteristic (rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); 00093 GattCharacteristic *myChars[] = {&txCharacteristic, &rxCharacteristic}; 00094 00095 //Service Setting 00096 GattService myService(base_uuid, myChars, sizeof(myChars) / sizeof(GattCharacteristic *)); 00097 00098 00099 //====================================================================== 00100 //onDisconnection 00101 //====================================================================== 00102 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00103 { 00104 ble.startAdvertising(); 00105 } 00106 00107 void onDataWritten(const GattWriteCallbackParams *params) 00108 { 00109 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { 00110 uint16_t bytesRead = params->len; 00111 DEBUG("received %u bytes\n\r", bytesRead); 00112 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); 00113 } 00114 } 00115 00116 00117 //====================================================================== 00118 //onDataWritten 00119 //====================================================================== 00120 void WrittenHandler(const GattWriteCallbackParams *Handler) 00121 { 00122 uint8_t buf[TXRX_BUF_LEN]; 00123 uint16_t bytesRead; 00124 00125 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 00126 { 00127 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); 00128 memset(txPayload, 0, TXRX_BUF_LEN); 00129 memcpy(txPayload, buf, TXRX_BUF_LEN); 00130 00131 if(buf[0] == 1) 00132 LED_SET = 1; 00133 else 00134 LED_SET = 0; 00135 } 00136 } 00137 00138 00139 //====================================================================== 00140 //onTimeout 00141 //====================================================================== 00142 00143 00144 void m_status_check_handle(void) 00145 { 00146 //uint8_t x,y,z; 00147 uint8_t x[1]; 00148 x[0] = adxl362.scanx_u8(); 00149 //y=adxl362.scany_u8(); 00150 //z=adxl362.scanz_u8(); 00151 //printf("x = %x y = %x z = %x\r\n",x,y,z); 00152 //wait_ms(10); 00153 00154 //Send out 00155 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), x, 1); 00156 } 00157 00158 00159 //====================================================================== 00160 //convert reverse UUID 00161 //====================================================================== 00162 void reverseUUID(const uint8_t* src, uint8_t* dst) 00163 { 00164 int i; 00165 00166 for(i=0;i<16;i++) 00167 dst[i] = src[15 - i]; 00168 } 00169 00170 00171 //====================================================================== 00172 //main 00173 //====================================================================== 00174 int main(void) 00175 { 00176 //ADXL362 Reset 00177 adxl362.reset(); 00178 wait_ms(600); // we need to wait at least 500ms after ADXL362 reset 00179 adxl362.set_mode(ADXL362::MEASUREMENT); 00180 00181 uint8_t base_uuid_rev[16]; 00182 00183 //Timer Setting [us] 00184 Ticker ticker; 00185 ticker.attach_us(m_status_check_handle, TICKER_TIME); 00186 00187 00188 //BLE init 00189 DEBUG("Initialising the nRF51822\n\r"); 00190 ble.init(); 00191 00192 //EventListener 00193 ble.onDisconnection(disconnectionCallback); 00194 ble.onDataWritten(WrittenHandler); 00195 00196 //------------------------------------------------------------ 00197 //setup advertising 00198 //------------------------------------------------------------ 00199 //Classic BT not support 00200 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00201 00202 //Connectable to Central 00203 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00204 00205 //Local Name 00206 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, 00207 (const uint8_t *)DEVICE_LOCAL_NAME, sizeof(DEVICE_LOCAL_NAME) - 1); 00208 00209 //GAP AdvertisingData 00210 reverseUUID(base_uuid, base_uuid_rev); 00211 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00212 (uint8_t *)base_uuid_rev, sizeof(base_uuid)); 00213 00214 //Advertising Interval 00215 ble.setAdvertisingInterval(ADVERTISING_INTERVAL); 00216 00217 //Add Service 00218 ble.addService(myService); 00219 00220 //Start Advertising 00221 ble.startAdvertising(); 00222 00223 UARTService uartService(ble); 00224 uartServicePtr = &uartService; 00225 00226 //------------------------------------------------------------ 00227 //Loop 00228 //------------------------------------------------------------ 00229 while(1) 00230 { 00231 ble.waitForEvent(); 00232 } 00233 }
Generated on Sun Jul 17 2022 13:35:05 by
1.7.2
