this code needed help please
Dependencies: ADXL362_v3 BLE_API mbed nRF51822
Fork of BLENano_SimpleTemplate_adxl362_170813 by
main.cpp@10:c90237505549, 2017-10-19 (annotated)
- Committer:
- asyrofi
- Date:
- Thu Oct 19 11:59:32 2017 +0000
- Revision:
- 10:c90237505549
- Parent:
- 9:ca9a58478ddd
still confuse
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RedBearLab | 1:81a97eb70d3d | 1 | /* |
RedBearLab | 1:81a97eb70d3d | 2 | |
RedBearLab | 1:81a97eb70d3d | 3 | Copyright (c) 2012-2014 RedBearLab |
RedBearLab | 1:81a97eb70d3d | 4 | |
RedBearLab | 1:81a97eb70d3d | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
RedBearLab | 1:81a97eb70d3d | 6 | and associated documentation files (the "Software"), to deal in the Software without restriction, |
RedBearLab | 1:81a97eb70d3d | 7 | including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
RedBearLab | 1:81a97eb70d3d | 8 | and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
RedBearLab | 1:81a97eb70d3d | 9 | subject to the following conditions: |
RedBearLab | 1:81a97eb70d3d | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
RedBearLab | 1:81a97eb70d3d | 11 | |
RedBearLab | 1:81a97eb70d3d | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
RedBearLab | 1:81a97eb70d3d | 13 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
RedBearLab | 1:81a97eb70d3d | 14 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
RedBearLab | 1:81a97eb70d3d | 15 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
RedBearLab | 1:81a97eb70d3d | 16 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
RedBearLab | 1:81a97eb70d3d | 17 | |
RedBearLab | 1:81a97eb70d3d | 18 | */ |
RedBearLab | 0:be2e4095513a | 19 | |
electricbaka | 4:7e5d4a18cd22 | 20 | /* |
electricbaka | 5:95238da08cee | 21 | * The application works with the BlueJelly.js |
electricbaka | 5:95238da08cee | 22 | * |
electricbaka | 5:95238da08cee | 23 | * http://jellyware.jp/ |
electricbaka | 5:95238da08cee | 24 | * |
electricbaka | 4:7e5d4a18cd22 | 25 | */ |
electricbaka | 4:7e5d4a18cd22 | 26 | |
electricbaka | 4:7e5d4a18cd22 | 27 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 28 | //Grobal |
electricbaka | 4:7e5d4a18cd22 | 29 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 30 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 31 | //Include Header Files |
electricbaka | 4:7e5d4a18cd22 | 32 | //------------------------------------------------------------ |
RedBearLab | 0:be2e4095513a | 33 | #include "mbed.h" |
RedBearLab | 2:3cd654f42efa | 34 | #include "ble/BLE.h" |
Jun_adi | 6:29ffd540b089 | 35 | #include "ADXL362.h" |
Jun_adi | 6:29ffd540b089 | 36 | |
asyrofi | 10:c90237505549 | 37 | #include "ble/services/UARTService.h" |
asyrofi | 10:c90237505549 | 38 | |
asyrofi | 10:c90237505549 | 39 | #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; |
asyrofi | 10:c90237505549 | 40 | * it will have an impact on code-size and power consumption. */ |
asyrofi | 10:c90237505549 | 41 | |
asyrofi | 10:c90237505549 | 42 | #if NEED_CONSOLE_OUTPUT |
asyrofi | 10:c90237505549 | 43 | #define DEBUG(...) { printf(__VA_ARGS__); } |
asyrofi | 10:c90237505549 | 44 | #else |
asyrofi | 10:c90237505549 | 45 | #define DEBUG(...) /* nothing */ |
asyrofi | 10:c90237505549 | 46 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
RedBearLab | 0:be2e4095513a | 47 | |
electricbaka | 4:7e5d4a18cd22 | 48 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 49 | //Definition |
electricbaka | 4:7e5d4a18cd22 | 50 | //------------------------------------------------------------ |
electricbaka | 5:95238da08cee | 51 | #define TXRX_BUF_LEN 20 //max 20[byte] |
Jun_adi | 6:29ffd540b089 | 52 | #define DEVICE_LOCAL_NAME "ADXL362" //Change Name |
Jun_adi | 7:2d9b5491b2d3 | 53 | #define ADVERTISING_INTERVAL 160 //16 * 0.625[ms] = 100[ms] |
Jun_adi | 7:2d9b5491b2d3 | 54 | #define TICKER_TIME 200000 //200000[us] = 200[ms] |
electricbaka | 5:95238da08cee | 55 | #define DIGITAL_OUT_PIN P0_9 |
Jun_adi | 6:29ffd540b089 | 56 | //#define ANALOG_IN_PIN P0_4 |
Jun_adi | 6:29ffd540b089 | 57 | |
Jun_adi | 7:2d9b5491b2d3 | 58 | //Set SPI Pin |
asyrofi | 10:c90237505549 | 59 | #define CS P0_8 |
asyrofi | 10:c90237505549 | 60 | #define MOSI P0_5 |
asyrofi | 10:c90237505549 | 61 | #define MISO P0_6 |
asyrofi | 10:c90237505549 | 62 | #define SCK P0_7 |
RedBearLab | 0:be2e4095513a | 63 | |
RedBearLab | 0:be2e4095513a | 64 | |
electricbaka | 4:7e5d4a18cd22 | 65 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 66 | //Object generation |
electricbaka | 4:7e5d4a18cd22 | 67 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 68 | BLE ble; |
electricbaka | 5:95238da08cee | 69 | DigitalOut LED_SET(DIGITAL_OUT_PIN); |
Jun_adi | 6:29ffd540b089 | 70 | //AnalogIn ANALOG(ANALOG_IN_PIN); |
asyrofi | 10:c90237505549 | 71 | UARTService *uartServicePtr; |
electricbaka | 4:7e5d4a18cd22 | 72 | |
Jun_adi | 7:2d9b5491b2d3 | 73 | //SPI pin setting and change header file.(see ADXL362.h line185) |
Jun_adi | 6:29ffd540b089 | 74 | ADXL362 adxl362(CS, MOSI, MISO, SCK); |
electricbaka | 4:7e5d4a18cd22 | 75 | |
electricbaka | 4:7e5d4a18cd22 | 76 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 77 | //Service & Characteristic Setting |
electricbaka | 4:7e5d4a18cd22 | 78 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 79 | //Service UUID |
electricbaka | 4:7e5d4a18cd22 | 80 | static const uint8_t base_uuid[] = { 0x71, 0x3D, 0x00, 0x00, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ; |
electricbaka | 4:7e5d4a18cd22 | 81 | |
electricbaka | 4:7e5d4a18cd22 | 82 | //Characteristic UUID |
electricbaka | 4:7e5d4a18cd22 | 83 | static const uint8_t tx_uuid[] = { 0x71, 0x3D, 0x00, 0x03, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ; |
electricbaka | 4:7e5d4a18cd22 | 84 | static const uint8_t rx_uuid[] = { 0x71, 0x3D, 0x00, 0x02, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ; |
electricbaka | 4:7e5d4a18cd22 | 85 | |
electricbaka | 4:7e5d4a18cd22 | 86 | //Characteristic Value |
RedBearLab | 0:be2e4095513a | 87 | uint8_t txPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:be2e4095513a | 88 | uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:be2e4095513a | 89 | |
electricbaka | 4:7e5d4a18cd22 | 90 | //Characteristic Property Setting etc |
electricbaka | 5:95238da08cee | 91 | GattCharacteristic txCharacteristic (tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
electricbaka | 4:7e5d4a18cd22 | 92 | GattCharacteristic rxCharacteristic (rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
electricbaka | 4:7e5d4a18cd22 | 93 | GattCharacteristic *myChars[] = {&txCharacteristic, &rxCharacteristic}; |
electricbaka | 4:7e5d4a18cd22 | 94 | |
electricbaka | 4:7e5d4a18cd22 | 95 | //Service Setting |
electricbaka | 4:7e5d4a18cd22 | 96 | GattService myService(base_uuid, myChars, sizeof(myChars) / sizeof(GattCharacteristic *)); |
RedBearLab | 0:be2e4095513a | 97 | |
RedBearLab | 0:be2e4095513a | 98 | |
electricbaka | 4:7e5d4a18cd22 | 99 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 100 | //onDisconnection |
electricbaka | 4:7e5d4a18cd22 | 101 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 102 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
electricbaka | 4:7e5d4a18cd22 | 103 | { |
electricbaka | 4:7e5d4a18cd22 | 104 | ble.startAdvertising(); |
electricbaka | 4:7e5d4a18cd22 | 105 | } |
RedBearLab | 0:be2e4095513a | 106 | |
asyrofi | 10:c90237505549 | 107 | void onDataWritten(const GattWriteCallbackParams *params) |
asyrofi | 10:c90237505549 | 108 | { |
asyrofi | 10:c90237505549 | 109 | if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { |
asyrofi | 10:c90237505549 | 110 | uint16_t bytesRead = params->len; |
asyrofi | 10:c90237505549 | 111 | DEBUG("received %u bytes\n\r", bytesRead); |
asyrofi | 10:c90237505549 | 112 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); |
asyrofi | 10:c90237505549 | 113 | } |
asyrofi | 10:c90237505549 | 114 | } |
asyrofi | 10:c90237505549 | 115 | |
RedBearLab | 0:be2e4095513a | 116 | |
electricbaka | 4:7e5d4a18cd22 | 117 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 118 | //onDataWritten |
electricbaka | 4:7e5d4a18cd22 | 119 | //====================================================================== |
RedBearLab | 2:3cd654f42efa | 120 | void WrittenHandler(const GattWriteCallbackParams *Handler) |
RedBearLab | 0:be2e4095513a | 121 | { |
RedBearLab | 0:be2e4095513a | 122 | uint8_t buf[TXRX_BUF_LEN]; |
RedBearLab | 0:be2e4095513a | 123 | uint16_t bytesRead; |
RedBearLab | 0:be2e4095513a | 124 | |
RedBearLab | 2:3cd654f42efa | 125 | if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) |
RedBearLab | 0:be2e4095513a | 126 | { |
RedBearLab | 0:be2e4095513a | 127 | ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); |
electricbaka | 5:95238da08cee | 128 | memset(txPayload, 0, TXRX_BUF_LEN); |
electricbaka | 5:95238da08cee | 129 | memcpy(txPayload, buf, TXRX_BUF_LEN); |
electricbaka | 5:95238da08cee | 130 | |
electricbaka | 5:95238da08cee | 131 | if(buf[0] == 1) |
electricbaka | 5:95238da08cee | 132 | LED_SET = 1; |
electricbaka | 5:95238da08cee | 133 | else |
electricbaka | 5:95238da08cee | 134 | LED_SET = 0; |
RedBearLab | 0:be2e4095513a | 135 | } |
RedBearLab | 0:be2e4095513a | 136 | } |
RedBearLab | 0:be2e4095513a | 137 | |
RedBearLab | 0:be2e4095513a | 138 | |
electricbaka | 4:7e5d4a18cd22 | 139 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 140 | //onTimeout |
electricbaka | 4:7e5d4a18cd22 | 141 | //====================================================================== |
Jun_adi | 6:29ffd540b089 | 142 | |
Jun_adi | 9:ca9a58478ddd | 143 | |
electricbaka | 4:7e5d4a18cd22 | 144 | void m_status_check_handle(void) |
electricbaka | 4:7e5d4a18cd22 | 145 | { |
Jun_adi | 9:ca9a58478ddd | 146 | //uint8_t x,y,z; |
Jun_adi | 9:ca9a58478ddd | 147 | uint8_t x[1]; |
Jun_adi | 9:ca9a58478ddd | 148 | x[0] = adxl362.scanx_u8(); |
Jun_adi | 9:ca9a58478ddd | 149 | //y=adxl362.scany_u8(); |
Jun_adi | 9:ca9a58478ddd | 150 | //z=adxl362.scanz_u8(); |
Jun_adi | 9:ca9a58478ddd | 151 | //printf("x = %x y = %x z = %x\r\n",x,y,z); |
Jun_adi | 9:ca9a58478ddd | 152 | //wait_ms(10); |
Jun_adi | 9:ca9a58478ddd | 153 | |
Jun_adi | 9:ca9a58478ddd | 154 | //Send out |
Jun_adi | 9:ca9a58478ddd | 155 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), x, 1); |
Jun_adi | 9:ca9a58478ddd | 156 | } |
electricbaka | 4:7e5d4a18cd22 | 157 | |
electricbaka | 4:7e5d4a18cd22 | 158 | |
electricbaka | 4:7e5d4a18cd22 | 159 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 160 | //convert reverse UUID |
electricbaka | 4:7e5d4a18cd22 | 161 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 162 | void reverseUUID(const uint8_t* src, uint8_t* dst) |
electricbaka | 4:7e5d4a18cd22 | 163 | { |
electricbaka | 4:7e5d4a18cd22 | 164 | int i; |
electricbaka | 4:7e5d4a18cd22 | 165 | |
electricbaka | 4:7e5d4a18cd22 | 166 | for(i=0;i<16;i++) |
electricbaka | 4:7e5d4a18cd22 | 167 | dst[i] = src[15 - i]; |
electricbaka | 4:7e5d4a18cd22 | 168 | } |
electricbaka | 4:7e5d4a18cd22 | 169 | |
electricbaka | 4:7e5d4a18cd22 | 170 | |
electricbaka | 4:7e5d4a18cd22 | 171 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 172 | //main |
electricbaka | 4:7e5d4a18cd22 | 173 | //====================================================================== |
RedBearLab | 0:be2e4095513a | 174 | int main(void) |
electricbaka | 4:7e5d4a18cd22 | 175 | { |
Jun_adi | 6:29ffd540b089 | 176 | //ADXL362 Reset |
Jun_adi | 6:29ffd540b089 | 177 | adxl362.reset(); |
Jun_adi | 6:29ffd540b089 | 178 | wait_ms(600); // we need to wait at least 500ms after ADXL362 reset |
Jun_adi | 6:29ffd540b089 | 179 | adxl362.set_mode(ADXL362::MEASUREMENT); |
Jun_adi | 9:ca9a58478ddd | 180 | |
electricbaka | 4:7e5d4a18cd22 | 181 | uint8_t base_uuid_rev[16]; |
electricbaka | 4:7e5d4a18cd22 | 182 | |
electricbaka | 4:7e5d4a18cd22 | 183 | //Timer Setting [us] |
RedBearLab | 0:be2e4095513a | 184 | Ticker ticker; |
Jun_adi | 9:ca9a58478ddd | 185 | ticker.attach_us(m_status_check_handle, TICKER_TIME); |
RedBearLab | 0:be2e4095513a | 186 | |
asyrofi | 10:c90237505549 | 187 | |
electricbaka | 4:7e5d4a18cd22 | 188 | //BLE init |
asyrofi | 10:c90237505549 | 189 | DEBUG("Initialising the nRF51822\n\r"); |
RedBearLab | 0:be2e4095513a | 190 | ble.init(); |
electricbaka | 4:7e5d4a18cd22 | 191 | |
electricbaka | 4:7e5d4a18cd22 | 192 | //EventListener |
RedBearLab | 0:be2e4095513a | 193 | ble.onDisconnection(disconnectionCallback); |
RedBearLab | 0:be2e4095513a | 194 | ble.onDataWritten(WrittenHandler); |
electricbaka | 4:7e5d4a18cd22 | 195 | |
electricbaka | 4:7e5d4a18cd22 | 196 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 197 | //setup advertising |
electricbaka | 4:7e5d4a18cd22 | 198 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 199 | //Classic BT not support |
electricbaka | 4:7e5d4a18cd22 | 200 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
RedBearLab | 0:be2e4095513a | 201 | |
electricbaka | 4:7e5d4a18cd22 | 202 | //Connectable to Central |
RedBearLab | 0:be2e4095513a | 203 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
electricbaka | 4:7e5d4a18cd22 | 204 | |
electricbaka | 4:7e5d4a18cd22 | 205 | //Local Name |
electricbaka | 4:7e5d4a18cd22 | 206 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, |
electricbaka | 4:7e5d4a18cd22 | 207 | (const uint8_t *)DEVICE_LOCAL_NAME, sizeof(DEVICE_LOCAL_NAME) - 1); |
electricbaka | 4:7e5d4a18cd22 | 208 | |
electricbaka | 4:7e5d4a18cd22 | 209 | //GAP AdvertisingData |
electricbaka | 4:7e5d4a18cd22 | 210 | reverseUUID(base_uuid, base_uuid_rev); |
RedBearLab | 0:be2e4095513a | 211 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
electricbaka | 4:7e5d4a18cd22 | 212 | (uint8_t *)base_uuid_rev, sizeof(base_uuid)); |
electricbaka | 4:7e5d4a18cd22 | 213 | |
electricbaka | 4:7e5d4a18cd22 | 214 | //Advertising Interval |
electricbaka | 4:7e5d4a18cd22 | 215 | ble.setAdvertisingInterval(ADVERTISING_INTERVAL); |
RedBearLab | 0:be2e4095513a | 216 | |
electricbaka | 4:7e5d4a18cd22 | 217 | //Add Service |
electricbaka | 4:7e5d4a18cd22 | 218 | ble.addService(myService); |
RedBearLab | 0:be2e4095513a | 219 | |
electricbaka | 4:7e5d4a18cd22 | 220 | //Start Advertising |
RedBearLab | 0:be2e4095513a | 221 | ble.startAdvertising(); |
RedBearLab | 0:be2e4095513a | 222 | |
asyrofi | 10:c90237505549 | 223 | UARTService uartService(ble); |
asyrofi | 10:c90237505549 | 224 | uartServicePtr = &uartService; |
asyrofi | 10:c90237505549 | 225 | |
electricbaka | 4:7e5d4a18cd22 | 226 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 227 | //Loop |
electricbaka | 4:7e5d4a18cd22 | 228 | //------------------------------------------------------------ |
RedBearLab | 0:be2e4095513a | 229 | while(1) |
RedBearLab | 0:be2e4095513a | 230 | { |
RedBearLab | 0:be2e4095513a | 231 | ble.waitForEvent(); |
RedBearLab | 0:be2e4095513a | 232 | } |
electricbaka | 4:7e5d4a18cd22 | 233 | } |