This website was originally written in Ruby and hosted on the Raspberry Pi. It's now archived at GitHub for posterity!

WiringPi As A Ruby Gem

Thanks in no small part to the excellent work of Gordon on his inspired WiringPi library, which I use for pretty much all my GPIO access, I present the WiringPi Ruby Gem

To install, simply "gem install wiringpi"

WiringPi is a library which seeks to make GPIO access on the Pi a very similar affair to that on the Arduino by providing simple read/write/mode language constructs.

It's pretty alpha at the moment, but as far as I know it works. Thank Gordon for all the hard work, I just figured out how to wrap it for Ruby and pack it up into a Gem file for ease of installation. With any luck it'll actually work for you, nobody has yet told me yay or nay.

Documentation will follow soon, but it's all pretty straight forward. By including a simple Ruby wrapper I've taken care of sanity checking inputs and calling the initWiringPi function for you. One caveat; you still need to run your Ruby scripts as root if you want them to be able to access the GPIO with the methods WiringPi uses. To do this you'll most likely need to call: "rvmsudo ruby myfile.rb", and you can play about with WiringPi with "rvmsudo irb" (assuming you use RVM, that is!)

Reading and writing to the GPIO pins is done like so:

require 'wiringpi'
WiringPi.mode(1,OUTPUT)
WiringPi.write(1,HIGH)
WiringPi.read(1)

I have a few little helpers planned for managing things like shift registers. If you want to get started shifting-out already, then I use something like this:

require 'wiringpi'

DATA_PIN = 2
CLOCK_PIN = 3
LATCH_PIN = 0

WiringPi.mode(DATA_PIN,OUTPUT);
WiringPi.mode(CLOCK_PIN,OUTPUT);
WiringPi.mode(LATCH_PIN,OUTPUT);

def shiftout(bits)

    WiringPi.write(LATCH_PIN,LOW);

    bits.each do |bit|

        WiringPi.write(CLOCK_PIN,LOW);

        WiringPi.write(DATA_PIN,bit);

        WiringPi.write(CLOCK_PIN,HIGH);

    end

    WiringPi.write(LATCH_PIN,HIGH);

end

# Output an array of 16 ones or zeros ( two shift registers )
shiftout [0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0]

# You can also do this
shiftout [HIGH, HIGH, LOW, LOW, HIGH, HIGH, LOW, LOW]

« Back to index Posted on 2012-06-22 by