timex_datalink_client 0.3.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/lib/timex_datalink_client/helpers/char_encoders.rb +12 -7
  3. data/lib/timex_datalink_client/helpers/cpacket_paginator.rb +2 -4
  4. data/lib/timex_datalink_client/protocol_1/alarm.rb +82 -0
  5. data/lib/timex_datalink_client/protocol_1/eeprom.rb +100 -0
  6. data/lib/timex_datalink_client/protocol_1/end.rb +10 -0
  7. data/lib/timex_datalink_client/protocol_1/start.rb +20 -0
  8. data/lib/timex_datalink_client/protocol_1/sync.rb +10 -0
  9. data/lib/timex_datalink_client/protocol_1/time.rb +61 -0
  10. data/lib/timex_datalink_client/protocol_1/time_name.rb +46 -0
  11. data/lib/timex_datalink_client/protocol_3/alarm.rb +59 -0
  12. data/lib/timex_datalink_client/protocol_3/eeprom.rb +95 -0
  13. data/lib/timex_datalink_client/protocol_3/end.rb +10 -0
  14. data/lib/timex_datalink_client/protocol_3/sound_options.rb +42 -0
  15. data/lib/timex_datalink_client/protocol_3/sound_theme.rb +65 -0
  16. data/lib/timex_datalink_client/protocol_3/start.rb +20 -0
  17. data/lib/timex_datalink_client/protocol_3/sync.rb +10 -0
  18. data/lib/timex_datalink_client/protocol_3/time.rb +77 -0
  19. data/lib/timex_datalink_client/protocol_3/wrist_app.rb +67 -0
  20. data/lib/timex_datalink_client/version.rb +1 -1
  21. data/lib/timex_datalink_client.rb +23 -12
  22. metadata +21 -12
  23. data/lib/timex_datalink_client/alarm.rb +0 -61
  24. data/lib/timex_datalink_client/eeprom.rb +0 -92
  25. data/lib/timex_datalink_client/sound_options.rb +0 -40
  26. data/lib/timex_datalink_client/sound_theme.rb +0 -62
  27. data/lib/timex_datalink_client/start.rb +0 -18
  28. data/lib/timex_datalink_client/time.rb +0 -69
  29. data/lib/timex_datalink_client/wrist_app.rb +0 -64
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 187352acb16bdc8baa7fec37d4793942f49b6c3d60da9a778b4afd94505c9f1e
4
- data.tar.gz: 55214e32e3536da008eb7739d0921af0012c1af6ff70d17491ef37a2fd6008a8
3
+ metadata.gz: 061a85e62bc26614e71a4fa43dc9c17cc6092a516a417d5545d7ecd029a34af6
4
+ data.tar.gz: 4e3fc39203b45c38599c6980c72c42e6e05efdc87e7c7d5c9f4cc099e995d39e
5
5
  SHA512:
6
- metadata.gz: 9106e98e689a8646ab779cd1ca38bce9c220c8aa80ae0fe984237e6a92270b0ee10bbdf9b31bc40c4492dc7b17d25c796baf7f3a5720a4649d7deb09a37b540f
7
- data.tar.gz: 70e7601d4ba51ecfc3cb6d3f51549e8272ec3baf55ee39174a6fbc5f48272569efcfad25b3eef447a05af767c855f79a4b84d15c286a39d30d2ad7506d273213
6
+ metadata.gz: 40b7dbbfd0af7b4600b5d94623584615b193b2011b976ed229ee7ba084892ba05d31002150dd9decfafcaecb301d2fa44d1c9a5a03ecace2a20441edda50b6ca
7
+ data.tar.gz: b902ecee9de6ebf920e86a08c57e9651730335492c1e05ccfe55d0169fbf7c714ac5ba02f8640983ea69bc5d34ae1269e8f1d97b9e36f4d3921e07cdea0f039c
@@ -3,19 +3,24 @@
3
3
  class TimexDatalinkClient
4
4
  class Helpers
5
5
  module CharEncoders
