Search code examples
c++arduinosparkfun

I am using "SparkFun Qwiic Keypad", For Keypad Library some do not understand


I am using "SparkFun Qwiic Keypad",

and I am learning about the Arduino_Library provided by "SparkFun".

Some do not understand.

In "SparkFun_Qwiic_Keypad_Arduino_Library.h",lines 50 and 62,

What is "TwoWire *_i2cPort" in it? what is the purpose?

I am a super newbie in C & C++, I hope everyone can help explain.

Thank you so much=]

lines (50) boolean begin(TwoWire &wirePort = Wire, uint8_t deviceAddress = QWIIC_KEYPAD_ADDR);

lines (61) private: lines (62) TwoWire *_i2cPort;

https://github.com/sparkfun/SparkFun_Qwiic_Keypad_Arduino_Library/blob/master/src/SparkFun_Qwiic_Keypad_Arduino_Library.h


Solution

  • The Sparkfun Qwiic series components use I2C protocol to communicate. So the sensors, actuators, displays are not directly connected to digital or analog pins of the Arduino, but all have a chip which uses I2C protocol to communicate with the MCU.

    The I2C uses two wires and is a bus where each device has its address. This allows to chain the Qwiic components.

    For license reasons the Arduino named the I2C library "Wire" and not I2C. The Wire library defines a class named TwoWire and creates a global object of class TwoWire. This global object is named Wire.

    The Qwiic libraries are initialized with function begin() in setup() of your sketch as it is usual in Arduino. In this case the begin() function has optional parameters.

    boolean begin(TwoWire &wirePort = Wire, uint8_t deviceAddress = QWIIC_KEYPAD_ADDR);
    

    The parameters are optional because default values are provided. The first optional parameter is a reference & to a object of type TwoWire, so TwoWire&. And the default value is the global Wire object I mentioned before.

    The begin() finction takes the TwoWire instance for use on Arduinos with multiple I2C peripheral, so to be able to use Wire1.

    The begin() functions stores a pointer to TwoWire object it should use. They named the pointer variable _i2cPort, so it is TwoWire *_i2cPort;