Module: Denko::Behaviors::Poller
- Included in:
- AnalogIO::ADS1100, AnalogIO::Input, DigitalIO::Input, OneWire::Peripheral, Sensor::AHT1X, Sensor::BME280, Sensor::BMP180, Sensor::DHT, Sensor::HCSR04, Sensor::HDC1080, Sensor::HTU21D, Sensor::HTU31D, Sensor::JSNSR04T, Sensor::QMP6988, Sensor::RCWL9620, Sensor::SHT3X, Sensor::SHT4X, Sensor::VL53L0X
- Defined in:
- lib/denko/behaviors/poller.rb
Constant Summary
Constants included from Reader
Constants included from Lifecycle
Instance Attribute Summary
Attributes included from Threaded
Attributes included from State
Instance Method Summary collapse
- #poll(interval, *args, &block) ⇒ Object
- #poll_using(method, interval, *args, &block) ⇒ Object
- #stop ⇒ Object
Methods included from Threaded
#enable_interrupts, included, #mruby_thread_check, #stop_thread, #threaded, #threaded_loop
Methods included from Reader
#_read, #read, #read_busy?, #read_nb, #read_raw, #read_using, #update
Methods included from Callbacks
#add_callback, #callbacks, #pre_callback_filter, #remove_callback, #update
Methods included from State
Methods included from Lifecycle
Instance Method Details
#poll(interval, *args, &block) ⇒ Object
30 31 32 |
# File 'lib/denko/behaviors/poller.rb', line 30 def poll(interval, *args, &block) poll_using(self.method(:_read), interval, *args, &block) end |
#poll_using(method, interval, *args, &block) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/denko/behaviors/poller.rb', line 7 def poll_using(method, interval, *args, &block) mruby_thread_check unless [Integer, Float].include? interval.class raise ArgumentError, "wrong interval given to #poll : #{interval.inspect}" end stop add_callback(:poll, &block) if block_given? threaded_loop do # Lock, THEN wait for other normal reads to finish. @reader_mutex.lock sleep 0.001 while read_busy? @reading_normally = true method.call(*args) @reader_mutex.unlock sleep interval end end |
#stop ⇒ Object
34 35 36 37 |
# File 'lib/denko/behaviors/poller.rb', line 34 def stop begin; super; rescue NoMethodError; end remove_callbacks :poll end |