Skip to content

Instantly share code, notes, and snippets.

@tiegz
Last active December 12, 2015 02:58
Show Gist options
  • Save tiegz/4702540 to your computer and use it in GitHub Desktop.
Save tiegz/4702540 to your computer and use it in GitHub Desktop.
Simple controls in Ruby for controlling the GPIO pins on RaspbPi (reference: http://elinux.org/images/2/2a/GPIOs.png)
def with_pin(pin)
begin
puts "Connecting..."
export(pin)
direction(pin, "out")
yield(pin)
ensure
puts "Disconnecting..."
unexport(pin)
end
end
def echo(input, gpio_file); `echo "#{input}" > /sys/class/gpio/#{gpio_file}`; end
def export(pin); echo(pin, "export"); end
def unexport(pin); echo(pin, "unexport"); end
def on(pin); echo(1, "gpio#{pin}/value"); end
def off(pin); echo(0, "gpio#{pin}/value"); end
def pwm(pin, value); echo(value, "gpio#{pin}/value"); end # only GPIO18 is PWM, 0
def direction(pin, direction="out"); echo(direction, "gpio#{pin}/direction"); end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment