Class: Rupi::Pin

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

Defined Under Namespace

Classes: GPIO

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ Pin

Returns a new instance of Pin.



14
15
16
17
18
19
# File 'lib/rupi/pin.rb', line 14

def initialize(number)
  @number = number

  @up_handlers   = []
  @down_handlers = []
end

Instance Attribute Details

#down_handlersObject (readonly)

Returns the value of attribute down_handlers.



12
13
14
# File 'lib/rupi/pin.rb', line 12

def down_handlers
  @down_handlers
end

#numberObject (readonly)

Returns the value of attribute number.



12
13
14
# File 'lib/rupi/pin.rb', line 12

def number
  @number
end

#up_handlersObject (readonly)

Returns the value of attribute up_handlers.



12
13
14
# File 'lib/rupi/pin.rb', line 12

def up_handlers
  @up_handlers
end

Class Method Details

.gpioObject



25
26
27
# File 'lib/rupi/pin.rb', line 25

def self.gpio
  @gpio ||= GPIO.new
end

.join_watch_threadObject



122
123
124
125
# File 'lib/rupi/pin.rb', line 122

def self.join_watch_thread
  return unless @watch_thread
  @watch_thread.join
end

.start_watchingObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rupi/pin.rb', line 86

def self.start_watching
  @watching = true

  @watch_thread ||= Thread.new do

    while @watching && !@watched_pins.empty? do
      @watched_pins.each do |pin, previous_value|
        value = pin.value
        if previous_value && previous_value != value
          handlers = value == 1 ? pin.up_handlers : pin.down_handlers
          handlers.each do |handler|
            begin
              handler.call(value)
            rescue StandardError => e
              puts e
            end
          end
        end
        @watched_pins[pin] = value
      end
      sleep(0.1)
    end

  end
end

.stop_watchingObject



112
113
114
115
116
# File 'lib/rupi/pin.rb', line 112

def self.stop_watching
  @watching = false
  join_watch_thread
  @watch_thread = nil
end

.unwatch(pin) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/rupi/pin.rb', line 75

def self.unwatch(pin)
  return unless @watched_pins
  if pin == :all
    @watched_pins.keys.each { |p| unwatch(p) }
  else
    pin.down_handlers.clear
    pin.up_handlers.clear
    @watched_pins.delete(pin)
  end
end

.watch(pin) ⇒ Object



69
70
71
72
73
# File 'lib/rupi/pin.rb', line 69

def self.watch(pin)
  @watched_pins ||= {}
  @watched_pins[pin] ||= nil
  start_watching
end

.watching?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/rupi/pin.rb', line 118

def self.watching?
  @watching
end

Instance Method Details

#change(&block) ⇒ Object



63
64
65
66
67
# File 'lib/rupi/pin.rb', line 63

def change(&block)
  @up_handlers   << block
  @down_handlers << block
  self.class.watch(self)
end

#down(&block) ⇒ Object



58
59
60
61
# File 'lib/rupi/pin.rb', line 58

def down(&block)
  @down_handlers << block
  self.class.watch(self)
end

#gpioObject



21
22
23
# File 'lib/rupi/pin.rb', line 21

def gpio
  self.class.gpio
end

#input!(pud = PUD_DOWN) ⇒ Object



29
30
31
32
# File 'lib/rupi/pin.rb', line 29

def input!(pud = PUD_DOWN)
  gpio.mode(number, INPUT)
  gpio.pullUpDnControl(number, pud) if pud
end

#output!Object



34
35
36
# File 'lib/rupi/pin.rb', line 34

def output!
  gpio.mode(number, OUTPUT)
end

#toggleObject



49
50
51
# File 'lib/rupi/pin.rb', line 49

def toggle
  self.value = value != 1
end

#up(&block) ⇒ Object



53
54
55
56
# File 'lib/rupi/pin.rb', line 53

def up(&block)
  @up_handlers << block
  self.class.watch(self)
end

#valueObject



38
39
40
# File 'lib/rupi/pin.rb', line 38

def value
  gpio.read(number)
end

#value=(value) ⇒ Object



42
43
44
45
46
47
# File 'lib/rupi/pin.rb', line 42

def value=(value)
  value = 1 if value == true
  value = 0 if value == false

  gpio.write(number, value)
end