Skip to content

Commit

Permalink
Merge pull request #139 from adafruit/genericdevice2
Browse files Browse the repository at this point in the history
refactor the whole thing to get rid of static-ness. also clang examples
  • Loading branch information
ladyada authored Jan 8, 2025
2 parents cc66c42 + 186ea35 commit 055013b
Show file tree
Hide file tree
Showing 15 changed files with 413 additions and 304 deletions.
60 changes: 32 additions & 28 deletions Adafruit_GenericDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,77 @@

#include "Adafruit_GenericDevice.h"

/*! @brief Create a Generic device with the provided read/write functions
@param read_func Function pointer for reading raw data
@param write_func Function pointer for writing raw data
@param readreg_func Function pointer for reading registers (optional)
@param writereg_func Function pointer for writing registers (optional) */
/*!
* @brief Create a Generic device with the provided read/write functions
* @param obj Pointer to object instance
* @param read_func Function pointer for reading raw data
* @param write_func Function pointer for writing raw data
* @param readreg_func Function pointer for reading registers (optional)
* @param writereg_func Function pointer for writing registers (optional) */
Adafruit_GenericDevice::Adafruit_GenericDevice(
busio_genericdevice_read_t read_func,
void *obj, busio_genericdevice_read_t read_func,
busio_genericdevice_write_t write_func,
busio_genericdevice_readreg_t readreg_func,
busio_genericdevice_writereg_t writereg_func) {
_obj = obj;
_read_func = read_func;
_write_func = write_func;
_readreg_func = readreg_func;
_writereg_func = writereg_func;
_begun = false;
}

/*! @brief Initializes the device
@return true if initialization was successful, otherwise false */
/*! @brief Simple begin function (doesn't do much at this time)
@return true always
*/
bool Adafruit_GenericDevice::begin(void) {
_begun = true;
return true;
}

/*! @brief Write a buffer of data
@param buffer Pointer to buffer of data to write
@param len Number of bytes to write
@return true if write was successful, otherwise false */
@param buffer Pointer to buffer of data to write
@param len Number of bytes to write
@return true if write was successful, otherwise false */
bool Adafruit_GenericDevice::write(const uint8_t *buffer, size_t len) {
if (!_begun)
return false;
return _write_func(buffer, len);
return _write_func(_obj, buffer, len);
}

/*! @brief Read data into a buffer
@param buffer Pointer to buffer to read data into
@param len Number of bytes to read
@return true if read was successful, otherwise false */
@param buffer Pointer to buffer to read data into
@param len Number of bytes to read
@return true if read was successful, otherwise false */
bool Adafruit_GenericDevice::read(uint8_t *buffer, size_t len) {
if (!_begun)
return false;
return _read_func(buffer, len);
return _read_func(_obj, buffer, len);
}

/*! @brief Read from a register location
@param addr_buf Buffer containing register address
@param addrsiz Size of register address in bytes
@param buf Buffer to store read data
@param bufsiz Size of data to read in bytes
@return true if read was successful, otherwise false */
@param addr_buf Buffer containing register address
@param addrsiz Size of register address in bytes
@param buf Buffer to store read data
@param bufsiz Size of data to read in bytes
@return true if read was successful, otherwise false */
bool Adafruit_GenericDevice::readRegister(uint8_t *addr_buf, uint8_t addrsiz,
uint8_t *buf, uint16_t bufsiz) {
if (!_begun || !_readreg_func)
return false;
return _readreg_func(addr_buf, addrsiz, buf, bufsiz);
return _readreg_func(_obj, addr_buf, addrsiz, buf, bufsiz);
}

/*! @brief Write to a register location
@param addr_buf Buffer containing register address
@param addrsiz Size of register address in bytes
@param buf Buffer containing data to write
@param bufsiz Size of data to write in bytes
@return true if write was successful, otherwise false */
@param addr_buf Buffer containing register address
@param addrsiz Size of register address in bytes
@param buf Buffer containing data to write
@param bufsiz Size of data to write in bytes
@return true if write was successful, otherwise false */
bool Adafruit_GenericDevice::writeRegister(uint8_t *addr_buf, uint8_t addrsiz,
const uint8_t *buf,
uint16_t bufsiz) {
if (!_begun || !_writereg_func)
return false;
return _writereg_func(addr_buf, addrsiz, buf, bufsiz);
return _writereg_func(_obj, addr_buf, addrsiz, buf, bufsiz);
}
23 changes: 14 additions & 9 deletions Adafruit_GenericDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@

#include <Arduino.h>

typedef bool (*busio_genericdevice_read_t)(uint8_t *buffer, size_t len);
typedef bool (*busio_genericdevice_write_t)(const uint8_t *buffer, size_t len);
typedef bool (*busio_genericdevice_readreg_t)(uint8_t *addr_buf,
uint8_t addrsiz, uint8_t *buf,
uint16_t bufsiz);
typedef bool (*busio_genericdevice_writereg_t)(uint8_t *addr_buf,
typedef bool (*busio_genericdevice_read_t)(void *obj, uint8_t *buffer,
size_t len);
typedef bool (*busio_genericdevice_write_t)(void *obj, const uint8_t *buffer,
size_t len);
typedef bool (*busio_genericdevice_readreg_t)(void *obj, uint8_t *addr_buf,
uint8_t addrsiz, uint8_t *data,
uint16_t datalen);
typedef bool (*busio_genericdevice_writereg_t)(void *obj, uint8_t *addr_buf,
uint8_t addrsiz,
const uint8_t *buf,
uint16_t bufsiz);
const uint8_t *data,
uint16_t datalen);

/*!
* @brief Class for communicating with a device via generic read/write functions
*/
class Adafruit_GenericDevice {
public:
Adafruit_GenericDevice(
busio_genericdevice_read_t read_func,
void *obj, busio_genericdevice_read_t read_func,
busio_genericdevice_write_t write_func,
busio_genericdevice_readreg_t readreg_func = nullptr,
busio_genericdevice_writereg_t writereg_func = nullptr);
Expand All @@ -45,6 +47,9 @@ class Adafruit_GenericDevice {

bool _begun; ///< whether we have initialized yet (in case the function needs
///< to do something)

private:
void *_obj; ///< Pointer to object instance
};

#endif // ADAFRUIT_GENERICDEVICE_H
2 changes: 1 addition & 1 deletion Adafruit_I2CDevice.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "Adafruit_I2CDevice.h"

//#define DEBUG_SERIAL Serial
// #define DEBUG_SERIAL Serial

/*!
* @brief Create an I2C device at a given address
Expand Down
2 changes: 1 addition & 1 deletion Adafruit_SPIDevice.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "Adafruit_SPIDevice.h"

//#define DEBUG_SERIAL Serial
// #define DEBUG_SERIAL Serial

/*!
* @brief Create an SPI device with the given CS pin and settings
Expand Down
2 changes: 1 addition & 1 deletion Adafruit_SPIDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ typedef BitOrder BusIOBitOrder;
// ports set and clear registers which are atomic.
// typedef volatile uint32_t BusIO_PortReg;
// typedef uint32_t BusIO_PortMask;
//#define BUSIO_USE_FAST_PINIO
// #define BUSIO_USE_FAST_PINIO

#elif defined(__MBED__) || defined(__ZEPHYR__)
// Boards based on RTOS cores like mbed or Zephyr are not going to expose the
Expand Down
Loading

0 comments on commit 055013b

Please sign in to comment.