|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, Niklas Hauser |
| 3 | + * |
| 4 | + * This file is part of the modm project. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | + */ |
| 10 | +// ---------------------------------------------------------------------------- |
| 11 | + |
| 12 | +#pragma once |
| 13 | + |
| 14 | +#include "base.hpp" |
| 15 | + |
| 16 | +namespace modm::platform |
| 17 | +{ |
| 18 | + |
| 19 | +/** |
| 20 | + * Gpio OpenDrain template, which remaps the behavior of the Gpio pin to |
| 21 | + * simulate an open-drain output (with internal pullups if needed). |
| 22 | + * |
| 23 | + * @see BitBangI2cMaster |
| 24 | + * @ingroup modm_platform_gpio |
| 25 | + */ |
| 26 | +template< class Pin > |
| 27 | +class GpioOpenDrain : public Pin |
| 28 | +{ |
| 29 | + static inline Gpio::InputType inputType = Gpio::InputType::Floating; |
| 30 | + static_assert(Pin::direction == modm::Gpio::Direction::InOut, "Pin must inherit from modm::GpioIO"); |
| 31 | +public: |
| 32 | + using IO = GpioOpenDrain<typename Pin::IO>; |
| 33 | + using Output = IO; |
| 34 | + using Input = IO; |
| 35 | + using Type = typename Pin::Type; |
| 36 | + |
| 37 | + static constexpr modm::Gpio::Direction direction = modm::Gpio::Direction::Out; |
| 38 | + |
| 39 | + enum class |
| 40 | + OutputType |
| 41 | + { |
| 42 | + PushPull, |
| 43 | + OpenDrain, |
| 44 | + }; |
| 45 | + |
| 46 | +public: |
| 47 | + inline static void setInput() {} |
| 48 | + inline static void setInput(Gpio::InputType type) { inputType = type; } |
| 49 | + inline static void configure(Gpio::InputType type) { inputType = type; } |
| 50 | + |
| 51 | + inline static void setOutput() {} |
| 52 | + inline static void setOutput(OutputType) {} |
| 53 | + inline static void setOutput(bool status) { set(status); } |
| 54 | + |
| 55 | + /// maps to `setInput(InputType::Floating)` or `setInput(InputType::PullUp)` |
| 56 | + inline static void set() { Pin::setInput(inputType); } |
| 57 | + /// maps to `setOutput(::modm::Gpio::Low)` |
| 58 | + inline static void reset() { Pin::setOutput(::modm::Gpio::Low); } |
| 59 | + inline static void set(bool status) { status ? set() : reset(); } |
| 60 | + inline static bool isSet() |
| 61 | + { return (Pin::getDirection() == modm::Gpio::Direction::In); } |
| 62 | + |
| 63 | + inline static modm::Gpio::Direction getDirection() |
| 64 | + { return modm::Gpio::Direction::Out; } |
| 65 | +}; |
| 66 | + |
| 67 | +} // namespace modm::platform |
0 commit comments