Skip to content

Commit

Permalink
Merge pull request #149 from synthead/add-sync-start-end-classes-for-…
Browse files Browse the repository at this point in the history
…protocol-7

Add sync start end classes for protocol 7
  • Loading branch information
synthead authored Nov 25, 2022
2 parents 349c094 + 5a54845 commit 67b7f04
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/timex_datalink_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
require "timex_datalink_client/protocol_4/time"
require "timex_datalink_client/protocol_4/wrist_app"

require "timex_datalink_client/protocol_7/end"
require "timex_datalink_client/protocol_7/start"
require "timex_datalink_client/protocol_7/sync"

require "timex_datalink_client/protocol_9/alarm"
require "timex_datalink_client/protocol_9/eeprom"
require "timex_datalink_client/protocol_9/eeprom/chrono"
Expand Down
21 changes: 21 additions & 0 deletions lib/timex_datalink_client/protocol_7/end.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require "timex_datalink_client/helpers/crc_packets_wrapper"

class TimexDatalinkClient
class Protocol7
class End
prepend Helpers::CrcPacketsWrapper

SETUP_PACKET = [0x92, 0x05]
CPACKET_SKIP = [0x21]

# Compile packets for data end command.
#
# @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
def packets
[SETUP_PACKET, CPACKET_SKIP]
end
end
end
end
28 changes: 28 additions & 0 deletions lib/timex_datalink_client/protocol_7/start.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require "timex_datalink_client/helpers/crc_packets_wrapper"

class TimexDatalinkClient
class Protocol7
class Start
prepend Helpers::CrcPacketsWrapper

CPACKET_START = [0x20, 0x00, 0x00, 0x07]

SETUP_PACKETS = [
[
0x90, 0x05, 0x01, 0x44, 0x53, 0x49, 0x20, 0x54, 0x6f, 0x79, 0x73, 0x20, 0x70, 0x72, 0x65, 0x73, 0x65, 0x6e,
0x74, 0x73, 0x2e, 0x2e, 0x2e, 0x65, 0x42, 0x72, 0x61, 0x69, 0x6e, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00
],
[0x91, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04]
]

# Compile packets for data start command.
#
# @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
def packets
[CPACKET_START] + SETUP_PACKETS
end
end
end
end
40 changes: 40 additions & 0 deletions lib/timex_datalink_client/protocol_7/sync.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

class TimexDatalinkClient
class Protocol7
class Sync
PING_BYTE = [0x78]
SYNC_1_BYTE = [0x55]
SYNC_2_BYTE = [0xaa]

SYNC_2_LENGTH = 5

attr_accessor :length

# Create a Sync instance.
#
# @param length [Integer] Number of 0x55 sync bytes to use.
# @return [Sync] Sync instance.
def initialize(length: 300)
@length = length
end

# Compile packets for syncronization data.
#
# @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
def packets
[PING_BYTE + render_sync_1 + render_sync_2]
end

private

def render_sync_1
SYNC_1_BYTE * length
end

def render_sync_2
SYNC_2_BYTE * SYNC_2_LENGTH
end
end
end
end
13 changes: 13 additions & 0 deletions spec/lib/timex_datalink_client/protocol_7/end_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require "spec_helper"

describe TimexDatalinkClient::Protocol7::End do
let(:end_instance) { described_class.new }

describe "#packets", :crc do
subject(:packets) { end_instance.packets }

it_behaves_like "CRC-wrapped packets", [[0x92, 0x05], [0x21]]
end
end
20 changes: 20 additions & 0 deletions spec/lib/timex_datalink_client/protocol_7/start_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require "spec_helper"

describe TimexDatalinkClient::Protocol7::Start do
let(:start) { described_class.new }

describe "#packets", :crc do
subject(:packets) { start.packets }

it_behaves_like "CRC-wrapped packets", [
[0x20, 0x00, 0x00, 0x07],
[
0x90, 0x05, 0x01, 0x44, 0x53, 0x49, 0x20, 0x54, 0x6f, 0x79, 0x73, 0x20, 0x70, 0x72, 0x65, 0x73, 0x65, 0x6e,
0x74, 0x73, 0x2e, 0x2e, 0x2e, 0x65, 0x42, 0x72, 0x61, 0x69, 0x6e, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00
],
[0x91, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04]
]
end
end
20 changes: 20 additions & 0 deletions spec/lib/timex_datalink_client/protocol_7/sync_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require "spec_helper"

describe TimexDatalinkClient::Protocol7::Sync do
let(:length) { 200 }
let(:sync) { described_class.new(length: length) }

describe "#packets" do
subject(:packets) { sync.packets }

it { should eq([[0x78] + [0x55] * length + [0xaa] * 5]) }

context "when length is 350" do
let(:length) { 350 }

it { should eq([[0x78] + [0x55] * length + [0xaa] * 5]) }
end
end
end
4 changes: 4 additions & 0 deletions timex_datalink_client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ Gem::Specification.new do |s|
"lib/timex_datalink_client/protocol_4/time.rb",
"lib/timex_datalink_client/protocol_4/wrist_app.rb",

"lib/timex_datalink_client/protocol_7/end.rb",
"lib/timex_datalink_client/protocol_7/start.rb",
"lib/timex_datalink_client/protocol_7/sync.rb",

"lib/timex_datalink_client/protocol_9/alarm.rb",
"lib/timex_datalink_client/protocol_9/eeprom.rb",
"lib/timex_datalink_client/protocol_9/eeprom/chrono.rb",
Expand Down

0 comments on commit 67b7f04

Please sign in to comment.