Class: BBB::Pins::IO::GPIO

Inherits:
Object
  • Object
show all
Includes:
Mapped
Defined in:
lib/BBB/pins/io/gpio.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mapped

#pin_map, #pin_map_key

Constructor Details

#initialize(direction, position) ⇒ GPIO



9
10
11
12
13
14
# File 'lib/BBB/pins/io/gpio.rb', line 9

def initialize(direction, position)
  self.direction = direction
  @position = position
  @converted_position = pin_map.gpio
  self.export
end

Instance Attribute Details

#converted_positionObject (readonly)

Returns the value of attribute converted_position.



7
8
9
# File 'lib/BBB/pins/io/gpio.rb', line 7

def converted_position
  @converted_position
end

#directionObject

Returns the value of attribute direction.



7
8
9
# File 'lib/BBB/pins/io/gpio.rb', line 7

def direction
  @direction
end

#file_modeObject (readonly)

Returns the value of attribute file_mode.



7
8
9
# File 'lib/BBB/pins/io/gpio.rb', line 7

def file_mode
  @file_mode
end

#positionObject (readonly)

Returns the value of attribute position.



7
8
9
# File 'lib/BBB/pins/io/gpio.rb', line 7

def position
  @position
end

Instance Method Details

#exportObject



62
63
64
65
# File 'lib/BBB/pins/io/gpio.rb', line 62

def export
  file_class.open(export_path, "w") { |f| f.write("#{ converted_position }") }
  set_mode
end

#export_pathObject



50
51
52
# File 'lib/BBB/pins/io/gpio.rb', line 50

def export_path
  gpio_path + "/export"
end

#file_classObject



71
72
73
# File 'lib/BBB/pins/io/gpio.rb', line 71

def file_class
  File
end

#gpio_pathObject



46
47
48
# File 'lib/BBB/pins/io/gpio.rb', line 46

def gpio_path
  "/sys/class/gpio"
end

#gpio_pin_dirObject



58
59
60
# File 'lib/BBB/pins/io/gpio.rb', line 58

def gpio_pin_dir
  "#{gpio_path}/gpio#{converted_position}"
end

#ioObject



26
27
28
29
30
# File 'lib/BBB/pins/io/gpio.rb', line 26

def io
  return @io unless @io.nil?
  value_file = gpio_pin_dir + "/value"
  @io = file_class.open(value_file, file_mode)
end

#readObject



37
38
39
40
# File 'lib/BBB/pins/io/gpio.rb', line 37

def read
  io.rewind
  value_map[io.read]
end

#set_modeObject



21
22
23
24
# File 'lib/BBB/pins/io/gpio.rb', line 21

def set_mode
  direction_file = gpio_pin_dir + "/direction"
  file_class.open(direction_file, "w") {|f| f.write(direction)}
end

#unexportObject



67
68
69
# File 'lib/BBB/pins/io/gpio.rb', line 67

def unexport
  file_class.open(unexport_path, "w") { |f| f.write("#{converted_position}") }
end

#unexport_pathObject



54
55
56
# File 'lib/BBB/pins/io/gpio.rb', line 54

def unexport_path
  gpio_path + "/unexport"
end

#value_mapObject



42
43
44
# File 'lib/BBB/pins/io/gpio.rb', line 42

def value_map
  @value_map ||= {:high=>1, :low=>0, 1=>:high, 0=>:low}
end

#write(value) ⇒ Object



32
33
34
35
# File 'lib/BBB/pins/io/gpio.rb', line 32

def write(value)
  io.write(value_map[value])
  io.flush
end