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