Class: BBB::Pins::IO::PWM

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cape

#cape_dir

Methods included from Mapped

#pin_map, #pin_map_key

Constructor Details

#initialize(position) ⇒ PWM

Returns a new instance of PWM.



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

def initialize(position)
  @position = position
  self.export
  @handles = get_file_handles
end

Instance Attribute Details

#handlesObject (readonly)

Returns the value of attribute handles.



8
9
10
# File 'lib/BBB/pins/io/pwm.rb', line 8

def handles
  @handles
end

#positionObject (readonly)

Returns the value of attribute position.



8
9
10
# File 'lib/BBB/pins/io/pwm.rb', line 8

def position
  @position
end

Instance Method Details

#exportObject



35
36
37
38
39
40
41
# File 'lib/BBB/pins/io/pwm.rb', line 35

def export
  pin_map_key = pin_map.key # This calls the pin map, which raises an error in case pin can't be mapped.

  system("echo am33xx_pwm > #{cape_dir}")
  system("echo bone_pwm_#{pin_map_key} > #{cape_dir}")
  sleep(0.2) # This seems to be necessary for te driver to load
end

#get_file_handlesObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/BBB/pins/io/pwm.rb', line 21

def get_file_handles
  handles = {}
  files = %w(duty period polarity run)

  files.each do |file|
    file_path = File.expand_path(file, path)
    f = File.open(file_path, "r+")
    f.sync = true
    handles[file.to_sym] = f
  end

  return handles
end

#pathObject



16
17
18
19
# File 'lib/BBB/pins/io/pwm.rb', line 16

def path
  return @path unless @path.nil?
  @path = Dir.glob("/sys/devices/ocp.*/pwm_test_#{pin_map.key}.*").first
end

#read(symbol) ⇒ Object



54
55
56
57
58
# File 'lib/BBB/pins/io/pwm.rb', line 54

def read(symbol)
  handle = handles[symbol]
  handle.rewind
  handle.read.to_i
end

#write(symbol, value) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/BBB/pins/io/pwm.rb', line 43

def write(symbol, value)
  handle = handles[symbol]
  handle.rewind
  begin
    handle.write(value)
  rescue Errno::EINVAL
    puts "Could not write the value #{value} to the handle #{symbol}"
  end
  return value
end