Class: PinX

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

Overview

This is a base clase used by gems rpi_pwm, rpi_led_simulator, myfirmata-plugin-led, and simple_wiimote.

Constant Summary collapse

HIGH =
true
LOW =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ PinX

Returns a new instance of PinX.



15
16
17
18
# File 'lib/pinx.rb', line 15

def initialize(id)
  @id = id
  @on, @blinking = false, false
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



13
14
15
# File 'lib/pinx.rb', line 13

def id
  @id
end

Instance Method Details



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pinx.rb', line 57

def blink(seconds=0.5, duration: nil)

  self.stop if blinking?
  
  @blinking = true
  t2 = Time.now + duration if duration

  Thread.new do
    while @blinking do
      
      set_pin HIGH
      sleep seconds / 2.0
      break if !@blinking
      sleep seconds / 2.0 
      break if !@blinking
      
      set_pin LOW
      sleep seconds / 2.0
      break if !@blinking
      sleep seconds / 2.0 
      break if !@blinking
      
      self.off if duration and Time.now >= t2
    end
    
  end
end

#blinking?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/pinx.rb', line 20

def blinking?()
  @blinking
end

#off(durationx = nil, duration: nil) ⇒ Object Also known as: stop, low, close, unlock



35
36
37
38
39
40
41
42
43
44
# File 'lib/pinx.rb', line 35

def off(durationx=nil, duration: nil)
  
  set_pin LOW
  @on, @blinking = false, false      
  duration ||=  durationx
  
  @on_thread.exit if @on_thread
  @off_thread = Thread.new { (sleep duration; on()) } if duration

end

#off?Boolean

Returns:

  • (Boolean)


88
# File 'lib/pinx.rb', line 88

def off?() !@on end

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



24
25
26
27
28
29
30
31
32
33
# File 'lib/pinx.rb', line 24

def on(durationx=nil, duration: nil)
  
  set_pin HIGH
  @on, @blinking = true, false
  duration ||=  durationx
  
  @off_thread.exit if @off_thread
  @on_thread = Thread.new {(sleep duration; off()) } if duration

end

#on?Boolean

Returns:

  • (Boolean)


87
# File 'lib/pinx.rb', line 87

def on?()  @on  end

#to_sObject



90
91
92
# File 'lib/pinx.rb', line 90

def to_s()
  @id
end