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
MaximInterfaceCore/RunCommand.hpp@12:7eb41621ba22, 2020-05-29 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Fri May 29 16:19:22 2020 -0500
- Revision:
- 12:7eb41621ba22
- Parent:
- 8:5ea891c7d1a1
Updated to version 2.2.
Who changed what in which revision?
User | Revision | Line number | New 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 | #ifndef MaximInterfaceCore_RunCommand_hpp |
IanBenzMaxim | 8:5ea891c7d1a1 | 34 | #define MaximInterfaceCore_RunCommand_hpp |
IanBenzMaxim | 7:9cd16581b578 | 35 | |
IanBenzMaxim | 7:9cd16581b578 | 36 | #include "Config.hpp" |
IanBenzMaxim | 7:9cd16581b578 | 37 | #include "Function.hpp" |
IanBenzMaxim | 7:9cd16581b578 | 38 | #include "SelectRom.hpp" |
IanBenzMaxim | 7:9cd16581b578 | 39 | #include "span.hpp" |
IanBenzMaxim | 7:9cd16581b578 | 40 | #include "system_error.hpp" |
IanBenzMaxim | 7:9cd16581b578 | 41 | |
IanBenzMaxim | 7:9cd16581b578 | 42 | namespace MaximInterfaceCore { |
IanBenzMaxim | 7:9cd16581b578 | 43 | |
IanBenzMaxim | 7:9cd16581b578 | 44 | class I2CMaster; |
IanBenzMaxim | 7:9cd16581b578 | 45 | class OneWireMaster; |
IanBenzMaxim | 7:9cd16581b578 | 46 | class Sleep; |
IanBenzMaxim | 7:9cd16581b578 | 47 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 48 | /// @brief |
IanBenzMaxim | 8:5ea891c7d1a1 | 49 | /// Runs a command sequence by writing the command request, waiting the |
IanBenzMaxim | 8:5ea891c7d1a1 | 50 | /// specified amount of time, and reading back the command response. |
IanBenzMaxim | 8:5ea891c7d1a1 | 51 | /// @details |
IanBenzMaxim | 8:5ea891c7d1a1 | 52 | /// The parameters for this function are the command request, the delay time in |
IanBenzMaxim | 8:5ea891c7d1a1 | 53 | /// milliseconds, and a mutable buffer for the command response. The actual |
IanBenzMaxim | 8:5ea891c7d1a1 | 54 | /// buffer used for the command response is returned. |
IanBenzMaxim | 8:5ea891c7d1a1 | 55 | typedef Function<Result<span<uint_least8_t> >(span<const uint_least8_t>, int, |
IanBenzMaxim | 8:5ea891c7d1a1 | 56 | span<uint_least8_t>)> |
IanBenzMaxim | 7:9cd16581b578 | 57 | RunCommand; |
IanBenzMaxim | 7:9cd16581b578 | 58 | |
IanBenzMaxim | 7:9cd16581b578 | 59 | class RunCommandWithOneWireMaster { |
IanBenzMaxim | 7:9cd16581b578 | 60 | public: |
IanBenzMaxim | 7:9cd16581b578 | 61 | typedef RunCommand::result_type result_type; |
IanBenzMaxim | 7:9cd16581b578 | 62 | |
IanBenzMaxim | 7:9cd16581b578 | 63 | enum ErrorValue { CrcError = 1, InvalidResponseError }; |
IanBenzMaxim | 7:9cd16581b578 | 64 | MaximInterfaceCore_EXPORT static const error_category & errorCategory(); |
IanBenzMaxim | 7:9cd16581b578 | 65 | |
IanBenzMaxim | 7:9cd16581b578 | 66 | RunCommandWithOneWireMaster(Sleep & sleep, OneWireMaster & master, |
IanBenzMaxim | 7:9cd16581b578 | 67 | const SelectRom & selectRom) |
IanBenzMaxim | 7:9cd16581b578 | 68 | : selectRom(selectRom), master(&master), sleep(&sleep) {} |
IanBenzMaxim | 7:9cd16581b578 | 69 | |
IanBenzMaxim | 7:9cd16581b578 | 70 | void setSleep(Sleep & sleep) { this->sleep = &sleep; } |
IanBenzMaxim | 7:9cd16581b578 | 71 | |
IanBenzMaxim | 7:9cd16581b578 | 72 | void setMaster(OneWireMaster & master) { this->master = &master; } |
IanBenzMaxim | 7:9cd16581b578 | 73 | |
IanBenzMaxim | 7:9cd16581b578 | 74 | void setSelectRom(const SelectRom & selectRom) { |
IanBenzMaxim | 7:9cd16581b578 | 75 | this->selectRom = selectRom; |
IanBenzMaxim | 7:9cd16581b578 | 76 | } |
IanBenzMaxim | 7:9cd16581b578 | 77 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 78 | MaximInterfaceCore_EXPORT Result<span<uint_least8_t> > |
IanBenzMaxim | 7:9cd16581b578 | 79 | operator()(span<const uint_least8_t> request, int delayTime, |
IanBenzMaxim | 8:5ea891c7d1a1 | 80 | span<uint_least8_t> response) const; |
IanBenzMaxim | 7:9cd16581b578 | 81 | |
IanBenzMaxim | 7:9cd16581b578 | 82 | private: |
IanBenzMaxim | 7:9cd16581b578 | 83 | SelectRom selectRom; |
IanBenzMaxim | 7:9cd16581b578 | 84 | OneWireMaster * master; |
IanBenzMaxim | 7:9cd16581b578 | 85 | const Sleep * sleep; |
IanBenzMaxim | 7:9cd16581b578 | 86 | }; |
IanBenzMaxim | 7:9cd16581b578 | 87 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 88 | template <> |
IanBenzMaxim | 8:5ea891c7d1a1 | 89 | struct is_error_code_enum<RunCommandWithOneWireMaster::ErrorValue> |
IanBenzMaxim | 8:5ea891c7d1a1 | 90 | : true_type {}; |
IanBenzMaxim | 8:5ea891c7d1a1 | 91 | |
IanBenzMaxim | 7:9cd16581b578 | 92 | inline error_code make_error_code(RunCommandWithOneWireMaster::ErrorValue e) { |
IanBenzMaxim | 7:9cd16581b578 | 93 | return error_code(e, RunCommandWithOneWireMaster::errorCategory()); |
IanBenzMaxim | 7:9cd16581b578 | 94 | } |
IanBenzMaxim | 7:9cd16581b578 | 95 | |
IanBenzMaxim | 7:9cd16581b578 | 96 | class RunCommandWithI2CMaster { |
IanBenzMaxim | 7:9cd16581b578 | 97 | public: |
IanBenzMaxim | 7:9cd16581b578 | 98 | typedef RunCommand::result_type result_type; |
IanBenzMaxim | 7:9cd16581b578 | 99 | |
IanBenzMaxim | 7:9cd16581b578 | 100 | enum ErrorValue { InvalidResponseError = 1 }; |
IanBenzMaxim | 7:9cd16581b578 | 101 | MaximInterfaceCore_EXPORT static const error_category & errorCategory(); |
IanBenzMaxim | 7:9cd16581b578 | 102 | |
IanBenzMaxim | 7:9cd16581b578 | 103 | RunCommandWithI2CMaster(Sleep & sleep, I2CMaster & master, |
IanBenzMaxim | 7:9cd16581b578 | 104 | uint_least8_t address) |
IanBenzMaxim | 7:9cd16581b578 | 105 | : sleep(&sleep), master(&master), address_(address & 0xFE) {} |
IanBenzMaxim | 7:9cd16581b578 | 106 | |
IanBenzMaxim | 7:9cd16581b578 | 107 | void setSleep(Sleep & sleep) { this->sleep = &sleep; } |
IanBenzMaxim | 7:9cd16581b578 | 108 | |
IanBenzMaxim | 7:9cd16581b578 | 109 | void setMaster(I2CMaster & master) { this->master = &master; } |
IanBenzMaxim | 7:9cd16581b578 | 110 | |
IanBenzMaxim | 7:9cd16581b578 | 111 | uint_least8_t address() const { return address_; } |
IanBenzMaxim | 7:9cd16581b578 | 112 | |
IanBenzMaxim | 7:9cd16581b578 | 113 | void setAddress(uint_least8_t address) { address_ = address & 0xFE; } |
IanBenzMaxim | 7:9cd16581b578 | 114 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 115 | MaximInterfaceCore_EXPORT Result<span<uint_least8_t> > |
IanBenzMaxim | 7:9cd16581b578 | 116 | operator()(span<const uint_least8_t> request, int delayTime, |
IanBenzMaxim | 8:5ea891c7d1a1 | 117 | span<uint_least8_t> response) const; |
IanBenzMaxim | 7:9cd16581b578 | 118 | |
IanBenzMaxim | 7:9cd16581b578 | 119 | private: |
IanBenzMaxim | 7:9cd16581b578 | 120 | const Sleep * sleep; |
IanBenzMaxim | 7:9cd16581b578 | 121 | I2CMaster * master; |
IanBenzMaxim | 7:9cd16581b578 | 122 | uint_least8_t address_; |
IanBenzMaxim | 7:9cd16581b578 | 123 | }; |
IanBenzMaxim | 7:9cd16581b578 | 124 | |
IanBenzMaxim | 8:5ea891c7d1a1 | 125 | template <> |
IanBenzMaxim | 8:5ea891c7d1a1 | 126 | struct is_error_code_enum<RunCommandWithI2CMaster::ErrorValue> : true_type {}; |
IanBenzMaxim | 8:5ea891c7d1a1 | 127 | |
IanBenzMaxim | 7:9cd16581b578 | 128 | inline error_code make_error_code(RunCommandWithI2CMaster::ErrorValue e) { |
IanBenzMaxim | 7:9cd16581b578 | 129 | return error_code(e, RunCommandWithI2CMaster::errorCategory()); |
IanBenzMaxim | 7:9cd16581b578 | 130 | } |
IanBenzMaxim | 7:9cd16581b578 | 131 | |
IanBenzMaxim | 7:9cd16581b578 | 132 | } // namespace MaximInterfaceCore |
IanBenzMaxim | 7:9cd16581b578 | 133 | |
IanBenzMaxim | 7:9cd16581b578 | 134 | #endif |