Sha256: b94784ab40d7d0f1c2244697a516dbd2a44b085c8f0daf5739cb47adc2e38751

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

module Denko
  module Behaviors
    module Reader
      include Callbacks

      #
      # Defalt behavior for #read is to delegate to #_read.
      # Define #_read in including classes.
      #
      def read(*args, **kwargs, &block)
        read_using(self.method(:_read), *args, **kwargs, &block)
      end

      #
      # Delegate reading to another method that sends a command to the board. 
      # Accepts blocks as one-time callbacks stored in the :read key.
      # Blocks until a value is recieved from the board.
      # Returns the value after #pre_callback_filter runs on it.
      #
      # Give procs as methods to build more complex functionality for buses.
      #
      def read_using(method, *args, **kwargs, &block)
        add_callback(:read, &block) if block_given?

        value = nil
        add_callback(:read) do |filtered_data|
          value = filtered_data
        end
        
        method.call(*args, **kwargs)
        block_until_read

        value
      end
      
      def block_until_read
        loop do
          break if !callbacks[:read]
          sleep 0.001
        end
      end

      def _read
        raise NotImplementedError
          .new("#{self.class.name}#_read is not defined.")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
denko-0.13.1 lib/denko/behaviors/reader.rb
denko-0.13.0 lib/denko/behaviors/reader.rb