Module: ADAM6050::State

Defined in:
lib/adam6050/state.rb

Overview

The application state is stored as an integer an updated in an immutable fashion. This module includes helper functions that simplify reading and creating new states.

Constant Summary collapse

NUM_INPUTS =

Returns the number of inputs.

Returns:

  • (Integer)

    the number of inputs.

12
NUM_OUTPUTS =

Returns the number of outputs.

Returns:

  • (Integer)

    the number of outputs.

6

Class Method Summary collapse

Class Method Details

.initialInteger

Returns the initial state.

Returns:

  • (Integer)

    the initial state.



24
25
26
# File 'lib/adam6050/state.rb', line 24

def initial
  0
end

.input_set?(state, input_channel) ⇒ true, false

Returns the state of the specified input.

Parameters:

  • state (Integer)

    the current state.

  • input_channel (Integer)

    the input channel number.

Returns:

  • (true, false)

    the state of the specified input.

Raises:

  • (RangeError)

    if the given channel index exceeds the number of available input channels.



34
35
36
37
38
# File 'lib/adam6050/state.rb', line 34

def input_set?(state, input_channel)
  raise RangeError if input_channel >= NUM_INPUTS

  state & (1 << input_channel) != 0
end

.inspect(state) ⇒ String

Returns a string representation of the state.

Parameters:

  • state (Integer)

    the current state.

Returns:

  • (String)

    a string representation of the state.



88
89
90
91
# File 'lib/adam6050/state.rb', line 88

def inspect(state)
  compact = format '%018b', state
  compact[0...6] + ' ' + compact[6..-1]
end

.output_set?(state, output_channel) ⇒ true, false

Returns the state of the specified output.

Parameters:

  • state (Integer)

    the current state.

  • output_channel (Integer)

    the output channel number.

Returns:

  • (true, false)

    the state of the specified output.

Raises:

  • (RangeError)

    if the given channel index exceeds the number of available output channels.



59
60
61
62
63
# File 'lib/adam6050/state.rb', line 59

def output_set?(state, output_channel)
  raise RangeError if output_channel >= NUM_OUTPUTS

  state & (1 << output_channel + NUM_INPUTS) != 0
end

.set_input(state, input_channel, value) ⇒ true, false

Returns the state of the specified input.

Parameters:

  • state (Integer)

    the current state.

  • input_channel (Integer)

    the input channel number.

Returns:

  • (true, false)

    the state of the specified input.

Raises:

  • (RangeError)

    if the given channel index exceeds the number of available input channels.



46
47
48
49
50
51
# File 'lib/adam6050/state.rb', line 46

def set_input(state, input_channel, value)
  raise RangeError if input_channel >= NUM_INPUTS

  mask = (1 << input_channel)
  value ? state | mask : state & ~mask
end

.to_bin(state) ⇒ String

Returns a binary representation expected by the protocol.

Parameters:

  • state (Integer)

    the current state.

Returns:

  • (String)

    a binary representation expected by the protocol.



95
96
97
# File 'lib/adam6050/state.rb', line 95

def to_bin(state)
  format '%05X', (~state & MASK)
end

.update(state, output_channel, value) ⇒ Integer

Returns the next state.

Parameters:

  • state (Integer)

    the current state.

  • output_channel (Integer)

    the output channel number.

  • value (0, Integer)

    the value to update with.

Returns:

  • (Integer)

    the next state.

Raises:

  • (RangeError)

    if the given channel index exceeds the number of available output channels.



72
73
74
75
76
77
# File 'lib/adam6050/state.rb', line 72

def update(state, output_channel, value)
  raise RangeError if output_channel >= NUM_OUTPUTS

  mask = (1 << output_channel + NUM_INPUTS)
  value.zero? ? state & ~mask : state | mask
end

.update_all(state, values) ⇒ Integer

Returns the next state.

Parameters:

  • state (Integer)

    the current state.

  • values (Integer)

    the next output values.

Returns:

  • (Integer)

    the next state.



82
83
84
# File 'lib/adam6050/state.rb', line 82

def update_all(state, values)
  state & INPUT_MASK | (values << NUM_INPUTS) & MASK
end