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 =
12
NUM_OUTPUTS =
6

Class Method Summary collapse

Class Method Details

.initialInteger



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.

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



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.

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.

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



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.

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



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

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