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.

Defined Under Namespace

Classes: Void

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.



22
23
24
25
# File 'lib/pinx.rb', line 22

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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

Instance Method Details



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pinx.rb', line 64

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)


27
28
29
# File 'lib/pinx.rb', line 27

def blinking?()
  @blinking
end

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



42
43
44
45
46
47
48
49
50
51
# File 'lib/pinx.rb', line 42

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)


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

def off?() !@on end

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



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

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)


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

def on?()  @on  end

#to_sObject



97
98
99
# File 'lib/pinx.rb', line 97

def to_s()
  @id
end