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 Sep 16 11:13:37 2019 -0500
Revision:
8:5ea891c7d1a1
Parent:
7:9cd16581b578
Updated to version 2.0.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IanBenzMaxim 7:9cd16581b578 1 /*******************************************************************************
IanBenzMaxim 8:5ea891c7d1a1 2 * Copyright (C) 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 8:5ea891c7d1a1 33 #include "HexString.hpp"
IanBenzMaxim 7:9cd16581b578 34 #include "LoggingOneWireMaster.hpp"
IanBenzMaxim 7:9cd16581b578 35
IanBenzMaxim 7:9cd16581b578 36 using std::string;
IanBenzMaxim 7:9cd16581b578 37
IanBenzMaxim 7:9cd16581b578 38 namespace MaximInterfaceCore {
IanBenzMaxim 7:9cd16581b578 39
IanBenzMaxim 7:9cd16581b578 40 static const char strongLevelString[] = "<SP_ON>";
IanBenzMaxim 7:9cd16581b578 41
IanBenzMaxim 7:9cd16581b578 42 static string formatDataString(span<const uint_least8_t> data, bool read) {
IanBenzMaxim 7:9cd16581b578 43 string dataBuilder;
IanBenzMaxim 7:9cd16581b578 44 for (span<const uint_least8_t>::index_type i = 0; i < data.size(); ++i) {
IanBenzMaxim 7:9cd16581b578 45 if (read) {
IanBenzMaxim 7:9cd16581b578 46 dataBuilder.append(1, '[');
IanBenzMaxim 7:9cd16581b578 47 }
IanBenzMaxim 8:5ea891c7d1a1 48 dataBuilder.append(toHexString(data.subspan(i, 1)));
IanBenzMaxim 7:9cd16581b578 49 if (read) {
IanBenzMaxim 7:9cd16581b578 50 dataBuilder.append(1, ']');
IanBenzMaxim 7:9cd16581b578 51 }
IanBenzMaxim 7:9cd16581b578 52 dataBuilder.append(1, ' ');
IanBenzMaxim 7:9cd16581b578 53 }
IanBenzMaxim 7:9cd16581b578 54 return dataBuilder;
IanBenzMaxim 7:9cd16581b578 55 }
IanBenzMaxim 7:9cd16581b578 56
IanBenzMaxim 7:9cd16581b578 57 void LoggingOneWireMaster::tryWriteMessage(const std::string & message) {
IanBenzMaxim 7:9cd16581b578 58 if (writeMessage) {
IanBenzMaxim 7:9cd16581b578 59 writeMessage(message);
IanBenzMaxim 7:9cd16581b578 60 }
IanBenzMaxim 7:9cd16581b578 61 }
IanBenzMaxim 7:9cd16581b578 62
IanBenzMaxim 8:5ea891c7d1a1 63 Result<void> LoggingOneWireMaster::reset() {
IanBenzMaxim 8:5ea891c7d1a1 64 Result<void> result = OneWireMasterDecorator::reset();
IanBenzMaxim 8:5ea891c7d1a1 65 tryWriteMessage(result ? "RP" : "RN");
IanBenzMaxim 7:9cd16581b578 66 return result;
IanBenzMaxim 7:9cd16581b578 67 }
IanBenzMaxim 7:9cd16581b578 68
IanBenzMaxim 8:5ea891c7d1a1 69 Result<void> LoggingOneWireMaster::writeByteSetLevel(uint_least8_t sendByte,
IanBenzMaxim 8:5ea891c7d1a1 70 Level afterLevel) {
IanBenzMaxim 7:9cd16581b578 71 tryWriteMessage(formatDataString(make_span(&sendByte, 1), false));
IanBenzMaxim 7:9cd16581b578 72 if (afterLevel == StrongLevel) {
IanBenzMaxim 7:9cd16581b578 73 tryWriteMessage(strongLevelString);
IanBenzMaxim 7:9cd16581b578 74 }
IanBenzMaxim 7:9cd16581b578 75 return OneWireMasterDecorator::writeByteSetLevel(sendByte, afterLevel);
IanBenzMaxim 7:9cd16581b578 76 }
IanBenzMaxim 7:9cd16581b578 77
IanBenzMaxim 8:5ea891c7d1a1 78 Result<uint_least8_t> LoggingOneWireMaster::readByteSetLevel(Level afterLevel) {
IanBenzMaxim 8:5ea891c7d1a1 79 const Result<uint_least8_t> recvByte =
IanBenzMaxim 8:5ea891c7d1a1 80 OneWireMasterDecorator::readByteSetLevel(afterLevel);
IanBenzMaxim 8:5ea891c7d1a1 81 if (recvByte) {
IanBenzMaxim 8:5ea891c7d1a1 82 tryWriteMessage(formatDataString(make_span(&recvByte.value(), 1), true));
IanBenzMaxim 7:9cd16581b578 83 if (afterLevel == StrongLevel) {
IanBenzMaxim 7:9cd16581b578 84 tryWriteMessage(strongLevelString);
IanBenzMaxim 7:9cd16581b578 85 }
IanBenzMaxim 7:9cd16581b578 86 }
IanBenzMaxim 8:5ea891c7d1a1 87 return recvByte;
IanBenzMaxim 7:9cd16581b578 88 }
IanBenzMaxim 7:9cd16581b578 89
IanBenzMaxim 8:5ea891c7d1a1 90 Result<void>
IanBenzMaxim 8:5ea891c7d1a1 91 LoggingOneWireMaster::writeBlock(span<const uint_least8_t> sendBuf) {
IanBenzMaxim 7:9cd16581b578 92 tryWriteMessage(formatDataString(sendBuf, false));
IanBenzMaxim 7:9cd16581b578 93 return OneWireMasterDecorator::writeBlock(sendBuf);
IanBenzMaxim 7:9cd16581b578 94 }
IanBenzMaxim 7:9cd16581b578 95
IanBenzMaxim 8:5ea891c7d1a1 96 Result<void> LoggingOneWireMaster::readBlock(span<uint_least8_t> recvBuf) {
IanBenzMaxim 8:5ea891c7d1a1 97 Result<void> result = OneWireMasterDecorator::readBlock(recvBuf);
IanBenzMaxim 8:5ea891c7d1a1 98 if (result) {
IanBenzMaxim 7:9cd16581b578 99 tryWriteMessage(formatDataString(recvBuf, true));
IanBenzMaxim 7:9cd16581b578 100 }
IanBenzMaxim 7:9cd16581b578 101 return result;
IanBenzMaxim 7:9cd16581b578 102 }
IanBenzMaxim 7:9cd16581b578 103
IanBenzMaxim 8:5ea891c7d1a1 104 Result<void> LoggingOneWireMaster::setSpeed(Speed newSpeed) {
IanBenzMaxim 8:5ea891c7d1a1 105 Result<void> result = OneWireMasterDecorator::setSpeed(newSpeed);
IanBenzMaxim 8:5ea891c7d1a1 106 if (result) {
IanBenzMaxim 7:9cd16581b578 107 string newSpeedString;
IanBenzMaxim 7:9cd16581b578 108 switch (newSpeed) {
IanBenzMaxim 7:9cd16581b578 109 case StandardSpeed:
IanBenzMaxim 7:9cd16581b578 110 newSpeedString = "<STD>";
IanBenzMaxim 7:9cd16581b578 111 break;
IanBenzMaxim 7:9cd16581b578 112
IanBenzMaxim 7:9cd16581b578 113 case OverdriveSpeed:
IanBenzMaxim 7:9cd16581b578 114 newSpeedString = "<OVR>";
IanBenzMaxim 7:9cd16581b578 115 break;
IanBenzMaxim 7:9cd16581b578 116 }
IanBenzMaxim 7:9cd16581b578 117 if (!newSpeedString.empty()) {
IanBenzMaxim 7:9cd16581b578 118 tryWriteMessage(newSpeedString);
IanBenzMaxim 7:9cd16581b578 119 }
IanBenzMaxim 7:9cd16581b578 120 }
IanBenzMaxim 7:9cd16581b578 121 return result;
IanBenzMaxim 7:9cd16581b578 122 }
IanBenzMaxim 7:9cd16581b578 123
IanBenzMaxim 8:5ea891c7d1a1 124 Result<void> LoggingOneWireMaster::setLevel(Level newLevel) {
IanBenzMaxim 8:5ea891c7d1a1 125 Result<void> result = OneWireMasterDecorator::setLevel(newLevel);
IanBenzMaxim 8:5ea891c7d1a1 126 if (result) {
IanBenzMaxim 7:9cd16581b578 127 string newLevelString;
IanBenzMaxim 7:9cd16581b578 128 switch (newLevel) {
IanBenzMaxim 7:9cd16581b578 129 case NormalLevel:
IanBenzMaxim 7:9cd16581b578 130 newLevelString = "<SP_OFF>";
IanBenzMaxim 7:9cd16581b578 131 break;
IanBenzMaxim 7:9cd16581b578 132
IanBenzMaxim 7:9cd16581b578 133 case StrongLevel:
IanBenzMaxim 7:9cd16581b578 134 newLevelString = strongLevelString;
IanBenzMaxim 7:9cd16581b578 135 break;
IanBenzMaxim 7:9cd16581b578 136 }
IanBenzMaxim 7:9cd16581b578 137 if (!newLevelString.empty()) {
IanBenzMaxim 7:9cd16581b578 138 tryWriteMessage(newLevelString);
IanBenzMaxim 7:9cd16581b578 139 }
IanBenzMaxim 7:9cd16581b578 140 }
IanBenzMaxim 7:9cd16581b578 141 return result;
IanBenzMaxim 7:9cd16581b578 142 }
IanBenzMaxim 7:9cd16581b578 143
IanBenzMaxim 7:9cd16581b578 144 } // namespace MaximInterfaceCore