Class: Beaglebone::PWMPin

Inherits:
Object
  • Object
show all
Defined in:
lib/beaglebone/pwm.rb

Overview

Object Oriented PWM Implementation. This treats the pin as an object.

Instance Method Summary collapse

Constructor Details

#initialize(pin, duty = nil, frequency = nil, polarity = nil, run = true) ⇒ PWMPin

Initialize a PWM pin

Examples:

p9_14 = PWMPin.new(:P9_14, 90, 10, :NORMAL)

Parameters:

  • duty (defaults to: nil)

    should specify the duty cycle

  • frequency (defaults to: nil)

    should specify cycles per second

  • polarity (defaults to: nil)

    optional, should specify the polarity, :NORMAL or :INVERTED

  • run (defaults to: true)

    optional, if false, pin will be configured but will not run



416
417
418
419
# File 'lib/beaglebone/pwm.rb', line 416

def initialize(pin, duty=nil, frequency=nil, polarity=nil, run=true)
  @pin = pin
  PWM::start(@pin, duty, frequency, polarity, run)
end

Instance Method Details

#disable_pwm_pinObject

Disable PWM pin



478
479
480
# File 'lib/beaglebone/pwm.rb', line 478

def disable_pwm_pin
  PWM::disable_pwm_pin(@pin)
end

#runObject

Start PWM output on pin. Pin must have been previously started



427
428
429
# File 'lib/beaglebone/pwm.rb', line 427

def run
  PWM::run(@pin)
end

#set_duty_cycle(duty, newperiod = nil) ⇒ Object

Set duty cycle of pin in percentage

Examples:

p9_14.set_duty_cycle(25)

Parameters:

  • duty

    should specify the duty cycle in percentage



446
447
448
# File 'lib/beaglebone/pwm.rb', line 446

def set_duty_cycle(duty, newperiod=nil)
  PWM::set_duty_cycle(@pin, duty, newperiod)
end

#set_duty_cycle_ns(duty) ⇒ Object

Set duty cycle of pin in nanoseconds

Examples:

p9_14.set_duty_cycle_ns(2500000)

Parameters:

  • duty

    should specify the duty cycle in nanoseconds



455
456
457
# File 'lib/beaglebone/pwm.rb', line 455

def set_duty_cycle_ns(duty)
  PWM::set_duty_cycle_ns(@pin, duty)
end

#set_frequency(frequency) ⇒ Object

Set frequency of pin in cycles per second

Examples:

p9_14.set_frequency(100)

Parameters:

  • frequency

    should specify the frequency in cycles per second



464
465
466
# File 'lib/beaglebone/pwm.rb', line 464

def set_frequency(frequency)
  PWM::set_frequency(@pin, frequency)
end

#set_period_ns(period) ⇒ Object

Set frequency of pin based on period duration

Examples:

p9_14.set_frequency_ns(100000000)

Parameters:

  • period

    should specify the length of a cycle in nanoseconds



473
474
475
# File 'lib/beaglebone/pwm.rb', line 473

def set_period_ns(period)
  PWM::set_period_ns(@pin, period)
end

#set_polarity(polarity) ⇒ Object

Set polarity on pin

Examples:

p9_14.set_polarity(:INVERTED)

Parameters:

  • polarity

    should specify the polarity, :NORMAL or :INVERTED



436
437
438
# File 'lib/beaglebone/pwm.rb', line 436

def set_polarity(polarity)
  PWM::set_polarity(@pin, polarity)
end

#stopObject

Stop PWM output on pin



422
423
424
# File 'lib/beaglebone/pwm.rb', line 422

def stop
  PWM::stop(@pin)
end