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 #include "common/common.h"
jksoft 0:8c643bfe55b7 17
jksoft 0:8c643bfe55b7 18 #include "app_timer.h"
jksoft 0:8c643bfe55b7 19 #include "btle.h"
jksoft 0:8c643bfe55b7 20
jksoft 0:8c643bfe55b7 21 #include "ble_stack_handler_types.h"
jksoft 0:8c643bfe55b7 22 #include "ble_radio_notification.h"
jksoft 0:8c643bfe55b7 23 #include "ble_flash.h"
jksoft 0:8c643bfe55b7 24 #include "ble_bondmngr.h"
jksoft 0:8c643bfe55b7 25 #include "ble_conn_params.h"
jksoft 0:8c643bfe55b7 26
jksoft 0:8c643bfe55b7 27 #include "btle_gap.h"
jksoft 0:8c643bfe55b7 28 #include "btle_advertising.h"
jksoft 0:8c643bfe55b7 29 #include "custom/custom_helper.h"
jksoft 0:8c643bfe55b7 30
jksoft 0:8c643bfe55b7 31 #include "nordic_common.h"
jksoft 0:8c643bfe55b7 32 #include "softdevice_handler.h"
jksoft 0:8c643bfe55b7 33 #include "pstorage.h"
jksoft 0:8c643bfe55b7 34
jksoft 0:8c643bfe55b7 35 #include "hw/GapEvents.h"
jksoft 0:8c643bfe55b7 36 #include "hw/nRF51822n/nRF51Gap.h"
jksoft 0:8c643bfe55b7 37 #include "hw/nRF51822n/nRF51GattServer.h"
jksoft 0:8c643bfe55b7 38
jksoft 0:8c643bfe55b7 39 static void service_error_callback(uint32_t nrf_error);
jksoft 0:8c643bfe55b7 40 void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name);
jksoft 0:8c643bfe55b7 41 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name);
jksoft 0:8c643bfe55b7 42
jksoft 0:8c643bfe55b7 43 static error_t bond_manager_init(void);
jksoft 0:8c643bfe55b7 44
jksoft 0:8c643bfe55b7 45 static void btle_handler(ble_evt_t * p_ble_evt);
jksoft 0:8c643bfe55b7 46
jksoft 0:8c643bfe55b7 47 /**************************************************************************/
jksoft 0:8c643bfe55b7 48 /*!
jksoft 0:8c643bfe55b7 49
jksoft 0:8c643bfe55b7 50 */
jksoft 0:8c643bfe55b7 51 /**************************************************************************/
jksoft 0:8c643bfe55b7 52 static void sys_evt_dispatch(uint32_t sys_evt)
jksoft 0:8c643bfe55b7 53 {
jksoft 0:8c643bfe55b7 54 pstorage_sys_event_handler(sys_evt);
jksoft 0:8c643bfe55b7 55 }
jksoft 0:8c643bfe55b7 56
jksoft 0:8c643bfe55b7 57 /**************************************************************************/
jksoft 0:8c643bfe55b7 58 /*!
jksoft 0:8c643bfe55b7 59 @brief Initialises BTLE and the underlying HW/SoftDevice
jksoft 0:8c643bfe55b7 60
jksoft 0:8c643bfe55b7 61 @returns
jksoft 0:8c643bfe55b7 62 */
jksoft 0:8c643bfe55b7 63 /**************************************************************************/
jksoft 0:8c643bfe55b7 64 error_t btle_init(void)
jksoft 0:8c643bfe55b7 65 {
jksoft 0:8c643bfe55b7 66 APP_TIMER_INIT(0, 8, 5, false);
jksoft 0:8c643bfe55b7 67 // SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, false);
jksoft 0:8c643bfe55b7 68 SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_RC_250_PPM_4000MS_CALIBRATION, false);
jksoft 0:8c643bfe55b7 69
jksoft 0:8c643bfe55b7 70 ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler) );
jksoft 0:8c643bfe55b7 71 ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch) );
jksoft 0:8c643bfe55b7 72
jksoft 0:8c643bfe55b7 73 bond_manager_init();
jksoft 0:8c643bfe55b7 74 btle_gap_init();
jksoft 0:8c643bfe55b7 75
jksoft 0:8c643bfe55b7 76 return ERROR_NONE;
jksoft 0:8c643bfe55b7 77 }
jksoft 0:8c643bfe55b7 78
jksoft 0:8c643bfe55b7 79 /**************************************************************************/
jksoft 0:8c643bfe55b7 80 /*!
jksoft 0:8c643bfe55b7 81 @brief
jksoft 0:8c643bfe55b7 82
jksoft 0:8c643bfe55b7 83 @param[in] p_ble_evt
jksoft 0:8c643bfe55b7 84
jksoft 0:8c643bfe55b7 85 @returns
jksoft 0:8c643bfe55b7 86 */
jksoft 0:8c643bfe55b7 87 /**************************************************************************/
jksoft 0:8c643bfe55b7 88 static void btle_handler(ble_evt_t * p_ble_evt)
jksoft 0:8c643bfe55b7 89 {
jksoft 0:8c643bfe55b7 90 /* Library service handlers */
jksoft 0:8c643bfe55b7 91 ble_bondmngr_on_ble_evt(p_ble_evt);
jksoft 0:8c643bfe55b7 92 ble_conn_params_on_ble_evt(p_ble_evt);
jksoft 0:8c643bfe55b7 93
jksoft 0:8c643bfe55b7 94 /* Custom event handler */
jksoft 0:8c643bfe55b7 95 switch (p_ble_evt->header.evt_id)
jksoft 0:8c643bfe55b7 96 {
jksoft 0:8c643bfe55b7 97 case BLE_GAP_EVT_CONNECTED:
jksoft 0:8c643bfe55b7 98 nRF51GattServer::getInstance().m_connectionHandle = p_ble_evt->evt.gap_evt.conn_handle;
jksoft 0:8c643bfe55b7 99 nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_CONNECTED);
jksoft 0:8c643bfe55b7 100 break;
jksoft 0:8c643bfe55b7 101
jksoft 0:8c643bfe55b7 102 case BLE_GAP_EVT_DISCONNECTED:
jksoft 0:8c643bfe55b7 103 // Since we are not in a connection and have not started advertising, store bonds
jksoft 0:8c643bfe55b7 104 nRF51GattServer::getInstance().m_connectionHandle = BLE_CONN_HANDLE_INVALID;
jksoft 0:8c643bfe55b7 105 ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store() );
jksoft 0:8c643bfe55b7 106 nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_DISCONNECTED);
jksoft 0:8c643bfe55b7 107 break;
jksoft 0:8c643bfe55b7 108
jksoft 0:8c643bfe55b7 109 case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
jksoft 0:8c643bfe55b7 110 {
jksoft 0:8c643bfe55b7 111 ble_gap_sec_params_t sec_params = { 0 };
jksoft 0:8c643bfe55b7 112
jksoft 0:8c643bfe55b7 113 sec_params.timeout = 30 ; /**< Timeout for Pairing Request or Security Request (in seconds). */
jksoft 0:8c643bfe55b7 114 sec_params.bond = 1 ; /**< Perform bonding. */
jksoft 0:8c643bfe55b7 115 sec_params.mitm = CFG_BLE_SEC_PARAM_MITM ;
jksoft 0:8c643bfe55b7 116 sec_params.io_caps = CFG_BLE_SEC_PARAM_IO_CAPABILITIES ;
jksoft 0:8c643bfe55b7 117 sec_params.oob = CFG_BLE_SEC_PARAM_OOB ;
jksoft 0:8c643bfe55b7 118 sec_params.min_key_size = CFG_BLE_SEC_PARAM_MIN_KEY_SIZE ;
jksoft 0:8c643bfe55b7 119 sec_params.max_key_size = CFG_BLE_SEC_PARAM_MAX_KEY_SIZE ;
jksoft 0:8c643bfe55b7 120
jksoft 0:8c643bfe55b7 121 ASSERT_STATUS_RET_VOID ( sd_ble_gap_sec_params_reply(nRF51GattServer::getInstance().m_connectionHandle, BLE_GAP_SEC_STATUS_SUCCESS, &sec_params) );
jksoft 0:8c643bfe55b7 122 }
jksoft 0:8c643bfe55b7 123 break;
jksoft 0:8c643bfe55b7 124
jksoft 0:8c643bfe55b7 125 case BLE_GAP_EVT_TIMEOUT:
jksoft 0:8c643bfe55b7 126 if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT)
jksoft 0:8c643bfe55b7 127 {
jksoft 0:8c643bfe55b7 128 nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_TIMEOUT);
jksoft 0:8c643bfe55b7 129 }
jksoft 0:8c643bfe55b7 130 break;
jksoft 0:8c643bfe55b7 131
jksoft 0:8c643bfe55b7 132 case BLE_GATTC_EVT_TIMEOUT:
jksoft 0:8c643bfe55b7 133 case BLE_GATTS_EVT_TIMEOUT:
jksoft 0:8c643bfe55b7 134 // Disconnect on GATT Server and Client timeout events.
jksoft 0:8c643bfe55b7 135 // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
jksoft 0:8c643bfe55b7 136 break;
jksoft 0:8c643bfe55b7 137
jksoft 0:8c643bfe55b7 138 default:
jksoft 0:8c643bfe55b7 139 break;
jksoft 0:8c643bfe55b7 140 }
jksoft 0:8c643bfe55b7 141
jksoft 0:8c643bfe55b7 142 nRF51GattServer::getInstance().hwCallback(p_ble_evt);
jksoft 0:8c643bfe55b7 143 }
jksoft 0:8c643bfe55b7 144
jksoft 0:8c643bfe55b7 145 /**************************************************************************/
jksoft 0:8c643bfe55b7 146 /*!
jksoft 0:8c643bfe55b7 147 @brief Initialises the bond manager
jksoft 0:8c643bfe55b7 148
jksoft 0:8c643bfe55b7 149 @note Bond data will be cleared on reset if the bond delete
jksoft 0:8c643bfe55b7 150 button is pressed during initialisation (the button is
jksoft 0:8c643bfe55b7 151 defined as CFG_BLE_BOND_DELETE_BUTTON_NUM).
jksoft 0:8c643bfe55b7 152
jksoft 0:8c643bfe55b7 153 @returns
jksoft 0:8c643bfe55b7 154 */
jksoft 0:8c643bfe55b7 155 /**************************************************************************/
jksoft 0:8c643bfe55b7 156 static error_t bond_manager_init(void)
jksoft 0:8c643bfe55b7 157 {
jksoft 0:8c643bfe55b7 158 ble_bondmngr_init_t bond_para = { 0 };
jksoft 0:8c643bfe55b7 159
jksoft 0:8c643bfe55b7 160 ASSERT_STATUS ( pstorage_init() );
jksoft 0:8c643bfe55b7 161
jksoft 0:8c643bfe55b7 162 bond_para.flash_page_num_bond = CFG_BLE_BOND_FLASH_PAGE_BOND ;
jksoft 0:8c643bfe55b7 163 bond_para.flash_page_num_sys_attr = CFG_BLE_BOND_FLASH_PAGE_SYS_ATTR ;
jksoft 0:8c643bfe55b7 164 //bond_para.bonds_delete = boardButtonCheck(CFG_BLE_BOND_DELETE_BUTTON_NUM) ;
jksoft 0:8c643bfe55b7 165 bond_para.evt_handler = NULL ;
jksoft 0:8c643bfe55b7 166 bond_para.error_handler = service_error_callback ;
jksoft 0:8c643bfe55b7 167
jksoft 0:8c643bfe55b7 168 ASSERT_STATUS( ble_bondmngr_init( &bond_para ) );
jksoft 0:8c643bfe55b7 169
jksoft 0:8c643bfe55b7 170 /* Init radio active/inactive notification to flash (to only perform flashing when the radio is inactive) */
jksoft 0:8c643bfe55b7 171 // ASSERT_STATUS( ble_radio_notification_init(NRF_APP_PRIORITY_HIGH,
jksoft 0:8c643bfe55b7 172 // NRF_RADIO_NOTIFICATION_DISTANCE_4560US,
jksoft 0:8c643bfe55b7 173 // ble_flash_on_radio_active_evt) );
jksoft 0:8c643bfe55b7 174
jksoft 0:8c643bfe55b7 175 return ERROR_NONE;
jksoft 0:8c643bfe55b7 176 }
jksoft 0:8c643bfe55b7 177
jksoft 0:8c643bfe55b7 178 /**************************************************************************/
jksoft 0:8c643bfe55b7 179 /*!
jksoft 0:8c643bfe55b7 180 @brief
jksoft 0:8c643bfe55b7 181 @param[in] nrf_error
jksoft 0:8c643bfe55b7 182 @returns
jksoft 0:8c643bfe55b7 183 */
jksoft 0:8c643bfe55b7 184 /**************************************************************************/
jksoft 0:8c643bfe55b7 185 static void service_error_callback(uint32_t nrf_error)
jksoft 0:8c643bfe55b7 186 {
jksoft 0:8c643bfe55b7 187 ASSERT_STATUS_RET_VOID( nrf_error );
jksoft 0:8c643bfe55b7 188 }
jksoft 0:8c643bfe55b7 189
jksoft 0:8c643bfe55b7 190 /**************************************************************************/
jksoft 0:8c643bfe55b7 191 /*!
jksoft 0:8c643bfe55b7 192 @brief Callback when an error occurs inside the SoftDevice
jksoft 0:8c643bfe55b7 193
jksoft 0:8c643bfe55b7 194 @param[in] line_num
jksoft 0:8c643bfe55b7 195 @param[in] p-file_name
jksoft 0:8c643bfe55b7 196
jksoft 0:8c643bfe55b7 197 @returns
jksoft 0:8c643bfe55b7 198 */
jksoft 0:8c643bfe55b7 199 /**************************************************************************/
jksoft 0:8c643bfe55b7 200 void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
jksoft 0:8c643bfe55b7 201 {
jksoft 0:8c643bfe55b7 202 ASSERT(false, (void) 0);
jksoft 0:8c643bfe55b7 203 }
jksoft 0:8c643bfe55b7 204
jksoft 0:8c643bfe55b7 205 /**************************************************************************/
jksoft 0:8c643bfe55b7 206 /*!
jksoft 0:8c643bfe55b7 207 @brief Handler for general errors above the SoftDevice layer.
jksoft 0:8c643bfe55b7 208 Typically we can' recover from this so we do a reset.
jksoft 0:8c643bfe55b7 209
jksoft 0:8c643bfe55b7 210 @param[in] error_code
jksoft 0:8c643bfe55b7 211 @param[in] line_num
jksoft 0:8c643bfe55b7 212 @param[in] p-file_name
jksoft 0:8c643bfe55b7 213
jksoft 0:8c643bfe55b7 214 @returns
jksoft 0:8c643bfe55b7 215 */
jksoft 0:8c643bfe55b7 216 /**************************************************************************/
jksoft 0:8c643bfe55b7 217 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
jksoft 0:8c643bfe55b7 218 {
jksoft 0:8c643bfe55b7 219 ASSERT_STATUS_RET_VOID( error_code );
jksoft 0:8c643bfe55b7 220 NVIC_SystemReset();
jksoft 0:8c643bfe55b7 221 }