RADLib
RADical C++ application framework
RADDevice.h
Go to the documentation of this file.
1 #ifndef RADDEVICE_H
2 #define RADDEVICE_H
3 
9 #pragma once
10 
11 #include <RADCore/RADCore.h>
12 
13 #include <cstdint>
14 #include <string>
15 #include <vector>
16 
17 namespace RADDevice {
18 
20  struct DevicePaths {
22  std::string sysClassGpio = "/sys/class/gpio";
24  std::string sysClassPwm = "/sys/class/pwm";
26  std::string sysBusIio = "/sys/bus/iio/devices";
28  std::string sysClassLeds = "/sys/class/leds";
30  std::string devRoot = "/dev";
31  };
32 
34  enum class GpioDirection {
36  In,
38  Out
39  };
40 
42  enum class GpioEdge {
44  None,
46  Rising,
48  Falling,
50  Both
51  };
52 
54  class I2CDevice {
55  public:
61  bool open(int bus, int address, const DevicePaths& paths = {}, std::string* error = nullptr);
63  void close();
65  ssize_t write(const std::vector<uint8_t>& bytes);
67  std::vector<uint8_t> read(size_t count);
69  bool isOpen() const;
71  int fd() const;
72 
73  private:
74  int fd_ = -1;
75  };
76 
78  class SPIDevice {
79  public:
85  bool open(int bus, int chipSelect, const DevicePaths& paths = {}, std::string* error = nullptr);
87  void close();
89  bool configure(uint8_t mode, uint8_t bitsPerWord, uint32_t speedHz, std::string* error = nullptr);
91  std::vector<uint8_t> transfer(const std::vector<uint8_t>& tx, std::string* error = nullptr);
93  bool isOpen() const;
95  int fd() const;
96 
97  private:
98  int fd_ = -1;
99  uint32_t speedHz_ = 1000000;
100  uint8_t bitsPerWord_ = 8;
101  };
102 
104  class GpioPin {
105  public:
107  GpioPin(int number, DevicePaths paths = {});
109  bool exportPin(std::string* error = nullptr) const;
111  bool unexportPin(std::string* error = nullptr) const;
113  bool setDirection(GpioDirection direction, std::string* error = nullptr) const;
115  bool setEdge(GpioEdge edge, std::string* error = nullptr) const;
117  bool writeValue(bool high, std::string* error = nullptr) const;
121  std::string pinPath() const;
122 
123  private:
124  int number_;
125  DevicePaths paths_;
126  };
127 
129  class PwmChannel {
130  public:
132  PwmChannel(int chip, int channel, DevicePaths paths = {});
134  bool exportChannel(std::string* error = nullptr) const;
136  bool setEnabled(bool enabled, std::string* error = nullptr) const;
138  bool setPeriodNs(uint64_t periodNs, std::string* error = nullptr) const;
140  bool setDutyCycleNs(uint64_t dutyNs, std::string* error = nullptr) const;
142  std::string channelPath() const;
143 
144  private:
145  int chip_;
146  int channel_;
147  DevicePaths paths_;
148  };
149 
151  class AdcChannel {
152  public:
154  AdcChannel(int device, int channel, DevicePaths paths = {});
161 
162  private:
163  int device_;
164  int channel_;
165  DevicePaths paths_;
166  };
167 
169  class Watchdog {
170  public:
176  bool open(int index = 0, const DevicePaths& paths = {}, std::string* error = nullptr);
178  void close();
180  bool keepAlive(std::string* error = nullptr);
182  bool isOpen() const;
183 
184  private:
185  int fd_ = -1;
186  };
187 
189  class Led {
190  public:
192  Led(std::string name, DevicePaths paths = {});
194  bool setBrightness(int brightness, std::string* error = nullptr) const;
199 
200  private:
201  std::string name_;
202  DevicePaths paths_;
203  };
204 
206  std::vector<std::string> listLeds(const DevicePaths& paths = {});
208  std::vector<std::string> listIioDevices(const DevicePaths& paths = {});
209 
210 } // namespace RADDevice
211 
212 #endif
Success-or-error return value for APIs that should not throw.
Definition: RADCore.h:158
IIO ADC channel reader.
Definition: RADDevice.h:151
RADCore::RADResult< double > readScaled() const
Reads scaled voltage/value when scale is available.
AdcChannel(int device, int channel, DevicePaths paths={})
Creates an ADC channel wrapper.
RADCore::RADResult< double > readScale() const
Reads scale value when available.
RADCore::RADResult< int > readRaw() const
Reads raw integer sample.
GPIO sysfs helper.
Definition: RADDevice.h:104
bool writeValue(bool high, std::string *error=nullptr) const
Writes 0 or 1.
bool setEdge(GpioEdge edge, std::string *error=nullptr) const
Sets pin edge mode.
bool setDirection(GpioDirection direction, std::string *error=nullptr) const
Sets pin direction.
bool exportPin(std::string *error=nullptr) const
Exports the pin through sysfs.
bool unexportPin(std::string *error=nullptr) const
Unexports the pin through sysfs.
std::string pinPath() const
Returns sysfs pin directory.
RADCore::RADResult< bool > readValue() const
Reads current value.
GpioPin(int number, DevicePaths paths={})
Creates a GPIO pin wrapper.
Parity-free I2C device wrapper using /dev/i2c-* and I2C_SLAVE ioctl.
Definition: RADDevice.h:54
bool open(int bus, int address, const DevicePaths &paths={}, std::string *error=nullptr)
Opens bus number and selects 7-bit address.
void close()
Closes the descriptor.
~I2CDevice()
Closes the device.
ssize_t write(const std::vector< uint8_t > &bytes)
Writes bytes to the selected device.
std::vector< uint8_t > read(size_t count)
Reads bytes from the selected device.
bool isOpen() const
Returns true when open.
I2CDevice()
Creates a closed I2C device wrapper.
int fd() const
Returns owned file descriptor.
LED sysfs helper.
Definition: RADDevice.h:189
RADCore::RADResult< int > maxBrightness() const
Returns maximum brightness.
bool setBrightness(int brightness, std::string *error=nullptr) const
Writes brightness.
Led(std::string name, DevicePaths paths={})
Creates an LED wrapper.
RADCore::RADResult< int > brightness() const
Reads brightness.
PWM sysfs channel helper.
Definition: RADDevice.h:129
std::string channelPath() const
Returns sysfs PWM path.
bool exportChannel(std::string *error=nullptr) const
Exports the PWM channel.
bool setDutyCycleNs(uint64_t dutyNs, std::string *error=nullptr) const
Sets duty cycle in nanoseconds.
bool setPeriodNs(uint64_t periodNs, std::string *error=nullptr) const
Sets period in nanoseconds.
bool setEnabled(bool enabled, std::string *error=nullptr) const
Enables or disables output.
PwmChannel(int chip, int channel, DevicePaths paths={})
Creates a PWM channel wrapper.
SPI device wrapper using /dev/spidev* and SPI_IOC_MESSAGE.
Definition: RADDevice.h:78
SPIDevice()
Creates a closed SPI device wrapper.
bool isOpen() const
Returns true when open.
std::vector< uint8_t > transfer(const std::vector< uint8_t > &tx, std::string *error=nullptr)
Full-duplex transfer.
bool open(int bus, int chipSelect, const DevicePaths &paths={}, std::string *error=nullptr)
Opens a SPI bus/chip-select device.
bool configure(uint8_t mode, uint8_t bitsPerWord, uint32_t speedHz, std::string *error=nullptr)
Configures mode, bits, and max speed through ioctl.
void close()
Closes the descriptor.
int fd() const
Returns owned file descriptor.
~SPIDevice()
Closes the device.
Linux watchdog wrapper.
Definition: RADDevice.h:169
Watchdog()
Creates a closed watchdog wrapper.
~Watchdog()
Closes the descriptor.
bool isOpen() const
Returns true when open.
void close()
Closes the descriptor.
bool open(int index=0, const DevicePaths &paths={}, std::string *error=nullptr)
Opens /dev/watchdogN.
bool keepAlive(std::string *error=nullptr)
Pings the watchdog through ioctl.
Definition: RADDevice.h:17
GpioDirection
GPIO direction.
Definition: RADDevice.h:34
std::vector< std::string > listIioDevices(const DevicePaths &paths={})
Lists visible IIO device directory names.
std::vector< std::string > listLeds(const DevicePaths &paths={})
Lists LED device names.
GpioEdge
GPIO edge trigger mode.
Definition: RADDevice.h:42
@ Falling
Falling edge.
@ Both
Both edges.
@ Rising
Rising edge.
@ None
No edge trigger.
Filesystem roots used for real devices or test fixtures.
Definition: RADDevice.h:20
std::string sysClassGpio
GPIO sysfs root.
Definition: RADDevice.h:22
std::string sysBusIio
IIO sysfs root.
Definition: RADDevice.h:26
std::string sysClassPwm
PWM sysfs root.
Definition: RADDevice.h:24
std::string sysClassLeds
LED sysfs root.
Definition: RADDevice.h:28
std::string devRoot
Device node root.
Definition: RADDevice.h:30