Last active
December 12, 2015 02:58
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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