-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpi_gpio.rb
69 lines (58 loc) · 2.09 KB
/
rpi_gpio.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env ruby
# This file is in the public domain. Share and enjoy.
# Class for controlling one input/output pin on raspberry pi
class RPi_GPIO_Pin
EXPORT="/sys/class/gpio/export"
UNEXPORT="/sys/class/gpio/unexport"
VALUE="/sys/class/gpio/gpio%d/value"
DIRECTION="/sys/class/gpio/gpio%d/direction"
# Class method that creates a finalizer proc. The finalizer calls unexport.
def RPi_GPIO_Pin.create_finalizer(whichPin)
return proc{unexport(whichPin)}
end
# Class method which deactivates a pin
def RPi_GPIO_Pin.unexport(whichPin)
whichPin = canonicalizePin(whichPin)
IO.write(UNEXPORT, whichPin.to_s)
end
# Class method which activates a pin
def RPi_GPIO_Pin.export(whichPin)
whichPin = canonicalizePin(whichPin)
IO.write(EXPORT, whichPin.to_s)
end
# Set direction of an already activated pin
def RPi_GPIO_Pin.setDirection(whichPin, direction)
whichPin = canonicalizePin(whichPin)
if (direction == "out")
IO.write(DIRECTION % whichPin, direction)
else
raise("unsupported direction #{direction.inspect}")
end
end
# canonicalized the given pin specification to an Integer
def RPi_GPIO_Pin.canonicalizePin(whichPin)
return whichPin if (whichPin.is_a?(Integer))
whichPinStr = whichPin.to_s()
if (whichPinStr.length > 0)
return whichPinStr.to_i if (whichPinStr[0] =~ /\d/)
return whichPinStr[4..-1].to_i if (whichPinStr[0,5] =~ /gpio./i)
end
raise "invalid Pin specification #{whichPin.inspect}"
end
# Export the given GPIO Pin and set its direction.
# Adds a finalizer which unexports the PIN when Object is GCed.
def initialize(whichPin, direction)
@pin=RPi_GPIO_Pin.canonicalizePin(whichPin)
RPi_GPIO_Pin.export(@pin)
ObjectSpace.define_finalizer(self, RPi_GPIO_Pin.create_finalizer(@pin))
RPi_GPIO_Pin.setDirection(@pin,direction)
end
# for output pins, set the voltage to high (1) or low (0)
def set(value)
if (value == 0) || (value == 1)
IO.write(VALUE % @pin, "%d" % value)
else
raise "invalid value #{value.inspect}"
end
end
end