I created a python module that makes interacting with specific hardware easy. You can find it here:
µCasts Raspberry Pi Library
I’ve found myself writing quite a bit of code lately to interface with different pieces of hardware on the Raspberry Pi. I finally realized that for every new project I was copying the same code from the previous project to do things like turn on an LED or read the state of a switch. I decided that it was time to take all of that work and put it into a library that I could easily reuse with each project.
The python and node modules I’ve used to create projects in the past are very simple and generally easy to use. What they don’t provide, and aren’t meant to provide, is a higher level of abstraction around certain pieces of hardware. For example, let’s look at the TMP102 Sensor.
TMP102 image provided by Sparkfun as CC BY-NC-SA 3.0
It’s an I²C temperature sensor that will return a reading of the current temperature. But like any I²C module you need to know things like what registers to set and read to get that reading and how the reading is returned by the sensor. For this sensor the current reading is accessed via register 0x00 of the module and is returned as a 12-bit value which requires a two byte read. Then you have to know that the value you get back is an integer that needs to be multiplied by .0625 to get a reading in degrees Celsius. Here’s what the code looks like.
Not overly complicated but it took some work and time reading through the datasheet. Now apply this to everything in your project; display, buttons, switches, etc.
What I wanted was to drop a module into my project that had all of this code I previously figured out wrapped up nicely and ready to use. So here is how I get a temperature reading from the TMP102 with the ucasts Raspberry Pi python module.
from ucasts import TMP102
The library handles the I²C bus and all other sensor-specific details. I’ve added a handful of components already and intend to expand it as I experiment with more modules for µCast episodes. I’ve tried to thoroughly document it so it’s easy to pick up and use.
If you do use the library I’d love to hear about it and I’m always happy to answer any questions.