ADT7410 sample
Dependencies: ADT7410 BLE_API mbed nRF51822
Fork of BLENano_SimpleTemplate_temp_170802 by
main.cpp@4:7e5d4a18cd22, 2017-05-20 (annotated)
- Committer:
- electricbaka
- Date:
- Sat May 20 11:54:14 2017 +0000
- Revision:
- 4:7e5d4a18cd22
- Parent:
- 3:f530ca03e014
- Child:
- 5:95238da08cee
????????????
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 | 4:7e5d4a18cd22 | 21 | * The application works with the BlueJelly.js |
electricbaka | 4:7e5d4a18cd22 | 22 | */ |
electricbaka | 4:7e5d4a18cd22 | 23 | |
electricbaka | 4:7e5d4a18cd22 | 24 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 25 | //Grobal |
electricbaka | 4:7e5d4a18cd22 | 26 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 27 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 28 | //Include Header Files |
electricbaka | 4:7e5d4a18cd22 | 29 | //------------------------------------------------------------ |
RedBearLab | 0:be2e4095513a | 30 | #include "mbed.h" |
RedBearLab | 2:3cd654f42efa | 31 | #include "ble/BLE.h" |
RedBearLab | 0:be2e4095513a | 32 | |
RedBearLab | 0:be2e4095513a | 33 | |
electricbaka | 4:7e5d4a18cd22 | 34 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 35 | //Definition |
electricbaka | 4:7e5d4a18cd22 | 36 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 37 | #define TXRX_BUF_LEN 20 //max 20 |
electricbaka | 4:7e5d4a18cd22 | 38 | #define DEVICE_LOCAL_NAME "BlueJelly!!" |
electricbaka | 4:7e5d4a18cd22 | 39 | #define ADVERTISING_INTERVAL 160 //160 * 0.625[ms] = 100[ms] |
electricbaka | 4:7e5d4a18cd22 | 40 | #define TICKER_TIME 200000 //[us] |
electricbaka | 4:7e5d4a18cd22 | 41 | #define ANALOG_IN_PIN P0_4 |
RedBearLab | 0:be2e4095513a | 42 | |
RedBearLab | 0:be2e4095513a | 43 | |
electricbaka | 4:7e5d4a18cd22 | 44 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 45 | //Object generation |
electricbaka | 4:7e5d4a18cd22 | 46 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 47 | BLE ble; |
electricbaka | 4:7e5d4a18cd22 | 48 | AnalogIn ANALOG(ANALOG_IN_PIN); |
electricbaka | 4:7e5d4a18cd22 | 49 | |
electricbaka | 4:7e5d4a18cd22 | 50 | |
electricbaka | 4:7e5d4a18cd22 | 51 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 52 | //Service & Characteristic Setting |
electricbaka | 4:7e5d4a18cd22 | 53 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 54 | //Service UUID |
electricbaka | 4:7e5d4a18cd22 | 55 | 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 | 56 | |
electricbaka | 4:7e5d4a18cd22 | 57 | //Characteristic UUID |
electricbaka | 4:7e5d4a18cd22 | 58 | 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 | 59 | 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 | 60 | |
electricbaka | 4:7e5d4a18cd22 | 61 | //Characteristic Value |
RedBearLab | 0:be2e4095513a | 62 | uint8_t txPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:be2e4095513a | 63 | uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:be2e4095513a | 64 | |
electricbaka | 4:7e5d4a18cd22 | 65 | //Characteristic Property Setting etc |
electricbaka | 4:7e5d4a18cd22 | 66 | GattCharacteristic txCharacteristic (tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
electricbaka | 4:7e5d4a18cd22 | 67 | GattCharacteristic rxCharacteristic (rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
electricbaka | 4:7e5d4a18cd22 | 68 | GattCharacteristic *myChars[] = {&txCharacteristic, &rxCharacteristic}; |
electricbaka | 4:7e5d4a18cd22 | 69 | |
electricbaka | 4:7e5d4a18cd22 | 70 | //Service Setting |
electricbaka | 4:7e5d4a18cd22 | 71 | GattService myService(base_uuid, myChars, sizeof(myChars) / sizeof(GattCharacteristic *)); |
RedBearLab | 0:be2e4095513a | 72 | |
RedBearLab | 0:be2e4095513a | 73 | |
electricbaka | 4:7e5d4a18cd22 | 74 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 75 | //onDisconnection |
electricbaka | 4:7e5d4a18cd22 | 76 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 77 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
electricbaka | 4:7e5d4a18cd22 | 78 | { |
electricbaka | 4:7e5d4a18cd22 | 79 | ble.startAdvertising(); |
electricbaka | 4:7e5d4a18cd22 | 80 | } |
RedBearLab | 0:be2e4095513a | 81 | |
RedBearLab | 0:be2e4095513a | 82 | |
electricbaka | 4:7e5d4a18cd22 | 83 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 84 | //onDataWritten |
electricbaka | 4:7e5d4a18cd22 | 85 | //====================================================================== |
RedBearLab | 2:3cd654f42efa | 86 | void WrittenHandler(const GattWriteCallbackParams *Handler) |
RedBearLab | 0:be2e4095513a | 87 | { |
RedBearLab | 0:be2e4095513a | 88 | uint8_t buf[TXRX_BUF_LEN]; |
RedBearLab | 0:be2e4095513a | 89 | uint16_t bytesRead; |
RedBearLab | 0:be2e4095513a | 90 | |
RedBearLab | 2:3cd654f42efa | 91 | if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) |
RedBearLab | 0:be2e4095513a | 92 | { |
RedBearLab | 0:be2e4095513a | 93 | ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); |
electricbaka | 4:7e5d4a18cd22 | 94 | memset(txPayload, 0, TXRX_BUF_LEN); //todo |
electricbaka | 4:7e5d4a18cd22 | 95 | memcpy(txPayload, buf, TXRX_BUF_LEN); //todo |
RedBearLab | 0:be2e4095513a | 96 | } |
RedBearLab | 0:be2e4095513a | 97 | } |
RedBearLab | 0:be2e4095513a | 98 | |
RedBearLab | 0:be2e4095513a | 99 | |
electricbaka | 4:7e5d4a18cd22 | 100 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 101 | //onTimeout |
electricbaka | 4:7e5d4a18cd22 | 102 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 103 | void m_status_check_handle(void) |
electricbaka | 4:7e5d4a18cd22 | 104 | { |
electricbaka | 4:7e5d4a18cd22 | 105 | uint8_t buf[2]; |
electricbaka | 4:7e5d4a18cd22 | 106 | |
electricbaka | 4:7e5d4a18cd22 | 107 | //Read Analog port |
electricbaka | 4:7e5d4a18cd22 | 108 | float s = ANALOG; |
electricbaka | 4:7e5d4a18cd22 | 109 | uint16_t value = s*1024; |
electricbaka | 4:7e5d4a18cd22 | 110 | buf[0] = (value >> 8); |
electricbaka | 4:7e5d4a18cd22 | 111 | buf[1] = (value); |
electricbaka | 4:7e5d4a18cd22 | 112 | |
electricbaka | 4:7e5d4a18cd22 | 113 | //Send out |
electricbaka | 4:7e5d4a18cd22 | 114 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 2); |
electricbaka | 4:7e5d4a18cd22 | 115 | } |
electricbaka | 4:7e5d4a18cd22 | 116 | |
electricbaka | 4:7e5d4a18cd22 | 117 | |
electricbaka | 4:7e5d4a18cd22 | 118 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 119 | //convert reverse UUID |
electricbaka | 4:7e5d4a18cd22 | 120 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 121 | void reverseUUID(const uint8_t* src, uint8_t* dst) |
electricbaka | 4:7e5d4a18cd22 | 122 | { |
electricbaka | 4:7e5d4a18cd22 | 123 | int i; |
electricbaka | 4:7e5d4a18cd22 | 124 | |
electricbaka | 4:7e5d4a18cd22 | 125 | for(i=0;i<16;i++) |
electricbaka | 4:7e5d4a18cd22 | 126 | dst[i] = src[15 - i]; |
electricbaka | 4:7e5d4a18cd22 | 127 | } |
electricbaka | 4:7e5d4a18cd22 | 128 | |
electricbaka | 4:7e5d4a18cd22 | 129 | |
electricbaka | 4:7e5d4a18cd22 | 130 | //====================================================================== |
electricbaka | 4:7e5d4a18cd22 | 131 | //main |
electricbaka | 4:7e5d4a18cd22 | 132 | //====================================================================== |
RedBearLab | 0:be2e4095513a | 133 | int main(void) |
electricbaka | 4:7e5d4a18cd22 | 134 | { |
electricbaka | 4:7e5d4a18cd22 | 135 | uint8_t base_uuid_rev[16]; |
electricbaka | 4:7e5d4a18cd22 | 136 | |
electricbaka | 4:7e5d4a18cd22 | 137 | //Timer Setting [us] |
RedBearLab | 0:be2e4095513a | 138 | Ticker ticker; |
electricbaka | 4:7e5d4a18cd22 | 139 | ticker.attach_us(m_status_check_handle, TICKER_TIME); |
RedBearLab | 0:be2e4095513a | 140 | |
electricbaka | 4:7e5d4a18cd22 | 141 | //BLE init |
RedBearLab | 0:be2e4095513a | 142 | ble.init(); |
electricbaka | 4:7e5d4a18cd22 | 143 | |
electricbaka | 4:7e5d4a18cd22 | 144 | //EventListener |
RedBearLab | 0:be2e4095513a | 145 | ble.onDisconnection(disconnectionCallback); |
RedBearLab | 0:be2e4095513a | 146 | ble.onDataWritten(WrittenHandler); |
electricbaka | 4:7e5d4a18cd22 | 147 | |
electricbaka | 4:7e5d4a18cd22 | 148 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 149 | //setup advertising |
electricbaka | 4:7e5d4a18cd22 | 150 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 151 | //Classic BT not support |
electricbaka | 4:7e5d4a18cd22 | 152 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
RedBearLab | 0:be2e4095513a | 153 | |
electricbaka | 4:7e5d4a18cd22 | 154 | //Connectable to Central |
RedBearLab | 0:be2e4095513a | 155 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
electricbaka | 4:7e5d4a18cd22 | 156 | |
electricbaka | 4:7e5d4a18cd22 | 157 | //Local Name |
electricbaka | 4:7e5d4a18cd22 | 158 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, |
electricbaka | 4:7e5d4a18cd22 | 159 | (const uint8_t *)DEVICE_LOCAL_NAME, sizeof(DEVICE_LOCAL_NAME) - 1); |
electricbaka | 4:7e5d4a18cd22 | 160 | |
electricbaka | 4:7e5d4a18cd22 | 161 | //GAP AdvertisingData |
electricbaka | 4:7e5d4a18cd22 | 162 | reverseUUID(base_uuid, base_uuid_rev); |
RedBearLab | 0:be2e4095513a | 163 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
electricbaka | 4:7e5d4a18cd22 | 164 | (uint8_t *)base_uuid_rev, sizeof(base_uuid)); |
electricbaka | 4:7e5d4a18cd22 | 165 | |
electricbaka | 4:7e5d4a18cd22 | 166 | //Advertising Interval |
electricbaka | 4:7e5d4a18cd22 | 167 | ble.setAdvertisingInterval(ADVERTISING_INTERVAL); |
RedBearLab | 0:be2e4095513a | 168 | |
electricbaka | 4:7e5d4a18cd22 | 169 | //Add Service |
electricbaka | 4:7e5d4a18cd22 | 170 | ble.addService(myService); |
RedBearLab | 0:be2e4095513a | 171 | |
electricbaka | 4:7e5d4a18cd22 | 172 | //Start Advertising |
RedBearLab | 0:be2e4095513a | 173 | ble.startAdvertising(); |
RedBearLab | 0:be2e4095513a | 174 | |
electricbaka | 4:7e5d4a18cd22 | 175 | //------------------------------------------------------------ |
electricbaka | 4:7e5d4a18cd22 | 176 | //Loop |
electricbaka | 4:7e5d4a18cd22 | 177 | //------------------------------------------------------------ |
RedBearLab | 0:be2e4095513a | 178 | while(1) |
RedBearLab | 0:be2e4095513a | 179 | { |
RedBearLab | 0:be2e4095513a | 180 | ble.waitForEvent(); |
RedBearLab | 0:be2e4095513a | 181 | } |
electricbaka | 4:7e5d4a18cd22 | 182 | } |