Module: Beaglebone::PWM
- Defined in:
- lib/beaglebone/pwm.rb
Overview
PWM
procedural methods for PWM control
Summary
#start is called to enable a PWM pin
Constant Summary collapse
- POLARITIES =
Polarity hash
{ :NORMAL => 0, :INVERTED => 1 }
Class Method Summary collapse
-
.cleanup ⇒ Object
reset all PWM pins we’ve used to IN and unexport them.
-
.disable_pwm_pin(pin) ⇒ Object
Disable a PWM pin.
-
.enabled?(pin) ⇒ Boolean
Returns true if specified pin is enabled in PWM mode, else false.
-
.get_pwm_pins ⇒ Array<Symbol>
Return an array of PWM pins in use.
-
.run(pin) ⇒ Object
Start PWM output on specified pin.
-
.set_duty_cycle(pin, duty, newperiod = nil) ⇒ Object
Set duty cycle of specified pin in percentage.
-
.set_duty_cycle_ns(pin, duty) ⇒ Object
Set duty cycle of specified pin in nanoseconds.
-
.set_frequency(pin, frequency) ⇒ Object
Set frequency of specified pin in cycles per second.
-
.set_period_ns(pin, period) ⇒ Object
Set frequency of specified pin based on period duration.
-
.set_polarity(pin, polarity) ⇒ Object
Set polarity on specified pin.
-
.start(pin, duty = nil, frequency = nil, polarity = nil, run = true) ⇒ Object
Initialize a PWM pin.
-
.stop(pin) ⇒ Object
Stop PWM output on specified pin.
Class Method Details
.cleanup ⇒ Object
reset all PWM pins we’ve used to IN and unexport them
285 286 287 |
# File 'lib/beaglebone/pwm.rb', line 285 def cleanup get_pwm_pins.each { |x| disable_pwm_pin(x) } end |
.disable_pwm_pin(pin) ⇒ Object
Disable a PWM pin
302 303 304 305 |
# File 'lib/beaglebone/pwm.rb', line 302 def disable_pwm_pin(pin) Beaglebone::check_valid_pin(pin, :pwm) Beaglebone::delete_pin_status(pin) if Beaglebone::device_tree_unload("#{TREES[:PWM][:pin]}#{pin}") end |
.enabled?(pin) ⇒ Boolean
Returns true if specified pin is enabled in PWM mode, else false
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/beaglebone/pwm.rb', line 74 def enabled?(pin) return true if Beaglebone::get_pin_status(pin, :type) == :pwm return false unless valid?(pin) if Dir.exists?(pwm_directory(pin)) start(pin, nil, nil, nil, false) return true end false end |
.get_pwm_pins ⇒ Array<Symbol>
Return an array of PWM pins in use
295 296 297 |
# File 'lib/beaglebone/pwm.rb', line 295 def get_pwm_pins Beaglebone.pinstatus.clone.select { |x,y| x if y[:type] == :pwm}.keys end |
.run(pin) ⇒ Object
Start PWM output on specified pin. Pin must have been previously started
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/beaglebone/pwm.rb', line 110 def run(pin) Beaglebone::check_valid_pin(pin, :pwm) return false unless enabled?(pin) raise StandardError, "Pin is not PWM enabled: #{pin}" unless Beaglebone::get_pin_status(pin, :type) == :pwm run_fd = Beaglebone::get_pin_status(pin, :fd_run) raise StandardError, "Pin is not PWM enabled: #{pin}" unless run_fd run_fd.write('1') run_fd.flush raise StandardError, "Could not start PWM: #{pin}" unless read_run_value(pin) == 1 true end |
.set_duty_cycle(pin, duty, newperiod = nil) ⇒ Object
Set duty cycle of specified pin in percentage
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/beaglebone/pwm.rb', line 155 def set_duty_cycle(pin, duty, newperiod=nil) raise ArgumentError, "Duty cycle must be >= 0 and <= 100, #{duty} invalid" if duty < 0 || duty > 100 check_pwm_enabled(pin) fd = Beaglebone::get_pin_status(pin, :fd_duty) raise StandardError, "Pin is not PWM enabled: #{pin}" unless fd period = newperiod || Beaglebone::get_pin_status(pin, :period) value = ((duty * period) / 100).to_i fd.write(value.to_s) fd.flush raise StandardError, "Could not set duty cycle: #{pin} (#{value})" unless read_duty_value(pin) == value value end |
.set_duty_cycle_ns(pin, duty) ⇒ Object
Set duty cycle of specified pin in nanoseconds
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/beaglebone/pwm.rb', line 184 def set_duty_cycle_ns(pin, duty) check_pwm_enabled(pin) fd = Beaglebone::get_pin_status(pin, :fd_duty) raise StandardError, "Pin is not PWM enabled: #{pin}" unless fd period = Beaglebone::get_pin_status(pin, :period) duty = duty.to_i if duty < 0 || duty > period raise ArgumentError, "Duty cycle ns must be >= 0 and <= #{period} (current period), #{duty} invalid" end value = duty fd.write(value.to_s) fd.flush raise StandardError, "Could not set duty cycle: #{pin} (#{value})" unless read_duty_value(pin) == value value end |
.set_frequency(pin, frequency) ⇒ Object
Set frequency of specified pin in cycles per second
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/beaglebone/pwm.rb', line 215 def set_frequency(pin, frequency) frequency = frequency.to_i raise ArgumentError, "Frequency must be > 0 and <= 1000000000, #{frequency} invalid" if frequency < 1 || frequency > 1000000000 check_pwm_enabled(pin) fd = Beaglebone::get_pin_status(pin, :fd_period) raise StandardError, "Pin is not PWM enabled: #{pin}" unless fd duty_ns = Beaglebone::get_pin_status(pin, :duty) duty_pct = Beaglebone::get_pin_status(pin, :duty_pct) value = (1000000000 / frequency).to_i #we can't set the frequency lower than the previous duty cycle #adjust if necessary if duty_ns > value set_duty_cycle(pin, Beaglebone::get_pin_status(pin, :duty_pct), value) end fd.write(value.to_s) fd.flush raise StandardError, "Could not set frequency: #{pin} (#{value})" unless read_period_value(pin) == value #adjust the duty cycle if we haven't already if duty_ns <= value set_duty_cycle(pin, duty_pct, value) end value end |
.set_period_ns(pin, period) ⇒ Object
Set frequency of specified pin based on period duration
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/beaglebone/pwm.rb', line 254 def set_period_ns(pin, period) period = period.to_i raise ArgumentError, "period must be > 0 and <= 1000000000, #{period} invalid" if period < 1 || period > 1000000000 check_pwm_enabled(pin) fd = Beaglebone::get_pin_status(pin, :fd_period) raise StandardError, "Pin is not PWM enabled: #{pin}" unless fd duty_ns = Beaglebone::get_pin_status(pin, :duty) value = period.to_i #we can't set the frequency lower than the previous duty cycle #adjust if necessary if duty_ns > value set_duty_cycle(pin, Beaglebone::get_pin_status(pin, :duty_pct), value) end fd.write(value.to_s) fd.flush raise StandardError, "Could not set period: #{pin} (#{value})" unless read_period_value(pin) == value #adjust the duty cycle if we haven't already if duty_ns <= value set_duty_cycle(pin, Beaglebone::get_pin_status(pin, :duty_pct), value) end value end |
.set_polarity(pin, polarity) ⇒ Object
Set polarity on specified pin
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/beaglebone/pwm.rb', line 135 def set_polarity(pin, polarity) check_valid_polarity(polarity) check_pwm_enabled(pin) polarity_fd = Beaglebone::get_pin_status(pin, :fd_polarity) raise StandardError, "Pin is not PWM enabled: #{pin}" unless polarity_fd polarity_fd.write(POLARITIES[polarity.to_sym].to_s) polarity_fd.flush raise StandardError, "Could not set polarity: #{pin}" unless read_polarity_value(pin) == POLARITIES[polarity.to_sym] end |
.start(pin, duty = nil, frequency = nil, polarity = nil, run = true) ⇒ Object
Initialize a PWM pin
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/beaglebone/pwm.rb', line 25 def start(pin, duty=nil, frequency=nil, polarity=nil, run=true) #make sure the pwm controller dtb is loaded Beaglebone::device_tree_load(TREES[:PWM][:global]) Beaglebone::check_valid_pin(pin, :pwm) #if pin is enabled for something else, disable it if Beaglebone::get_pin_status(pin) && Beaglebone::get_pin_status(pin, :type) != :pwm Beaglebone::disable_pin(pin) end #load device tree for pin if not already loaded unless Beaglebone::get_pin_status(pin, :type) == :pwm Beaglebone::device_tree_load("#{TREES[:PWM][:pin]}#{pin}") Beaglebone::set_pin_status(pin, :type, :pwm) end duty_fd = File.open("#{pwm_directory(pin)}/duty", 'r+') period_fd = File.open("#{pwm_directory(pin)}/period", 'r+') polarity_fd = File.open("#{pwm_directory(pin)}/polarity", 'r+') run_fd = File.open("#{pwm_directory(pin)}/run", 'r+') Beaglebone::set_pin_status(pin, :fd_duty, duty_fd) Beaglebone::set_pin_status(pin, :fd_period, period_fd) Beaglebone::set_pin_status(pin, :fd_polarity, polarity_fd) Beaglebone::set_pin_status(pin, :fd_run, run_fd) read_period_value(pin) read_duty_value(pin) read_polarity_value(pin) run_fd.write('0') run_fd.flush set_polarity(pin, polarity) if polarity set_frequency(pin, frequency) if frequency set_duty_cycle(pin, duty) if duty if run run_fd.write('1') run_fd.flush end raise StandardError, "Could not start PWM: #{pin}" unless read_run_value(pin) == 1 true end |
.stop(pin) ⇒ Object
Stop PWM output on specified pin
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/beaglebone/pwm.rb', line 89 def stop(pin) Beaglebone::check_valid_pin(pin, :pwm) return false unless enabled?(pin) raise StandardError, "Pin is not PWM enabled: #{pin}" unless Beaglebone::get_pin_status(pin, :type) == :pwm run_fd = Beaglebone::get_pin_status(pin, :fd_run) raise StandardError, "Pin is not PWM enabled: #{pin}" unless run_fd run_fd.write('0') run_fd.flush raise StandardError, "Could not stop PWM: #{pin}" unless read_run_value(pin) == 0 true end |