6
- CHARS = "0123456789abcdefghijklmnopqrstuvwxyz !\"#$%&'()*+,-./:\\;=@?ABCDEF"
6
+ CHARS = "0123456789abcdefghijklmnopqrstuvwxyz !\"#$%&'()*+,-./:\\;=@?_|<>[]"
7
+ EEPROM_CHARS = "0123456789abcdefghijklmnopqrstuvwxyz !\"#$%&'()*+,-./:\\;=@?_|<>["
8
+ PHONE_CHARS = "0123456789cfhpw "
9
+ INVALID_CHAR = " "
10
+
7
11
  EEPROM_TERMINATOR = 0x3f
8
12
 
9
- PHONE_CHARS = "0123456789cfhpw "
13
+ def chars_for(string_chars, char_map: CHARS, length: nil, pad: false)
14
+ formatted_chars = string_chars.downcase[0..length.to_i - 1]
15
+ formatted_chars = formatted_chars.ljust(length) if pad
10
16
 
11
- def chars_for(string_chars, char_map: CHARS)
12
- string_chars.each_char.map do |string_char|
13
- char_map.index(string_char)
17
+ formatted_chars.each_char.map do |char|
18
+ char_map.index(char) || char_map.index(INVALID_CHAR)
14
19
  end
15
20
  end
16
21
 
17
22
  def eeprom_chars_for(string_chars)
18
- chars = chars_for(string_chars).append(EEPROM_TERMINATOR)
23
+ chars = chars_for(string_chars, char_map: EEPROM_CHARS, length: 31).append(EEPROM_TERMINATOR)
19
24
 
20
25
  packed_int = chars.each_with_index.sum do |char, index|
21
26
  char << (6 * index)
@@ -25,7 +30,7 @@ class TimexDatalinkClient
25
30
  end
26
31
 
27
32
  def phone_chars_for(string_chars)
28
- chars = chars_for(string_chars, char_map: PHONE_CHARS)
33
+ chars = chars_for(string_chars, char_map: PHONE_CHARS, length: 12)
29
34
 
30
35
  packed_int = chars.each_with_index.sum do |char, index|
31
36
  char << (4 * index)
@@ -3,10 +3,8 @@
3
3
  class TimexDatalinkClient
4
4
  class Helpers
5
5
  module CpacketPaginator
6
- ITEMS_PER_PACKET = 32
7
-
8
- def paginate_cpackets(header:, cpackets:)
9
- paginated_cpackets = cpackets.each_slice(ITEMS_PER_PACKET)
6
+ def paginate_cpackets(header:, length:, cpackets:)
7
+ paginated_cpackets = cpackets.each_slice(length)
10
8
 
11
9
  paginated_cpackets.map.with_index(1) do |paginated_cpacket, index|
