Class: SimpleRaspberryPi::PinX

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_raspberrypi.rb

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ PinX

Returns a new instance of PinX.



17
18
19
20
21
22
23
24
# File 'lib/simple_raspberrypi.rb', line 17

def initialize(id)

  File.write '/sys/class/gpio/export', id
  File.write "/sys/class/gpio/gpio#{id}/direction", 'out'

  @id = id
  
end

Instance Method Details



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/simple_raspberrypi.rb', line 50

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



33
34
35
36
37
38
39
# File 'lib/simple_raspberrypi.rb', line 33

def off(duration=nil)

  return if self.off?
  set_pin LOW      
  @state = :off
  (sleep duration; self.on) if duration
end

#off?Boolean

Returns:

  • (Boolean)


67
# File 'lib/simple_raspberrypi.rb', line 67

def off?() @state == :off end

#on(duration = nil) ⇒ Object Also known as: high, open, lock



26
27
28
29
30
31
# File 'lib/simple_raspberrypi.rb', line 26

def on(duration=nil)

  set_pin HIGH; 
  @state = :on
  (sleep duration; self.off) if duration
end

#on?Boolean

Returns:

  • (Boolean)


66
# File 'lib/simple_raspberrypi.rb', line 66

def on?()  @state == :on  end

#set_pin(val) ⇒ Object

set val with 0 (off) or 1 (on)



71
72
73
74
75
76
# File 'lib/simple_raspberrypi.rb', line 71

def set_pin(val)

  state = @state
  File.write "/sys/class/gpio/gpio#{@id}/value", val
  @state = state
end

#to_sObject



78
79
80
# File 'lib/simple_raspberrypi.rb', line 78

def to_s()
  @id
end