ArPiRobot-CoreLib C++
C++ library for ArPiRobot robots
Io.hpp
1 /*
2  * Copyright 2021 Marcus Behel
3  *
4  * This file is part of ArPiRobot-CoreLib.
5  *
6  * ArPiRobot-CoreLib is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * ArPiRobot-CoreLib is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with ArPiRobot-CoreLib. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 
21 #pragma once
22 
23 #include <arpirobot/core/io/IoProvider.hpp>
24 #include <arpirobot/core/io/IoDevice.hpp>
25 #include <arpirobot/core/io/exceptions.hpp>
26 
27 namespace arpirobot{
28 
32  class Io{
33  public:
37 
38  const static unsigned int GPIO_OUT = IoProvider::GPIO_OUT;
39  const static unsigned int GPIO_IN = IoProvider::GPIO_IN;
40  const static unsigned int GPIO_LOW = IoProvider::GPIO_LOW;
41  const static unsigned int GPIO_HIGH = IoProvider::GPIO_HIGH;
42 
43  const static char *PROVIDER_PIGPIO; // PIGPIO library
44  const static char *PROVIDER_LIBSOC; // libsoc library
45  const static char *PROVIDER_DUMMY; // Fake provider. Prints log messages.
46  const static char *PROVIDER_SERIAL; // UART only. Works on desktop platforms
47 
51 
52  static void init(std::string provider = "");
53 
54  static void terminate();
55 
56  static void addDevice(IoDevice *device);
57 
58  static void removeDevice(IoDevice *device);
59 
60 
62  // Provider independent (typically board specific)
64 
65  static int getDefaultI2cBus();
66 
67 
71 
75  static void gpioMode(unsigned int pin, unsigned int mode);
76 
77  static void gpioWrite(unsigned int pin, unsigned int state);
78 
79  static unsigned int gpioRead(unsigned int pin);
80 
81  static void gpioSetPwmFrequency(unsigned int pin, unsigned int frequency);
82 
83  static unsigned int gpioGetPwmFrequency(unsigned int pin);
84 
85  static void gpioPwm(unsigned int pin, unsigned int value);
86 
87 
91  static unsigned int i2cOpen(unsigned int bus, unsigned int address);
92 
93  static void i2cClose(unsigned int handle);
94 
95  static void i2cWriteByte(unsigned int handle, uint8_t data);
96 
97  static uint8_t i2cReadByte(unsigned int handle);
98 
99  static void i2cWriteBytes(unsigned int handle, char *buf, unsigned int count);
100 
101  static unsigned int i2cReadBytes(unsigned int handle, char *buf, unsigned int count);
102 
103  static void i2cWriteReg8(unsigned int handle, uint8_t reg, uint8_t value);
104 
105  static uint8_t i2cReadReg8(unsigned int handle, uint8_t reg);
106 
107  static void i2cWriteReg16(unsigned int handle, uint8_t reg, uint16_t value);
108 
109  static uint16_t i2cReadReg16(unsigned int handle, uint8_t reg);
110 
111 
115 
116  // Bus = spi bus
117  // Channel = which builtin CS pin
118  // Mode = SPI mode (0, 1, 2, 3)
119  static unsigned int spiOpen(unsigned int bus, unsigned int channel, unsigned int baud, unsigned int mode);
120 
121  static void spiClose(unsigned int handle);
122 
123  static void spiWrite(unsigned int handle, char *buf, unsigned int count);
124 
125  static unsigned int spiRead(unsigned int handle, char *buf, unsigned int count);
126 
127 
131 
132  static unsigned int uartOpen(char *port, unsigned int baud);
133 
134  static void uartClose(unsigned int handle);
135 
136  static unsigned int uartAvailable(unsigned int handle);
137 
138  static void uartWrite(unsigned int handle, char* buf, unsigned int count);
139 
140  static unsigned int uartRead(unsigned int handle, char *buf, unsigned int count);
141 
142  static void uartWriteByte(unsigned int handle, uint8_t b);
143 
144  static uint8_t uartReadByte(unsigned int handle);
145 
146  private:
147  static IoProvider *instance;
148  static std::vector<IoDevice*> ioDevices;
149  static std::mutex ioDevicesLock;
150  };
151 
152 }
Definition: IoDevice.hpp:27
Definition: IoProvider.hpp:30
Definition: Io.hpp:32
static unsigned int i2cOpen(unsigned int bus, unsigned int address)
I2C.
static void gpioMode(unsigned int pin, unsigned int mode)
Matches pure virtual functions of IoProvider.
static void init(std::string provider="")
IoProvider instance and IoDevice management.
static const unsigned int GPIO_OUT
Constants.
Definition: Io.hpp:38
static unsigned int spiOpen(unsigned int bus, unsigned int channel, unsigned int baud, unsigned int mode)
SPI.
static unsigned int uartOpen(char *port, unsigned int baud)
UART.
Definition: ArduinoDevice.hpp:27