Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bryantaylor 0:eafc3fd41f75 1 /* mbed Microcontroller Library
bryantaylor 0:eafc3fd41f75 2 * Copyright (c) 2006-2013 ARM Limited
bryantaylor 0:eafc3fd41f75 3 *
bryantaylor 0:eafc3fd41f75 4 * Licensed under the Apache License, Version 2.0 (the "License");
bryantaylor 0:eafc3fd41f75 5 * you may not use this file except in compliance with the License.
bryantaylor 0:eafc3fd41f75 6 * You may obtain a copy of the License at
bryantaylor 0:eafc3fd41f75 7 *
bryantaylor 0:eafc3fd41f75 8 * http://www.apache.org/licenses/LICENSE-2.0
bryantaylor 0:eafc3fd41f75 9 *
bryantaylor 0:eafc3fd41f75 10 * Unless required by applicable law or agreed to in writing, software
bryantaylor 0:eafc3fd41f75 11 * distributed under the License is distributed on an "AS IS" BASIS,
bryantaylor 0:eafc3fd41f75 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bryantaylor 0:eafc3fd41f75 13 * See the License for the specific language governing permissions and
bryantaylor 0:eafc3fd41f75 14 * limitations under the License.
bryantaylor 0:eafc3fd41f75 15 */
bryantaylor 0:eafc3fd41f75 16
bryantaylor 0:eafc3fd41f75 17 #ifndef __GAP_ADVERTISING_PARAMS_H__
bryantaylor 0:eafc3fd41f75 18 #define __GAP_ADVERTISING_PARAMS_H__
bryantaylor 0:eafc3fd41f75 19
bryantaylor 0:eafc3fd41f75 20 /**
bryantaylor 0:eafc3fd41f75 21 * This class provides a wrapper for the core advertising parameters,
bryantaylor 0:eafc3fd41f75 22 * including the advertising type (Connectable Undirected,
bryantaylor 0:eafc3fd41f75 23 * Non Connectable Undirected and so on), as well as the advertising and
bryantaylor 0:eafc3fd41f75 24 * timeout intervals.
bryantaylor 0:eafc3fd41f75 25 */
bryantaylor 0:eafc3fd41f75 26 class GapAdvertisingParams {
bryantaylor 0:eafc3fd41f75 27 public:
bryantaylor 0:eafc3fd41f75 28 /**
bryantaylor 0:eafc3fd41f75 29 * Minimum Advertising interval for connectable undirected and connectable
bryantaylor 0:eafc3fd41f75 30 * directed events in 625us units - 20ms.
bryantaylor 0:eafc3fd41f75 31 */
bryantaylor 0:eafc3fd41f75 32 static const unsigned GAP_ADV_PARAMS_INTERVAL_MIN = 0x0020;
bryantaylor 0:eafc3fd41f75 33 /**
bryantaylor 0:eafc3fd41f75 34 * Minimum Advertising interval for scannable and non-connectable
bryantaylor 0:eafc3fd41f75 35 * undirected events in 625us units - 100ms.
bryantaylor 0:eafc3fd41f75 36 */
bryantaylor 0:eafc3fd41f75 37 static const unsigned GAP_ADV_PARAMS_INTERVAL_MIN_NONCON = 0x00A0;
bryantaylor 0:eafc3fd41f75 38 /**
bryantaylor 0:eafc3fd41f75 39 * Maximum Advertising interval in 625us units - 10.24s.
bryantaylor 0:eafc3fd41f75 40 */
bryantaylor 0:eafc3fd41f75 41 static const unsigned GAP_ADV_PARAMS_INTERVAL_MAX = 0x4000;
bryantaylor 0:eafc3fd41f75 42 /**
bryantaylor 0:eafc3fd41f75 43 * Maximum advertising timeout seconds.
bryantaylor 0:eafc3fd41f75 44 */
bryantaylor 0:eafc3fd41f75 45 static const unsigned GAP_ADV_PARAMS_TIMEOUT_MAX = 0x3FFF;
bryantaylor 0:eafc3fd41f75 46
bryantaylor 0:eafc3fd41f75 47 /**
bryantaylor 0:eafc3fd41f75 48 * Encapsulates the peripheral advertising modes, which determine how
bryantaylor 0:eafc3fd41f75 49 * the device appears to other central devices in hearing range.
bryantaylor 0:eafc3fd41f75 50 */
bryantaylor 0:eafc3fd41f75 51 enum AdvertisingType_t {
bryantaylor 0:eafc3fd41f75 52 ADV_CONNECTABLE_UNDIRECTED, /**< Vol 3, Part C, Section 9.3.4 and Vol 6, Part B, Section 2.3.1.1. */
bryantaylor 0:eafc3fd41f75 53 ADV_CONNECTABLE_DIRECTED, /**< Vol 3, Part C, Section 9.3.3 and Vol 6, Part B, Section 2.3.1.2. */
bryantaylor 0:eafc3fd41f75 54 ADV_SCANNABLE_UNDIRECTED, /**< Include support for Scan Response payloads, see Vol 6, Part B, Section 2.3.1.4. */
bryantaylor 0:eafc3fd41f75 55 ADV_NON_CONNECTABLE_UNDIRECTED /**< Vol 3, Part C, Section 9.3.2 and Vol 6, Part B, Section 2.3.1.3. */
bryantaylor 0:eafc3fd41f75 56 };
bryantaylor 0:eafc3fd41f75 57 /**
bryantaylor 0:eafc3fd41f75 58 * Type alias for GapAdvertisingParams::AdvertisingType_t.
bryantaylor 0:eafc3fd41f75 59 *
bryantaylor 0:eafc3fd41f75 60 * @deprecated This type alias will be dropped in future releases.
bryantaylor 0:eafc3fd41f75 61 */
bryantaylor 0:eafc3fd41f75 62 typedef enum AdvertisingType_t AdvertisingType;
bryantaylor 0:eafc3fd41f75 63
bryantaylor 0:eafc3fd41f75 64 public:
bryantaylor 0:eafc3fd41f75 65 /**
bryantaylor 0:eafc3fd41f75 66 * Construct an instance of GapAdvertisingParams.
bryantaylor 0:eafc3fd41f75 67 *
bryantaylor 0:eafc3fd41f75 68 * @param[in] advType
bryantaylor 0:eafc3fd41f75 69 * Type of advertising. Default is
bryantaylor 0:eafc3fd41f75 70 * GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED.
bryantaylor 0:eafc3fd41f75 71 * @param[in] interval
bryantaylor 0:eafc3fd41f75 72 * Advertising interval in units of 0.625ms. Default is
bryantaylor 0:eafc3fd41f75 73 * GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MIN_NONCON.
bryantaylor 0:eafc3fd41f75 74 * @param[in] timeout
bryantaylor 0:eafc3fd41f75 75 * Advertising timeout. Default is 0.
bryantaylor 0:eafc3fd41f75 76 */
bryantaylor 0:eafc3fd41f75 77 GapAdvertisingParams(AdvertisingType_t advType = ADV_CONNECTABLE_UNDIRECTED,
bryantaylor 0:eafc3fd41f75 78 uint16_t interval = GAP_ADV_PARAMS_INTERVAL_MIN_NONCON,
bryantaylor 0:eafc3fd41f75 79 uint16_t timeout = 0) : _advType(advType), _interval(interval), _timeout(timeout) {
bryantaylor 0:eafc3fd41f75 80 /* Interval checks. */
bryantaylor 0:eafc3fd41f75 81 if (_advType == ADV_CONNECTABLE_DIRECTED) {
bryantaylor 0:eafc3fd41f75 82 /* Interval must be 0 in directed connectable mode. */
bryantaylor 0:eafc3fd41f75 83 _interval = 0;
bryantaylor 0:eafc3fd41f75 84 } else if (_advType == ADV_NON_CONNECTABLE_UNDIRECTED) {
bryantaylor 0:eafc3fd41f75 85 /* Min interval is slightly larger than in other modes. */
bryantaylor 0:eafc3fd41f75 86 if (_interval < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) {
bryantaylor 0:eafc3fd41f75 87 _interval = GAP_ADV_PARAMS_INTERVAL_MIN_NONCON;
bryantaylor 0:eafc3fd41f75 88 }
bryantaylor 0:eafc3fd41f75 89 if (_interval > GAP_ADV_PARAMS_INTERVAL_MAX) {
bryantaylor 0:eafc3fd41f75 90 _interval = GAP_ADV_PARAMS_INTERVAL_MAX;
bryantaylor 0:eafc3fd41f75 91 }
bryantaylor 0:eafc3fd41f75 92 } else {
bryantaylor 0:eafc3fd41f75 93 /* Stay within interval limits. */
bryantaylor 0:eafc3fd41f75 94 if (_interval < GAP_ADV_PARAMS_INTERVAL_MIN) {
bryantaylor 0:eafc3fd41f75 95 _interval = GAP_ADV_PARAMS_INTERVAL_MIN;
bryantaylor 0:eafc3fd41f75 96 }
bryantaylor 0:eafc3fd41f75 97 if (_interval > GAP_ADV_PARAMS_INTERVAL_MAX) {
bryantaylor 0:eafc3fd41f75 98 _interval = GAP_ADV_PARAMS_INTERVAL_MAX;
bryantaylor 0:eafc3fd41f75 99 }
bryantaylor 0:eafc3fd41f75 100 }
bryantaylor 0:eafc3fd41f75 101
bryantaylor 0:eafc3fd41f75 102 /* Timeout checks. */
bryantaylor 0:eafc3fd41f75 103 if (timeout) {
bryantaylor 0:eafc3fd41f75 104 /* Stay within timeout limits. */
bryantaylor 0:eafc3fd41f75 105 if (_timeout > GAP_ADV_PARAMS_TIMEOUT_MAX) {
bryantaylor 0:eafc3fd41f75 106 _timeout = GAP_ADV_PARAMS_TIMEOUT_MAX;
bryantaylor 0:eafc3fd41f75 107 }
bryantaylor 0:eafc3fd41f75 108 }
bryantaylor 0:eafc3fd41f75 109 }
bryantaylor 0:eafc3fd41f75 110
bryantaylor 0:eafc3fd41f75 111 static const uint16_t UNIT_0_625_MS = 625; /**< Number of microseconds in 0.625 milliseconds. */
bryantaylor 0:eafc3fd41f75 112 /**
bryantaylor 0:eafc3fd41f75 113 * Convert milliseconds to units of 0.625ms.
bryantaylor 0:eafc3fd41f75 114 *
bryantaylor 0:eafc3fd41f75 115 * @param[in] durationInMillis
bryantaylor 0:eafc3fd41f75 116 * The number of milliseconds to convert.
bryantaylor 0:eafc3fd41f75 117 *
bryantaylor 0:eafc3fd41f75 118 * @return The value of @p durationInMillis in units of 0.625ms.
bryantaylor 0:eafc3fd41f75 119 */
bryantaylor 0:eafc3fd41f75 120 static uint16_t MSEC_TO_ADVERTISEMENT_DURATION_UNITS(uint32_t durationInMillis) {
bryantaylor 0:eafc3fd41f75 121 return (durationInMillis * 1000) / UNIT_0_625_MS;
bryantaylor 0:eafc3fd41f75 122 }
bryantaylor 0:eafc3fd41f75 123 /**
bryantaylor 0:eafc3fd41f75 124 * Convert units of 0.625ms to milliseconds.
bryantaylor 0:eafc3fd41f75 125 *
bryantaylor 0:eafc3fd41f75 126 * @param[in] gapUnits
bryantaylor 0:eafc3fd41f75 127 * The number of units of 0.625ms to convert.
bryantaylor 0:eafc3fd41f75 128 *
bryantaylor 0:eafc3fd41f75 129 * @return The value of @p gapUnits in milliseconds.
bryantaylor 0:eafc3fd41f75 130 */
bryantaylor 0:eafc3fd41f75 131 static uint16_t ADVERTISEMENT_DURATION_UNITS_TO_MS(uint16_t gapUnits) {
bryantaylor 0:eafc3fd41f75 132 return (gapUnits * UNIT_0_625_MS) / 1000;
bryantaylor 0:eafc3fd41f75 133 }
bryantaylor 0:eafc3fd41f75 134
bryantaylor 0:eafc3fd41f75 135 /**
bryantaylor 0:eafc3fd41f75 136 * Get the advertising type.
bryantaylor 0:eafc3fd41f75 137 *
bryantaylor 0:eafc3fd41f75 138 * @return The advertising type.
bryantaylor 0:eafc3fd41f75 139 */
bryantaylor 0:eafc3fd41f75 140 AdvertisingType_t getAdvertisingType(void) const {
bryantaylor 0:eafc3fd41f75 141 return _advType;
bryantaylor 0:eafc3fd41f75 142 }
bryantaylor 0:eafc3fd41f75 143
bryantaylor 0:eafc3fd41f75 144 /**
bryantaylor 0:eafc3fd41f75 145 * Get the advertising interval in milliseconds.
bryantaylor 0:eafc3fd41f75 146 *
bryantaylor 0:eafc3fd41f75 147 * @return The advertisement interval (in milliseconds).
bryantaylor 0:eafc3fd41f75 148 */
bryantaylor 0:eafc3fd41f75 149 uint16_t getInterval(void) const {
bryantaylor 0:eafc3fd41f75 150 return ADVERTISEMENT_DURATION_UNITS_TO_MS(_interval);
bryantaylor 0:eafc3fd41f75 151 }
bryantaylor 0:eafc3fd41f75 152
bryantaylor 0:eafc3fd41f75 153 /**
bryantaylor 0:eafc3fd41f75 154 * Get the advertisement interval in units of 0.625ms.
bryantaylor 0:eafc3fd41f75 155 *
bryantaylor 0:eafc3fd41f75 156 * @return The advertisement interval in advertisement duration units (0.625ms units).
bryantaylor 0:eafc3fd41f75 157 */
bryantaylor 0:eafc3fd41f75 158 uint16_t getIntervalInADVUnits(void) const {
bryantaylor 0:eafc3fd41f75 159 return _interval;
bryantaylor 0:eafc3fd41f75 160 }
bryantaylor 0:eafc3fd41f75 161
bryantaylor 0:eafc3fd41f75 162 /**
bryantaylor 0:eafc3fd41f75 163 * Get The advertising timeout.
bryantaylor 0:eafc3fd41f75 164 *
bryantaylor 0:eafc3fd41f75 165 * @return The advertising timeout (in seconds).
bryantaylor 0:eafc3fd41f75 166 */
bryantaylor 0:eafc3fd41f75 167 uint16_t getTimeout(void) const {
bryantaylor 0:eafc3fd41f75 168 return _timeout;
bryantaylor 0:eafc3fd41f75 169 }
bryantaylor 0:eafc3fd41f75 170
bryantaylor 0:eafc3fd41f75 171 /**
bryantaylor 0:eafc3fd41f75 172 * Set the advertising type.
bryantaylor 0:eafc3fd41f75 173 *
bryantaylor 0:eafc3fd41f75 174 * @param[in] newAdvType
bryantaylor 0:eafc3fd41f75 175 * The new advertising type.
bryantaylor 0:eafc3fd41f75 176 */
bryantaylor 0:eafc3fd41f75 177 void setAdvertisingType(AdvertisingType_t newAdvType) {
bryantaylor 0:eafc3fd41f75 178 _advType = newAdvType;
bryantaylor 0:eafc3fd41f75 179 }
bryantaylor 0:eafc3fd41f75 180
bryantaylor 0:eafc3fd41f75 181 /**
bryantaylor 0:eafc3fd41f75 182 * Set the advertising interval in milliseconds.
bryantaylor 0:eafc3fd41f75 183 *
bryantaylor 0:eafc3fd41f75 184 * @param[in] newInterval
bryantaylor 0:eafc3fd41f75 185 * The new advertising interval in milliseconds.
bryantaylor 0:eafc3fd41f75 186 */
bryantaylor 0:eafc3fd41f75 187 void setInterval(uint16_t newInterval) {
bryantaylor 0:eafc3fd41f75 188 _interval = MSEC_TO_ADVERTISEMENT_DURATION_UNITS(newInterval);
bryantaylor 0:eafc3fd41f75 189 }
bryantaylor 0:eafc3fd41f75 190
bryantaylor 0:eafc3fd41f75 191 /**
bryantaylor 0:eafc3fd41f75 192 * Set the advertising timeout.
bryantaylor 0:eafc3fd41f75 193 *
bryantaylor 0:eafc3fd41f75 194 * @param[in] newTimeout
bryantaylor 0:eafc3fd41f75 195 * The new advertising timeout (in seconds).
bryantaylor 0:eafc3fd41f75 196 */
bryantaylor 0:eafc3fd41f75 197 void setTimeout(uint16_t newTimeout) {
bryantaylor 0:eafc3fd41f75 198 _timeout = newTimeout;
bryantaylor 0:eafc3fd41f75 199 }
bryantaylor 0:eafc3fd41f75 200
bryantaylor 0:eafc3fd41f75 201 private:
bryantaylor 0:eafc3fd41f75 202 AdvertisingType_t _advType; /**< The advertising type. */
bryantaylor 0:eafc3fd41f75 203 uint16_t _interval; /**< The advertising interval in ADV duration units (i.e. 0.625ms). */
bryantaylor 0:eafc3fd41f75 204 uint16_t _timeout; /**< The advertising timeout in seconds. */
bryantaylor 0:eafc3fd41f75 205 };
bryantaylor 0:eafc3fd41f75 206
bryantaylor 0:eafc3fd41f75 207 #endif /* ifndef __GAP_ADVERTISING_PARAMS_H__ */