Class: PiPiper::Pwm

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Pwm

Initializes a new PWM pin.

either :balanced or :markspace but depends from driver. Defaults to :balanced.

Defaults to 19.2Mhz.

for the value. Defaults to 1024.

Defaults to 0.

Parameters:

  • options (Hash)

    A hash of options

Options Hash (options):

  • :pin (Fixnum)

    The pin number to initialize. Required.

  • :mode (Symbol)

    The modulation's mode,

  • :clock (Fixnum)

    Indicates the internal clock of the signal.

  • :range (Fixnum)

    Indicates the number of step allowed

  • :value (Fixnum)

    Indicates the value at_start.


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pi_piper/pwm.rb', line 24

def initialize(options)
  @options = {
    :mode => :balanced,
    :clock => 19.2.megahertz,
    :range => 1024,
    :value => 0,
  }.merge(options)
  
  PiPiper.driver.pwm_set_pin(@options[:pin])
  PiPiper.driver.pwm_set_clock(get_clock_divider(@options[:clock]))
  PiPiper.driver.pwm_set_range(@options[:pin], options[:range])
  
  self.value= @options.delete(:value)

  PiPiper.driver.pwm_set_mode(@options[:pin], options[:mode])
  
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options


5
6
7
# File 'lib/pi_piper/pwm.rb', line 5

def options
  @options
end

#valueObject

Returns the value of attribute value


5
6
7
# File 'lib/pi_piper/pwm.rb', line 5

def value
  @value
end

Instance Method Details

#offObject Also known as: stop

Stop the Pwm signal


53
54
55
56
# File 'lib/pi_piper/pwm.rb', line 53

def off
  @on = false
  PiPiper.driver.pwm_set_mode(@options[:pin], options[:mode], 0) #check if compatible with other driver than bcm2835
end

#off?Boolean

Returns:

  • (Boolean)

64
65
66
# File 'lib/pi_piper/pwm.rb', line 64

def off?
  not on?
end

#onObject Also known as: start

Start the Pwm signal


44
45
46
47
# File 'lib/pi_piper/pwm.rb', line 44

def on
  @on = true
  PiPiper.driver.pwm_set_mode(@options[:pin], options[:mode], 1) #check if compatible with other driver than bcm2835
end

#on?Boolean

Returns:

  • (Boolean)

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

def on?
  @on
end