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.
12- NUM_OUTPUTS =
Returns the number of outputs.
6
Class Method Summary collapse
-
.initial ⇒ Integer
The initial state.
-
.input_set?(state, input_channel) ⇒ true, false
The state of the specified input.
-
.inspect(state) ⇒ String
A string representation of the state.
-
.output_set?(state, output_channel) ⇒ true, false
The state of the specified output.
-
.set_input(state, input_channel, value) ⇒ true, false
The state of the specified input.
-
.to_bin(state) ⇒ String
A binary representation expected by the protocol.
-
.update(state, output_channel, value) ⇒ Integer
The next state.
-
.update_all(state, values) ⇒ Integer
The next state.
Class Method Details
.initial ⇒ Integer
Returns 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.
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.
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.
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.
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.
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.
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.
82 83 84 |
# File 'lib/adam6050/state.rb', line 82 def update_all(state, values) state & INPUT_MASK | (values << NUM_INPUTS) & MASK end |