Device interface library for multiple platforms including Mbed.

Dependents:   DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#

Maxim Interface is a library framework focused on providing flexible and expressive hardware interfaces. Both communication interfaces such as I2C and 1-Wire and device interfaces such as DS18B20 are supported. Modern C++ concepts are used extensively while keeping compatibility with C++98/C++03 and requiring no external dependencies. The embedded-friendly design does not depend on exceptions or RTTI.

The full version of the project is hosted on GitLab: https://gitlab.com/iabenz/MaximInterface

Committer:
IanBenzMaxim
Date:
Mon Jul 22 11:44:07 2019 -0500
Revision:
7:9cd16581b578
Updated to version 1.9.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IanBenzMaxim 7:9cd16581b578 1 /*******************************************************************************
IanBenzMaxim 7:9cd16581b578 2 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
IanBenzMaxim 7:9cd16581b578 3 *
IanBenzMaxim 7:9cd16581b578 4 * Permission is hereby granted, free of charge, to any person obtaining a
IanBenzMaxim 7:9cd16581b578 5 * copy of this software and associated documentation files (the "Software"),
IanBenzMaxim 7:9cd16581b578 6 * to deal in the Software without restriction, including without limitation
IanBenzMaxim 7:9cd16581b578 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
IanBenzMaxim 7:9cd16581b578 8 * and/or sell copies of the Software, and to permit persons to whom the
IanBenzMaxim 7:9cd16581b578 9 * Software is furnished to do so, subject to the following conditions:
IanBenzMaxim 7:9cd16581b578 10 *
IanBenzMaxim 7:9cd16581b578 11 * The above copyright notice and this permission notice shall be included
IanBenzMaxim 7:9cd16581b578 12 * in all copies or substantial portions of the Software.
IanBenzMaxim 7:9cd16581b578 13 *
IanBenzMaxim 7:9cd16581b578 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
IanBenzMaxim 7:9cd16581b578 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
IanBenzMaxim 7:9cd16581b578 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IanBenzMaxim 7:9cd16581b578 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
IanBenzMaxim 7:9cd16581b578 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
IanBenzMaxim 7:9cd16581b578 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
IanBenzMaxim 7:9cd16581b578 20 * OTHER DEALINGS IN THE SOFTWARE.
IanBenzMaxim 7:9cd16581b578 21 *
IanBenzMaxim 7:9cd16581b578 22 * Except as contained in this notice, the name of Maxim Integrated
IanBenzMaxim 7:9cd16581b578 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
IanBenzMaxim 7:9cd16581b578 24 * Products, Inc. Branding Policy.
IanBenzMaxim 7:9cd16581b578 25 *
IanBenzMaxim 7:9cd16581b578 26 * The mere transfer of this software does not imply any licenses
IanBenzMaxim 7:9cd16581b578 27 * of trade secrets, proprietary technology, copyrights, patents,
IanBenzMaxim 7:9cd16581b578 28 * trademarks, maskwork rights, or any other form of intellectual
IanBenzMaxim 7:9cd16581b578 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
IanBenzMaxim 7:9cd16581b578 30 * ownership rights.
IanBenzMaxim 7:9cd16581b578 31 *******************************************************************************/
IanBenzMaxim 7:9cd16581b578 32
IanBenzMaxim 7:9cd16581b578 33 #ifndef MaximInterfaceCore_Segment
IanBenzMaxim 7:9cd16581b578 34 #define MaximInterfaceCore_Segment
IanBenzMaxim 7:9cd16581b578 35
IanBenzMaxim 7:9cd16581b578 36 #include <iterator>
IanBenzMaxim 7:9cd16581b578 37 #include <utility>
IanBenzMaxim 7:9cd16581b578 38 #include "type_traits.hpp"
IanBenzMaxim 7:9cd16581b578 39
IanBenzMaxim 7:9cd16581b578 40 namespace MaximInterfaceCore {
IanBenzMaxim 7:9cd16581b578 41
IanBenzMaxim 7:9cd16581b578 42 /// @brief
IanBenzMaxim 7:9cd16581b578 43 /// Advances a given iterator by a given number of elements with bounds checking.
IanBenzMaxim 7:9cd16581b578 44 /// @tparam InputIt Must meet the requirements of InputIterator.
IanBenzMaxim 7:9cd16581b578 45 /// @param[in,out] it Iterator to advance.
IanBenzMaxim 7:9cd16581b578 46 /// @param bound
IanBenzMaxim 7:9cd16581b578 47 /// Past-the-end boundary iterator. If distance is positive, bound must be
IanBenzMaxim 7:9cd16581b578 48 /// reachable by incrementing the given iterator. If distance is negative, bound
IanBenzMaxim 7:9cd16581b578 49 /// must be reachable by decrementing the given iterator.
IanBenzMaxim 7:9cd16581b578 50 /// @param distance
IanBenzMaxim 7:9cd16581b578 51 /// Number of elements to advance the given iterator. If distance is positive,
IanBenzMaxim 7:9cd16581b578 52 /// the given iterator is incremented. If distance is negative, the given
IanBenzMaxim 7:9cd16581b578 53 /// iterator is decremented, and InputIt must meet the requirements of
IanBenzMaxim 7:9cd16581b578 54 /// BidirectionalIterator.
IanBenzMaxim 7:9cd16581b578 55 /// @returns The number of elements that the given iterator was advanced.
IanBenzMaxim 7:9cd16581b578 56 template <typename InputIt>
IanBenzMaxim 7:9cd16581b578 57 typename std::iterator_traits<InputIt>::difference_type checkedAdvance(
IanBenzMaxim 7:9cd16581b578 58 InputIt & it, const InputIt bound,
IanBenzMaxim 7:9cd16581b578 59 typename std::iterator_traits<InputIt>::difference_type distance) {
IanBenzMaxim 7:9cd16581b578 60 typedef
IanBenzMaxim 7:9cd16581b578 61 typename std::iterator_traits<InputIt>::difference_type difference_type;
IanBenzMaxim 7:9cd16581b578 62
IanBenzMaxim 7:9cd16581b578 63 // Use constant-time operations if InputIt is a random access iterator.
IanBenzMaxim 7:9cd16581b578 64 if (is_same<typename std::iterator_traits<InputIt>::iterator_category,
IanBenzMaxim 7:9cd16581b578 65 std::random_access_iterator_tag>::value) {
IanBenzMaxim 7:9cd16581b578 66 const difference_type boundDistance = std::distance(it, bound);
IanBenzMaxim 7:9cd16581b578 67 if (boundDistance >= 0) {
IanBenzMaxim 7:9cd16581b578 68 if (distance > boundDistance) {
IanBenzMaxim 7:9cd16581b578 69 distance = boundDistance;
IanBenzMaxim 7:9cd16581b578 70 } else if (distance < 0) {
IanBenzMaxim 7:9cd16581b578 71 distance = 0;
IanBenzMaxim 7:9cd16581b578 72 }
IanBenzMaxim 7:9cd16581b578 73 } else {
IanBenzMaxim 7:9cd16581b578 74 if (distance < boundDistance) {
IanBenzMaxim 7:9cd16581b578 75 distance = boundDistance;
IanBenzMaxim 7:9cd16581b578 76 } else if (distance > 0) {
IanBenzMaxim 7:9cd16581b578 77 distance = 0;
IanBenzMaxim 7:9cd16581b578 78 }
IanBenzMaxim 7:9cd16581b578 79 }
IanBenzMaxim 7:9cd16581b578 80 std::advance(it, distance);
IanBenzMaxim 7:9cd16581b578 81 } else {
IanBenzMaxim 7:9cd16581b578 82 const difference_type startingDistance = distance;
IanBenzMaxim 7:9cd16581b578 83 while (distance != 0 && it != bound) {
IanBenzMaxim 7:9cd16581b578 84 if (distance > 0) {
IanBenzMaxim 7:9cd16581b578 85 ++it;
IanBenzMaxim 7:9cd16581b578 86 --distance;
IanBenzMaxim 7:9cd16581b578 87 } else {
IanBenzMaxim 7:9cd16581b578 88 --it;
IanBenzMaxim 7:9cd16581b578 89 ++distance;
IanBenzMaxim 7:9cd16581b578 90 }
IanBenzMaxim 7:9cd16581b578 91 }
IanBenzMaxim 7:9cd16581b578 92 if (startingDistance > 0) {
IanBenzMaxim 7:9cd16581b578 93 distance = startingDistance - distance;
IanBenzMaxim 7:9cd16581b578 94 } else {
IanBenzMaxim 7:9cd16581b578 95 distance = startingDistance + distance;
IanBenzMaxim 7:9cd16581b578 96 }
IanBenzMaxim 7:9cd16581b578 97 }
IanBenzMaxim 7:9cd16581b578 98 return distance;
IanBenzMaxim 7:9cd16581b578 99 }
IanBenzMaxim 7:9cd16581b578 100
IanBenzMaxim 7:9cd16581b578 101 /// @brief Locates an iterator sub-range using segment number addressing.
IanBenzMaxim 7:9cd16581b578 102 /// @details
IanBenzMaxim 7:9cd16581b578 103 /// Useful for devices that divide the memory space into uniform chunks such as
IanBenzMaxim 7:9cd16581b578 104 /// pages and segments.
IanBenzMaxim 7:9cd16581b578 105 /// @tparam ForwardIt Must meet the requirements of ForwardIterator.
IanBenzMaxim 7:9cd16581b578 106 /// @param begin Beginning of the input data range.
IanBenzMaxim 7:9cd16581b578 107 /// @param end End of the input data range.
IanBenzMaxim 7:9cd16581b578 108 /// @param segmentSize Number of elements contained in a segment.
IanBenzMaxim 7:9cd16581b578 109 /// @param segmentNum Zero-indexed number of the desired segment.
IanBenzMaxim 7:9cd16581b578 110 /// @returns
IanBenzMaxim 7:9cd16581b578 111 /// Pair of iterators representing the sub-range of the segment within
IanBenzMaxim 7:9cd16581b578 112 /// the input range. If the segment does not exist within the input range, both
IanBenzMaxim 7:9cd16581b578 113 /// iterators in the pair are set to the end iterator of the input range.
IanBenzMaxim 7:9cd16581b578 114 template <typename ForwardIt, typename Index>
IanBenzMaxim 7:9cd16581b578 115 std::pair<ForwardIt, ForwardIt> createSegment(
IanBenzMaxim 7:9cd16581b578 116 ForwardIt begin, const ForwardIt end,
IanBenzMaxim 7:9cd16581b578 117 const typename std::iterator_traits<ForwardIt>::difference_type segmentSize,
IanBenzMaxim 7:9cd16581b578 118 Index segmentNum) {
IanBenzMaxim 7:9cd16581b578 119 ForwardIt segmentEnd = begin;
IanBenzMaxim 7:9cd16581b578 120 typename std::iterator_traits<ForwardIt>::difference_type lastSegmentSize =
IanBenzMaxim 7:9cd16581b578 121 checkedAdvance(segmentEnd, end, segmentSize);
IanBenzMaxim 7:9cd16581b578 122 while (segmentNum > 0 && segmentEnd != end) {
IanBenzMaxim 7:9cd16581b578 123 begin = segmentEnd;
IanBenzMaxim 7:9cd16581b578 124 lastSegmentSize = checkedAdvance(segmentEnd, end, segmentSize);
IanBenzMaxim 7:9cd16581b578 125 --segmentNum;
IanBenzMaxim 7:9cd16581b578 126 }
IanBenzMaxim 7:9cd16581b578 127 if (segmentNum > 0 || lastSegmentSize != segmentSize) {
IanBenzMaxim 7:9cd16581b578 128 begin = segmentEnd;
IanBenzMaxim 7:9cd16581b578 129 }
IanBenzMaxim 7:9cd16581b578 130 return std::make_pair(begin, segmentEnd);
IanBenzMaxim 7:9cd16581b578 131 }
IanBenzMaxim 7:9cd16581b578 132
IanBenzMaxim 7:9cd16581b578 133 } // namespace MaximInterfaceCore
IanBenzMaxim 7:9cd16581b578 134
IanBenzMaxim 7:9cd16581b578 135 #endif