iOSのBLEコントローラアプリ「RCBController」と接続し、コントローラの操作を取得するサンプルプログラムです。 mbed HRM1017で動作を確認しています。

Dependencies:   BLE_API_Native_IRC BLE_API mbed

Committer:
jksoft
Date:
Wed Aug 20 13:30:11 2014 +0000
Revision:
2:dd85fdc18224
Parent:
1:48f6e08a3ac2
???

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:8c643bfe55b7 1 /**************************************************************************/
jksoft 0:8c643bfe55b7 2 /*!
jksoft 0:8c643bfe55b7 3 @file common.h
jksoft 0:8c643bfe55b7 4 @author hathach (tinyusb.org)
jksoft 0:8c643bfe55b7 5
jksoft 0:8c643bfe55b7 6 @section LICENSE
jksoft 0:8c643bfe55b7 7
jksoft 0:8c643bfe55b7 8 Software License Agreement (BSD License)
jksoft 0:8c643bfe55b7 9
jksoft 0:8c643bfe55b7 10 Copyright (c) 2013, K. Townsend (microBuilder.eu)
jksoft 0:8c643bfe55b7 11 All rights reserved.
jksoft 0:8c643bfe55b7 12
jksoft 0:8c643bfe55b7 13 Redistribution and use in source and binary forms, with or without
jksoft 0:8c643bfe55b7 14 modification, are permitted provided that the following conditions are met:
jksoft 0:8c643bfe55b7 15 1. Redistributions of source code must retain the above copyright
jksoft 0:8c643bfe55b7 16 notice, this list of conditions and the following disclaimer.
jksoft 0:8c643bfe55b7 17 2. Redistributions in binary form must reproduce the above copyright
jksoft 0:8c643bfe55b7 18 notice, this list of conditions and the following disclaimer in the
jksoft 0:8c643bfe55b7 19 documentation and/or other materials provided with the distribution.
jksoft 0:8c643bfe55b7 20 3. Neither the name of the copyright holders nor the
jksoft 0:8c643bfe55b7 21 names of its contributors may be used to endorse or promote products
jksoft 0:8c643bfe55b7 22 derived from this software without specific prior written permission.
jksoft 0:8c643bfe55b7 23
jksoft 0:8c643bfe55b7 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
jksoft 0:8c643bfe55b7 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
jksoft 0:8c643bfe55b7 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
jksoft 0:8c643bfe55b7 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
jksoft 0:8c643bfe55b7 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
jksoft 0:8c643bfe55b7 29 INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
jksoft 0:8c643bfe55b7 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND
jksoft 0:8c643bfe55b7 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
jksoft 0:8c643bfe55b7 32 INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
jksoft 0:8c643bfe55b7 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jksoft 0:8c643bfe55b7 34 */
jksoft 0:8c643bfe55b7 35 /**************************************************************************/
jksoft 0:8c643bfe55b7 36
jksoft 0:8c643bfe55b7 37 /** \defgroup Group_Common Common Files
jksoft 0:8c643bfe55b7 38 * @{
jksoft 0:8c643bfe55b7 39 *
jksoft 0:8c643bfe55b7 40 * \defgroup Group_CommonH common.h
jksoft 0:8c643bfe55b7 41 *
jksoft 0:8c643bfe55b7 42 * @{
jksoft 0:8c643bfe55b7 43 */
jksoft 0:8c643bfe55b7 44
jksoft 0:8c643bfe55b7 45 #ifndef _COMMON_H_
jksoft 0:8c643bfe55b7 46 #define _COMMON_H_
jksoft 0:8c643bfe55b7 47
jksoft 0:8c643bfe55b7 48 #ifdef __cplusplus
jksoft 0:8c643bfe55b7 49 extern "C" {
jksoft 0:8c643bfe55b7 50 #endif
jksoft 0:8c643bfe55b7 51
jksoft 0:8c643bfe55b7 52 //--------------------------------------------------------------------+
jksoft 0:8c643bfe55b7 53 // INCLUDES
jksoft 0:8c643bfe55b7 54 //--------------------------------------------------------------------+
jksoft 0:8c643bfe55b7 55
jksoft 0:8c643bfe55b7 56 //------------- Standard Header -------------//
jksoft 0:8c643bfe55b7 57 #include <stdint.h>
jksoft 0:8c643bfe55b7 58 #include <stdbool.h>
jksoft 0:8c643bfe55b7 59 #include <stddef.h>
jksoft 0:8c643bfe55b7 60 #include <string.h>
jksoft 0:8c643bfe55b7 61 #include <stdio.h>
jksoft 0:8c643bfe55b7 62
jksoft 0:8c643bfe55b7 63 //------------- General Header -------------//
jksoft 0:8c643bfe55b7 64 #include "projectconfig.h"
jksoft 0:8c643bfe55b7 65 #include "compiler.h"
jksoft 0:8c643bfe55b7 66 #include "assertion.h"
jksoft 0:8c643bfe55b7 67 #include "binary.h"
jksoft 0:8c643bfe55b7 68 #include "ble_error.h"
jksoft 0:8c643bfe55b7 69
jksoft 0:8c643bfe55b7 70 //------------- MCU header -------------//
jksoft 0:8c643bfe55b7 71 //#include "nrf.h"
jksoft 0:8c643bfe55b7 72
jksoft 0:8c643bfe55b7 73 //--------------------------------------------------------------------+
jksoft 0:8c643bfe55b7 74 // TYPEDEFS
jksoft 0:8c643bfe55b7 75 //--------------------------------------------------------------------+
jksoft 0:8c643bfe55b7 76 typedef unsigned char byte_t;
jksoft 0:8c643bfe55b7 77 typedef float float32_t;
jksoft 0:8c643bfe55b7 78 typedef double float64_t;
jksoft 0:8c643bfe55b7 79
jksoft 0:8c643bfe55b7 80 //--------------------------------------------------------------------+
jksoft 0:8c643bfe55b7 81 // MACROS
jksoft 0:8c643bfe55b7 82 //--------------------------------------------------------------------+
jksoft 0:8c643bfe55b7 83 #define STRING_(x) #x // stringify without expand
jksoft 0:8c643bfe55b7 84 #define XSTRING_(x) STRING_(x) // expand then stringify
jksoft 0:8c643bfe55b7 85 #define STRING_CONCAT_(a, b) a##b // concat without expand
jksoft 0:8c643bfe55b7 86 #define XSTRING_CONCAT_(a, b) STRING_CONCAT_(a, b) // expand then concat
jksoft 0:8c643bfe55b7 87
jksoft 0:8c643bfe55b7 88 #define U16_HIGH_U8(u16) ((uint8_t) (((u16) >> 8) & 0x00ff))
jksoft 0:8c643bfe55b7 89 #define U16_LOW_U8(u16) ((uint8_t) ((u16) & 0x00ff))
jksoft 0:8c643bfe55b7 90 #define U16_TO_U8S_BE(u16) U16_HIGH_U8(u16), U16_LOW_U8(u16)
jksoft 0:8c643bfe55b7 91 #define U16_TO_U8S_LE(u16) U16_LOW_U8(u16), U16_HIGH_U8(u16)
jksoft 0:8c643bfe55b7 92
jksoft 0:8c643bfe55b7 93 #define U32_B1_U8(u32) ((uint8_t) (((u32) >> 24) & 0x000000ff)) // MSB
jksoft 0:8c643bfe55b7 94 #define U32_B2_U8(u32) ((uint8_t) (((u32) >> 16) & 0x000000ff))
jksoft 0:8c643bfe55b7 95 #define U32_B3_U8(u32) ((uint8_t) (((u32) >> 8) & 0x000000ff))
jksoft 0:8c643bfe55b7 96 #define U32_B4_U8(u32) ((uint8_t) ((u32) & 0x000000ff)) // LSB
jksoft 0:8c643bfe55b7 97
jksoft 0:8c643bfe55b7 98 #define U32_TO_U8S_BE(u32) U32_B1_U8(u32), U32_B2_U8(u32), U32_B3_U8(u32), U32_B4_U8(u32)
jksoft 0:8c643bfe55b7 99 #define U32_TO_U8S_LE(u32) U32_B4_U8(u32), U32_B3_U8(u32), U32_B2_U8(u32), U32_B1_U8(u32)
jksoft 0:8c643bfe55b7 100
jksoft 0:8c643bfe55b7 101 //--------------------------------------------------------------------+
jksoft 0:8c643bfe55b7 102 // INLINE FUNCTION
jksoft 0:8c643bfe55b7 103 //--------------------------------------------------------------------+
jksoft 0:8c643bfe55b7 104 #define memclr_(buffer, size) memset(buffer, 0, size)
jksoft 0:8c643bfe55b7 105
jksoft 0:8c643bfe55b7 106 //------------- Conversion -------------//
jksoft 0:8c643bfe55b7 107 /// form an uint32_t from 4 x uint8_t
jksoft 0:8c643bfe55b7 108 static inline uint32_t u32_from_u8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4) ATTR_ALWAYS_INLINE ATTR_CONST;
jksoft 0:8c643bfe55b7 109 static inline uint32_t u32_from_u8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4)
jksoft 0:8c643bfe55b7 110 {
jksoft 0:8c643bfe55b7 111 return (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
jksoft 0:8c643bfe55b7 112 }
jksoft 0:8c643bfe55b7 113
jksoft 0:8c643bfe55b7 114 static inline uint8_t u16_high_u8(uint16_t u16) ATTR_CONST ATTR_ALWAYS_INLINE;
jksoft 0:8c643bfe55b7 115 static inline uint8_t u16_high_u8(uint16_t u16)
jksoft 0:8c643bfe55b7 116 {
jksoft 0:8c643bfe55b7 117 return (uint8_t) ((u16 >> 8) & 0x00ff);
jksoft 0:8c643bfe55b7 118 }
jksoft 0:8c643bfe55b7 119
jksoft 0:8c643bfe55b7 120 static inline uint8_t u16_low_u8(uint16_t u16) ATTR_CONST ATTR_ALWAYS_INLINE;
jksoft 0:8c643bfe55b7 121 static inline uint8_t u16_low_u8(uint16_t u16)
jksoft 0:8c643bfe55b7 122 {
jksoft 0:8c643bfe55b7 123 return (uint8_t) (u16 & 0x00ff);
jksoft 0:8c643bfe55b7 124 }
jksoft 0:8c643bfe55b7 125
jksoft 0:8c643bfe55b7 126 //------------- Min -------------//
jksoft 0:8c643bfe55b7 127 static inline uint8_t min8_of(uint8_t x, uint8_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
jksoft 0:8c643bfe55b7 128 static inline uint8_t min8_of(uint8_t x, uint8_t y)
jksoft 0:8c643bfe55b7 129 {
jksoft 0:8c643bfe55b7 130 return (x < y) ? x : y;
jksoft 0:8c643bfe55b7 131 }
jksoft 0:8c643bfe55b7 132
jksoft 0:8c643bfe55b7 133 static inline uint16_t min16_of(uint16_t x, uint16_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
jksoft 0:8c643bfe55b7 134 static inline uint16_t min16_of(uint16_t x, uint16_t y)
jksoft 0:8c643bfe55b7 135 {
jksoft 0:8c643bfe55b7 136 return (x < y) ? x : y;
jksoft 0:8c643bfe55b7 137 }
jksoft 0:8c643bfe55b7 138
jksoft 0:8c643bfe55b7 139 static inline uint32_t min32_of(uint32_t x, uint32_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
jksoft 0:8c643bfe55b7 140 static inline uint32_t min32_of(uint32_t x, uint32_t y)
jksoft 0:8c643bfe55b7 141 {
jksoft 0:8c643bfe55b7 142 return (x < y) ? x : y;
jksoft 0:8c643bfe55b7 143 }
jksoft 0:8c643bfe55b7 144
jksoft 0:8c643bfe55b7 145 //------------- Max -------------//
jksoft 0:8c643bfe55b7 146 static inline uint32_t max32_of(uint32_t x, uint32_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
jksoft 0:8c643bfe55b7 147 static inline uint32_t max32_of(uint32_t x, uint32_t y)
jksoft 0:8c643bfe55b7 148 {
jksoft 0:8c643bfe55b7 149 return (x > y) ? x : y;
jksoft 0:8c643bfe55b7 150 }
jksoft 0:8c643bfe55b7 151
jksoft 0:8c643bfe55b7 152 //------------- Align -------------//
jksoft 0:8c643bfe55b7 153 static inline uint32_t align32 (uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
jksoft 0:8c643bfe55b7 154 static inline uint32_t align32 (uint32_t value)
jksoft 0:8c643bfe55b7 155 {
jksoft 0:8c643bfe55b7 156 return (value & 0xFFFFFFE0UL);
jksoft 0:8c643bfe55b7 157 }
jksoft 0:8c643bfe55b7 158
jksoft 0:8c643bfe55b7 159 static inline uint32_t align16 (uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
jksoft 0:8c643bfe55b7 160 static inline uint32_t align16 (uint32_t value)
jksoft 0:8c643bfe55b7 161 {
jksoft 0:8c643bfe55b7 162 return (value & 0xFFFFFFF0UL);
jksoft 0:8c643bfe55b7 163 }
jksoft 0:8c643bfe55b7 164
jksoft 0:8c643bfe55b7 165 static inline uint32_t align_n (uint32_t alignment, uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
jksoft 0:8c643bfe55b7 166 static inline uint32_t align_n (uint32_t alignment, uint32_t value)
jksoft 0:8c643bfe55b7 167 {
jksoft 0:8c643bfe55b7 168 return value & (~(alignment-1));
jksoft 0:8c643bfe55b7 169 }
jksoft 0:8c643bfe55b7 170
jksoft 0:8c643bfe55b7 171 static inline uint32_t align4k (uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
jksoft 0:8c643bfe55b7 172 static inline uint32_t align4k (uint32_t value)
jksoft 0:8c643bfe55b7 173 {
jksoft 0:8c643bfe55b7 174 return (value & 0xFFFFF000UL);
jksoft 0:8c643bfe55b7 175 }
jksoft 0:8c643bfe55b7 176
jksoft 0:8c643bfe55b7 177 static inline uint32_t offset4k(uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
jksoft 0:8c643bfe55b7 178 static inline uint32_t offset4k(uint32_t value)
jksoft 0:8c643bfe55b7 179 {
jksoft 0:8c643bfe55b7 180 return (value & 0xFFFUL);
jksoft 0:8c643bfe55b7 181 }
jksoft 0:8c643bfe55b7 182
jksoft 0:8c643bfe55b7 183 //------------- Mathematics -------------//
jksoft 0:8c643bfe55b7 184 /// inclusive range checking
jksoft 0:8c643bfe55b7 185 static inline bool is_in_range(uint32_t lower, uint32_t value, uint32_t upper) ATTR_ALWAYS_INLINE ATTR_CONST;
jksoft 0:8c643bfe55b7 186 static inline bool is_in_range(uint32_t lower, uint32_t value, uint32_t upper)
jksoft 0:8c643bfe55b7 187 {
jksoft 0:8c643bfe55b7 188 return (lower <= value) && (value <= upper);
jksoft 0:8c643bfe55b7 189 }
jksoft 0:8c643bfe55b7 190
jksoft 0:8c643bfe55b7 191 /// exclusive range checking
jksoft 0:8c643bfe55b7 192 static inline bool is_in_range_exclusive(uint32_t lower, uint32_t value, uint32_t upper) ATTR_ALWAYS_INLINE ATTR_CONST;
jksoft 0:8c643bfe55b7 193 static inline bool is_in_range_exclusive(uint32_t lower, uint32_t value, uint32_t upper)
jksoft 0:8c643bfe55b7 194 {
jksoft 0:8c643bfe55b7 195 return (lower < value) && (value < upper);
jksoft 0:8c643bfe55b7 196 }
jksoft 0:8c643bfe55b7 197
jksoft 0:8c643bfe55b7 198 static inline uint8_t log2_of(uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
jksoft 0:8c643bfe55b7 199 static inline uint8_t log2_of(uint32_t value)
jksoft 0:8c643bfe55b7 200 {
jksoft 0:8c643bfe55b7 201 uint8_t result = 0; // log2 of a value is its MSB's position
jksoft 0:8c643bfe55b7 202
jksoft 0:8c643bfe55b7 203 while (value >>= 1)
jksoft 0:8c643bfe55b7 204 {
jksoft 0:8c643bfe55b7 205 result++;
jksoft 0:8c643bfe55b7 206 }
jksoft 0:8c643bfe55b7 207 return result;
jksoft 0:8c643bfe55b7 208 }
jksoft 0:8c643bfe55b7 209
jksoft 0:8c643bfe55b7 210 // return the number of set bits in value
jksoft 0:8c643bfe55b7 211 static inline uint8_t cardinality_of(uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
jksoft 0:8c643bfe55b7 212 static inline uint8_t cardinality_of(uint32_t value)
jksoft 0:8c643bfe55b7 213 {
jksoft 0:8c643bfe55b7 214 // Brian Kernighan's method goes through as many iterations as there are set bits. So if we have a 32-bit word with only
jksoft 0:8c643bfe55b7 215 // the high bit set, then it will only go once through the loop
jksoft 0:8c643bfe55b7 216 // Published in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan and Dennis M. Ritchie)
jksoft 0:8c643bfe55b7 217 // mentions this in exercise 2-9. On April 19, 2006 Don Knuth pointed out to me that this method
jksoft 0:8c643bfe55b7 218 // "was first published by Peter Wegner in CACM 3 (1960), 322. (Also discovered independently by Derrick Lehmer and
jksoft 0:8c643bfe55b7 219 // published in 1964 in a book edited by Beckenbach.)"
jksoft 0:8c643bfe55b7 220 uint8_t count;
jksoft 0:8c643bfe55b7 221 for (count = 0; value; count++)
jksoft 0:8c643bfe55b7 222 {
jksoft 0:8c643bfe55b7 223 value &= value - 1; // clear the least significant bit set
jksoft 0:8c643bfe55b7 224 }
jksoft 0:8c643bfe55b7 225
jksoft 0:8c643bfe55b7 226 return count;
jksoft 0:8c643bfe55b7 227 }
jksoft 0:8c643bfe55b7 228
jksoft 0:8c643bfe55b7 229 #ifdef __cplusplus
jksoft 0:8c643bfe55b7 230 }
jksoft 0:8c643bfe55b7 231 #endif
jksoft 0:8c643bfe55b7 232
jksoft 0:8c643bfe55b7 233 #endif /* _COMMON_H_ */
jksoft 0:8c643bfe55b7 234
jksoft 0:8c643bfe55b7 235 /** @} */
jksoft 0:8c643bfe55b7 236 /** @} */