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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pinx.rb', line 53

def blink(seconds=0.5, duration: nil)

  @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

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



32
33
34
35
36
37
38
39
40
41
# File 'lib/pinx.rb', line 32

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)


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

def off?() !@on end

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



21
22
23
24
25
26
27
28
29
30
# File 'lib/pinx.rb', line 21

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)


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

def on?()  @on  end

#to_sObject



84
85
86
# File 'lib/pinx.rb', line 84

def to_s()
  @id
end