LoRaWANCH341 Library
Loading...
Searching...
No Matches
Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | List of all members
CH341SPI Class Reference

A class to interface with the CH341 USB to SPI adapter. More...

#include <CH341SPI.hpp>

Inheritance diagram for CH341SPI:
Inheritance graph
Collaboration diagram for CH341SPI:
Collaboration graph

Public Member Functions

 CH341SPI (int device_index=0, bool lsb_first=false)
 Constructor for CH341SPI.
 
 ~CH341SPI ()
 Destructor for CH341SPI.
 
bool open ()
 Opens the SPI connection.
 
void close ()
 Closes the SPI connection.
 
std::vector< uint8_t > transfer (const std::vector< uint8_t > &write_data, size_t read_length=0)
 Transfers data over SPI.
 
bool digitalWrite (uint8_t pin, bool value)
 Writes a digital value to a specified pin.
 
bool digitalRead (uint8_t pin)
 Reads a digital value from a specified pin.
 
bool pinMode (uint8_t pin, uint8_t mode)
 Sets the mode of a specified pin.
 
bool configureInterrupt (uint8_t pin, bool enable) override
 Configure interrupt settings for a pin.
 
bool setInterruptCallback (InterruptCallback callback) override
 Set interrupt callback.
 
bool enableInterrupt (bool enable) override
 Enable or disable interrupts.
 
bool isActive () const override
 Check if the device is active/connected.
 
- Public Member Functions inherited from SPIInterface
virtual ~SPIInterface ()=default
 

Static Public Attributes

static const uint8_t PIN_D0 = 0x01
 
static const uint8_t PIN_D1 = 0x02
 
static const uint8_t PIN_D2 = 0x04
 
static const uint8_t PIN_D3 = 0x08
 
static const uint8_t PIN_D4 = 0x10
 
static const uint8_t PIN_D5 = 0x20
 
- Static Public Attributes inherited from SPIInterface
static constexpr uint8_t INPUT = 0
 
static constexpr uint8_t OUTPUT = 1
 
static constexpr uint8_t INPUT_PULLUP = 2
 

Private Member Functions

bool configStream ()
 Configures the SPI stream.
 
bool enablePins (bool enable)
 Enables or disables the pins.
 
uint8_t swapBits (uint8_t byte)
 Swaps the bits of a byte.
 
void interruptMonitoringThread ()
 Thread function for monitoring interrupts.
 

Private Attributes

libusb_device_handle * device
 Handle to the libusb device.
 
libusb_context * context
 Context for libusb.
 
int device_index
 Index of the CH341 device.
 
bool lsb_first
 Flag to indicate if LSB should be sent first.
 
bool is_open
 Flag to indicate whether the device is open and active.
 
uint8_t _gpio_direction
 Direction of GPIO pins.
 
uint8_t _gpio_output
 Output state of GPIO pins.
 
InterruptCallback interruptCallback
 Callback function for interrupts.
 
bool interruptEnabled
 Flag to indicate if interrupts are enabled.
 
std::thread interruptThread
 Thread for monitoring interrupts.
 
bool threadRunning
 Flag to indicate if the interrupt monitoring thread is running.
 

Additional Inherited Members

- Public Types inherited from SPIInterface
using InterruptCallback = std::function< void(void)>
 

Detailed Description

A class to interface with the CH341 USB to SPI adapter.

Constructor & Destructor Documentation

◆ CH341SPI()

CH341SPI::CH341SPI ( int  device_index = 0,
bool  lsb_first = false 
)

Constructor for CH341SPI.

Parameters
device_indexIndex of the CH341 device to use (default is 0).
lsb_firstSet to true if least significant bit should be sent first (default is false).

◆ ~CH341SPI()

CH341SPI::~CH341SPI ( )

Destructor for CH341SPI.

Here is the call graph for this function:

Member Function Documentation

◆ close()

void CH341SPI::close ( )
virtual

Closes the SPI connection.

Implements SPIInterface.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ configStream()

bool CH341SPI::configStream ( )
private

Configures the SPI stream.

Returns
True if the configuration was successful, false otherwise.
Here is the caller graph for this function:

◆ configureInterrupt()

bool CH341SPI::configureInterrupt ( uint8_t  pin,
bool  enable 
)
inlineoverridevirtual

Configure interrupt settings for a pin.

Parameters
pinThe pin number
enableTrue to enable the interrupt, false to disable it
Returns
True if the operation was successful, false otherwise

Implements SPIInterface.

◆ digitalRead()

bool CH341SPI::digitalRead ( uint8_t  pin)
virtual

Reads a digital value from a specified pin.

