Module: TimexDatalinkClient::Helpers::CharEncoders

Constant Summary collapse

CHARS =
"0123456789abcdefghijklmnopqrstuvwxyz !\"#$%&'()*+,-./:\\;=@?_|<>[]"
CHARS_PROTOCOL_6 =
"0123456789 abcdefghijklmnopqrstuvwxyz!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
EEPROM_CHARS =
"0123456789abcdefghijklmnopqrstuvwxyz !\"#$%&'()*+,-./:\\;=@?_|<>["
PHONE_CHARS =
"0123456789cfhpw "
INVALID_CHAR =
" "
EEPROM_TERMINATOR =
0x3f

Instance Method Summary collapse

Instance Method Details

#chars_for(string_chars, char_map: CHARS, length: nil, pad: false) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/timex_datalink_client/helpers/char_encoders.rb', line 14

def chars_for(string_chars, char_map: CHARS, length: nil, pad: false)
  formatted_chars = string_chars.downcase[0..length.to_i - 1]
  formatted_chars = formatted_chars.ljust(length) if pad

  formatted_chars.each_char.map do |char|
    char_map.index(char) || char_map.index(INVALID_CHAR)
  end
end

#eeprom_chars_for(string_chars, length: 31) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/timex_datalink_client/helpers/char_encoders.rb', line 27

def eeprom_chars_for(string_chars, length: 31)
  chars = chars_for(string_chars, char_map: EEPROM_CHARS, length:).append(EEPROM_TERMINATOR)

  packed_int = chars.each_with_index.sum do |char, index|
    char << (6 * index)
  end

  packed_int.digits(256)
end

#phone_chars_for(string_chars) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/timex_datalink_client/helpers/char_encoders.rb', line 37

def phone_chars_for(string_chars)
  chars = chars_for(string_chars, char_map: PHONE_CHARS, length: 12)

  packed_int = chars.each_with_index.sum do |char, index|
    char << (4 * index)
  end

  packed_int.digits(256)
end

#protocol_6_chars_for(string_chars, length: nil, pad: false) ⇒ Object



23
24
25
# File 'lib/timex_datalink_client/helpers/char_encoders.rb', line 23

def protocol_6_chars_for(string_chars, length: nil, pad: false)
  chars_for(string_chars, char_map: CHARS_PROTOCOL_6, length:, pad:)
end