Class: Minilab::Minilab

Inherits:
Object
  • Object
show all
Defined in:
lib/minilab/minilab.rb

Overview

The main interface to the minilab library. Create one of these objects using the build method. After you’ve created a minilab object, use the #connect method to establish the connection to the device. Minilab objects are not usable until they’ve been connected.

Author:

Instance Method Summary collapse

Instance Method Details

#configure_input_port(port) ⇒ true

Configure one of the DB37 ports for input.

Parameters:

  • port (Symbol)

    the port. Valid ports are :porta, :portb, :portcl, and :portch.

Returns:

  • (true)

Raises:

  • (RuntimeError)

    the port is invalid



64
65
66
67
# File 'lib/minilab/minilab.rb', line 64

def configure_input_port(port)
  ensure_connected_to_device
  @digital_port_io.configure_input_port(port)
end

#configure_output_port(port) ⇒ true

Configure one of the DB37 ports for output.

Parameters:

  • port (Symbol)

    the port. Valid ports are :porta, :portb, :portcl, and :portch.

Returns:

  • (true)

Raises:

  • (RuntimeError)

    the port is invalid



74
75
76
77
# File 'lib/minilab/minilab.rb', line 74

def configure_output_port(port)
  ensure_connected_to_device
  @digital_port_io.configure_output_port(port)
end

#connecttrue

Connect to the device. There are two side effects of connecting.

  • Error reporting from MCC’s universal library is set to print errors to standard out instead of popping up a Windows dialog.

  • Each of the DB37 digital ports is setup for input.

Returns:

  • (true)


24
25
26
27
28
29
30
# File 'lib/minilab/minilab.rb', line 24

def connect
  @minilab_wrapper.setup_error_handling(DONTPRINT, STOPALL)
  @minilab_wrapper.declare_revision(CURRENTREVNUM)
  @connection_state.connected = true
  DigitalConfiguration::PORTS.each { |port| configure_input_port(port) }
  true
end

#read_analog(channel) ⇒ Float

Read from one of the eight analog channels on top of the device.

Single-ended mode is the only analog mode supported.

Parameters:

  • channel (Fixnum)

    the analog channel (0 - 7)

Returns:

  • (Float)

    the voltage

Raises:

  • (RuntimeError)

    the channel number is invalid



39
40
41
42
# File 'lib/minilab/minilab.rb', line 39

def read_analog(channel)
  ensure_connected_to_device
  @analog_io.read_analog(channel)
end

#read_digital(pin) ⇒ Fixnum

Read a single bit from one of the auxport pins (on the top of the device) or from a DB37 pin. Ensure the pin has been configured for input (see #configure_input_port) before using this method. auxport pins do not need to be configured.

Parameters:

  • pin (String, Fixnum)

    a String name of the auxport (e.g. “DIO1”) or a Fixnum DB37 pin number (e.g. 13)

Returns:

  • (Fixnum)

    the bit value

Raises:

  • (RuntimeError)

    the given pin doesn’t exist

  • (RuntimeError)

    the DB37 pin has not been configured for input



89
90
91
92
# File 'lib/minilab/minilab.rb', line 89

def read_digital(pin)
  ensure_connected_to_device
  perform_digital_op(pin, :read, pin)
end

#read_digital_byte(port) ⇒ Fixnum

Read a byte from one of the DB37 ports.

Parameters:

  • port (Symbol)

    the port. Valid ports are :porta, :portb, :portcl, and :portch.

Returns:

  • (Fixnum)

    the byte value

Raises:

  • (RuntimeError)

    the port is invalid



117
118
119
120
# File 'lib/minilab/minilab.rb', line 117

def read_digital_byte(port)
  ensure_connected_to_device
  @digital_port_io.read_port(port)
end

#write_analog(channel, volts) ⇒ Object

Output a voltage on one of the eight analog channels. The output voltage must be in the range of 0.0 - 5.0 volts.

Single-ended mode is the only analog mode supported.

Parameters:

  • channel (Fixnum)

    the analog channel (0 or 1)

  • volts (Float)

    the voltage (0.0 - 5.0)

Raises:

  • (RuntimeError)

    the channel number is invalid

  • (RuntimeError)

    the voltage is out of range



53
54
55
56
# File 'lib/minilab/minilab.rb', line 53

def write_analog(channel, volts)
  ensure_connected_to_device
  @analog_io.write_analog(channel, volts)
end

#write_digital(pin, value) ⇒ true

Write a single bit to one of the auxport pins (on the top of the device) or to a DB37 pin. Ensure the pin has been configured for output (see #configure_output_port) before using this method. auxport pins do not need to be configured.

Parameters:

  • pin (String, Fixnum)

    a String name of the auxport (e.g. “DIO1”) or a Fixnum DB37 pin number (e.g. 13)

  • value (Fixnum)

    the value (0 or 1) to write. values above 1 are interpreted as 1.

Returns:

  • (true)

Raises:

  • (RuntimeError)

    the given pin doesn’t exist

  • (RuntimeError)

    the DB37 pin has not been configured for input

  • (RuntimeError)

    a negative value is given



107
108
109
110
# File 'lib/minilab/minilab.rb', line 107

def write_digital(pin, value)
  ensure_connected_to_device
  perform_digital_op(pin, :write, pin, value)
end