Class: Brewby::Outputs::GPIO

Inherits:
Object
  • Object
show all
Defined in:
lib/brewby/outputs/gpio.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GPIO

Returns a new instance of GPIO.



5
6
7
8
9
10
11
# File 'lib/brewby/outputs/gpio.rb', line 5

def initialize options = {}
  @pin = options[:pin]
  @gpio_path = options[:gpio_path] || '/sys/class/gpio'

  initialize_gpio_pin
  initialize_gpio_direction
end

Instance Attribute Details

#gpio_pathObject (readonly)

Returns the value of attribute gpio_path.



4
5
6
# File 'lib/brewby/outputs/gpio.rb', line 4

def gpio_path
  @gpio_path
end

#pinObject (readonly)

Returns the value of attribute pin.



4
5
6
# File 'lib/brewby/outputs/gpio.rb', line 4

def pin
  @pin
end

Instance Method Details

#initialize_gpio_directionObject



19
20
21
# File 'lib/brewby/outputs/gpio.rb', line 19

def initialize_gpio_direction
  File.write File.join(gpio_path, "gpio#{pin}", "direction"), 'out'
end

#initialize_gpio_pinObject



13
14
15
16
17
# File 'lib/brewby/outputs/gpio.rb', line 13

def initialize_gpio_pin
  unless File.exists? File.join(gpio_path, "gpio#{pin}", "value")
    File.write File.join(gpio_path, "export"), pin
  end
end

#offObject



27
28
29
# File 'lib/brewby/outputs/gpio.rb', line 27

def off
  write(0)
end

#onObject



23
24
25
# File 'lib/brewby/outputs/gpio.rb', line 23

def on
  write(1)
end

#on?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/brewby/outputs/gpio.rb', line 31

def on?
  '1' == File.read(File.join(gpio_path, "gpio#{pin}", "value"))
end

#write(value) ⇒ Object



35
36
37
# File 'lib/brewby/outputs/gpio.rb', line 35

def write value
  File.write File.join(gpio_path, "gpio#{pin}", "value"), value
end