timex_datalink_client 0.4.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/lib/timex_datalink_client/helpers/char_encoders.rb +2 -2
  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/anniversary.rb +44 -0
  6. data/lib/timex_datalink_client/protocol_1/eeprom/appointment.rb +49 -0
  7. data/lib/timex_datalink_client/protocol_1/eeprom/list.rb +43 -0
  8. data/lib/timex_datalink_client/protocol_1/eeprom/phone_number.rb +56 -0
  9. data/lib/timex_datalink_client/protocol_1/eeprom.rb +100 -0
  10. data/lib/timex_datalink_client/protocol_1/end.rb +20 -0
  11. data/lib/timex_datalink_client/protocol_1/start.rb +20 -0
  12. data/lib/timex_datalink_client/protocol_1/sync.rb +40 -0
  13. data/lib/timex_datalink_client/protocol_1/time.rb +61 -0
  14. data/lib/timex_datalink_client/protocol_1/time_name.rb +46 -0
  15. data/lib/timex_datalink_client/protocol_3/alarm.rb +59 -0
  16. data/lib/timex_datalink_client/protocol_3/eeprom/anniversary.rb +44 -0
  17. data/lib/timex_datalink_client/protocol_3/eeprom/appointment.rb +49 -0
  18. data/lib/timex_datalink_client/protocol_3/eeprom/list.rb +43 -0
  19. data/lib/timex_datalink_client/protocol_3/eeprom/phone_number.rb +56 -0
  20. data/lib/timex_datalink_client/protocol_3/eeprom.rb +95 -0
  21. data/lib/timex_datalink_client/protocol_3/end.rb +20 -0
  22. data/lib/timex_datalink_client/protocol_3/sound_options.rb +42 -0
  23. data/lib/timex_datalink_client/protocol_3/sound_theme.rb +65 -0
  24. data/lib/timex_datalink_client/protocol_3/start.rb +20 -0
  25. data/lib/timex_datalink_client/protocol_3/sync.rb +40 -0
  26. data/lib/timex_datalink_client/protocol_3/time.rb +77 -0
  27. data/lib/timex_datalink_client/protocol_3/wrist_app.rb +67 -0
  28. data/lib/timex_datalink_client/protocol_9/alarm.rb +63 -0
  29. data/lib/timex_datalink_client/protocol_9/eeprom/chrono.rb +47 -0
  30. data/lib/timex_datalink_client/protocol_9/eeprom/phone_number.rb +74 -0
  31. data/lib/timex_datalink_client/protocol_9/eeprom.rb +85 -0
  32. data/lib/timex_datalink_client/protocol_9/end.rb +20 -0
  33. data/lib/timex_datalink_client/protocol_9/sound_options.rb +42 -0
  34. data/lib/timex_datalink_client/protocol_9/start.rb +20 -0
  35. data/lib/timex_datalink_client/protocol_9/sync.rb +40 -0
  36. data/lib/timex_datalink_client/protocol_9/time.rb +61 -0
  37. data/lib/timex_datalink_client/protocol_9/time_name.rb +46 -0
  38. data/lib/timex_datalink_client/protocol_9/timer.rb +54 -0
  39. data/lib/timex_datalink_client/version.rb +1 -1
  40. data/lib/timex_datalink_client.rb +44 -16
  41. metadata +38 -16
  42. data/lib/timex_datalink_client/alarm.rb +0 -59
  43. data/lib/timex_datalink_client/eeprom/anniversary.rb +0 -42
  44. data/lib/timex_datalink_client/eeprom/appointment.rb +0 -47
  45. data/lib/timex_datalink_client/eeprom/list.rb +0 -41
  46. data/lib/timex_datalink_client/eeprom/phone_number.rb +0 -56
  47. data/lib/timex_datalink_client/eeprom.rb +0 -92
  48. data/lib/timex_datalink_client/end.rb +0 -18
  49. data/lib/timex_datalink_client/sound_options.rb +0 -40
  50. data/lib/timex_datalink_client/sound_theme.rb +0 -62
  51. data/lib/timex_datalink_client/start.rb +0 -18
  52. data/lib/timex_datalink_client/sync.rb +0 -37
  53. data/lib/timex_datalink_client/time.rb +0 -75
  54. data/lib/timex_datalink_client/wrist_app.rb +0 -64
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "timex_datalink_client/helpers/char_encoders"
4
+ require "timex_datalink_client/helpers/length_packet_wrapper"
5
+
6
+ class TimexDatalinkClient
7
+ class Protocol3
8
+ class Eeprom
9
+ class Anniversary
10
+ include Helpers::CharEncoders
11
+ prepend Helpers::LengthPacketWrapper
12
+
13
+ attr_accessor :time, :anniversary
14
+
15
+ # Create an Anniversary instance.
16
+ #
17
+ # @param time [::Time] Time of anniversary.
18
+ # @param anniversary [String] Anniversary text.
19
+ # @return [Anniversary] Anniversary instance.
20
+ def initialize(time:, anniversary:)
21
+ @time = time
22
+ @anniversary = anniversary
23
+ end
24
+
25
+ # Compile a packet for an anniversary.
26
+ #
27
+ # @return [Array<Integer>] Array of integers that represent bytes.
28
+ def packet
29
+ [
30
+ time.month,
31
+ time.day,
32
+ anniversary_characters
33
+ ].flatten
34
+ end
35
+
36
+ private
37
+
38
+ def anniversary_characters
39
+ eeprom_chars_for(anniversary)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "timex_datalink_client/helpers/char_encoders"
4
+ require "timex_datalink_client/helpers/length_packet_wrapper"
5
+
6
+ class TimexDatalinkClient
7
+ class Protocol3
8
+ class Eeprom
9
+ class Appointment
10
+ include Helpers::CharEncoders
11
+ prepend Helpers::LengthPacketWrapper
12
+
13
+ attr_accessor :time, :message
14
+
15
+ # Create an Appointment instance.
16
+ #
17
+ # @param time [::Time] Time of appointment.
18
+ # @param message [String] Appointment text.
19
+ # @return [Appointment] Appointment instance.
20
+ def initialize(time:, message:)
21
+ @time = time
22
+ @message = message
23
+ end
24
+
25
+ # Compile a packet for an appointment.
26
+ #
27
+ # @return [Array<Integer>] Array of integers that represent bytes.
28
+ def packet
29
+ [
30
+ time.month,
31
+ time.day,
32
+ time_15m,
33
+ message_characters
34
+ ].flatten
35
+ end
36
+
37
+ private
38
+
39
+ def time_15m
40
+ time.hour * 4 + time.min / 15
41
+ end
42
+
43
+ def message_characters
44
+ eeprom_chars_for(message)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "timex_datalink_client/helpers/char_encoders"
4
+ require "timex_datalink_client/helpers/length_packet_wrapper"
5
+
6
+ class TimexDatalinkClient
7
+ class Protocol3
8
+ class Eeprom
9
+ class List
10
+ include Helpers::CharEncoders
11
+ prepend Helpers::LengthPacketWrapper
12
+
13
+ attr_accessor :list_entry, :priority
14
+
15
+ # Create a List instance.
16
+ #
17
+ # @param list_entry [String] List entry text.
18
+ # @param priority [Integer] List priority.
19
+ # @return [List] List instance.
20
+ def initialize(list_entry:, priority:)
21
+ @list_entry = list_entry
22
+ @priority = priority
23
+ end
24
+
25
+ # Compile a packet for a list.
26
+ #
27
+ # @return [Array<Integer>] Array of integers that represent bytes.
28
+ def packet
29
+ [
30
+ priority,
31
+ list_entry_characters
32
+ ].flatten
33
+ end
34
+
35
+ private
36
+
37
+ def list_entry_characters
38
+ eeprom_chars_for(list_entry)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "timex_datalink_client/helpers/char_encoders"
4
+ require "timex_datalink_client/helpers/length_packet_wrapper"
5
+
6
+ class TimexDatalinkClient
7
+ class Protocol3
8
+ class Eeprom
9
+ class PhoneNumber
10
+ include Helpers::CharEncoders
11
+ prepend Helpers::LengthPacketWrapper
12
+
13
+ PHONE_DIGITS = 12
14
+
15
+ attr_accessor :name, :number, :type
16
+
17
+ # Create a PhoneNumber instance.
18
+ #
19
+ # @param name [String] Name associated to phone number.
20
+ # @param number [String] Phone number text.
21
+ # @param type [String] Phone number type.
22
+ # @return [PhoneNumber] PhoneNumber instance.
23
+ def initialize(name:, number:, type: " ")
24
+ @name = name
25
+ @number = number
26
+ @type = type
27
+ end
28
+
29
+ # Compile a packet for a phone number.
30
+ #
31
+ # @return [Array<Integer>] Array of integers that represent bytes.
32
+ def packet
33
+ [
34
+ number_with_type_characters,
35
+ name_characters
36
+ ].flatten
37
+ end
38
+
39
+ private
40
+
41
+ def number_with_type_padded
42
+ number_with_type = "#{number} #{type}"
43
+ number_with_type.rjust(PHONE_DIGITS)
44
+ end
45
+
46
+ def number_with_type_characters
47
+ phone_chars_for(number_with_type_padded)
48
+ end
49
+
50
+ def name_characters
51
+ eeprom_chars_for(name)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ 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,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 End
8
+ prepend Helpers::CrcPacketsWrapper
9
+
10
+ CPACKET_SKIP = [0x21]
11
+
12
+ # Compile packets for data end command.
13
+ #
14
+ # @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
15
+ def packets
16
+ [CPACKET_SKIP]
17
+ end
18
+ end
19
+ end
20
+ 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
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ class TimexDatalinkClient
4
+ class Protocol3
5
+ class Sync
6
+ PING_BYTE = [0x78]
7
+ SYNC_1_BYTE = [0x55]
8
+ SYNC_2_BYTE = [0xaa]
9
+
10
+ SYNC_2_LENGTH = 40
11
+
12
+ attr_accessor :length
13
+
14
+ # Create a Sync instance.
15
+ #
16
+ # @param length [Integer] Number of 0x55 sync bytes to use.
17
+ # @return [Sync] Sync instance.
18
+ def initialize(length: 300)
19
+ @length = length
20
+ end
21
+
22
+ # Compile packets for syncronization data.
23
+ #
24
+ # @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
25
+ def packets
26
+ [PING_BYTE + render_sync_1 + render_sync_2]
27
+ end
28
+
29
+ private
30
+
31
+ def render_sync_1
32
+ SYNC_1_BYTE * length
33
+ end
34
+
35
+ def render_sync_2
36
+ SYNC_2_BYTE * SYNC_2_LENGTH
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,77 @@
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 Time
9
+ include Helpers::CharEncoders
10
+ prepend Helpers::CrcPacketsWrapper
11
+
12
+ CPACKET_TIME = [0x32]
13
+
14
+ attr_accessor :zone, :is_24h, :date_format, :time
15
+
16
+ # Create a Time instance.
17
+ #
18
+ # @param zone [Integer] Time zone number (1 or 2).
19
+ # @param is_24h [Boolean] Toggle 24 hour time.
20
+ # @param date_format [Integer] Date format.
21
+ # @param time [::Time] Time to set (including time zone).
22
+ # @param name [String, nil] Name of time zone (defaults to zone from time; 3 chars max)
23
+ # @return [Time] Time instance.
24
+ def initialize(zone:, is_24h:, date_format:, time:, name: nil)
25
+ @zone = zone
26
+ @is_24h = is_24h
27
+ @date_format = date_format
28
+ @time = time
29
+ @name = name
30
+ end
31
+
32
+ # Compile packets for a time.
33
+ #
34
+ # @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
35
+ def packets
36
+ [
37
+ [
38
+ CPACKET_TIME,
39
+ zone,
40
+ time.sec,
41
+ time.hour,
42
+ time.min,
43
+ time.month,
44
+ time.day,
45
+ year_mod_1900,
46
+ name_characters,
47
+ wday_from_monday,
48
+ is_24h_value,
49
+ date_format
50
+ ].flatten
51
+ ]
52
+ end
53
+
54
+ private
55
+
56
+ def name
57
+ @name || time.zone.downcase
58
+ end
59
+
60
+ def name_characters
61
+ chars_for(name, length: 3, pad: true)
62
+ end
63
+
64
+ def year_mod_1900
65
+ time.year % 100
66
+ end
67
+
68
+ def wday_from_monday
69
+ (time.wday + 6) % 7
70
+ end
71
+
72
+ def is_24h_value
73
+ is_24h ? 2 : 1
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,67 @@
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 WristApp
9
+ include Helpers::CpacketPaginator
10
+ prepend Helpers::CrcPacketsWrapper
11
+
12
+ CPACKET_CLEAR = [0x93, 0x02]
13
+ CPACKET_SECT = [0x90, 0x02]
14
+ CPACKET_DATA = [0x91, 0x02]
15
+ CPACKET_END = [0x92, 0x02]
16
+
17
+ CPACKET_DATA_LENGTH = 32
18
+ WRIST_APP_DELIMITER = /\xac.*\r\n/n
19
+ WRIST_APP_CODE_INDEX = 8
20
+
21
+ attr_accessor :zap_file
22
+
23
+ # Create a WristApp instance.
24
+ #
25
+ # @param wrist_app_data [String, nil] WristApp data.
26
+ # @param zap_file [String, nil] Path to ZAP file.
27
+ # @return [WristApp] WristApp instance.
28
+ def initialize(wrist_app_data: nil, zap_file: nil)
29
+ @wrist_app_data = wrist_app_data
30
+ @zap_file = zap_file
31
+ end
32
+
33
+ # Compile packets for an alarm.
34
+ #
35
+ # @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
36
+ def packets
37
+ [CPACKET_CLEAR, cpacket_sect] + payloads + [CPACKET_END]
38
+ end
39
+
40
+ private
41
+
42
+ def cpacket_sect
43
+ CPACKET_SECT + [payloads.length, 1]
44
+ end
45
+
46
+ def payloads
47
+ paginate_cpackets(header: CPACKET_DATA, length: CPACKET_DATA_LENGTH, cpackets: wrist_app_data.bytes)
48
+ end
49
+
50
+ def wrist_app_data
51
+ @wrist_app_data || zap_file_data_binary
52
+ end
53
+
54
+ def zap_file_data
55
+ File.open(zap_file, "rb").read
56
+ end
57
+
58
+ def zap_file_data_ascii
59
+ zap_file_data.split(WRIST_APP_DELIMITER)[WRIST_APP_CODE_INDEX]
60
+ end
61
+
62
+ def zap_file_data_binary
63
+ [zap_file_data_ascii].pack("H*")
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,63 @@
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 Protocol9
8
+ class Alarm
9
+ include Helpers::CharEncoders
10
+ prepend Helpers::CrcPacketsWrapper
11
+
12
+ CPACKET_ALARM = [0x50]
13
+
14
+ attr_accessor :number, :audible, :time, :message, :month, :day
15
+
16
+ # Create an Alarm instance.
17
+ #
18
+ # @param number [Integer] Alarm number (from 1 to 10).
19
+ # @param audible [Boolean] Toggle alarm sounds.
20
+ # @param time [::Time] Time of alarm.
21
+ # @param message [String] Alarm message text.
22
+ # @param month [Integer, nil] Month of alarm.
23
+ # @param day [Integer, nil] Day of alarm.
24
+ # @return [Alarm] Alarm instance.
25
+ def initialize(number:, audible:, time:, message:, month: nil, day: nil)
26
+ @number = number
27
+ @audible = audible
28
+ @time = time
29
+ @message = message
30
+ @month = month
31
+ @day = day
32
+ end
33
+
34
+ # Compile packets for an alarm.
35
+ #
36
+ # @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
37
+ def packets
38
+ [
39
+ [
40
+ CPACKET_ALARM,
41
+ number,
42
+ time.hour,
43
+ time.min,
44
+ month.to_i,
45
+ day.to_i,
46
+ audible_integer,
47
+ message_characters
48
+ ].flatten
49
+ ]
50
+ end
51
+
52
+ private
53
+
54
+ def message_characters
55
+ chars_for(message, length: 16, pad: true)
56
+ end
57
+
58
+ def audible_integer
59
+ audible ? 1 : 0
60
+ end
61
+ end
62
+ end
63
+ end