iOSのBLEコントローラアプリ「RCBController」とmbed HRM1017を接続し、RCサーボモータを操作するテストプログラムです。

Dependencies:   BLE_API_Native_IRC Servo mbed

Fork of BLE_RCBController by Junichi Katsu

  • 古いBLEライブラリを使っているのでプラットフォームは”Nordic nRF51822”を選択してください。
  • ライブラリ類はUpdateしないでください。コンパイルエラーになります。

うまく接続できない時は、iPhone/iPadのBluetoothをOFF->ONしてキャッシュをクリアしてみてください。

/media/uploads/robo8080/img_1560.jpg

Committer:
jksoft
Date:
Thu Jul 10 14:21:52 2014 +0000
Revision:
0:8c643bfe55b7
??

Who changed what in which revision?

UserRevisionLine numberNew 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 "nRF51Gap.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 "ble_advdata.h"
jksoft 0:8c643bfe55b7 22
jksoft 0:8c643bfe55b7 23 /**************************************************************************/
jksoft 0:8c643bfe55b7 24 /*!
jksoft 0:8c643bfe55b7 25 @brief Sets the advertising parameters and payload for the device
jksoft 0:8c643bfe55b7 26
jksoft 0:8c643bfe55b7 27 @param[in] params
jksoft 0:8c643bfe55b7 28 Basic advertising details, including the advertising
jksoft 0:8c643bfe55b7 29 delay, timeout and how the device should be advertised
jksoft 0:8c643bfe55b7 30 @params[in] advData
jksoft 0:8c643bfe55b7 31 The primary advertising data payload
jksoft 0:8c643bfe55b7 32 @params[in] scanResponse
jksoft 0:8c643bfe55b7 33 The optional Scan Response payload if the advertising
jksoft 0:8c643bfe55b7 34 type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED
jksoft 0:8c643bfe55b7 35 in \ref GapAdveritinngParams
jksoft 0:8c643bfe55b7 36
jksoft 0:8c643bfe55b7 37 @returns \ref ble_error_t
jksoft 0:8c643bfe55b7 38
jksoft 0:8c643bfe55b7 39 @retval BLE_ERROR_NONE
jksoft 0:8c643bfe55b7 40 Everything executed properly
jksoft 0:8c643bfe55b7 41
jksoft 0:8c643bfe55b7 42 @retval BLE_ERROR_BUFFER_OVERFLOW
jksoft 0:8c643bfe55b7 43 The proposed action would cause a buffer overflow. All
jksoft 0:8c643bfe55b7 44 advertising payloads must be <= 31 bytes, for example.
jksoft 0:8c643bfe55b7 45
jksoft 0:8c643bfe55b7 46 @retval BLE_ERROR_NOT_IMPLEMENTED
jksoft 0:8c643bfe55b7 47 A feature was requested that is not yet supported in the
jksoft 0:8c643bfe55b7 48 nRF51 firmware or hardware.
jksoft 0:8c643bfe55b7 49
jksoft 0:8c643bfe55b7 50 @retval BLE_ERROR_PARAM_OUT_OF_RANGE
jksoft 0:8c643bfe55b7 51 One of the proposed values is outside the valid range.
jksoft 0:8c643bfe55b7 52
jksoft 0:8c643bfe55b7 53 @section EXAMPLE
jksoft 0:8c643bfe55b7 54
jksoft 0:8c643bfe55b7 55 @code
jksoft 0:8c643bfe55b7 56
jksoft 0:8c643bfe55b7 57 @endcode
jksoft 0:8c643bfe55b7 58 */
jksoft 0:8c643bfe55b7 59 /**************************************************************************/
jksoft 0:8c643bfe55b7 60 ble_error_t nRF51Gap::setAdvertisingData(GapAdvertisingData & advData, GapAdvertisingData & scanResponse)
jksoft 0:8c643bfe55b7 61 {
jksoft 0:8c643bfe55b7 62 /* Make sure we don't exceed the advertising payload length */
jksoft 0:8c643bfe55b7 63 if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
jksoft 0:8c643bfe55b7 64 {
jksoft 0:8c643bfe55b7 65 return BLE_ERROR_BUFFER_OVERFLOW;
jksoft 0:8c643bfe55b7 66 }
jksoft 0:8c643bfe55b7 67
jksoft 0:8c643bfe55b7 68 /* Make sure we have a payload! */
jksoft 0:8c643bfe55b7 69 if (advData.getPayloadLen() == 0)
jksoft 0:8c643bfe55b7 70 {
jksoft 0:8c643bfe55b7 71 return BLE_ERROR_PARAM_OUT_OF_RANGE;
jksoft 0:8c643bfe55b7 72 }
jksoft 0:8c643bfe55b7 73
jksoft 0:8c643bfe55b7 74 /* Check the scan response payload limits */
jksoft 0:8c643bfe55b7 75 //if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED))
jksoft 0:8c643bfe55b7 76 //{
jksoft 0:8c643bfe55b7 77 // /* Check if we're within the upper limit */
jksoft 0:8c643bfe55b7 78 // if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
jksoft 0:8c643bfe55b7 79 // {
jksoft 0:8c643bfe55b7 80 // return BLE_ERROR_BUFFER_OVERFLOW;
jksoft 0:8c643bfe55b7 81 // }
jksoft 0:8c643bfe55b7 82 // /* Make sure we have a payload! */
jksoft 0:8c643bfe55b7 83 // if (advData.getPayloadLen() == 0)
jksoft 0:8c643bfe55b7 84 // {
jksoft 0:8c643bfe55b7 85 // return BLE_ERROR_PARAM_OUT_OF_RANGE;
jksoft 0:8c643bfe55b7 86 // }
jksoft 0:8c643bfe55b7 87 //}
jksoft 0:8c643bfe55b7 88
jksoft 0:8c643bfe55b7 89 /* Send advertising data! */
jksoft 0:8c643bfe55b7 90 ASSERT( ERROR_NONE == sd_ble_gap_adv_data_set(advData.getPayload(), advData.getPayloadLen(),
jksoft 0:8c643bfe55b7 91 scanResponse.getPayload(), scanResponse.getPayloadLen()), BLE_ERROR_PARAM_OUT_OF_RANGE);
jksoft 0:8c643bfe55b7 92
jksoft 0:8c643bfe55b7 93 /* Make sure the GAP Service appearance value is aligned with the appearance from GapAdvertisingData */
jksoft 0:8c643bfe55b7 94 ASSERT( ERROR_NONE == sd_ble_gap_appearance_set(advData.getAppearance()), BLE_ERROR_PARAM_OUT_OF_RANGE);
jksoft 0:8c643bfe55b7 95
jksoft 0:8c643bfe55b7 96 /* ToDo: Perform some checks on the payload, for example the Scan Response can't */
jksoft 0:8c643bfe55b7 97 /* contains a flags AD type, etc. */
jksoft 0:8c643bfe55b7 98
jksoft 0:8c643bfe55b7 99 return BLE_ERROR_NONE;
jksoft 0:8c643bfe55b7 100 }
jksoft 0:8c643bfe55b7 101
jksoft 0:8c643bfe55b7 102 /**************************************************************************/
jksoft 0:8c643bfe55b7 103 /*!
jksoft 0:8c643bfe55b7 104 @brief Starts the BLE HW, initialising any services that were
jksoft 0:8c643bfe55b7 105 added before this function was called.
jksoft 0:8c643bfe55b7 106
jksoft 0:8c643bfe55b7 107 @note All services must be added before calling this function!
jksoft 0:8c643bfe55b7 108
jksoft 0:8c643bfe55b7 109 @returns ble_error_t
jksoft 0:8c643bfe55b7 110
jksoft 0:8c643bfe55b7 111 @retval BLE_ERROR_NONE
jksoft 0:8c643bfe55b7 112 Everything executed properly
jksoft 0:8c643bfe55b7 113
jksoft 0:8c643bfe55b7 114 @section EXAMPLE
jksoft 0:8c643bfe55b7 115
jksoft 0:8c643bfe55b7 116 @code
jksoft 0:8c643bfe55b7 117
jksoft 0:8c643bfe55b7 118 @endcode
jksoft 0:8c643bfe55b7 119 */
jksoft 0:8c643bfe55b7 120 /**************************************************************************/
jksoft 0:8c643bfe55b7 121 ble_error_t nRF51Gap::startAdvertising(GapAdvertisingParams & params)
jksoft 0:8c643bfe55b7 122 {
jksoft 0:8c643bfe55b7 123 /* Make sure we support the advertising type */
jksoft 0:8c643bfe55b7 124 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED)
jksoft 0:8c643bfe55b7 125 {
jksoft 0:8c643bfe55b7 126 /* ToDo: This requires a propery security implementation, etc. */
jksoft 0:8c643bfe55b7 127 return BLE_ERROR_NOT_IMPLEMENTED;
jksoft 0:8c643bfe55b7 128 }
jksoft 0:8c643bfe55b7 129
jksoft 0:8c643bfe55b7 130 /* Check interval range */
jksoft 0:8c643bfe55b7 131 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED)
jksoft 0:8c643bfe55b7 132 {
jksoft 0:8c643bfe55b7 133 /* Min delay is slightly longer for unconnectable devices */
jksoft 0:8c643bfe55b7 134 if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) ||
jksoft 0:8c643bfe55b7 135 (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX))
jksoft 0:8c643bfe55b7 136 {
jksoft 0:8c643bfe55b7 137 return BLE_ERROR_PARAM_OUT_OF_RANGE;
jksoft 0:8c643bfe55b7 138 }
jksoft 0:8c643bfe55b7 139 }
jksoft 0:8c643bfe55b7 140 else
jksoft 0:8c643bfe55b7 141 {
jksoft 0:8c643bfe55b7 142 if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN) ||
jksoft 0:8c643bfe55b7 143 (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX))
jksoft 0:8c643bfe55b7 144 {
jksoft 0:8c643bfe55b7 145 return BLE_ERROR_PARAM_OUT_OF_RANGE;
jksoft 0:8c643bfe55b7 146 }
jksoft 0:8c643bfe55b7 147 }
jksoft 0:8c643bfe55b7 148
jksoft 0:8c643bfe55b7 149 /* Check timeout is zero for Connectable Directed */
jksoft 0:8c643bfe55b7 150 if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) &&
jksoft 0:8c643bfe55b7 151 (params.getTimeout() != 0))
jksoft 0:8c643bfe55b7 152 {
jksoft 0:8c643bfe55b7 153 /* Timeout must be 0 with this type, although we'll never get here */
jksoft 0:8c643bfe55b7 154 /* since this isn't implemented yet anyway */
jksoft 0:8c643bfe55b7 155 return BLE_ERROR_PARAM_OUT_OF_RANGE;
jksoft 0:8c643bfe55b7 156 }
jksoft 0:8c643bfe55b7 157
jksoft 0:8c643bfe55b7 158 /* Check timeout for other advertising types */
jksoft 0:8c643bfe55b7 159 if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) &&
jksoft 0:8c643bfe55b7 160 (params.getTimeout() > GAP_ADV_PARAMS_TIMEOUT_MAX))
jksoft 0:8c643bfe55b7 161 {
jksoft 0:8c643bfe55b7 162 return BLE_ERROR_PARAM_OUT_OF_RANGE;
jksoft 0:8c643bfe55b7 163 }
jksoft 0:8c643bfe55b7 164
jksoft 0:8c643bfe55b7 165 /* ToDo: Start Advertising */
jksoft 0:8c643bfe55b7 166 ble_gap_adv_params_t adv_para = { 0 };
jksoft 0:8c643bfe55b7 167
jksoft 0:8c643bfe55b7 168 adv_para.type = params.getAdvertisingType() ;
jksoft 0:8c643bfe55b7 169 adv_para.p_peer_addr = NULL ; // Undirected advertisement
jksoft 0:8c643bfe55b7 170 adv_para.fp = BLE_GAP_ADV_FP_ANY ;
jksoft 0:8c643bfe55b7 171 adv_para.p_whitelist = NULL ;
jksoft 0:8c643bfe55b7 172 adv_para.interval = params.getInterval() ; // advertising interval (in units of 0.625 ms)
jksoft 0:8c643bfe55b7 173 adv_para.timeout = params.getTimeout() ;
jksoft 0:8c643bfe55b7 174
jksoft 0:8c643bfe55b7 175 ASSERT( ERROR_NONE == sd_ble_gap_adv_start(&adv_para), BLE_ERROR_PARAM_OUT_OF_RANGE);
jksoft 0:8c643bfe55b7 176
jksoft 0:8c643bfe55b7 177 state.advertising = 1;
jksoft 0:8c643bfe55b7 178
jksoft 0:8c643bfe55b7 179 return BLE_ERROR_NONE;
jksoft 0:8c643bfe55b7 180 }
jksoft 0:8c643bfe55b7 181
jksoft 0:8c643bfe55b7 182 /**************************************************************************/
jksoft 0:8c643bfe55b7 183 /*!
jksoft 0:8c643bfe55b7 184 @brief Stops the BLE HW and disconnects from any devices
jksoft 0:8c643bfe55b7 185
jksoft 0:8c643bfe55b7 186 @returns ble_error_t
jksoft 0:8c643bfe55b7 187
jksoft 0:8c643bfe55b7 188 @retval BLE_ERROR_NONE
jksoft 0:8c643bfe55b7 189 Everything executed properly
jksoft 0:8c643bfe55b7 190
jksoft 0:8c643bfe55b7 191 @section EXAMPLE
jksoft 0:8c643bfe55b7 192
jksoft 0:8c643bfe55b7 193 @code
jksoft 0:8c643bfe55b7 194
jksoft 0:8c643bfe55b7 195 @endcode
jksoft 0:8c643bfe55b7 196 */
jksoft 0:8c643bfe55b7 197 /**************************************************************************/
jksoft 0:8c643bfe55b7 198 ble_error_t nRF51Gap::stopAdvertising(void)
jksoft 0:8c643bfe55b7 199 {
jksoft 0:8c643bfe55b7 200 /* ToDo: Stop Advertising */
jksoft 0:8c643bfe55b7 201
jksoft 0:8c643bfe55b7 202 /* ToDo: Check response */
jksoft 0:8c643bfe55b7 203 wait(0.1);
jksoft 0:8c643bfe55b7 204
jksoft 0:8c643bfe55b7 205 state.advertising = 0;
jksoft 0:8c643bfe55b7 206
jksoft 0:8c643bfe55b7 207 return BLE_ERROR_NONE;
jksoft 0:8c643bfe55b7 208 }
jksoft 0:8c643bfe55b7 209
jksoft 0:8c643bfe55b7 210 /**************************************************************************/
jksoft 0:8c643bfe55b7 211 /*!
jksoft 0:8c643bfe55b7 212 @brief Disconnects if we are connected to a central device
jksoft 0:8c643bfe55b7 213
jksoft 0:8c643bfe55b7 214 @returns ble_error_t
jksoft 0:8c643bfe55b7 215
jksoft 0:8c643bfe55b7 216 @retval BLE_ERROR_NONE
jksoft 0:8c643bfe55b7 217 Everything executed properly
jksoft 0:8c643bfe55b7 218
jksoft 0:8c643bfe55b7 219 @section EXAMPLE
jksoft 0:8c643bfe55b7 220
jksoft 0:8c643bfe55b7 221 @code
jksoft 0:8c643bfe55b7 222
jksoft 0:8c643bfe55b7 223 @endcode
jksoft 0:8c643bfe55b7 224 */
jksoft 0:8c643bfe55b7 225 /**************************************************************************/
jksoft 0:8c643bfe55b7 226 ble_error_t nRF51Gap::disconnect(void)
jksoft 0:8c643bfe55b7 227 {
jksoft 0:8c643bfe55b7 228 /* ToDo: Disconnect if we are connected to a central device */
jksoft 0:8c643bfe55b7 229
jksoft 0:8c643bfe55b7 230 state.advertising = 0;
jksoft 0:8c643bfe55b7 231 state.connected = 0;
jksoft 0:8c643bfe55b7 232
jksoft 0:8c643bfe55b7 233 return BLE_ERROR_NONE;
jksoft 0:8c643bfe55b7 234 }