Knight Rider PIO sample for teckBASIC, konashi.js
Dependencies: BLE_API_Native_IRC mbed
Fork of BLE_konashi_PIO_test by
konashi.js mbed HRM1017でもナイトライダー!: http://jsdo.it/micutil/g1Hn
サンプル動画: https://www.youtube.com/watch?v=HSLdzS3sGLw
techBASICサンプル: https://www.dropbox.com/s/c1k25jlen7x3vqo/KnightRider.txt
BLE_API_Native_IRC/hw/nRF51822n/nRF51GattServer.cpp@2:6a3257fffa8c, 2014-07-27 (annotated)
- Committer:
- micono
- Date:
- Sun Jul 27 00:02:25 2014 +0000
- Revision:
- 2:6a3257fffa8c
- Parent:
- 0:8c643bfe55b7
PIO sample for teckBASIC, konashi.js
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:8c643bfe55b7 | 1 | /* mbed Microcontroller Library |
jksoft | 0:8c643bfe55b7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
jksoft | 0:8c643bfe55b7 | 3 | * |
jksoft | 0:8c643bfe55b7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
jksoft | 0:8c643bfe55b7 | 5 | * you may not use this file except in compliance with the License. |
jksoft | 0:8c643bfe55b7 | 6 | * You may obtain a copy of the License at |
jksoft | 0:8c643bfe55b7 | 7 | * |
jksoft | 0:8c643bfe55b7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
jksoft | 0:8c643bfe55b7 | 9 | * |
jksoft | 0:8c643bfe55b7 | 10 | * Unless required by applicable law or agreed to in writing, software |
jksoft | 0:8c643bfe55b7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
jksoft | 0:8c643bfe55b7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
jksoft | 0:8c643bfe55b7 | 13 | * See the License for the specific language governing permissions and |
jksoft | 0:8c643bfe55b7 | 14 | * limitations under the License. |
jksoft | 0:8c643bfe55b7 | 15 | */ |
jksoft | 0:8c643bfe55b7 | 16 | |
jksoft | 0:8c643bfe55b7 | 17 | #include "nRF51GattServer.h" |
jksoft | 0:8c643bfe55b7 | 18 | #include "mbed.h" |
jksoft | 0:8c643bfe55b7 | 19 | |
jksoft | 0:8c643bfe55b7 | 20 | #include "common/common.h" |
jksoft | 0:8c643bfe55b7 | 21 | #include "btle/custom/custom_helper.h" |
jksoft | 0:8c643bfe55b7 | 22 | |
jksoft | 0:8c643bfe55b7 | 23 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 24 | /*! |
jksoft | 0:8c643bfe55b7 | 25 | @brief Adds a new service to the GATT table on the peripheral |
jksoft | 0:8c643bfe55b7 | 26 | |
jksoft | 0:8c643bfe55b7 | 27 | @returns ble_error_t |
jksoft | 0:8c643bfe55b7 | 28 | |
jksoft | 0:8c643bfe55b7 | 29 | @retval BLE_ERROR_NONE |
jksoft | 0:8c643bfe55b7 | 30 | Everything executed properly |
jksoft | 0:8c643bfe55b7 | 31 | |
jksoft | 0:8c643bfe55b7 | 32 | @section EXAMPLE |
jksoft | 0:8c643bfe55b7 | 33 | |
jksoft | 0:8c643bfe55b7 | 34 | @code |
jksoft | 0:8c643bfe55b7 | 35 | |
jksoft | 0:8c643bfe55b7 | 36 | @endcode |
jksoft | 0:8c643bfe55b7 | 37 | */ |
jksoft | 0:8c643bfe55b7 | 38 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 39 | ble_error_t nRF51GattServer::addService(GattService & service) |
jksoft | 0:8c643bfe55b7 | 40 | { |
jksoft | 0:8c643bfe55b7 | 41 | /* ToDo: Make sure we don't overflow the array, etc. */ |
jksoft | 0:8c643bfe55b7 | 42 | /* ToDo: Make sure this service UUID doesn't already exist (?) */ |
jksoft | 0:8c643bfe55b7 | 43 | /* ToDo: Basic validation */ |
jksoft | 0:8c643bfe55b7 | 44 | |
jksoft | 0:8c643bfe55b7 | 45 | /* Add the service to the nRF51 */ |
jksoft | 0:8c643bfe55b7 | 46 | ble_uuid_t uuid; |
jksoft | 0:8c643bfe55b7 | 47 | |
jksoft | 0:8c643bfe55b7 | 48 | if (service.primaryServiceID.type == UUID::UUID_TYPE_SHORT) |
jksoft | 0:8c643bfe55b7 | 49 | { |
jksoft | 0:8c643bfe55b7 | 50 | /* 16-bit BLE UUID */ |
jksoft | 0:8c643bfe55b7 | 51 | uuid.type = BLE_UUID_TYPE_BLE; |
jksoft | 0:8c643bfe55b7 | 52 | } |
jksoft | 0:8c643bfe55b7 | 53 | else |
jksoft | 0:8c643bfe55b7 | 54 | { |
jksoft | 0:8c643bfe55b7 | 55 | /* 128-bit Custom UUID */ |
jksoft | 0:8c643bfe55b7 | 56 | uuid.type = custom_add_uuid_base( service.primaryServiceID.base ); |
jksoft | 0:8c643bfe55b7 | 57 | } |
jksoft | 0:8c643bfe55b7 | 58 | |
jksoft | 0:8c643bfe55b7 | 59 | uuid.uuid = service.primaryServiceID.value; |
jksoft | 0:8c643bfe55b7 | 60 | |
jksoft | 0:8c643bfe55b7 | 61 | ASSERT( ERROR_NONE == sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &uuid, &service.handle), BLE_ERROR_PARAM_OUT_OF_RANGE ); |
jksoft | 0:8c643bfe55b7 | 62 | |
jksoft | 0:8c643bfe55b7 | 63 | /* Add characteristics to the service */ |
jksoft | 0:8c643bfe55b7 | 64 | for (uint8_t i = 0; i < service.characteristicCount; i++) |
jksoft | 0:8c643bfe55b7 | 65 | { |
jksoft | 0:8c643bfe55b7 | 66 | GattCharacteristic * p_char = service.characteristics[i]; |
jksoft | 0:8c643bfe55b7 | 67 | |
jksoft | 0:8c643bfe55b7 | 68 | uuid.uuid = p_char->uuid; |
jksoft | 0:8c643bfe55b7 | 69 | ASSERT ( ERROR_NONE == custom_add_in_characteristic(service.handle, &uuid, p_char->properties, |
jksoft | 0:8c643bfe55b7 | 70 | NULL, p_char->lenMin, p_char->lenMax, &nrfCharacteristicHandles[characteristicCount]), BLE_ERROR_PARAM_OUT_OF_RANGE ); |
jksoft | 0:8c643bfe55b7 | 71 | |
jksoft | 0:8c643bfe55b7 | 72 | /* Update the characteristic handle */ |
jksoft | 0:8c643bfe55b7 | 73 | p_char->handle = characteristicCount; |
jksoft | 0:8c643bfe55b7 | 74 | p_characteristics[characteristicCount++] = p_char; |
jksoft | 0:8c643bfe55b7 | 75 | } |
jksoft | 0:8c643bfe55b7 | 76 | |
jksoft | 0:8c643bfe55b7 | 77 | serviceCount++; |
jksoft | 0:8c643bfe55b7 | 78 | |
jksoft | 0:8c643bfe55b7 | 79 | return BLE_ERROR_NONE; |
jksoft | 0:8c643bfe55b7 | 80 | } |
jksoft | 0:8c643bfe55b7 | 81 | |
jksoft | 0:8c643bfe55b7 | 82 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 83 | /*! |
jksoft | 0:8c643bfe55b7 | 84 | @brief Reads the value of a characteristic, based on the service |
jksoft | 0:8c643bfe55b7 | 85 | and characteristic index fields |
jksoft | 0:8c643bfe55b7 | 86 | |
jksoft | 0:8c643bfe55b7 | 87 | @param[in] charHandle |
jksoft | 0:8c643bfe55b7 | 88 | The handle of the GattCharacteristic to read from |
jksoft | 0:8c643bfe55b7 | 89 | @param[in] buffer |
jksoft | 0:8c643bfe55b7 | 90 | Buffer to hold the the characteristic's value |
jksoft | 0:8c643bfe55b7 | 91 | (raw byte array in LSB format) |
jksoft | 0:8c643bfe55b7 | 92 | @param[in] len |
jksoft | 0:8c643bfe55b7 | 93 | The number of bytes read into the buffer |
jksoft | 0:8c643bfe55b7 | 94 | |
jksoft | 0:8c643bfe55b7 | 95 | @returns ble_error_t |
jksoft | 0:8c643bfe55b7 | 96 | |
jksoft | 0:8c643bfe55b7 | 97 | @retval BLE_ERROR_NONE |
jksoft | 0:8c643bfe55b7 | 98 | Everything executed properly |
jksoft | 0:8c643bfe55b7 | 99 | |
jksoft | 0:8c643bfe55b7 | 100 | @section EXAMPLE |
jksoft | 0:8c643bfe55b7 | 101 | |
jksoft | 0:8c643bfe55b7 | 102 | @code |
jksoft | 0:8c643bfe55b7 | 103 | |
jksoft | 0:8c643bfe55b7 | 104 | @endcode |
jksoft | 0:8c643bfe55b7 | 105 | */ |
jksoft | 0:8c643bfe55b7 | 106 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 107 | ble_error_t nRF51GattServer::readValue(uint16_t charHandle, uint8_t buffer[], uint16_t len) |
jksoft | 0:8c643bfe55b7 | 108 | { |
jksoft | 0:8c643bfe55b7 | 109 | ASSERT( ERROR_NONE == sd_ble_gatts_value_get(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE); |
jksoft | 0:8c643bfe55b7 | 110 | |
jksoft | 0:8c643bfe55b7 | 111 | return BLE_ERROR_NONE; |
jksoft | 0:8c643bfe55b7 | 112 | } |
jksoft | 0:8c643bfe55b7 | 113 | |
jksoft | 0:8c643bfe55b7 | 114 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 115 | /*! |
jksoft | 0:8c643bfe55b7 | 116 | @brief Updates the value of a characteristic, based on the service |
jksoft | 0:8c643bfe55b7 | 117 | and characteristic index fields |
jksoft | 0:8c643bfe55b7 | 118 | |
jksoft | 0:8c643bfe55b7 | 119 | @param[in] charHandle |
jksoft | 0:8c643bfe55b7 | 120 | The handle of the GattCharacteristic to write to |
jksoft | 0:8c643bfe55b7 | 121 | @param[in] buffer |
jksoft | 0:8c643bfe55b7 | 122 | Data to use when updating the characteristic's value |
jksoft | 0:8c643bfe55b7 | 123 | (raw byte array in LSB format) |
jksoft | 0:8c643bfe55b7 | 124 | @param[in] len |
jksoft | 0:8c643bfe55b7 | 125 | The number of bytes in buffer |
jksoft | 0:8c643bfe55b7 | 126 | |
jksoft | 0:8c643bfe55b7 | 127 | @returns ble_error_t |
jksoft | 0:8c643bfe55b7 | 128 | |
jksoft | 0:8c643bfe55b7 | 129 | @retval BLE_ERROR_NONE |
jksoft | 0:8c643bfe55b7 | 130 | Everything executed properly |
jksoft | 0:8c643bfe55b7 | 131 | |
jksoft | 0:8c643bfe55b7 | 132 | @section EXAMPLE |
jksoft | 0:8c643bfe55b7 | 133 | |
jksoft | 0:8c643bfe55b7 | 134 | @code |
jksoft | 0:8c643bfe55b7 | 135 | |
jksoft | 0:8c643bfe55b7 | 136 | @endcode |
jksoft | 0:8c643bfe55b7 | 137 | */ |
jksoft | 0:8c643bfe55b7 | 138 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 139 | ble_error_t nRF51GattServer::updateValue(uint16_t charHandle, uint8_t buffer[], uint16_t len) |
jksoft | 0:8c643bfe55b7 | 140 | { |
jksoft | 0:8c643bfe55b7 | 141 | if ((p_characteristics[charHandle]->properties & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && |
jksoft | 0:8c643bfe55b7 | 142 | (m_connectionHandle != BLE_CONN_HANDLE_INVALID) ) |
jksoft | 0:8c643bfe55b7 | 143 | { |
jksoft | 0:8c643bfe55b7 | 144 | /* HVX update for the characteristic value */ |
jksoft | 0:8c643bfe55b7 | 145 | ble_gatts_hvx_params_t hvx_params; |
jksoft | 0:8c643bfe55b7 | 146 | |
jksoft | 0:8c643bfe55b7 | 147 | hvx_params.handle = nrfCharacteristicHandles[charHandle].value_handle; |
jksoft | 0:8c643bfe55b7 | 148 | hvx_params.type = (p_characteristics[charHandle]->properties & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ? BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION; |
jksoft | 0:8c643bfe55b7 | 149 | hvx_params.offset = 0; |
jksoft | 0:8c643bfe55b7 | 150 | hvx_params.p_data = buffer; |
jksoft | 0:8c643bfe55b7 | 151 | hvx_params.p_len = &len; |
jksoft | 0:8c643bfe55b7 | 152 | |
jksoft | 0:8c643bfe55b7 | 153 | error_t error = (error_t) sd_ble_gatts_hvx(m_connectionHandle, &hvx_params); |
jksoft | 0:8c643bfe55b7 | 154 | |
jksoft | 0:8c643bfe55b7 | 155 | /* ERROR_INVALID_STATE, ERROR_BUSY, ERROR_GATTS_SYS_ATTR_MISSING and ERROR_NO_TX_BUFFERS the ATT table has been updated. */ |
jksoft | 0:8c643bfe55b7 | 156 | if ( (error != ERROR_NONE ) && (error != ERROR_INVALID_STATE) && |
jksoft | 0:8c643bfe55b7 | 157 | (error != ERROR_BLE_NO_TX_BUFFERS ) && (error != ERROR_BUSY ) && |
jksoft | 0:8c643bfe55b7 | 158 | (error != ERROR_BLEGATTS_SYS_ATTR_MISSING ) ) |
jksoft | 0:8c643bfe55b7 | 159 | { |
jksoft | 0:8c643bfe55b7 | 160 | ASSERT_INT( ERROR_NONE, sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE ); |
jksoft | 0:8c643bfe55b7 | 161 | } |
jksoft | 0:8c643bfe55b7 | 162 | } else |
jksoft | 0:8c643bfe55b7 | 163 | { |
jksoft | 0:8c643bfe55b7 | 164 | ASSERT_INT( ERROR_NONE, sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE ); |
jksoft | 0:8c643bfe55b7 | 165 | } |
jksoft | 0:8c643bfe55b7 | 166 | |
jksoft | 0:8c643bfe55b7 | 167 | return BLE_ERROR_NONE; |
jksoft | 0:8c643bfe55b7 | 168 | } |
jksoft | 0:8c643bfe55b7 | 169 | |
jksoft | 0:8c643bfe55b7 | 170 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 171 | /*! |
jksoft | 0:8c643bfe55b7 | 172 | @brief Callback handler for events getting pushed up from the SD |
jksoft | 0:8c643bfe55b7 | 173 | */ |
jksoft | 0:8c643bfe55b7 | 174 | /**************************************************************************/ |
jksoft | 0:8c643bfe55b7 | 175 | void nRF51GattServer::hwCallback(ble_evt_t * p_ble_evt) |
jksoft | 0:8c643bfe55b7 | 176 | { |
jksoft | 0:8c643bfe55b7 | 177 | uint16_t handle_value; |
jksoft | 0:8c643bfe55b7 | 178 | GattServerEvents::gattEvent_t event; |
jksoft | 0:8c643bfe55b7 | 179 | |
jksoft | 0:8c643bfe55b7 | 180 | switch (p_ble_evt->header.evt_id) |
jksoft | 0:8c643bfe55b7 | 181 | { |
jksoft | 0:8c643bfe55b7 | 182 | case BLE_GATTS_EVT_WRITE: |
jksoft | 0:8c643bfe55b7 | 183 | /* There are 2 use case here: Values being updated & CCCD (indicate/notify) enabled */ |
jksoft | 0:8c643bfe55b7 | 184 | |
jksoft | 0:8c643bfe55b7 | 185 | /* 1.) Handle CCCD changes */ |
jksoft | 0:8c643bfe55b7 | 186 | handle_value = p_ble_evt->evt.gatts_evt.params.write.handle; |
jksoft | 0:8c643bfe55b7 | 187 | for(uint8_t i=0; i<characteristicCount; i++) |
jksoft | 0:8c643bfe55b7 | 188 | { |
jksoft | 0:8c643bfe55b7 | 189 | if ( (p_characteristics[i]->properties & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && |
jksoft | 0:8c643bfe55b7 | 190 | (nrfCharacteristicHandles[i].cccd_handle == handle_value) ) |
jksoft | 0:8c643bfe55b7 | 191 | { |
jksoft | 0:8c643bfe55b7 | 192 | uint16_t cccd_value = (p_ble_evt->evt.gatts_evt.params.write.data[1] << 8) | p_ble_evt->evt.gatts_evt.params.write.data[0]; /* Little Endian but M0 may be mis-aligned */ |
jksoft | 0:8c643bfe55b7 | 193 | |
jksoft | 0:8c643bfe55b7 | 194 | if ( ((p_characteristics[i]->properties & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && (cccd_value & BLE_GATT_HVX_INDICATION )) || |
jksoft | 0:8c643bfe55b7 | 195 | ((p_characteristics[i]->properties & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY ) && (cccd_value & BLE_GATT_HVX_NOTIFICATION))) |
jksoft | 0:8c643bfe55b7 | 196 | { |
jksoft | 0:8c643bfe55b7 | 197 | event = GattServerEvents::GATT_EVENT_UPDATES_ENABLED; |
jksoft | 0:8c643bfe55b7 | 198 | } else |
jksoft | 0:8c643bfe55b7 | 199 | { |
jksoft | 0:8c643bfe55b7 | 200 | event = GattServerEvents::GATT_EVENT_UPDATES_DISABLED; |
jksoft | 0:8c643bfe55b7 | 201 | } |
jksoft | 0:8c643bfe55b7 | 202 | |
jksoft | 0:8c643bfe55b7 | 203 | handleEvent(event, i); |
jksoft | 0:8c643bfe55b7 | 204 | return; |
jksoft | 0:8c643bfe55b7 | 205 | } |
jksoft | 0:8c643bfe55b7 | 206 | } |
jksoft | 0:8c643bfe55b7 | 207 | |
jksoft | 0:8c643bfe55b7 | 208 | /* 2.) Changes to the characteristic value will be handled with other events below */ |
jksoft | 0:8c643bfe55b7 | 209 | event = GattServerEvents::GATT_EVENT_DATA_WRITTEN; |
jksoft | 0:8c643bfe55b7 | 210 | break; |
jksoft | 0:8c643bfe55b7 | 211 | |
jksoft | 0:8c643bfe55b7 | 212 | case BLE_GATTS_EVT_HVC: |
jksoft | 0:8c643bfe55b7 | 213 | /* Indication confirmation received */ |
jksoft | 0:8c643bfe55b7 | 214 | event = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED; |
jksoft | 0:8c643bfe55b7 | 215 | handle_value = p_ble_evt->evt.gatts_evt.params.hvc.handle; |
jksoft | 0:8c643bfe55b7 | 216 | break; |
jksoft | 0:8c643bfe55b7 | 217 | |
jksoft | 0:8c643bfe55b7 | 218 | default: |
jksoft | 0:8c643bfe55b7 | 219 | return; |
jksoft | 0:8c643bfe55b7 | 220 | } |
jksoft | 0:8c643bfe55b7 | 221 | |
jksoft | 0:8c643bfe55b7 | 222 | /* Find index (charHandle) in the pool */ |
jksoft | 0:8c643bfe55b7 | 223 | for(uint8_t i=0; i<characteristicCount; i++) |
jksoft | 0:8c643bfe55b7 | 224 | { |
jksoft | 0:8c643bfe55b7 | 225 | if (nrfCharacteristicHandles[i].value_handle == handle_value) |
jksoft | 0:8c643bfe55b7 | 226 | { |
jksoft | 0:8c643bfe55b7 | 227 | handleEvent(event, i); |
jksoft | 0:8c643bfe55b7 | 228 | break; |
jksoft | 0:8c643bfe55b7 | 229 | } |
jksoft | 0:8c643bfe55b7 | 230 | } |
jksoft | 0:8c643bfe55b7 | 231 | } |