| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "gate/io/gpiodevices.hpp" | ||
| 30 | #include "gate/exceptions.hpp" | ||
| 31 | |||
| 32 | namespace gate | ||
| 33 | { | ||
| 34 | namespace io | ||
| 35 | { | ||
| 36 | |||
| 37 | |||
| 38 | enumint_t const GPIODevice::LineConfig_Input = GATE_GPIODEVICE_LINE_CONFIG_INPUT; | ||
| 39 | enumint_t const GPIODevice::LineConfig_Output = GATE_GPIODEVICE_LINE_CONFIG_OUTPUT; | ||
| 40 | |||
| 41 | |||
| 42 | ✗ | String GPIODevice::getDefaultDevicePath() | |
| 43 | { | ||
| 44 | ✗ | gate_string_t path = GATE_STRING_INIT_EMPTY; | |
| 45 | ✗ | result_t result = gate_gpiodevice_default_path(&path); | |
| 46 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 47 | ✗ | return String::createFrom(path); | |
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | ✗ | GPIODevice::GPIODevice(String const& devicePath, enumint_t flags) | |
| 52 | { | ||
| 53 | ✗ | result_t result = gate_gpiodevice_open(devicePath.c_impl(), flags, &this->impl); | |
| 54 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 55 | ✗ | } | |
| 56 | |||
| 57 | ✗ | GPIODevice::~GPIODevice() noexcept | |
| 58 | { | ||
| 59 | ✗ | gate_gpiodevice_close(&this->impl); | |
| 60 | ✗ | } | |
| 61 | |||
| 62 | ✗ | static gate_bool_t GATE_CALL GPIODevice_listLines_cb(gate_intptr_t line_address, gate_string_t const* description, gate_bool_t is_used, void* param) | |
| 63 | { | ||
| 64 | ✗ | GPIODevice::LinesInfoMap* ptrMap = static_cast<GPIODevice::LinesInfoMap*>(param); | |
| 65 | ✗ | GPIODevice::LineInfo info; | |
| 66 | ✗ | info.Description = String(*description); | |
| 67 | ✗ | info.IsUsed = is_used; | |
| 68 | ✗ | ptrMap->add(line_address, info); | |
| 69 | ✗ | return true; | |
| 70 | } | ||
| 71 | |||
| 72 | ✗ | GPIODevice::LinesInfoMap GPIODevice::listLines() | |
| 73 | { | ||
| 74 | ✗ | LinesInfoMap ret; | |
| 75 | ✗ | result_t result = gate_gpiodevice_lines_list(&this->impl, &GPIODevice_listLines_cb, &ret); | |
| 76 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 77 | ✗ | return ret; | |
| 78 | } | ||
| 79 | |||
| 80 | ✗ | enumint_t GPIODevice::getLineConfig(line_t lineAddress) | |
| 81 | { | ||
| 82 | ✗ | enumint_t ret = 0; | |
| 83 | ✗ | result_t result = gate_gpiodevice_lines_get_config(&this->impl, lineAddress, &ret); | |
| 84 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 85 | ✗ | return ret; | |
| 86 | } | ||
| 87 | |||
| 88 | ✗ | void GPIODevice::setLineConfig(line_t lineAddress, enumint_t configFlags) | |
| 89 | { | ||
| 90 | ✗ | result_t result = gate_gpiodevice_lines_set_config(&this->impl, lineAddress, configFlags); | |
| 91 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 92 | ✗ | } | |
| 93 | |||
| 94 | ✗ | intptr_t GPIODevice::readLine(line_t lineAddress) | |
| 95 | { | ||
| 96 | ✗ | intptr_t ret = 0; | |
| 97 | ✗ | result_t result = gate_gpiodevice_lines_read(&this->impl, lineAddress, &ret); | |
| 98 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 99 | ✗ | return ret; | |
| 100 | } | ||
| 101 | |||
| 102 | ✗ | void GPIODevice::writeLine(line_t lineAddress, intptr_t value) | |
| 103 | { | ||
| 104 | ✗ | result_t result = gate_gpiodevice_lines_write(&this->impl, lineAddress, value); | |
| 105 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 106 | ✗ | } | |
| 107 | |||
| 108 | |||
| 109 | |||
| 110 | ✗ | String I2CDevice::getDefaultDevicePath() | |
| 111 | { | ||
| 112 | gate_string_t device_path; | ||
| 113 | ✗ | gate_result_t result = gate_i2cdevice_default_path(&device_path); | |
| 114 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 115 | ✗ | return String::createFrom(device_path); | |
| 116 | } | ||
| 117 | |||
| 118 | ✗ | I2CDevice::I2CDevice(String const& devicePath, enumint_t flags) | |
| 119 | { | ||
| 120 | ✗ | result_t result = gate_i2cdevice_open(devicePath.c_impl(), flags, &this->impl); | |
| 121 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 122 | ✗ | } | |
| 123 | |||
| 124 | ✗ | I2CDevice::~I2CDevice() noexcept | |
| 125 | { | ||
| 126 | ✗ | gate_i2cdevice_close(&this->impl); | |
| 127 | ✗ | } | |
| 128 | |||
| 129 | ✗ | Result<size_t> I2CDevice::trySend(uint8_t i2cAddress, void const* data, size_t dataLength) noexcept | |
| 130 | { | ||
| 131 | ✗ | return this->trySend(i2cAddress, &data, &dataLength, 1); | |
| 132 | } | ||
| 133 | |||
| 134 | ✗ | Result<size_t> I2CDevice::trySend(uint8_t i2cAddress, void const* const* blocks, size_t const* blocksLengths, size_t blocksCount) noexcept | |
| 135 | { | ||
| 136 | ✗ | size_t bytesSent = 0; | |
| 137 | ✗ | result_t result = gate_i2cdevice_send(&this->impl, i2cAddress, blocks, blocksLengths, blocksCount, &bytesSent); | |
| 138 | ✗ | return makeResult(result, bytesSent); | |
| 139 | } | ||
| 140 | |||
| 141 | ✗ | Result<size_t> I2CDevice::tryReceive(uint8_t i2cAddress, void* buffer, size_t bufferLength) noexcept | |
| 142 | { | ||
| 143 | ✗ | size_t bytesReceived = 0; | |
| 144 | ✗ | result_t result = gate_i2cdevice_receive(&this->impl, i2cAddress, buffer, bufferLength, &bytesReceived); | |
| 145 | ✗ | return makeResult(result, bytesReceived); | |
| 146 | } | ||
| 147 | |||
| 148 | ✗ | size_t I2CDevice::send(uint8_t i2cAddress, void const* data, size_t dataLength) | |
| 149 | { | ||
| 150 | ✗ | return this->trySend(i2cAddress, data, dataLength).valueOrExcept(); | |
| 151 | } | ||
| 152 | ✗ | size_t I2CDevice::send(uint8_t i2cAddress, void const* const* blocks, size_t const* blocksLengths, size_t blocksCount) | |
| 153 | { | ||
| 154 | ✗ | return this->trySend(i2cAddress, blocks, blocksLengths, blocksCount).valueOrExcept(); | |
| 155 | } | ||
| 156 | |||
| 157 | ✗ | size_t I2CDevice::receive(uint8_t i2cAddress, void* buffer, size_t bufferLength) | |
| 158 | { | ||
| 159 | ✗ | return this->tryReceive(i2cAddress, buffer, bufferLength).valueOrExcept(); | |
| 160 | } | ||
| 161 | |||
| 162 | } // end of namespace io | ||
| 163 | } // end of namespace gate | ||
| 164 |