Module: Wifly::Calculations

Included in:
Control
Defined in:
lib/wifly/calculations.rb

Instance Method Summary collapse

Instance Method Details

#parse_io(hex_str) ⇒ Object

Parse the hexadecimal string returned from the show_io command into an array of pins that are high



7
8
9
10
11
12
13
# File 'lib/wifly/calculations.rb', line 7

def parse_io(hex_str)
  # use sprintf to pad with 0's to 16 bits
  binary_string = "%016b"  % hex_str.hex.to_i
  binary_string.reverse.chars.each_with_index.map do |value, pin|
    pin if value == "1"
  end.compact
end

#pin_to_hex(pin) ⇒ Object

Given a pin number, return the hex code that corresponds to it



18
19
20
21
22
23
# File 'lib/wifly/calculations.rb', line 18

def pin_to_hex(pin)
  return "0x0" if pin == 0
  binstr = "0b1" + ("0" * pin) # make a binary string with a 1 in `pin` position
  base10 = binstr.to_i(2)      # convert to base 10
  "0x" + base10.to_s(16)       # return hexadecimal string
end