/
char_encoders.rb
42 lines (31 loc) · 1.09 KB
/
char_encoders.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true
class TimexDatalinkClient
class Helpers
module CharEncoders
CHARS = "0123456789abcdefghijklmnopqrstuvwxyz !\"#$%&'()*+,-./:\\;=@?_|<>[]"
EEPROM_TERMINATOR = 0x3f
PHONE_CHARS = "0123456789cfhpw "
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)
end
end
def eeprom_chars_for(string_chars)
chars = chars_for(string_chars, length: 31).append(EEPROM_TERMINATOR)
packed_int = chars.each_with_index.sum do |char, index|
char << (6 * index)
end
packed_int.digits(256)
end
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
end
end
end