pythonraspberry-pii2cadafruit-circuitpython

Where is the line of code in the Adafruit implementation of the PCA9685 PWM driver that writes to I2C?


Looking at the source of the PCA9685 python lib I do see that an instance of I2C is passed in with var name i2c_bus to create an instance of the PCA9685 class. The i2c_bus variable is used to create an instance of I2CDevice (source code of I2CDevice). But then I am lost. At some point this PWM driver should call the write method of the I2CDevice class I suppose. But this never happens! (FAFAIK). I also checked the PWMChannel class and PCAChannels class but neither do something related with i2c. Then I checked the pca9685_simpletest.py file and saw the following code:

# Set the PWM duty cycle for channel zero to 50%. duty_cycle is 16 bits to match other PWM objects
# but the PCA9685 will only actually give 12 bits of resolution.
pca.channels[0].duty_cycle = 0x7FFF

This should definitely cause some IO to happen on the I2C bus. But, no write method to some i2c bus or device there! It almost looks like DMA to me.

I have been looking certainly for an hour now, I must overlook something.

What is the mechanism of the PCA9685 class to write the data to the I2C bus?


Solution

  • It's the UnaryStruct and StructArray classes. Those are imported from adafruit_register.i2c_struct and adafruit_register.i2c_struct_array. When you write to an object of class UnaryStruct, it generate an I2C write of a single byte. When you write to StructArray, it generates a larger I2C write.

    Pretty clever, but it does hide the implementation.