-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from adafruit/genericdevice
add generic device for non-standard transports
- Loading branch information
Showing
10 changed files
with
491 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
Written with help by Claude! | ||
https://claude.ai/chat/335f50b1-3dd8-435e-9139-57ec7ca26a3c (at this time | ||
chats are not shareable :( | ||
*/ | ||
|
||
#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) */ | ||
Adafruit_GenericDevice::Adafruit_GenericDevice( | ||
busio_genericdevice_read_t read_func, | ||
busio_genericdevice_write_t write_func, | ||
busio_genericdevice_readreg_t readreg_func, | ||
busio_genericdevice_writereg_t writereg_func) { | ||
_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 */ | ||
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 */ | ||
bool Adafruit_GenericDevice::write(const uint8_t *buffer, size_t len) { | ||
if (!_begun) | ||
return false; | ||
return _write_func(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 */ | ||
bool Adafruit_GenericDevice::read(uint8_t *buffer, size_t len) { | ||
if (!_begun) | ||
return false; | ||
return _read_func(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 */ | ||
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); | ||
} | ||
|
||
/*! @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 */ | ||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#ifndef ADAFRUIT_GENERICDEVICE_H | ||
#define ADAFRUIT_GENERICDEVICE_H | ||
|
||
#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, | ||
uint8_t addrsiz, | ||
const uint8_t *buf, | ||
uint16_t bufsiz); | ||
|
||
/*! | ||
* @brief Class for communicating with a device via generic read/write functions | ||
*/ | ||
class Adafruit_GenericDevice { | ||
public: | ||
Adafruit_GenericDevice( | ||
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); | ||
|
||
bool begin(void); | ||
|
||
bool read(uint8_t *buffer, size_t len); | ||
bool write(const uint8_t *buffer, size_t len); | ||
bool readRegister(uint8_t *addr_buf, uint8_t addrsiz, uint8_t *buf, | ||
uint16_t bufsiz); | ||
bool writeRegister(uint8_t *addr_buf, uint8_t addrsiz, const uint8_t *buf, | ||
uint16_t bufsiz); | ||
|
||
protected: | ||
/*! @brief Function pointer for reading raw data from the device */ | ||
busio_genericdevice_read_t _read_func; | ||
/*! @brief Function pointer for writing raw data to the device */ | ||
busio_genericdevice_write_t _write_func; | ||
/*! @brief Function pointer for reading a 'register' from the device */ | ||
busio_genericdevice_readreg_t _readreg_func; | ||
/*! @brief Function pointer for writing a 'register' to the device */ | ||
busio_genericdevice_writereg_t _writereg_func; | ||
|
||
bool _begun; ///< whether we have initialized yet (in case the function needs | ||
///< to do something) | ||
}; | ||
|
||
#endif // ADAFRUIT_GENERICDEVICE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.