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