Class: GPIO::Pin

Inherits:
Object
  • Object
show all
Includes:
Concurrent::Async
Defined in:
lib/ruby-gpio.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, number) ⇒ Pin

Returns a new instance of Pin.



17
18
19
20
21
# File 'lib/ruby-gpio.rb', line 17

def initialize(name, number)
  init_mutex
  @name = name
  @number = number
end

Instance Attribute Details

#directionObject

Returns the value of attribute direction.



15
16
17
# File 'lib/ruby-gpio.rb', line 15

def direction
  @direction
end

#nameObject

Returns the value of attribute name.



15
16
17
# File 'lib/ruby-gpio.rb', line 15

def name
  @name
end

#numberObject

Returns the value of attribute number.



15
16
17
# File 'lib/ruby-gpio.rb', line 15

def number
  @number
end

Instance Method Details

#as(dir = :out) ⇒ Object

set input or output gpio_num is the GPIO pin number, dir is the direction - either :out or :in



25
26
27
28
# File 'lib/ruby-gpio.rb', line 25

def as(dir=:out)
  @direction = dir.to_s
  GPIO.write "gpio#{@number}/direction", @direction
end

#offObject

send a low value to the pin



36
37
38
# File 'lib/ruby-gpio.rb', line 36

def off
  GPIO.write "gpio#{@number}/value", "0"
end

#onObject

send a high value to the pin



31
32
33
# File 'lib/ruby-gpio.rb', line 31

def on
  GPIO.write "gpio#{@number}/value", "1"
end

#pwm(value) ⇒ Object

send a PWM (pulse width modulation) signal to the pin



41
42
43
# File 'lib/ruby-gpio.rb', line 41

def pwm(value)
  GPIO.write "gpio#{@number}/value", value
end

#readObject

read from the pin



46
47
48
# File 'lib/ruby-gpio.rb', line 46

def read
  GPIO.read @number
end

#watch_for(value, &block) ⇒ Object

watch the pin for change and trigger the block over and over again



62
63
64
65
66
67
68
# File 'lib/ruby-gpio.rb', line 62

def watch_for(value, &block)
  while true        
    if read == value
      GPIO.instance_eval &block
    end
  end
end

#watch_once_for(value, &block) ⇒ Object

watch the pin for change and trigger the block once



51
52
53
54
55
56
57
58
59
# File 'lib/ruby-gpio.rb', line 51

def watch_once_for(value, &block)
  watching = true
  while watching        
    if read == value
      GPIO.instance_eval &block
      watching = false
    end
  end
end