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