Module: Denko::Behaviors::Reader
- Includes:
- Callbacks
- Included in:
- AnalogIO::ADS111X, AnalogIO::Input, Poller, DigitalIO::Input, EEPROM::BuiltIn, I2C::Bus, I2C::Peripheral, OneWire::Bus, SPI::BitBang, SPI::Bus, Sensor::HTU21D
- Defined in:
- lib/denko/behaviors/reader.rb
Instance Attribute Summary
Attributes included from Callbacks
Instance Method Summary collapse
- #_read ⇒ Object
-
#read(*args, **kwargs, &block) ⇒ Object
Defalt behavior for #read is to delegate to #_read.
-
#read_using(reader, *args, **kwargs, &block) ⇒ Object
Take a proc/lambda/method as the first agrument and use it to read.
- #wait_for_read ⇒ Object
Methods included from Callbacks
#add_callback, #callbacks, #initialize, #pre_callback_filter, #remove_callback, #update
Methods included from State
Instance Method Details
#_read ⇒ Object
44 45 46 47 |
# File 'lib/denko/behaviors/reader.rb', line 44 def _read raise NotImplementedError .new("#{self.class.name}#_read is not defined.") end |
#read(*args, **kwargs, &block) ⇒ Object
Defalt behavior for #read is to delegate to #_read. Define #_read in including classes.
10 11 12 |
# File 'lib/denko/behaviors/reader.rb', line 10 def read(*args, **kwargs, &block) read_using(self.method(:_read), *args, **kwargs, &block) end |
#read_using(reader, *args, **kwargs, &block) ⇒ Object
Take a proc/lambda/method as the first agrument and use it to read. Arguments are passed through, allowing dynamic read methods to be defined. Eg. send commands (in args) to a bus, then wait for data read back.
Block given is added as a one-time callback in the :read key, and the curent thread waits until data is received. Returns the result of calling #pre_callback_filter with the data.
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/denko/behaviors/reader.rb', line 23 def read_using(reader, *args, **kwargs, &block) add_callback(:read, &block) if block_given? return_value = nil add_callback(:read) do |filtered_data| return_value = filtered_data end reader.call(*args, **kwargs) wait_for_read return_value end |
#wait_for_read ⇒ Object
37 38 39 40 41 42 |
# File 'lib/denko/behaviors/reader.rb', line 37 def wait_for_read loop do break if !callbacks[:read] sleep 0.001 end end |