This website is written in Ruby using the Sinatra framework and hosted with Passenger / Nginx on the Raspberry Pi ( temporarily not on a Pi! ) assisted by Cloudflare and a dash of jsDelivr

WiringPi Version 2 With Extra Python

It's no secret that my WiringPi- libraries have been hideously out of date and for months. But some awesome work by Gordon Henderson has spurred me back into action.

First and foremost, those of you still loyal to the old WiringPi, and my old, well publicised and well used wrappers will be pleased to hear that I'm going to give them a little TLC and try to fix 'em up, push them to PyPi and have a nose through the various different issues reported to see if I can find any resolution.

Second, WiringPi 2. WiringPi 2 includes a great new feature, or I should call it a concept, which allows you to easily set up IO expanders and write to them as if they were a normal GPIO pin. No poking values into registers, of fussing about with the nuances of SPI or I2C. Just dial in the address of your MCP23017 or the pins of your shift register and use their pins like you would any other GPIO pins.

A simple example of this is as follows:

import wiringpi2

pin_base = 65
i2c_addr = 0x20
pins = [65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80]

wiringpi2.wiringPiSetup()
wiringpi2.mcp23017Setup(pin_base,i2c_addr)

for pin in pins:
	wiringpi2.pinMode(pin,1)
	wiringpi2.digitalWrite(pin,1)

Gordon dubs these brilliant little helpers "Extensions" and they make chaining a metric ton of IO expanders onto your Pi absurdly easy.

To demonstrate just how conceptually awesome this is, here's some of the code I wrote when first playing with the MCP23017:

def portA
        "0x%x" % ( ( @leds & FIRST_BYTE ) >> 8 )
end

def portB
        "0x%x" % ( @leds & SECOND_BYTE )
end

def ledOn( led )
        led = LEDMAP[ led ]
        @leds = @leds | led
        updatePorts
end

def ledOff( led )
        led = LEDMAP[ led ]
        @leds = @leds & ~led
        updatePorts
end

def updatePorts

        system 'i2cset -y 1 0x20 0x12 ' + portA + ' && i2cset -y 1 0x20 0x13 ' + portB

end

Ignoring the differences between Python and Ruby, this code is hideous. But I'm not going to complain, because it taught me a lot about how the MCP23017 chip actually works; and that's a good thing.

Anyway. Moving swiftly back to WiringPi 2. You can install it right now on your Pi with:

sudo pip install wiringpi2

If you have a hobbytronics IO expander or a quick2wire board with an IO expander you can get started with my example above and be rocking 16 additional IO pins with ease. Otherwise, WiringPi works pretty much how you'd expect for built-in GPIO. If you have trouble installing the Python version, tell me not Gordon!

Gordon has a new website for WiringPi inbound, you can find it here: http://wiringpi.com/

I have pushed the WiringPi2-Python source up to GitHub, which you can find here: https://github.com/Gadgetoid/WiringPi2-Python. Please note that it's early days for WiringPi 2, and my code is definitely not perfect, bug free and feature complete.

« Back to index Posted on 2013-03-27 by