Class: Denko::PulseIO::PWMOutput

Inherits:
DigitalIO::Output show all
Includes:
Behaviors::Lifecycle
Defined in:
lib/denko/pulse_io/pwm_output.rb

Direct Known Subclasses

LED::Base, Buzzer

Constant Summary

Constants included from Behaviors::Lifecycle

Behaviors::Lifecycle::CALLBACK_METHODS

Constants included from Behaviors::OutputPin

Behaviors::OutputPin::OUTPUT_MODES

Instance Attribute Summary

Attributes included from Behaviors::Threaded

#interrupts_enabled, #thread

Attributes included from Behaviors::State

#state

Attributes included from Behaviors::SinglePin

#mode, #pin

Attributes included from Behaviors::Component

#board, #params

Instance Method Summary collapse

Methods included from Behaviors::Lifecycle

included

Methods inherited from DigitalIO::Output

#high, #high?, #low, #low?, #pre_callback_filter, #toggle

Methods included from Behaviors::Threaded

#enable_interrupts, included, #mruby_thread_check, #stop, #stop_thread, #threaded, #threaded_loop

Methods included from Behaviors::Callbacks

#add_callback, #callbacks, #pre_callback_filter, #remove_callback, #update

Methods included from Behaviors::State

#update_state

Methods included from Behaviors::SinglePin

#convert_pins, #initialize_pins

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Method Details

#_frequency=(value) ⇒ Object



72
73
74
75
# File 'lib/denko/pulse_io/pwm_output.rb', line 72

def _frequency=(value)
  @frequency = value
  @period    = nil
end

#_resolution=(value) ⇒ Object



77
78
79
80
# File 'lib/denko/pulse_io/pwm_output.rb', line 77

def _resolution=(value)
  @resolution = value
  @pwm_high   = nil
end

#digital_write(value) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/denko/pulse_io/pwm_output.rb', line 28

def digital_write(value)
  # Use regular #digital_write for speed until a PWM method is called.
  unless pwm_enabled?
    super(value)
  else
    # On Arduinos, disable PWM and switch back to regular #digital_write.
    if board.platform == :arduino
      pwm_disable
      super(value)
    # Can't do that on Linux, so mimic DigitalIO.
    else
      if value == 1
        pwm_write(period)
      else
        pwm_write(0)
      end
    end
  end
end

#duty=(percent) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/denko/pulse_io/pwm_output.rb', line 20

def duty=(percent)
  if board.platform == :arduino
    pwm_write((percent / 100.0 * pwm_high).round)
  else
    pwm_write((percent / 100.0 * period).round)
  end
end

#frequencyObject



56
57
58
# File 'lib/denko/pulse_io/pwm_output.rb', line 56

def frequency
  @frequency ||= params[:frequency] || 1000
end

#frequency=(value) ⇒ Object



82
83
84
85
# File 'lib/denko/pulse_io/pwm_output.rb', line 82

def frequency=(value)
  self._frequency = value
  pwm_enable
end

#periodObject



60
61
62
# File 'lib/denko/pulse_io/pwm_output.rb', line 60

def period
  @period ||= (1_000_000_000.0 / frequency).round
end

#pwm_disableObject



104
105
106
# File 'lib/denko/pulse_io/pwm_output.rb', line 104

def pwm_disable
  self.mode = :output if board.platform == :arduino
end

#pwm_enable(frequency: nil, resolution: nil) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/denko/pulse_io/pwm_output.rb', line 96

def pwm_enable(frequency: nil, resolution: nil)
  self._frequency  = frequency if frequency
  self._resolution = resolution if resolution

  board.set_pin_mode(pin, :output_pwm, pwm_settings_hash)
  @mode = :output_pwm
end

#pwm_enabled?Boolean



108
109
110
# File 'lib/denko/pulse_io/pwm_output.rb', line 108

def pwm_enabled?
  self.mode == :output_pwm
end

#pwm_highObject



68
69
70
# File 'lib/denko/pulse_io/pwm_output.rb', line 68

def pwm_high
  @pwm_high ||= (2**resolution-1)
end

#pwm_settings_hashObject



92
93
94
# File 'lib/denko/pulse_io/pwm_output.rb', line 92

def pwm_settings_hash
  { frequency: frequency, period: period, resolution: resolution }
end

#pwm_write(value) ⇒ Object Also known as: write

Raw write. Takes nanoseconds on Linux, 0..pwm_high on Arduino.



49
50
51
52
53
# File 'lib/denko/pulse_io/pwm_output.rb', line 49

def pwm_write(value)
  pwm_enable unless pwm_enabled?
  board.pwm_write(pin, value)
  self.state = value
end

#resolutionObject



64
65
66
# File 'lib/denko/pulse_io/pwm_output.rb', line 64

def resolution
  @resolution ||= params[:resolution] || board.analog_write_resolution
end

#resolution=(value) ⇒ Object



87
88
89
90
# File 'lib/denko/pulse_io/pwm_output.rb', line 87

def resolution=(value)
  self._resolution = value
  pwm_enable
end