2020-11-28 11:55:37 +00:00
|
|
|
/*!
|
|
|
|
|
* @file Adafruit_I2CDevice.h
|
|
|
|
|
*/
|
2019-03-07 19:23:42 +00:00
|
|
|
#include <Wire.h>
|
|
|
|
|
|
2019-03-07 21:15:30 +00:00
|
|
|
#ifndef Adafruit_I2CDevice_h
|
|
|
|
|
#define Adafruit_I2CDevice_h
|
2019-03-07 19:23:42 +00:00
|
|
|
|
2019-05-18 19:37:08 +01:00
|
|
|
///< The class which defines how we will talk to this device over I2C
|
2019-03-07 21:15:30 +00:00
|
|
|
class Adafruit_I2CDevice {
|
2019-12-28 00:49:16 +00:00
|
|
|
public:
|
|
|
|
|
Adafruit_I2CDevice(uint8_t addr, TwoWire *theWire = &Wire);
|
2019-03-07 19:23:42 +00:00
|
|
|
uint8_t address(void);
|
2019-12-28 00:49:16 +00:00
|
|
|
bool begin(bool addr_detect = true);
|
2019-03-07 19:23:42 +00:00
|
|
|
bool detected(void);
|
2019-03-07 20:13:03 +00:00
|
|
|
|
2019-12-28 00:49:16 +00:00
|
|
|
bool read(uint8_t *buffer, size_t len, bool stop = true);
|
2020-11-28 11:55:37 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
|
/*!
|
|
|
|
|
* @brief Writes to a I2C device register.
|
|
|
|
|
*
|
|
|
|
|
* @param buffer
|
|
|
|
|
* A pointer to a buffer containing data bytes.
|
|
|
|
|
*
|
|
|
|
|
* @param len
|
|
|
|
|
* Number of bytes to write from the previous buffer.
|
|
|
|
|
*
|
|
|
|
|
* @param stop
|
|
|
|
|
* If a stop bit needs to be sent at the end of the transmission.
|
|
|
|
|
*
|
|
|
|
|
* @param prefix_buffer
|
|
|
|
|
* A pointer to a buffer containing bytes that needs to be send
|
|
|
|
|
* before data bytes (e.g. register address).
|
|
|
|
|
*
|
|
|
|
|
* @param prefix_len
|
|
|
|
|
* Number of bytes to write from prefix_buffer.
|
|
|
|
|
*
|
|
|
|
|
* @return True if the event read was successful, otherwise false.
|
|
|
|
|
*/
|
|
|
|
|
/**************************************************************************/
|
2020-11-26 09:17:49 +00:00
|
|
|
bool write(const uint8_t *buffer, size_t len, bool stop = true,
|
|
|
|
|
const uint8_t *prefix_buffer = NULL, size_t prefix_len = 0);
|
2020-11-28 11:55:37 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
|
/*!
|
|
|
|
|
* @brief Writes to and then read from a I2C device register.
|
|
|
|
|
*
|
|
|
|
|
* @param write_buffer
|
|
|
|
|
* A pointer to a buffer containing write bytes.
|
|
|
|
|
*
|
|
|
|
|
* @param write_len
|
|
|
|
|
* Number of bytes to write from the previous buffer.
|
|
|
|
|
*
|
|
|
|
|
* @param read_buffer
|
|
|
|
|
* A pointer to a buffer to store bytes from the read operation.
|
|
|
|
|
*
|
|
|
|
|
* @param read_len
|
|
|
|
|
* Number of bytes to read.
|
|
|
|
|
*
|
|
|
|
|
* @param stop
|
|
|
|
|
* If a stop bit needs to be sent at the end of the transmission.
|
|
|
|
|
*
|
|
|
|
|
* @return True if the event read was successful, otherwise false.
|
|
|
|
|
*/
|
|
|
|
|
/**************************************************************************/
|
2020-11-26 09:17:49 +00:00
|
|
|
bool write_then_read(const uint8_t *write_buffer, size_t write_len,
|
2019-12-28 00:49:16 +00:00
|
|
|
uint8_t *read_buffer, size_t read_len,
|
|
|
|
|
bool stop = false);
|
2020-05-14 21:56:27 +01:00
|
|
|
bool setSpeed(uint32_t desiredclk);
|
2019-03-07 21:15:30 +00:00
|
|
|
|
2019-10-20 06:53:07 +01:00
|
|
|
/*! @brief How many bytes we can read in a transaction
|
2019-12-28 00:49:16 +00:00
|
|
|
* @return The size of the Wire receive/transmit buffer */
|
2020-02-04 16:22:06 +00:00
|
|
|
size_t maxBufferSize() { return _maxBufferSize; }
|
2019-03-07 21:15:30 +00:00
|
|
|
|
2019-12-28 00:49:16 +00:00
|
|
|
private:
|
2019-03-07 21:15:30 +00:00
|
|
|
uint8_t _addr;
|
|
|
|
|
TwoWire *_wire;
|
|
|
|
|
bool _begun;
|
2020-02-04 16:22:06 +00:00
|
|
|
size_t _maxBufferSize;
|
2019-03-07 19:23:42 +00:00
|
|
|
};
|
2019-03-07 21:15:30 +00:00
|
|
|
|
|
|
|
|
#endif // Adafruit_I2CDevice_h
|