Sha256: e9097ecb9beb4509e6ca634913e8e90ad36418f3d3220e417d840a7c61474cc7

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require 'constructor'
require 'extension/minilab_hardware'

class DigitalAuxportIo #:nodoc:
  constructor :minilab_hardware, :result_verifier
  include MinilabConstants

  VALID_PINS = ['DIO0', 'DIO1', 'DIO2', 'DIO3']

  def read_digital(pin)
    check_pin_valid(pin)
    pin = get_pin_number(pin)

    configuration = {:direction => DIGITALIN, :pin => pin}
    result = @minilab_hardware.configure_auxport(configuration)
    @result_verifier.verify(result, "configure_auxport_in")

    result = @minilab_hardware.read_auxport(pin)
    @result_verifier.verify(result, "read_auxport")

    result[:value]
  end

  def write_digital(pin, value)
    check_pin_valid(pin)
    pin = get_pin_number(pin)

    raise "#{value} is not a valid digital output." if (value < 0)
    value = 1 if (value > 0)

    configuration = { :direction => DIGITALOUT, :pin => pin }
    result = @minilab_hardware.configure_auxport(configuration)
    @result_verifier.verify(result, "configure_auxport_out")

    result = @minilab_hardware.write_auxport(pin, value)
    @result_verifier.verify(result, "write_auxport")
  end

  private
  def check_pin_valid(pin)
    if !VALID_PINS.include?(pin.to_s.upcase)
      raise "#{pin} is not a valid digital IO pin"
    end
  end

  def get_pin_number(pin)
    pin.match(/(\d)$/)[0].to_i
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
minilab-1.0.0-mswin32 lib/digital_auxport_io.rb