Parameters
pinThe pin number.
Returns
The value read from the pin (true for high, false for low).

Implements SPIInterface.

◆ digitalWrite()

bool CH341SPI::digitalWrite ( uint8_t  pin,
bool  value 
)
virtual

Writes a digital value to a specified pin.

Parameters
pinThe pin number.
valueThe value to write (true for high, false for low).
Returns
True if the operation was successful, false otherwise.

Implements SPIInterface.

◆ enableInterrupt()

bool CH341SPI::enableInterrupt ( bool  enable)
overridevirtual

Enable or disable interrupts.

Parameters
enableTrue to enable interrupts, false to disable them
Returns
True if successful, false otherwise

Implements SPIInterface.

Here is the call graph for this function:

◆ enablePins()

bool CH341SPI::enablePins ( bool  enable)
private

Enables or disables the pins.

Parameters
enableSet to true to enable pins, false to disable.
Returns
True if the operation was successful, false otherwise.
Here is the caller graph for this function:

◆ interruptMonitoringThread()

void CH341SPI::interruptMonitoringThread ( )
private

Thread function for monitoring interrupts.

Here is the caller graph for this function:

◆ isActive()

bool CH341SPI::isActive ( ) const
inlineoverridevirtual

Check if the device is active/connected.

Returns
True if the device is active, false otherwise

Implements SPIInterface.

◆ open()

bool CH341SPI::open ( )
virtual

Opens the SPI connection.

Returns
True if the connection was successfully opened, false otherwise.

Implements SPIInterface.

Here is the call graph for this function:

◆ pinMode()

bool CH341SPI::pinMode ( uint8_t  pin,
uint8_t  mode 
)
virtual

Sets the mode of a specified pin.

Parameters
pinThe pin number.
modeThe mode to set (e.g., input, output).
Returns
True if the operation was successful, false otherwise.

Implements SPIInterface.

◆ setInterruptCallback()

bool CH341SPI::setInterruptCallback ( InterruptCallback  callback)
overridevirtual

Set interrupt callback.

Parameters
callbackFunction to call when interrupt occurs
Returns
True if successful, false otherwise

Implements SPIInterface.

◆ swapBits()

uint8_t CH341SPI::swapBits ( uint8_t  byte)
private

Swaps the bits of a byte.

Parameters
byteThe byte whose bits are to be swapped.
Returns
The byte with swapped bits.
Here is the caller graph for this function:

◆ transfer()

std::vector< uint8_t > CH341SPI::transfer ( const std::vector< uint8_t > &  write_data,
size_t  read_length = 0 
)
virtual

Transfers data over SPI.

Parameters
write_dataData to be written to the SPI bus.
read_lengthNumber of bytes to read from the SPI bus (default is 0).
Returns
A vector containing the data read from the SPI bus.

Implements SPIInterface.

Here is the call graph for this function:

Member Data Documentation

◆ _gpio_direction

uint8_t CH341SPI::_gpio_direction
private

Direction of GPIO pins.

◆ _gpio_output

uint8_t CH341SPI::_gpio_output
private

Output state of GPIO pins.

◆ context

libusb_context* CH341SPI::context
private

Context for libusb.

◆ device

libusb_device_handle* CH341SPI::device
private

Handle to the libusb device.

◆ device_index

int CH341SPI::device_index
private

Index of the CH341 device.

◆ interruptCallback

InterruptCallback CH341SPI::interruptCallback
private

Callback function for interrupts.

◆ interruptEnabled

bool CH341SPI::interruptEnabled
private

Flag to indicate if interrupts are enabled.

◆ interruptThread

std::thread CH341SPI::interruptThread
private

Thread for monitoring interrupts.

◆ is_open

bool CH341SPI::is_open
private

Flag to indicate whether the device is open and active.

◆ lsb_first

bool CH341SPI::lsb_first
private

Flag to indicate if LSB should be sent first.

◆ PIN_D0

const uint8_t CH341SPI::PIN_D0 = 0x01
static

◆ PIN_D1

const uint8_t CH341SPI::PIN_D1 = 0x02
static

◆ PIN_D2

const uint8_t CH341SPI::PIN_D2 = 0x04
static

◆ PIN_D3

const uint8_t CH341SPI::PIN_D3 = 0x08
static

◆ PIN_D4

const uint8_t CH341SPI::PIN_D4 = 0x10
static

◆ PIN_D5

const uint8_t CH341SPI::PIN_D5 = 0x20
static

◆ threadRunning

bool CH341SPI::threadRunning
private

Flag to indicate if the interrupt monitoring thread is running.


The documentation for this class was generated from the following files: