Module: LittleWire::HardwarePWM
- Included in:
- LittleWire
- Defined in:
- lib/littlewire/hardware-pwm.rb
Constant Summary collapse
- PWMPrescaleSettings =
:nodoc:
[1, 8, 64, 256, 1024]
Instance Attribute Summary collapse
-
#hardware_pwm_enabled ⇒ Object
initially :unknown, then a boolean.
Instance Method Summary collapse
-
#hardware_pwm ⇒ Object
Get an array of the current values in the Hardware PWM module.
-
#hardware_pwm=(values) ⇒ Object
Set Hardware PWM to an array of new values - array must be two items long.
-
#hardware_pwm_prescale=(division) ⇒ Object
Set division of the Hardware PWM Prescaler - default 1024.
-
#hardware_pwm_read(channel) ⇒ Object
Get the current value of a Hardware PWM channel (stored in littlewire.rb library - not requested from device).
-
#hardware_pwm_write(channel, value) ⇒ Object
Set an individual Hardware PWM channel to a new value in the range of 0-255.
Instance Attribute Details
#hardware_pwm_enabled ⇒ Object
initially :unknown, then a boolean
2 3 4 |
# File 'lib/littlewire/hardware-pwm.rb', line 2 def hardware_pwm_enabled @hardware_pwm_enabled end |
Instance Method Details
#hardware_pwm ⇒ Object
Get an array of the current values in the Hardware PWM module
13 |
# File 'lib/littlewire/hardware-pwm.rb', line 13 def hardware_pwm; @hardware_pwm.dup; end |
#hardware_pwm=(values) ⇒ Object
Set Hardware PWM to an array of new values - array must be two items long
16 17 18 19 20 21 |
# File 'lib/littlewire/hardware-pwm.rb', line 16 def hardware_pwm= values self.hardware_pwm_enabled = true @hardware_pwm[0] = values[0].to_i % 256 @hardware_pwm[1] = values[1].to_i % 256 control_transfer(function: :update_pwm_compare, wValue: @hardware_pwm[0].to_i, wIndex: @hardware_pwm[1].to_i) end |
#hardware_pwm_prescale=(division) ⇒ Object
Set division of the Hardware PWM Prescaler - default 1024. This setting controls how quickly LittleWire’s PWM channels oscillate between their ‘high’ and ‘low’ state. Lower prescaler values are often nicer for lighting, while higher values can be better for motor speed control and 1024 is required for servo position control
1024: roughly 63hz
256: roughly 252hz
64: roughly 1khz
8: roughly 8khz
1: roughly 64khz
No other values are accepted
45 46 47 48 49 50 51 |
# File 'lib/littlewire/hardware-pwm.rb', line 45 def hardware_pwm_prescale= division raise "Unsupported Hardware PWM Prescale value, must be #{PWMPrescaleSettings.inspect}" unless PWMPrescaleSettings.include? division if @hardware_pwm_prescale != division @hardware_pwm_prescale = division control_transfer(function: :change_pwm_prescale, wValue: PWMPrescaleSettings.index(division)) end end |
#hardware_pwm_read(channel) ⇒ Object
Get the current value of a Hardware PWM channel (stored in littlewire.rb library - not requested from device)
24 |
# File 'lib/littlewire/hardware-pwm.rb', line 24 def hardware_pwm_read channel; hardware_pwm[get_pin(LittleWire::HardwarePWMPinMap, channel)]; end |
#hardware_pwm_write(channel, value) ⇒ Object
Set an individual Hardware PWM channel to a new value in the range of 0-255
27 28 29 30 31 |
# File 'lib/littlewire/hardware-pwm.rb', line 27 def hardware_pwm_write channel, value updated = self.hardware_pwm updated[get_pin(LittleWire::HardwarePWMPinMap, channel)] = value self.hardware_pwm = updated end |