/Simple_Wire

TowWire Library Helper Functions

Primary LanguageC++MIT LicenseMIT

Simple_Wire Library

This code implements a simple wrapper library for the Wire library in Arduino, which provides a set of convenient methods for communicating with I2C devices. The Simple_Wire library is designed to simplify I2C communication by providing a set of methods for reading and writing data to I2C devices in a simple and consistent manner.

The library provides methods for reading and writing individual bits, bytes, and integers, as well as methods for reading and writing arrays of data. The library also provides a method for setting the I2C address of the device to communicate with.

The library is designed to work with a variety of microcontrollers, including the AVR, ESP8266, ESP32, and Arduino RP2040. The library also includes support for setting the I2C clock speed and timeout values.

Using the Simple_Wire library can simplify the process of communicating with I2C devices in your Arduino project, allowing you to focus on your application logic rather than the details of the I2C protocol.

This code is the implementation of the Simple_Wire class, which provides an easy-to-use interface for I2C communication using the Wire library.

The class provides a number of methods for reading and writing data over I2C, with support for 8-bit, 16-bit, and bit-level access.

The class has a constructor that takes the SDA and SCL pins as arguments and initializes the Wire library accordingly. There is also a default constructor that doesn't initialize the Wire library, but this should not be called until after setup(), as the Wire library requires initialization before use.

The begin() method is used to initialize the Wire library with the given SDA and SCL pins. This method must be called at the earliest point in the setup() function of the main sketch, before any other methods of the Simple_Wire class are used.

The SetAddress() method sets the I2C address of the device that the Simple_Wire object will communicate with. This method returns a reference to the object, allowing for chaining of methods.

The SetIntMSBPos() method is used to set the position of the most significant bit (MSB) in a 16-bit integer. This is used to support devices that have different byte orderings for their data.

The ReadBit() method is used to read a single bit from a device register. The ReadByte() method is used to read a single byte. The ReadBytes() method is used to read multiple bytes.

The ReadInt() method is used to read a 16-bit integer from a device register. The ReadInts() method is used to read multiple 16-bit integers. The ReadUInt() and ReadUInts() methods are similar, but are used for unsigned integers.

The WriteBit() method is used to write a single bit to a device register. The WriteByte() method is used to write a single byte. The WriteBytes() method is used to write multiple bytes.

The WriteInt() method is used to write a 16-bit integer to a device register. The WriteInts() method is used to write multiple 16-bit integers. The WriteUInt() and WriteUInts() methods are similar, but are used for unsigned integers.

Macros can be used to quickly gather data from the desired registers for example: #define R_LIA_Data(Data) PG(0).ReadInts(0x28, 3, (int16_t *)Data)// Linear Acceleration Data. #define R_LIA_Data_X(Data) PG(0).ReadInt(0x28, (int16_t *)Data) // Linear Acceleration Data X <15:0> #define R_LIA_Data_Y(Data) PG(0).ReadInt(0x2A, (int16_t *)Data) // Linear Acceleration Data Y <15:0> #define R_LIA_Data_Z(Data) PG(0).ReadInt(0x2C, (int16_t *)Data) // Linear Acceleration Data Z <15:0>

Here is an explanation of the code:

  • The code defines a set of macros for reading data from a device register using the Simple_Wire library.
  • The macros use the ReadInt() and ReadInts() methods of the Simple_Wire class to read data from the specified register address.
  • The macro R_LIA_Data() reads all 3 INTs of data in one go and stores the result in the specified array.
  • The macros R_LIA_Data_X(), R_LIA_Data_Y(), and R_LIA_Data_Z() read individual INTs of data for each axis.
  • Using these macros simplifies the process of reading data from the device register, reducing the amount of code required.

Overall, this approach allows for easy and efficient access to the device register with minimal code.