module ADAM6050::State

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.

Constants

INPUT_MASK
MASK

@return [Integer] a binary mask selecting the bits of an integer used by

the state.
NUM_INPUTS

@return [Integer] the number of inputs.

NUM_OUTPUTS

@return [Integer] the number of outputs.

OUTPUT_MASK

Public Class Methods

initial() click to toggle source

@return [Integer] the initial state.

# File lib/adam6050/state.rb, line 24
def initial
  0
end
input_set?(state, input_channel) click to toggle source

@raise [RangeError] if the given channel index exceeds the number of

available input channels.

@param state [Integer] the current state. @param input_channel [Integer] the input channel number. @return [true, false] the state of the specified input.

# 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) click to toggle source

@param state [Integer] the current state. @return [String] a string representation of the state.

# 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) click to toggle source

@raise [RangeError] if the given channel index exceeds the number of

available output channels.

@param state [Integer] the current state. @param output_channel [Integer] the output channel number. @return [true, false] the state of the specified output.

# 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) click to toggle source

@raise [RangeError] if the given channel index exceeds the number of

available input channels.

@param state [Integer] the current state. @param input_channel [Integer] the input channel number. @return [true, false] the state of the specified input.

# 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) click to toggle source

@param state [Integer] the current state. @return [String] a binary representation expected by the protocol.

# File lib/adam6050/state.rb, line 95
def to_bin(state)
  format '%05X', (~state & MASK)
end
update(state, output_channel, value) click to toggle source

@raise [RangeError] if the given channel index exceeds the number of

available output channels.

@param state [Integer] the current state. @param output_channel [Integer] the output channel number. @param value [0,Integer] the value to update with. @return [Integer] the next state.

# 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) click to toggle source

@param state [Integer] the current state. @param values [Integer] the next output values. @return [Integer] the next state.

# File lib/adam6050/state.rb, line 82
def update_all(state, values)
  state & INPUT_MASK | (values << NUM_INPUTS) & MASK
end

Private Instance Methods

initial() click to toggle source

@return [Integer] the initial state.

# File lib/adam6050/state.rb, line 24
def initial
  0
end
input_set?(state, input_channel) click to toggle source

@raise [RangeError] if the given channel index exceeds the number of

available input channels.

@param state [Integer] the current state. @param input_channel [Integer] the input channel number. @return [true, false] the state of the specified input.

# 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) click to toggle source

@param state [Integer] the current state. @return [String] a string representation of the state.

# 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) click to toggle source

@raise [RangeError] if the given channel index exceeds the number of

available output channels.

@param state [Integer] the current state. @param output_channel [Integer] the output channel number. @return [true, false] the state of the specified output.

# 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) click to toggle source

@raise [RangeError] if the given channel index exceeds the number of

available input channels.

@param state [Integer] the current state. @param input_channel [Integer] the input channel number. @return [true, false] the state of the specified input.

# 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) click to toggle source

@param state [Integer] the current state. @return [String] a binary representation expected by the protocol.

# File lib/adam6050/state.rb, line 95
def to_bin(state)
  format '%05X', (~state & MASK)
end
update(state, output_channel, value) click to toggle source

@raise [RangeError] if the given channel index exceeds the number of

available output channels.

@param state [Integer] the current state. @param output_channel [Integer] the output channel number. @param value [0,Integer] the value to update with. @return [Integer] the next state.

# 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) click to toggle source

@param state [Integer] the current state. @param values [Integer] the next output values. @return [Integer] the next state.

# File lib/adam6050/state.rb, line 82
def update_all(state, values)
  state & INPUT_MASK | (values << NUM_INPUTS) & MASK
end