Class: RPiPinOut
- Inherits:
-
Object
- Object
- RPiPinOut
- Defined in:
- lib/rpi_pinout.rb
Instance Method Summary collapse
- #blink(seconds = 0.5, duration: nil) ⇒ Object (also: #oscillate)
-
#initialize(id) ⇒ RPiPinOut
constructor
A new instance of RPiPinOut.
- #off(duration = nil) ⇒ Object (also: #stop, #low, #close, #unlock)
- #off? ⇒ Boolean
- #on(duration = nil) ⇒ Object (also: #high, #open, #lock)
- #on? ⇒ Boolean
-
#set_pin(val) ⇒ Object
set val with 0 (off) or 1 (on).
- #to_s ⇒ Object
Constructor Details
#initialize(id) ⇒ RPiPinOut
Returns a new instance of RPiPinOut.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rpi_pinout.rb', line 12 def initialize(id) @id = id unexport() File.write '/sys/class/gpio/export', id File.write "/sys/class/gpio/gpio#{id}/direction", 'out' at_exit { unexport() } end |
Instance Method Details
#blink(seconds = 0.5, duration: nil) ⇒ Object Also known as: oscillate
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rpi_pinout.rb', line 48 def blink(seconds=0.5, duration: nil) @state = :blink t2 = Time.now + duration if duration Thread.new do while @state == :blink do (set_pin HIGH; sleep seconds; set_pin LOW; sleep seconds) self.off if duration and Time.now >= t2 end end end |
#off(duration = nil) ⇒ Object Also known as: stop, low, close, unlock
31 32 33 34 35 36 37 |
# File 'lib/rpi_pinout.rb', line 31 def off(duration=nil) return if self.off? set_pin LOW @state = :off (sleep duration; self.on) if duration end |
#off? ⇒ Boolean
65 |
# File 'lib/rpi_pinout.rb', line 65 def off?() @state == :off end |
#on(duration = nil) ⇒ Object Also known as: high, open, lock
24 25 26 27 28 29 |
# File 'lib/rpi_pinout.rb', line 24 def on(duration=nil) set_pin HIGH; @state = :on (sleep duration; self.off) if duration end |
#on? ⇒ Boolean
64 |
# File 'lib/rpi_pinout.rb', line 64 def on?() @state == :on end |
#set_pin(val) ⇒ Object
set val with 0 (off) or 1 (on)
69 70 71 72 73 74 75 |
# File 'lib/rpi_pinout.rb', line 69 def set_pin(val) state = @state File.write "/sys/class/gpio/gpio#{@id}/value", val @state = state end |
#to_s ⇒ Object
77 78 79 |
# File 'lib/rpi_pinout.rb', line 77 def to_s() @id end |