12
10
  header + [index] + paginated_cpacket
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "timex_datalink_client/helpers/char_encoders"
4
+ require "timex_datalink_client/helpers/crc_packets_wrapper"
5
+
6
+ class TimexDatalinkClient
7
+ class Protocol1
8
+ class Alarm
9
+ include Helpers::CharEncoders
10
+ prepend Helpers::CrcPacketsWrapper
11
+
12
+ CPACKET_ALARM = [0x50]
13
+ CPACKET_ALARM_SILENT = [0x70, 0x00]
14
+
15
+ ALARM_SILENT_START_INDEX = 0x61
16
+
17
+ attr_accessor :number, :audible, :time, :message, :month, :day
18
+
19
+ # Create an Alarm instance.
20
+ #
21
+ # @param number [Integer] Alarm number (from 1 to 5).
22
+ # @param audible [Boolean] Toggle alarm sounds.
23
+ # @param time [::Time] Time of alarm.
24
+ # @param message [String] Alarm message text.
25
+ # @param month [Integer, nil] Month of alarm.
26
+ # @param day [Integer, nil] Day of alarm.
27
+ # @return [Alarm] Alarm instance.
28
+ def initialize(number:, audible:, time:, message:, month: nil, day: nil)
29
+ @number = number
30
+ @audible = audible
31
+ @time = time
32
+ @message = message
33
+ @month = month
34
+ @day = day
35
+ end
36
+
37
+ # Compile packets for an alarm.
38
+ #
39
+ # @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
40
+ def packets
41
+ [alarm_data_packet].tap do |packets|
42
+ packets << alarm_silent_packet unless audible
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def alarm_data_packet
49
+ [
50
+ CPACKET_ALARM,
51
+ number,
52
+ time.hour,
53
+ time.min,
54
+ month.to_i,
55
+ day.to_i,
56
+ message_characters,
57
+ audible_integer
58
+ ].flatten
59
+ end
60
+
61
+ def alarm_silent_packet
62
+ [
63
+ CPACKET_ALARM_SILENT,
64
+ alarm_silent_index,
65
+ 0
66
+ ].flatten
67
+ end
68
+
69
+ def message_characters
70
+ chars_for(message, length: 8, pad: true)
71
+ end
72
+
73
+ def audible_integer
74
+ audible ? 1 : 0
75
+ end
76
+
77
+ def alarm_silent_index
78
+ ALARM_SILENT_START_INDEX + number
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "timex_datalink_client/helpers/cpacket_paginator"
4
+ require "timex_datalink_client/helpers/crc_packets_wrapper"
5
+
6
+ class TimexDatalinkClient
7
+ class Protocol1
8
+ class Eeprom
9
+ include Helpers::CpacketPaginator
10
+ prepend Helpers::CrcPacketsWrapper
11
+
12
+ CPACKET_SECT = [0x60]
13
+ CPACKET_DATA = [0x61]
14
+ CPACKET_END = [0x62]
15
+
16
+ CPACKET_DATA_LENGTH = 27
17
+ START_INDEX = 14
18
+ APPOINTMENT_NO_NOTIFICATION = 0xff
19
+
20
+ attr_accessor :appointments, :anniversaries, :phone_numbers, :lists, :appointment_notification
21
+
22
+ # Create an Eeprom instance.
23
+ #
24
+ # @param appointments [Array<Appointment>] Appointments to be added to EEPROM data.
25
+ # @param anniversaries [Array<Anniversary>] Anniversaries to be added to EEPROM data.
26
+ # @param phone_numbers [Array<PhoneNumber>] Phone numbers to be added to EEPROM data.
27
+ # @param lists [Array<List>] Lists to be added to EEPROM data.
28
+ # @param appointment_notification [Integer] Appointment notification (intervals of 15 minutes, 255 for no
29
+ # notification)
30
+ # @return [Eeprom] Eeprom instance.
31
+ def initialize(appointments: [], anniversaries: [], phone_numbers: [], lists: [], appointment_notification: APPOINTMENT_NO_NOTIFICATION)
32
+ @appointments = appointments
33
+ @anniversaries = anniversaries
34
+ @phone_numbers = phone_numbers
35
+ @lists = lists
36
+ @appointment_notification = appointment_notification
37
+ end
38
+
39
+ # Compile packets for EEPROM data.
40
+ #
41
+ # @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
42
+ def packets
43
+ [header] + payloads + [CPACKET_END]
44
+ end
45
+
46
+ private
47
+
48
+ def header
49
+ [
50
+ CPACKET_SECT,
51
+ payloads.length
52
+ ].flatten
53
+ end
54
+
55
+ def payload
56
+ [
57
+ items_indexes,
58
+ items_lengths,
59
+ earliest_appointment_year,
60
+ appointment_notification,
61
+ all_packets
62
+ ].flatten
63
+ end
64
+
65
+ def payloads
66
+ paginate_cpackets(header: CPACKET_DATA, length: CPACKET_DATA_LENGTH, cpackets: payload)
67
+ end
68
+
69
+ def all_items
70
+ [appointments, lists, phone_numbers, anniversaries]
71
+ end
72
+
73
+ def all_packets
74
+ all_items.flatten.map(&:packet).flatten
75
+ end
76
+
77
+ def items_indexes
78
+ index = START_INDEX
79
+
80
+ all_items.each_with_object([]) do |items, indexes|
81
+ indexes.concat(index.divmod(256))
82
+
83
+ index += items.sum { |item| item.packet.length }
84
+ end
85
+ end
86
+
87
+ def items_lengths
88
+ all_items.map(&:length)
89
+ end
90
+
91
+ def earliest_appointment_year
92
+ earliest_appointment = appointments.min_by(&:time)
93
+
94
+ return 0 unless earliest_appointment
95
+
96
+ earliest_appointment.time.year % 100
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "timex_datalink_client/end"
4
+
5
+ class TimexDatalinkClient
6
+ class Protocol1
7
+ class End < End
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "timex_datalink_client/helpers/crc_packets_wrapper"
4
+
5
+ class TimexDatalinkClient
6
+ class Protocol1
7
+ class Start
8
+ prepend Helpers::CrcPacketsWrapper
9
+
10
+ CPACKET_START = [0x20, 0x00, 0x00, 0x01]
11
+
12
+ # Compile packets for data start command.
13
+ #
14
+ # @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
15
+ def packets
16
+ [CPACKET_START]
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "timex_datalink_client/sync"
4
+
5
+ class TimexDatalinkClient
6
+ class Protocol1
7
+ class Sync < Sync
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "timex_datalink_client/helpers/crc_packets_wrapper"
4
+
5
+ class TimexDatalinkClient
6
+ class Protocol1
7
+ class Time
8
+ prepend Helpers::CrcPacketsWrapper
9
+
10
+ CPACKET_TIME = [0x30]
11
+
12
+ attr_accessor :zone, :is_24h, :time
13
+
14
+ # Create a Time instance.
15
+ #
16
+ # @param zone [Integer] Time zone number (1 or 2).
17
+ # @param is_24h [Boolean] Toggle 24 hour time.
18
+ # @param time [::Time] Time to set.
19
+ # @return [Time] Time instance.
20
+ def initialize(zone:, is_24h:, time:)
21
+ @zone = zone
22
+ @is_24h = is_24h
23
+ @time = time
24
+ end
25
+
26
+ # Compile packets for a time.
27
+ #
28
+ # @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
29
+ def packets
30
+ [
31
+ [
32
+ CPACKET_TIME,
33
+ zone,
34
+ time.hour,
35
+ time.min,
36
+ time.month,
37
+ time.day,
38
+ year_mod_1900,
39
+ wday_from_monday,
40
+ time.sec,
41
+ is_24h_value
42
+ ].flatten
43
+ ]
44
+ end
45
+
46
+ private
47
+
48
+ def year_mod_1900
49
+ time.year % 100
50
+ end
51
+
52
+ def wday_from_monday
53
+ (time.wday + 6) % 7
54
+ end
55
+
56
+ def is_24h_value
57
+ is_24h ? 2 : 1
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "timex_datalink_client/helpers/char_encoders"
4
+ require "timex_datalink_client/helpers/crc_packets_wrapper"
5
+
6
+ class TimexDatalinkClient
7
+ class Protocol1
8
+ class TimeName
9
+ include Helpers::CharEncoders
10
+ prepend Helpers::CrcPacketsWrapper
11
+
12
+ CPACKET_NAME = [0x31]
13
+
14
+ attr_accessor :zone, :name
15
+
16
+ # Create a TimeName instance.
17
+ #
18
+ # @param zone [Integer] Time zone number (1 or 2).
19
+ # @param name [String] Name of time zone (3 chars max)
20
+ # @return [TimeName] TimeName instance.
21
+ def initialize(zone:, name:)
22
+ @zone = zone
23
+ @name = name
24
+ end
25
+
26
+ # Compile packets for a time name.
27
+ #
28
+ # @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
29
+ def packets
30
+ [
31
+ [
32
+ CPACKET_NAME,
33
+ zone,
34
+ name_characters
35
+ ].flatten
36
+ ]
37
+ end
38
+
39
+ private
40
+
41
+ def name_characters
42
+ chars_for(name, length: 3, pad: true)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "timex_datalink_client/helpers/char_encoders"
4
+ require "timex_datalink_client/helpers/crc_packets_wrapper"
5
+
6
+ class TimexDatalinkClient
7
+ class Protocol3
8
+ class Alarm
9
+ include Helpers::CharEncoders
10
+ prepend Helpers::CrcPacketsWrapper
11
+
12
+ CPACKET_ALARM = [0x50]
13
+
14
+ attr_accessor :number, :audible, :time, :message
15
+
16
+ # Create an Alarm instance.
17
+ #
18
+ # @param number [Integer] Alarm number (from 1 to 5).
19
+ # @param audible [Boolean] Toggle alarm sounds.
20
+ # @param time [::Time] Time of alarm.
21
+ # @param message [String] Alarm message text.
22
+ # @return [Alarm] Alarm instance.
23
+ def initialize(number:, audible:, time:, message:)
24
+ @number = number
25
+ @audible = audible
26
+ @time = time
27
+ @message = message
28
+ end
29
+
30
+ # Compile packets for an alarm.
31
+ #
32
+ # @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
33
+ def packets
34
+ [
35
+ [
36
+ CPACKET_ALARM,
37
+ number,
38
+ time.hour,
39
+ time.min,
40
+ 0,
41
+ 0,
42
+ message_characters,
43
+ audible_integer
44
+ ].flatten
45
+ ]
46
+ end
47
+
48
+ private
49
+
50
+ def message_characters
51
+ chars_for(message, length: 8, pad: true)
52
+ end
53
+
54
+ def audible_integer
55
+ audible ? 1 : 0
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "timex_datalink_client/helpers/cpacket_paginator"
4
+ require "timex_datalink_client/helpers/crc_packets_wrapper"
5
+
6
+ class TimexDatalinkClient
7
+ class Protocol3
8
+ class Eeprom
9
+ include Helpers::CpacketPaginator
10
+ prepend Helpers::CrcPacketsWrapper
11
+
12
+ CPACKET_CLEAR = [0x93, 0x01]
13
+ CPACKET_SECT = [0x90, 0x01]
14
+ CPACKET_DATA = [0x91, 0x01]
15
+ CPACKET_END = [0x92, 0x01]
16
+
17
+ CPACKET_DATA_LENGTH = 32
18
+ START_ADDRESS = 0x0236
19
+ APPOINTMENT_NO_NOTIFICATION = 0xff
20
+
21
+ attr_accessor :appointments, :anniversaries, :phone_numbers, :lists, :appointment_notification
22
+
23
+ # Create an Eeprom instance.
24
+ #
25
+ # @param appointments [Array<Appointment>] Appointments to be added to EEPROM data.
26
+ # @param anniversaries [Array<Anniversary>] Anniversaries to be added to EEPROM data.
27
+ # @param phone_numbers [Array<PhoneNumber>] Phone numbers to be added to EEPROM data.
28
+ # @param lists [Array<List>] Lists to be added to EEPROM data.
29
+ # @param appointment_notification [Integer] Appointment notification (intervals of 15 minutes, 255 for no
30
+ # notification)
31
+ # @return [Eeprom] Eeprom instance.
32
+ def initialize(appointments: [], anniversaries: [], phone_numbers: [], lists: [], appointment_notification: APPOINTMENT_NO_NOTIFICATION)
33
+ @appointments = appointments
34
+ @anniversaries = anniversaries
35
+ @phone_numbers = phone_numbers
36
+ @lists = lists
37
+ @appointment_notification = appointment_notification
38
+ end
39
+
40
+ # Compile packets for EEPROM data.
41
+ #
42
+ # @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
43
+ def packets
44
+ [CPACKET_CLEAR, header] + payloads + [CPACKET_END]
45
+ end
46
+
47
+ private
48
+
49
+ def header
50
+ [
51
+ CPACKET_SECT,
52
+ payloads.length,
53
+ items_addresses,
54
+ items_lengths,
55
+ earliest_appointment_year,
56
+ appointment_notification
57
+ ].flatten
58
+ end
59
+
60
+ def payloads
61
+ paginate_cpackets(header: CPACKET_DATA, length: CPACKET_DATA_LENGTH, cpackets: all_packets)
62
+ end
63
+
64
+ def all_items
65
+ [appointments, lists, phone_numbers, anniversaries]
66
+ end
67
+
68
+ def all_packets
69
+ all_items.flatten.map(&:packet).flatten
70
+ end
71
+
72
+ def items_addresses
73
+ address = START_ADDRESS
74
+
75
+ all_items.each_with_object([]) do |items, addresses|
76
+ addresses.concat(address.divmod(256))
77
+
78
+ address += items.sum { |item| item.packet.length }
79
+ end
80
+ end
81
+
82
+ def items_lengths
83
+ all_items.map(&:length)
84
+ end
85
+
86
+ def earliest_appointment_year
87
+ earliest_appointment = appointments.min_by(&:time)
88
+
89
+ return 0 unless earliest_appointment
90
+
91
+ earliest_appointment.time.year % 100
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "timex_datalink_client/end"
4
+
5
+ class TimexDatalinkClient
6
+ class Protocol3
7
+ class End < End
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "timex_datalink_client/helpers/crc_packets_wrapper"
4
+
5
+ class TimexDatalinkClient
6
+ class Protocol3
7
+ class SoundOptions
8
+ prepend Helpers::CrcPacketsWrapper
9
+
10
+ CPACKET_BEEPS = [0x71]
11
+
12
+ attr_accessor :hourly_chime, :button_beep
13
+
14
+ # Create a SoundOptions instance.
15
+ #
16
+ # @param hourly_chime [Boolean] Toggle hourly chime sounds.
17
+ # @param button_beep [Boolean] Toggle button beep sounds.
18
+ # @return [SoundOptions] SoundOptions instance.
19
+ def initialize(hourly_chime:, button_beep:)
20
+ @hourly_chime = hourly_chime
21
+ @button_beep = button_beep
22
+ end
23
+
24
+ # Compile packets for sound options.
25
+ #
26
+ # @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
27
+ def packets
28
+ [
29
+ CPACKET_BEEPS + [hourly_chime_integer, button_beep_integer]
30
+ ]
31
+ end
32
+
33
+ def hourly_chime_integer
34
+ hourly_chime ? 1 : 0
35
+ end
36
+
37
+ def button_beep_integer
38
+ button_beep ? 1 : 0
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "timex_datalink_client/helpers/cpacket_paginator"
4
+ require "timex_datalink_client/helpers/crc_packets_wrapper"
5
+
6
+ class TimexDatalinkClient
7
+ class Protocol3
8
+ class SoundTheme
9
+ include Helpers::CpacketPaginator
10
+ prepend Helpers::CrcPacketsWrapper
11
+
12
+ CPACKET_SECT = [0x90, 0x03]
13
+ CPACKET_DATA = [0x91, 0x03]
14
+ CPACKET_END = [0x92, 0x03]
15
+
16
+ CPACKET_DATA_LENGTH = 32
17
+ SOUND_DATA_HEADER = "\x25\x04\x19\x69"
18
+
19
+ attr_accessor :spc_file
20
+
21
+ # Create a SoundTheme instance.
22
+ #
23
+ # @param sound_theme_data [String, nil] Sound theme data.
24
+ # @param spc_file [String, nil] Path to SPC file.
25
+ # @return [SoundTheme] SoundTheme instance.
26
+ def initialize(sound_theme_data: nil, spc_file: nil)
27
+ @sound_theme_data = sound_theme_data
28
+ @spc_file = spc_file
29
+ end
30
+
31
+ # Compile packets for a sound theme.
32
+ #
33
+ # @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
34
+ def packets
35
+ [load_sect] + payloads + [CPACKET_END]
36
+ end
37
+
38
+ private
39
+
40
+ def load_sect
41
+ CPACKET_SECT + [payloads.length, offset]
42
+ end
43
+
44
+ def payloads
45
+ paginate_cpackets(header: CPACKET_DATA, length: CPACKET_DATA_LENGTH, cpackets: sound_theme_data.bytes)
46
+ end
47
+
48
+ def sound_theme_data
49
+ @sound_theme_data || spc_file_data_without_header
50
+ end
51
+
52
+ def spc_file_data
53
+ File.open(spc_file, "rb").read
54
+ end
55
+
56
+ def spc_file_data_without_header
57
+ spc_file_data.delete_prefix(SOUND_DATA_HEADER)
58
+ end
59
+
60
+ def offset
61
+ 0x100 - sound_theme_data.bytesize
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "timex_datalink_client/helpers/crc_packets_wrapper"
4
+
5
+ class TimexDatalinkClient
6
+ class Protocol3
7
+ class Start
8
+ prepend Helpers::CrcPacketsWrapper
9
+
10
+ CPACKET_START = [0x20, 0x00, 0x00, 0x03]
11
+
12
+ # Compile packets for data start command.
13
+ #
14
+ # @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
15
+ def packets
16
+ [CPACKET_START]
17
+ end
18
+ end
19
+ end
20
+ end