I've packaged the quick2wire python3 library for the Raspberry Pi. This provides easy access to the i2c peripheral bus from Python3; I've packaged this up because I need it, and also to test and demo the package review process for Pidora.
Here's a little demo of the quick2wire library in action which I wrote some time back and have been using as a test for the package -- this reads a TCN75A (data sheet) thermal sensor chip:
#!/usr/bin/python3 # # test_tcn75a :: Test of reading a TCN75A digital # temperature sensor using I2C # # Assumes: # - TCN75A is powered at 3v3 # - I2C lines connected to Raspi GPIO # - Pins 5/6/7 are grounded (address = 72) # # Uses: # - quick2wire Python library # # CTyler 2012-10-03 - GPLv2+
# Using the quick2wire module for I2C access import quick2wire.i2c as i2c
# Using the time module for sleeping import time
# Address (unit number) of the TCN75A temperature # sensor on the I2C bus address = 72
# Register number within the TCN75A that contains # the current temperature temp_register = 0
# Register number within the TCN75A that contains # the configuration register conf_register = 1
with i2c.I2CMaster() as bus:
# Configure the resolution (optional step) # The configuration register is used to set the temperature # resolution. The higher the resolution, the more # accurate the temperature reading, but the lower the # sampling rate. Possible values are 0, 32, 64, and 96. # Value 96 = 0.01625C steps (highest resolution) # Value 0 = 0.5C steps (lowest resolution) (default) # bus.transaction( i2c.writing_bytes(address, conf_register, 96))
# Loop 100 times for i in range(1,100):
# Select the address (unit on the bus) and desired # register, and read 2 bytes read_temp = bus.transaction( i2c.writing_bytes(address, temp_register), i2c.reading(address,2))
# The first byte contains the temperature in degrees # Celsius (actually, this is a signed number, so # values over 127 are negative, but I'm ignoring # that here). The second byte contains 256ths of a # degress, but the default resolution of the sensor # is 0.5 degrees, so it will always be 0 (.0) or 128 (0.5) # unless the resolution is changed. # # This line converts the two bytes into a single # temperature and prints it. print("Temperature: %3.3f°C" % (read_temp+read_temp/256) )
# Delay half a second before getting next reading time.sleep(0.5)
The package is up for review in Pidora (not Fedora, but only because it's not useful on other platforms -- at least at this time). The package review, including links to the specfile and SRPM, is ticket #495.
These are my first two books: X Power Tools, a thorough guide to the X Window System (O'Reilly, ISBN 9780596101954) and Fedora Linux: A Complete Guide to Red Hat's Community Distro, a practical hands-on book on Fedora (O'Reilly, ISBN 9780596526825).
Fedora Linux is also available for online reading through Safari and in downloadable PDF format from oreilly.com