Class: BBB::Pins::IO::PWM
- Inherits:
-
Object
- Object
- BBB::Pins::IO::PWM
- Defined in:
- lib/BBB/pins/io/pwm.rb
Instance Attribute Summary collapse
-
#handles ⇒ Object
readonly
Returns the value of attribute handles.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
Instance Method Summary collapse
- #export ⇒ Object
- #get_file_handles ⇒ Object
-
#initialize(position) ⇒ PWM
constructor
A new instance of PWM.
- #path ⇒ Object
- #read(symbol) ⇒ Object
- #write(symbol, value) ⇒ Object
Methods included from Cape
Methods included from Mapped
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
#handles ⇒ Object (readonly)
Returns the value of attribute handles.
8 9 10 |
# File 'lib/BBB/pins/io/pwm.rb', line 8 def handles @handles end |
#position ⇒ Object (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
#export ⇒ Object
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_handles ⇒ Object
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.(file, path) f = File.open(file_path, "r+") f.sync = true handles[file.to_sym] = f end return handles end |
#path ⇒ Object
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 |