Module: TimexDatalinkClient::Helpers::FourByteFormatter

Included in:
Protocol7::Eeprom::Activity, Protocol7::Eeprom::Calendar, Protocol7::Eeprom::Games, Protocol7::Eeprom::PhoneNumber, Protocol7::Eeprom::Speech
Defined in:
lib/timex_datalink_client/helpers/four_byte_formatter.rb

Constant Summary collapse

BYTE_NULL =
0x000
BYTE_TERMINATOR_ENDF =
0x3fe
BYTE_TERMINATOR_ENDR =
0x3ff

Instance Method Summary collapse

Instance Method Details

#four_byte_format_for(byte_arrays) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/timex_datalink_client/helpers/four_byte_formatter.rb', line 10

def four_byte_format_for(byte_arrays)
  byte_arrays.each_with_index.flat_map do |bytes, bytes_index|
    last_index = bytes_index == byte_arrays.count - 1
    terminator = last_index ? BYTE_TERMINATOR_ENDR : BYTE_TERMINATOR_ENDF

    bytes_with_terminator = bytes + [terminator]

    bytes_with_terminator.each_slice(4).flat_map do |bytes_slice|
      bytes_slice.fill(BYTE_NULL, bytes_slice.count, 4 - bytes_slice.count)

      packet_lsbs_sum = bytes_slice.each_with_index.sum { |byte, index| byte / 256 << 6 - index * 2 }
      packet_msbs = bytes_slice.map { |byte| byte % 256 }

      [packet_lsbs_sum] + packet_msbs
    end
  end
end