Module: Beaglebone::AIN
- Defined in:
- lib/beaglebone/ain.rb
Overview
AIN
procedural methods for Analog Input
Summary
#read is called to get the analog value of a pin. More advanced polling methods are also available
Constant Summary collapse
- RANGE =
valid voltage readings in mv
(0..1799)
Class Method Summary collapse
-
.cleanup ⇒ Object
Disable all analog pins.
-
.disable_analog_pin(pin) ⇒ Object
Disable an analog pin.
-
.get_analog_pins ⇒ Array<Symbol>
Return an array of AIN pins in use.
-
.read(pin) ⇒ Integer
Read from an analog pin.
-
.run_on_change(callback, pin, mv_change = 10, interval = 0.01, repeats = nil) ⇒ Object
Runs a callback after voltage changes by specified amount.
-
.run_on_threshold(callback, pin, mv_lower, mv_upper, mv_reset = 10, interval = 0.01, repeats = nil) ⇒ Object
Runs a callback after voltage changes beyond a certain threshold.
-
.run_once_on_change(callback, pin, mv_change = 10, interval = 0.01) ⇒ Object
Runs a callback once after specified change in voltage detected Convenience method for run_on_change.
-
.run_once_on_threshold(callback, pin, mv_lower, mv_upper, mv_reset = 10, interval = 0.01) ⇒ Object
Runs a callback once after voltage crosses a specified threshold.
-
.stop_wait(pin) ⇒ Object
Stops any threads waiting for data on specified pin.
-
.wait_for_change(pin, mv_change, interval, mv_last = nil) ⇒ Object
Returns when voltage changes by specified amount.
-
.wait_for_threshold(pin, mv_lower, mv_upper, mv_reset = 10, interval = 0.01, mv_last = nil, state_last = nil) ⇒ Object
Runs a callback after voltage changes beyond a certain threshold.
Class Method Details
.cleanup ⇒ Object
Disable all analog pins
346 347 348 349 |
# File 'lib/beaglebone/ain.rb', line 346 def cleanup #reset all GPIO we've used to IN and unexport them get_analog_pins.each { |x| disable_analog_pin(x) } end |
.disable_analog_pin(pin) ⇒ Object
Disable an analog pin
340 341 342 343 |
# File 'lib/beaglebone/ain.rb', line 340 def disable_analog_pin(pin) Beaglebone::check_valid_pin(pin, :analog) Beaglebone::delete_pin_status(pin) end |
.get_analog_pins ⇒ Array<Symbol>
Return an array of AIN pins in use
AIN.get_analog_pins => [:P9_33, :P9_34]
333 334 335 |
# File 'lib/beaglebone/ain.rb', line 333 def get_analog_pins Beaglebone.pinstatus.clone.select { |x,y| x if y[:type] == :analog}.keys end |
.read(pin) ⇒ Integer
Read from an analog pin
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/beaglebone/ain.rb', line 23 def read(pin) Beaglebone::check_valid_pin(pin, :analog) Beaglebone::set_pin_status(pin, :type, :analog) ain_fd = Beaglebone::get_pin_status(pin, :fd_ain) unless ain_fd #ensure dtb is loaded Beaglebone::device_tree_load(TREES[:ADC][:global]) #open the AIN analog input file pininfo = PINS[pin] ain_file = Dir.glob("/sys/devices/ocp.*/helper.*/AIN#{pininfo[:analog]}").first #ain_file = Dir.glob("/sys/bus/iio/devices/iio:device0/in_voltage#{pininfo[:analog]}_raw").first ain_fd = File.open(ain_file, 'r') Beaglebone::set_pin_status(pin, :fd_ain, ain_fd) end ain_fd.rewind ain_fd.read.strip.to_i end |
.run_on_change(callback, pin, mv_change = 10, interval = 0.01, repeats = nil) ⇒ Object
Runs a callback after voltage changes by specified amount. This creates a new thread that runs in the background and polls at specified interval.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/beaglebone/ain.rb', line 61 def run_on_change(callback, pin, mv_change=10, interval=0.01, repeats=nil) raise StandardError, "Already waiting for change on pin: #{pin}" if Beaglebone::get_pin_status(pin, :waiting) raise StandardError, "Already waiting for thread on pin: #{pin}" if Beaglebone::get_pin_status(pin, :thread) thread = Thread.new(callback, pin, mv_change, interval, repeats) do |c, p, mvc, i, r| begin count = 0 mvl = nil loop do mvl,mv,itr = wait_for_change(p, mvc, i, mvl) c.call(p, mvl, mv, count) if c #if there was no delay in the wait_for_change, delay now. sleep(interval) if itr == 0 mvl = mv count += 1 break if r && count >= r end rescue => ex puts ex puts ex.backtrace ensure Beaglebone::delete_pin_status(p, :thread) Beaglebone::delete_pin_status(p, :waiting) end end Beaglebone::set_pin_status(pin, :thread, thread) end |
.run_on_threshold(callback, pin, mv_lower, mv_upper, mv_reset = 10, interval = 0.01, repeats = nil) ⇒ Object
Runs a callback after voltage changes beyond a certain threshold. This creates a new thread that runs in the background and polls at specified interval. When the voltage crosses the specified thresholds the callback is run.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/beaglebone/ain.rb', line 120 def run_on_threshold(callback, pin, mv_lower, mv_upper, mv_reset=10, interval=0.01, repeats=nil) raise StandardError, "Already waiting for change on pin: #{pin}" if Beaglebone::get_pin_status(pin, :waiting) raise StandardError, "Already waiting for thread on pin: #{pin}" if Beaglebone::get_pin_status(pin, :thread) thread = Thread.new(callback, pin, mv_lower, mv_upper, mv_reset, interval, repeats) do |c, p, mvl, mvu, mvr, i, r| begin count = 0 mv_last = nil state_last = nil loop do mv_last,mv,state_last,state,itr = wait_for_threshold(p, mvl, mvu, mvr, i, mv_last, state_last) c.call(p, mv_last, mv, state_last, state, count) if c #if there was no delay in the wait_for_change, delay now. sleep(interval) if itr == 0 mv_last = mv state_last = state count += 1 break if r && r >= count end rescue => ex puts ex puts ex.backtrace ensure Beaglebone::delete_pin_status(p, :thread) Beaglebone::delete_pin_status(p, :waiting) end end Beaglebone::set_pin_status(pin, :thread, thread) end |
.run_once_on_change(callback, pin, mv_change = 10, interval = 0.01) ⇒ Object
Runs a callback once after specified change in voltage detected Convenience method for run_on_change
98 99 100 |
# File 'lib/beaglebone/ain.rb', line 98 def run_once_on_change(callback, pin, mv_change=10, interval=0.01) run_on_change(callback, pin, mv_change, interval, 1) end |
.run_once_on_threshold(callback, pin, mv_lower, mv_upper, mv_reset = 10, interval = 0.01) ⇒ Object
Runs a callback once after voltage crosses a specified threshold. Convenience method for run_on_threshold
159 160 161 |
# File 'lib/beaglebone/ain.rb', line 159 def run_once_on_threshold(callback, pin, mv_lower, mv_upper, mv_reset=10, interval=0.01) run_on_threshold(callback, pin, mv_lower, mv_upper, mv_reset, interval, 1) end |
.stop_wait(pin) ⇒ Object
Stops any threads waiting for data on specified pin
320 321 322 323 324 325 |
# File 'lib/beaglebone/ain.rb', line 320 def stop_wait(pin) thread = Beaglebone::get_pin_status(pin, :thread) thread.exit if thread thread.join if thread end |
.wait_for_change(pin, mv_change, interval, mv_last = nil) ⇒ Object
Returns when voltage changes by specified amount
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/beaglebone/ain.rb', line 275 def wait_for_change(pin, mv_change, interval, mv_last=nil) Beaglebone::check_valid_pin(pin, :analog) raise ArgumentError, "mv_change needs to be between 0 and 1800: #{pin} (#{mv_change})" unless (0..1800).include?(mv_change) #ensure we're the only ones waiting for this trigger if Beaglebone::get_pin_status(pin, :thread) && Beaglebone::get_pin_status(pin, :thread) != Thread.current raise StandardError, "Already waiting for change on pin: #{pin}" end if Beaglebone::get_pin_status(pin, :waiting) && Beaglebone::get_pin_status(pin, :thread) != Thread.current raise StandardError, "Already waiting for change on pin: #{pin}" end Beaglebone::set_pin_status(pin, :waiting, true) mv_last = read(pin) unless mv_last change_max = [mv_last - 0, 1799 - mv_last].max mv_change = change_max if mv_change > change_max count = 0 loop do mv = read(pin) #if we've detected the change or hit the edge of the range if (mv - mv_last).abs >= mv_change Beaglebone::delete_pin_status(pin, :waiting) return [ mv_last, mv, count ] end sleep interval count += 1 end Beaglebone::delete_pin_status(pin, :waiting) [ mv_last, -1, count ] end |
.wait_for_threshold(pin, mv_lower, mv_upper, mv_reset = 10, interval = 0.01, mv_last = nil, state_last = nil) ⇒ Object
Runs a callback after voltage changes beyond a certain threshold. This creates a new thread that runs in the background and polls at specified interval. When the voltage crosses the specified thresholds the callback is run.
This method should take 6 arguments: the pin, the previous voltage, the current voltage, the previous state, the current state, and the counter
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/beaglebone/ain.rb', line 186 def wait_for_threshold(pin, mv_lower, mv_upper, mv_reset=10, interval=0.01, mv_last=nil, state_last=nil) Beaglebone::check_valid_pin(pin, :analog) raise ArgumentError, "mv_upper needs to be between 0 and 1800: #{pin} (#{mv_upper})" unless (0..1800).include?(mv_upper) raise ArgumentError, "mv_lower needs to be between 0 and 1800: #{pin} (#{mv_lower})" unless (0..1800).include?(mv_lower) raise ArgumentError, "mv_lower needs to be <= mv_upper: #{pin} (#{mv_lower}:#{mv_upper})" unless mv_lower <= mv_upper raise ArgumentError, "mv_reset needs to be between 0 and 1800: #{pin} (#{mv_reset})" unless (0..1800).include?(mv_reset) #ensure we're the only ones waiting for this trigger if Beaglebone::get_pin_status(pin, :thread) && Beaglebone::get_pin_status(pin, :thread) != Thread.current raise StandardError, "Already waiting for change on pin: #{pin}" end if Beaglebone::get_pin_status(pin, :waiting) && Beaglebone::get_pin_status(pin, :thread) != Thread.current raise StandardError, "Already waiting for change on pin: #{pin}" end Beaglebone::set_pin_status(pin, :waiting, true) mv_last = read(pin) unless mv_last if mv_last >= mv_upper state_last = :HIGH elsif mv_last <= mv_lower state_last = :LOW else state_last = :MID end unless state_last state = :UNKNOWN count = 0 loop do mv = read(pin) if state_last == :LOW #state remains low unless it crosses into high, or above mv_low + mv_reset if mv >= mv_upper && mv >= mv_lower + mv_reset state = :HIGH elsif mv >= mv_lower + mv_reset state = :MID else state = :LOW end elsif state_last == :HIGH #state remains high unless it crosses into low, or below mv_high - mv_reset if mv <= mv_lower && mv <= mv_upper - mv_reset state = :LOW elsif mv <= mv_upper - mv_reset state = :MID else state = :HIGH end elsif state_last == :MID #state changes from normal by crossing into upper or lower if mv >= mv_upper state = :HIGH elsif mv <= mv_lower state = :LOW else state = :MID end end #if we've detected a change of state if state != state_last Beaglebone::delete_pin_status(pin, :waiting) return [ mv_last, mv, state_last, state, count ] end sleep interval count += 1 end Beaglebone::delete_pin_status(pin, :waiting) [ mv_last, -1, state_last, state_last, count ] end |