Sha256: feb77e99d26ad6deeb994618b6bbadd37929a05d3d312b414497fe361a16c548

Contents?: true

Size: 1.5 KB

Versions: 7

Compression:

Stored size: 1.5 KB

Contents

module Denko
  module UART
    class Hardware
      include Behaviors::SinglePin
      include Behaviors::Callbacks

      attr_reader :index, :baud
      
      def before_initialize(options={})
        if options[:index] && (options[:index] > 0) && (options[:index] < 4)
          @index = options[:index]
        else
          raise ArgumentError, "UART index (#{options[:index]}) not given or out of range (1..3)"
        end

        # Set pin to a "virtual pin" in 251 - 253 that will match the board.
        options[:pin] = 250 + options[:index]
      end

      def after_initialize(options={})
        initialize_buffer
        start(options[:baud] ||= 9600)
      end

      def initialize_buffer
        @buffer       = ""
        @buffer_mutex = Mutex.new
        self.add_callback(:buffer) do |data|
          @buffer_mutex.synchronize do
            @buffer = "#{@buffer}#{data}"
          end
        end
      end

      def gets
        @buffer_mutex.synchronize do
          newline = @buffer.index("\n")
          return nil unless newline
          line = @buffer[0..newline-1]
          @buffer = @buffer[newline+1..-1]
          return line
        end
      end

      def flush
        @buffer_mutex.synchronize do
          @buffer = ""
        end
      end

      def start(baud)
        @baud = baud
        board.uart_start(index, baud, true)
      end

      def stop()
        board.uart_stop(index)
      end

      def write(data)
        board.uart_write(index, data)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
denko-0.13.6 lib/denko/uart/hardware.rb
denko-0.13.5 lib/denko/uart/hardware.rb
denko-0.13.4 lib/denko/uart/hardware.rb
denko-0.13.3 lib/denko/uart/hardware.rb
denko-0.13.2 lib/denko/uart/hardware.rb
denko-0.13.1 lib/denko/uart/hardware.rb
denko-0.13.0 lib/denko/uart/hardware.rb