BLE Simple Template
Dependencies: BLE_API mbed nRF51822
Fork of BLENano_SimpleControls 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 00036 00037 //------------------------------------------------------------ 00038 //Definition 00039 //------------------------------------------------------------ 00040 #define TXRX_BUF_LEN 20 //max 20[byte] 00041 #define DEVICE_LOCAL_NAME "BlueJelly" 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 00056 //------------------------------------------------------------ 00057 //Service & Characteristic Setting 00058 //------------------------------------------------------------ 00059 //Service UUID 00060 static const uint8_t base_uuid[] = { 0x71, 0x3D, 0x00, 0x00, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ; 00061 00062 //Characteristic UUID 00063 static const uint8_t tx_uuid[] = { 0x71, 0x3D, 0x00, 0x03, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ; 00064 static const uint8_t rx_uuid[] = { 0x71, 0x3D, 0x00, 0x02, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ; 00065 00066 //Characteristic Value 00067 uint8_t txPayload[TXRX_BUF_LEN] = {0,}; 00068 uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; 00069 00070 //Characteristic Property Setting etc 00071 GattCharacteristic txCharacteristic (tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); 00072 GattCharacteristic rxCharacteristic (rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); 00073 GattCharacteristic *myChars[] = {&txCharacteristic, &rxCharacteristic}; 00074 00075 //Service Setting 00076 GattService myService(base_uuid, myChars, sizeof(myChars) / sizeof(GattCharacteristic *)); 00077 00078 00079 //====================================================================== 00080 //onDisconnection 00081 //====================================================================== 00082 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00083 { 00084 ble.startAdvertising(); 00085 } 00086 00087 00088 //====================================================================== 00089 //onDataWritten 00090 //====================================================================== 00091 void WrittenHandler(const GattWriteCallbackParams *Handler) 00092 { 00093 uint8_t buf[TXRX_BUF_LEN]; 00094 uint16_t bytesRead; 00095 00096 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 00097 { 00098 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); 00099 memset(txPayload, 0, TXRX_BUF_LEN); 00100 memcpy(txPayload, buf, TXRX_BUF_LEN); 00101 00102 if(buf[0] == 1) 00103 LED_SET = 1; 00104 else 00105 LED_SET = 0; 00106 } 00107 } 00108 00109 00110 //====================================================================== 00111 //onTimeout 00112 //====================================================================== 00113 void m_status_check_handle(void) 00114 { 00115 uint8_t buf[2]; 00116 00117 //Read Analog port 00118 float s = ANALOG; 00119 uint16_t value = s*1024; 00120 buf[0] = (value >> 8); 00121 buf[1] = value; 00122 00123 //Send out 00124 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 2); 00125 } 00126 00127 00128 //====================================================================== 00129 //convert reverse UUID 00130 //====================================================================== 00131 void reverseUUID(const uint8_t* src, uint8_t* dst) 00132 { 00133 int i; 00134 00135 for(i=0;i<16;i++) 00136 dst[i] = src[15 - i]; 00137 } 00138 00139 00140 //====================================================================== 00141 //main 00142 //====================================================================== 00143 int main(void) 00144 { 00145 uint8_t base_uuid_rev[16]; 00146 00147 //Timer Setting [us] 00148 Ticker ticker; 00149 ticker.attach_us(m_status_check_handle, TICKER_TIME); 00150 00151 //BLE init 00152 ble.init(); 00153 00154 //EventListener 00155 ble.onDisconnection(disconnectionCallback); 00156 ble.onDataWritten(WrittenHandler); 00157 00158 //------------------------------------------------------------ 00159 //setup advertising 00160 //------------------------------------------------------------ 00161 //Classic BT not support 00162 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00163 00164 //Connectable to Central 00165 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00166 00167 //Local Name 00168 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, 00169 (const uint8_t *)DEVICE_LOCAL_NAME, sizeof(DEVICE_LOCAL_NAME) - 1); 00170 00171 //GAP AdvertisingData 00172 reverseUUID(base_uuid, base_uuid_rev); 00173 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00174 (uint8_t *)base_uuid_rev, sizeof(base_uuid)); 00175 00176 //Advertising Interval 00177 ble.setAdvertisingInterval(ADVERTISING_INTERVAL); 00178 00179 //Add Service 00180 ble.addService(myService); 00181 00182 //Start Advertising 00183 ble.startAdvertising(); 00184 00185 //------------------------------------------------------------ 00186 //Loop 00187 //------------------------------------------------------------ 00188 while(1) 00189 { 00190 ble.waitForEvent(); 00191 } 00192 }
Generated on Sun Jul 17 2022 03:02:37 by 1.7.2