Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 3 | #include "file.hpp" |
| 4 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 5 | #include <linux/gpio.h> |
| 6 | |
| 7 | #include <string> |
| 8 | #include <type_traits> |
| 9 | |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 10 | namespace witherspoon |
| 11 | { |
| 12 | namespace gpio |
| 13 | { |
| 14 | |
| 15 | typedef std::remove_reference<decltype( |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 16 | gpiohandle_request::lineoffsets[0])>::type gpioNum_t; |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 17 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 18 | typedef std::remove_reference<decltype(gpiohandle_data::values[0])>::type |
| 19 | gpioValue_t; |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * If the GPIO is an input or output |
| 23 | */ |
| 24 | enum class Direction |
| 25 | { |
| 26 | input, |
| 27 | output |
| 28 | }; |
| 29 | |
| 30 | /** |
| 31 | * The possible values - low or high |
| 32 | */ |
| 33 | enum class Value |
| 34 | { |
| 35 | low, |
| 36 | high |
| 37 | }; |
| 38 | |
| 39 | /** |
| 40 | * Represents a GPIO. |
| 41 | * |
| 42 | * Currently supports reading a GPIO. |
| 43 | * |
| 44 | * Write support may be added in the future. |
| 45 | */ |
| 46 | class GPIO |
| 47 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 48 | public: |
| 49 | GPIO() = delete; |
| 50 | GPIO(const GPIO&) = delete; |
| 51 | GPIO(GPIO&&) = default; |
| 52 | GPIO& operator=(const GPIO&) = delete; |
| 53 | GPIO& operator=(GPIO&&) = default; |
| 54 | ~GPIO() = default; |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 55 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 56 | /** |
| 57 | * Constructor |
| 58 | * |
| 59 | * @param[in] device - the GPIO device file |
| 60 | * @param[in] gpio - the GPIO number |
| 61 | * @param[in] direction - the GPIO direction |
| 62 | */ |
| 63 | GPIO(const std::string& device, gpioNum_t gpio, Direction direction) : |
| 64 | device(device), gpio(gpio), direction(direction) |
| 65 | { |
| 66 | } |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 67 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 68 | /** |
| 69 | * Reads the GPIO value |
| 70 | * |
| 71 | * Requests the GPIO line if it hasn't been done already. |
| 72 | * |
| 73 | * @return Value - the GPIO value |
| 74 | */ |
| 75 | Value read(); |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 76 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 77 | /** |
| 78 | * Sets the GPIO value to low or high |
| 79 | * |
| 80 | * Requests the GPIO line if it hasn't been done already. |
| 81 | * |
| 82 | * @param[in] Value - the value to set |
| 83 | */ |
| 84 | void set(Value value); |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 85 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 86 | private: |
| 87 | /** |
| 88 | * Requests a GPIO line from the GPIO device |
| 89 | * |
| 90 | * @param[in] defaultValue - The default value, required for |
| 91 | * output GPIOs only. |
| 92 | */ |
| 93 | void requestLine(Value defaultValue = Value::high); |
Matt Spinler | 35aa143 | 2018-01-18 11:13:20 -0600 | [diff] [blame] | 94 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 95 | /** |
| 96 | * The GPIO device name, like /dev/gpiochip0 |
| 97 | */ |
| 98 | const std::string device; |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 99 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 100 | /** |
| 101 | * The GPIO number |
| 102 | */ |
| 103 | const gpioNum_t gpio; |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 104 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 105 | /** |
| 106 | * The GPIO direction |
| 107 | */ |
| 108 | const Direction direction; |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 109 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 110 | /** |
| 111 | * File descriptor for the GPIO line |
| 112 | */ |
| 113 | power::util::FileDescriptor lineFD; |
Matt Spinler | ee7adb7 | 2017-08-21 15:17:19 -0500 | [diff] [blame] | 114 | }; |
| 115 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame^] | 116 | } // namespace gpio |
| 117 | } // namespace witherspoon |