blob: 2d3c6dd48c90f33987eb9494125f14716656b4ad [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
78 private:
79
80 /**
81 * Requests a GPIO line from the GPIO device
82 */
83 void requestLine();
84
85 /**
86 * The GPIO device name, like /dev/gpiochip0
87 */
88 const std::string device;
89
90 /**
91 * The GPIO number
92 */
93 const gpioNum_t gpio;
94
95 /**
96 * The GPIO direction
97 */
98 const Direction direction;
99
100 /**
101 * File descriptor for the GPIO line
102 */
103 power::util::FileDescriptor lineFD;
104};
105
106}
107}