【これは旧バージョンです】 AndroidのBLEラジコンプロポアプリ「BLEPropo」と接続し、RCサーボとDCモータを制御するプログラムです。 mbed HRM1017で動作を確認しています。 BLEPropo → https://github.com/lipoyang/BLEPropo
Fork of BLE_RCBController2 by
BLEを使ったAndroid用ラジコンプロポアプリ「BLEPropo」に対応するmbed HRM1017用ファームウェアです。
BLEPropoは、GitHubにて公開中。
https://github.com/lipoyang/BLEPropo
ラジコンは、mbed HRM1017とRCサーボやDCモータを組み合わせて作ります。
nRF51822/nRF51822n.cpp@1:48f6e08a3ac2, 2014-08-20 (annotated)
- Committer:
- jksoft
- Date:
- Wed Aug 20 13:24:20 2014 +0000
- Revision:
- 1:48f6e08a3ac2
2014.08.20?????BLE?????????????; ???mbed?????????????????; mbed HRM1017; Nordic nRF51822
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 1:48f6e08a3ac2 | 1 | /* mbed Microcontroller Library |
jksoft | 1:48f6e08a3ac2 | 2 | * Copyright (c) 2006-2013 ARM Limited |
jksoft | 1:48f6e08a3ac2 | 3 | * |
jksoft | 1:48f6e08a3ac2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
jksoft | 1:48f6e08a3ac2 | 5 | * you may not use this file except in compliance with the License. |
jksoft | 1:48f6e08a3ac2 | 6 | * You may obtain a copy of the License at |
jksoft | 1:48f6e08a3ac2 | 7 | * |
jksoft | 1:48f6e08a3ac2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
jksoft | 1:48f6e08a3ac2 | 9 | * |
jksoft | 1:48f6e08a3ac2 | 10 | * Unless required by applicable law or agreed to in writing, software |
jksoft | 1:48f6e08a3ac2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
jksoft | 1:48f6e08a3ac2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
jksoft | 1:48f6e08a3ac2 | 13 | * See the License for the specific language governing permissions and |
jksoft | 1:48f6e08a3ac2 | 14 | * limitations under the License. |
jksoft | 1:48f6e08a3ac2 | 15 | */ |
jksoft | 1:48f6e08a3ac2 | 16 | |
jksoft | 1:48f6e08a3ac2 | 17 | #include "mbed.h" |
jksoft | 1:48f6e08a3ac2 | 18 | #include "nRF51822n.h" |
jksoft | 1:48f6e08a3ac2 | 19 | #include "nrf_soc.h" |
jksoft | 1:48f6e08a3ac2 | 20 | |
jksoft | 1:48f6e08a3ac2 | 21 | #include "btle/btle.h" |
jksoft | 1:48f6e08a3ac2 | 22 | |
jksoft | 1:48f6e08a3ac2 | 23 | /** |
jksoft | 1:48f6e08a3ac2 | 24 | * The singleton which represents the nRF51822 transport for the BLEDevice. |
jksoft | 1:48f6e08a3ac2 | 25 | */ |
jksoft | 1:48f6e08a3ac2 | 26 | static nRF51822n deviceInstance; |
jksoft | 1:48f6e08a3ac2 | 27 | |
jksoft | 1:48f6e08a3ac2 | 28 | /** |
jksoft | 1:48f6e08a3ac2 | 29 | * BLE-API requires an implementation of the following function in order to |
jksoft | 1:48f6e08a3ac2 | 30 | * obtain its transport handle. |
jksoft | 1:48f6e08a3ac2 | 31 | */ |
jksoft | 1:48f6e08a3ac2 | 32 | BLEDeviceInstanceBase * |
jksoft | 1:48f6e08a3ac2 | 33 | createBLEDeviceInstance(void) |
jksoft | 1:48f6e08a3ac2 | 34 | { |
jksoft | 1:48f6e08a3ac2 | 35 | return (&deviceInstance); |
jksoft | 1:48f6e08a3ac2 | 36 | } |
jksoft | 1:48f6e08a3ac2 | 37 | |
jksoft | 1:48f6e08a3ac2 | 38 | /**************************************************************************/ |
jksoft | 1:48f6e08a3ac2 | 39 | /*! |
jksoft | 1:48f6e08a3ac2 | 40 | @brief Constructor |
jksoft | 1:48f6e08a3ac2 | 41 | */ |
jksoft | 1:48f6e08a3ac2 | 42 | /**************************************************************************/ |
jksoft | 1:48f6e08a3ac2 | 43 | nRF51822n::nRF51822n(void) |
jksoft | 1:48f6e08a3ac2 | 44 | { |
jksoft | 1:48f6e08a3ac2 | 45 | } |
jksoft | 1:48f6e08a3ac2 | 46 | |
jksoft | 1:48f6e08a3ac2 | 47 | /**************************************************************************/ |
jksoft | 1:48f6e08a3ac2 | 48 | /*! |
jksoft | 1:48f6e08a3ac2 | 49 | @brief Destructor |
jksoft | 1:48f6e08a3ac2 | 50 | */ |
jksoft | 1:48f6e08a3ac2 | 51 | /**************************************************************************/ |
jksoft | 1:48f6e08a3ac2 | 52 | nRF51822n::~nRF51822n(void) |
jksoft | 1:48f6e08a3ac2 | 53 | { |
jksoft | 1:48f6e08a3ac2 | 54 | } |
jksoft | 1:48f6e08a3ac2 | 55 | |
jksoft | 1:48f6e08a3ac2 | 56 | const char *nRF51822n::getVersion(void) |
jksoft | 1:48f6e08a3ac2 | 57 | { |
jksoft | 1:48f6e08a3ac2 | 58 | static char versionString[10]; |
jksoft | 1:48f6e08a3ac2 | 59 | static bool versionFetched = false; |
jksoft | 1:48f6e08a3ac2 | 60 | |
jksoft | 1:48f6e08a3ac2 | 61 | if (!versionFetched) { |
jksoft | 1:48f6e08a3ac2 | 62 | ble_version_t version; |
jksoft | 1:48f6e08a3ac2 | 63 | if (sd_ble_version_get(&version) == NRF_SUCCESS) { |
jksoft | 1:48f6e08a3ac2 | 64 | snprintf(versionString, sizeof(versionString), "%u.%u", version.version_number, version.subversion_number); |
jksoft | 1:48f6e08a3ac2 | 65 | versionFetched = true; |
jksoft | 1:48f6e08a3ac2 | 66 | } else { |
jksoft | 1:48f6e08a3ac2 | 67 | strncpy(versionString, "unknown", sizeof(versionString)); |
jksoft | 1:48f6e08a3ac2 | 68 | } |
jksoft | 1:48f6e08a3ac2 | 69 | } |
jksoft | 1:48f6e08a3ac2 | 70 | |
jksoft | 1:48f6e08a3ac2 | 71 | return versionString; |
jksoft | 1:48f6e08a3ac2 | 72 | } |
jksoft | 1:48f6e08a3ac2 | 73 | |
jksoft | 1:48f6e08a3ac2 | 74 | /* (Valid values are -40, -20, -16, -12, -8, -4, 0, 4) */ |
jksoft | 1:48f6e08a3ac2 | 75 | ble_error_t nRF51822n::setTxPower(int8_t txPower) |
jksoft | 1:48f6e08a3ac2 | 76 | { |
jksoft | 1:48f6e08a3ac2 | 77 | unsigned rc; |
jksoft | 1:48f6e08a3ac2 | 78 | if ((rc = sd_ble_gap_tx_power_set(txPower)) != NRF_SUCCESS) { |
jksoft | 1:48f6e08a3ac2 | 79 | switch (rc) { |
jksoft | 1:48f6e08a3ac2 | 80 | case NRF_ERROR_BUSY: |
jksoft | 1:48f6e08a3ac2 | 81 | return BLE_STACK_BUSY; |
jksoft | 1:48f6e08a3ac2 | 82 | case NRF_ERROR_INVALID_PARAM: |
jksoft | 1:48f6e08a3ac2 | 83 | default: |
jksoft | 1:48f6e08a3ac2 | 84 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
jksoft | 1:48f6e08a3ac2 | 85 | } |
jksoft | 1:48f6e08a3ac2 | 86 | } |
jksoft | 1:48f6e08a3ac2 | 87 | |
jksoft | 1:48f6e08a3ac2 | 88 | return BLE_ERROR_NONE; |
jksoft | 1:48f6e08a3ac2 | 89 | } |
jksoft | 1:48f6e08a3ac2 | 90 | |
jksoft | 1:48f6e08a3ac2 | 91 | /**************************************************************************/ |
jksoft | 1:48f6e08a3ac2 | 92 | /*! |
jksoft | 1:48f6e08a3ac2 | 93 | @brief Initialises anything required to start using BLE |
jksoft | 1:48f6e08a3ac2 | 94 | |
jksoft | 1:48f6e08a3ac2 | 95 | @returns ble_error_t |
jksoft | 1:48f6e08a3ac2 | 96 | |
jksoft | 1:48f6e08a3ac2 | 97 | @retval BLE_ERROR_NONE |
jksoft | 1:48f6e08a3ac2 | 98 | Everything executed properly |
jksoft | 1:48f6e08a3ac2 | 99 | |
jksoft | 1:48f6e08a3ac2 | 100 | @section EXAMPLE |
jksoft | 1:48f6e08a3ac2 | 101 | |
jksoft | 1:48f6e08a3ac2 | 102 | @code |
jksoft | 1:48f6e08a3ac2 | 103 | |
jksoft | 1:48f6e08a3ac2 | 104 | @endcode |
jksoft | 1:48f6e08a3ac2 | 105 | */ |
jksoft | 1:48f6e08a3ac2 | 106 | /**************************************************************************/ |
jksoft | 1:48f6e08a3ac2 | 107 | ble_error_t nRF51822n::init(void) |
jksoft | 1:48f6e08a3ac2 | 108 | { |
jksoft | 1:48f6e08a3ac2 | 109 | /* ToDo: Clear memory contents, reset the SD, etc. */ |
jksoft | 1:48f6e08a3ac2 | 110 | btle_init(); |
jksoft | 1:48f6e08a3ac2 | 111 | |
jksoft | 1:48f6e08a3ac2 | 112 | reset(); |
jksoft | 1:48f6e08a3ac2 | 113 | |
jksoft | 1:48f6e08a3ac2 | 114 | return BLE_ERROR_NONE; |
jksoft | 1:48f6e08a3ac2 | 115 | } |
jksoft | 1:48f6e08a3ac2 | 116 | |
jksoft | 1:48f6e08a3ac2 | 117 | /**************************************************************************/ |
jksoft | 1:48f6e08a3ac2 | 118 | /*! |
jksoft | 1:48f6e08a3ac2 | 119 | @brief Resets the BLE HW, removing any existing services and |
jksoft | 1:48f6e08a3ac2 | 120 | characteristics |
jksoft | 1:48f6e08a3ac2 | 121 | |
jksoft | 1:48f6e08a3ac2 | 122 | @returns ble_error_t |
jksoft | 1:48f6e08a3ac2 | 123 | |
jksoft | 1:48f6e08a3ac2 | 124 | @retval BLE_ERROR_NONE |
jksoft | 1:48f6e08a3ac2 | 125 | Everything executed properly |
jksoft | 1:48f6e08a3ac2 | 126 | |
jksoft | 1:48f6e08a3ac2 | 127 | @section EXAMPLE |
jksoft | 1:48f6e08a3ac2 | 128 | |
jksoft | 1:48f6e08a3ac2 | 129 | @code |
jksoft | 1:48f6e08a3ac2 | 130 | |
jksoft | 1:48f6e08a3ac2 | 131 | @endcode |
jksoft | 1:48f6e08a3ac2 | 132 | */ |
jksoft | 1:48f6e08a3ac2 | 133 | /**************************************************************************/ |
jksoft | 1:48f6e08a3ac2 | 134 | ble_error_t nRF51822n::reset(void) |
jksoft | 1:48f6e08a3ac2 | 135 | { |
jksoft | 1:48f6e08a3ac2 | 136 | wait(0.5); |
jksoft | 1:48f6e08a3ac2 | 137 | |
jksoft | 1:48f6e08a3ac2 | 138 | /* Wait for the radio to come back up */ |
jksoft | 1:48f6e08a3ac2 | 139 | wait(1); |
jksoft | 1:48f6e08a3ac2 | 140 | |
jksoft | 1:48f6e08a3ac2 | 141 | return BLE_ERROR_NONE; |
jksoft | 1:48f6e08a3ac2 | 142 | } |
jksoft | 1:48f6e08a3ac2 | 143 | |
jksoft | 1:48f6e08a3ac2 | 144 | void |
jksoft | 1:48f6e08a3ac2 | 145 | nRF51822n::waitForEvent(void) |
jksoft | 1:48f6e08a3ac2 | 146 | { |
jksoft | 1:48f6e08a3ac2 | 147 | sd_app_evt_wait(); |
jksoft | 1:48f6e08a3ac2 | 148 | } |