Class: SimpleRaspberryPi::PinX

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

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ PinX

Returns a new instance of PinX.



21
22
23
24
25
26
# File 'lib/simple_raspberrypi.rb', line 21

def initialize(id)
  
  GPIO.setup id, :as => :output
  @id = id
  
end

Instance Method Details



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

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



35
36
37
38
39
40
41
# File 'lib/simple_raspberrypi.rb', line 35

def off(duration=nil)

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

#off?Boolean

Returns:



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

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

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



28
29
30
31
32
33
# File 'lib/simple_raspberrypi.rb', line 28

def on(duration=nil)

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

#on?Boolean

Returns:



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

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

#set_pin(val) ⇒ Object

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



73
74
75
76
77
78
# File 'lib/simple_raspberrypi.rb', line 73

def set_pin(val)

  state = @state
  val == HIGH ? GPIO.set_high(@id) : GPIO.set_low(@id)
  @state = state
end

#to_sObject



80
81
82
# File 'lib/simple_raspberrypi.rb', line 80

def to_s()
  @id
end