Class: PiPiper::Pwm

Inherits:
Object
  • Object
show all
Includes:
PinValues
Defined in:
lib/pi_piper/pwm.rb

Overview

Represents a Pwm output on the Raspberry Pi (only GPIO18 is avaliable on header)

Constant Summary

Constants included from PinValues

PiPiper::PinValues::GPIO_FSEL_ALT0, PiPiper::PinValues::GPIO_FSEL_ALT1, PiPiper::PinValues::GPIO_FSEL_ALT2, PiPiper::PinValues::GPIO_FSEL_ALT3, PiPiper::PinValues::GPIO_FSEL_ALT4, PiPiper::PinValues::GPIO_FSEL_ALT5, PiPiper::PinValues::GPIO_FSEL_INPT, PiPiper::PinValues::GPIO_FSEL_MASK, PiPiper::PinValues::GPIO_FSEL_OUTP, PiPiper::PinValues::GPIO_HIGH, PiPiper::PinValues::GPIO_LOW, PiPiper::PinValues::GPIO_PUD_DOWN, PiPiper::PinValues::GPIO_PUD_OFF, PiPiper::PinValues::GPIO_PUD_UP, PiPiper::PinValues::PWM_MODE, PiPiper::PinValues::PWM_PIN

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Pwm

Initializes a new PWM pin.

Parameters:

  • options (Hash)

    A hash of options

Options Hash (options):

  • :pin (Fixnum)

    The pin number to initialize. Required.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pi_piper/pwm.rb', line 15

def initialize(options)
  @options = {
    :mode => :balanced,
    :clock => 19.2.megahertz,
    :range => 1024,
    :start => true,
    :value => 0
  }.merge(options)

  unless PWM_PIN[@options[:pin]]
    raise ArgumentError, ":pin should be one of  #{PWM_PIN.keys} not #{@options[:pin]}"
  end

  unless PWM_MODE.include? @options[:mode]
    raise ArgumentError, ":mode should be one of #{PWM_MODE}, not #{@options[:mode]}"
  end

  self.value= @options.delete(:value)
  self.pin=   @options[:pin]
  self.range= @options[:range]
  self.clock= @options[:clock]

  @options.delete(:start) ? self.on : self.off
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options



9
10
11
# File 'lib/pi_piper/pwm.rb', line 9

def options
  @options
end

#valueObject

Returns the value of attribute value



9
10
11
# File 'lib/pi_piper/pwm.rb', line 9

def value
  @value
end

Instance Method Details

#offObject Also known as: stop

Stop the Pwm signal



49
50
51
52
# File 'lib/pi_piper/pwm.rb', line 49

def off
  @on = false
  Platform.driver.pwm_mode(PWM_PIN[@options[:pin]][:channel], PWM_MODE.index(options[:mode]), 0)
end

#off?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/pi_piper/pwm.rb', line 60

def off?
  not on?
end

#onObject Also known as: start

Start the Pwm signal



41
42
43
44
# File 'lib/pi_piper/pwm.rb', line 41

def on
  @on = true
  Platform.driver.pwm_mode(PWM_PIN[@options[:pin]][:channel], PWM_MODE.index(options[:mode]), 1)
end

#on?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/pi_piper/pwm.rb', line 56

def on?
  @on
end