LoRaWANCH341 Library
Loading...
Searching...
No Matches
SPIInterface.hpp
Go to the documentation of this file.
1/***
2 * Abstract interface for SPI communication
3 *
4 * @file SPIInterface.hpp
5 * @brief Header file for the SPIInterface class
6 */
7#pragma once
8
9#include <vector>
10#include <cstdint>
11#include <memory>
12#include <functional>
13#include <string>
14
22public:
23 /***
24 * Constants for GPIO pin modes
25 */
26 static constexpr uint8_t INPUT = 0;
27 static constexpr uint8_t OUTPUT = 1;
28 static constexpr uint8_t INPUT_PULLUP = 2;
29
30 /***
31 * Destructor for SPIInterface, ensures proper cleanup of resources.
32 */
33 virtual ~SPIInterface() = default;
34
35 /***
36 * Opens the SPI connection.
37 * @return True if the connection was successfully opened, false otherwise.
38 */
39 virtual bool open() = 0;
40
41 /***
42 * Closes the SPI connection.
43 * Ensures that the resources are properly released.
44 */
45 virtual void close() = 0;
46
47 /***
48 * Transfers data over SPI.
49 * @param write_data The data to write to the SPI device.
50 * @param read_length The number of bytes to read from the SPI device.
51 * @return A vector containing the data read from the SPI device.
52 */
53 virtual std::vector<uint8_t> transfer(const std::vector<uint8_t>& write_data, size_t read_length = 0) = 0;
54
55 /***
56 * Writes a digital value to a specified pin.
57 * @param pin The pin number.
58 * @param value The value to write (true for high, false for low).
59 * @return True if the operation was successful, false otherwise.
60 */
61 virtual bool digitalWrite(uint8_t pin, bool value) = 0;
62
63 /***
64 * Reads a digital value from a specified pin.
65 * @param pin The pin number.
66 * @return The digital value read from the pin (true for high, false for low).
67 */
68 virtual bool digitalRead(uint8_t pin) = 0;
69
70 /***
71 * Sets the mode of a specified pin.
72 * @param pin The pin number.
73 * @param mode The mode to set (e.g., input, output).
74 * @return True if the operation was successful, false otherwise.
75 */
76 virtual bool pinMode(uint8_t pin, uint8_t mode) = 0;
77
78 /***
79 * Configures the interrupt settings for a pin.
80 * @param pin The pin number.
81 * @param enable True to enable the interrupt, false to disable it.
82 * @return True if the operation was successful, false otherwise.
83 */
84 virtual bool configureInterrupt(uint8_t pin, bool enable) = 0;
85
86 /***
87 * Callback function for handling interrupts.
88 */
89 using InterruptCallback = std::function<void(void)>;
90 virtual bool setInterruptCallback(InterruptCallback callback) = 0;
91
92 /***
93 * Enables or disables interrupts.
94 * @param enable True to enable interrupts, false to disable them.
95 * @return True if the operation was successful, false otherwise.
96 */
97 virtual bool enableInterrupt(bool enable) = 0;
98
99 /***
100 * Checks if the SPI device is currently active.
101 * @return True if the device is active, false otherwise.
102 */
103 virtual bool isActive() const = 0;
104};
105
113public:
114 static std::unique_ptr<SPIInterface> createCH341SPI(int device_index = 0, bool lsb_first = false);
115 static std::unique_ptr<SPIInterface> createLinuxSPI(const std::string& device = std::string("/dev/spidev0.0"),
116 uint32_t speed = 1000000,
117 uint8_t mode = 0);
118 /***
119 * Creates an SPI interface for a specific device type.
120 * @param device_type The type of SPI device to create (e.g., "CH341", "Linux").
121 * @param device_index Optional parameter for device index.
122 * @param lsb_first Optional parameter to set LSB first mode for CH341 devices.
123 * @return A unique pointer to an SPIInterface, or nullptr if the type is unsupported.
124 */
125 static std::unique_ptr<SPIInterface> createSPIInterface(const std::string& device_type, int device_index = 0, bool lsb_first = false);
126
127 /***
128 * Releases any resources used by the factory.
129 */
130 static void cleanupResources();
131
132 /***
133 * Destructor for SPIFactory, ensures proper cleanup of resources.
134 */
135 ~SPIFactory() = default;
136};
Factory class for creating SPI interfaces.
Definition SPIInterface.hpp:112
static std::unique_ptr< SPIInterface > createCH341SPI(int device_index=0, bool lsb_first=false)
Definition SPIFactory.cpp:6
static std::unique_ptr< SPIInterface > createSPIInterface(const std::string &device_type, int device_index=0, bool lsb_first=false)
static std::unique_ptr< SPIInterface > createLinuxSPI(const std::string &device=std::string("/dev/spidev0.0"), uint32_t speed=1000000, uint8_t mode=0)
Definition SPIFactory.cpp:10
~SPIFactory()=default
static void cleanupResources()
Abstract interface for SPI communication.
Definition SPIInterface.hpp:21
static constexpr uint8_t INPUT_PULLUP
Definition SPIInterface.hpp:28
virtual bool isActive() const =0
virtual bool enableInterrupt(bool enable)=0
virtual ~SPIInterface()=default
virtual bool setInterruptCallback(InterruptCallback callback)=0
virtual bool configureInterrupt(uint8_t pin, bool enable)=0
virtual bool open()=0
virtual bool pinMode(uint8_t pin, uint8_t mode)=0
std::function< void(void)> InterruptCallback
Definition SPIInterface.hpp:89
virtual bool digitalRead(uint8_t pin)=0
virtual void close()=0
virtual std::vector< uint8_t > transfer(const std::vector< uint8_t > &write_data, size_t read_length=0)=0
static constexpr uint8_t OUTPUT
Definition SPIInterface.hpp:27
static constexpr uint8_t INPUT
Definition SPIInterface.hpp:26
virtual bool digitalWrite(uint8_t pin, bool value)=0