Skip to content

Commit

Permalink
Merge pull request #51 from synthead/add-time-for-protocol-1
Browse files Browse the repository at this point in the history
Add Time model for protocol 1
  • Loading branch information
synthead committed Sep 12, 2022
2 parents 7784c3f + 9d2cf92 commit 09c721a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/timex_datalink_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "timex_datalink_client/protocol_1/end"
require "timex_datalink_client/protocol_1/start"
require "timex_datalink_client/protocol_1/sync"
require "timex_datalink_client/protocol_1/time"

require "timex_datalink_client/protocol_3/alarm"
require "timex_datalink_client/protocol_3/eeprom"
Expand Down
61 changes: 61 additions & 0 deletions lib/timex_datalink_client/protocol_1/time.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

require "timex_datalink_client/helpers/crc_packets_wrapper"

class TimexDatalinkClient
class Protocol1
class Time
prepend Helpers::CrcPacketsWrapper

CPACKET_TIME = [0x30]

attr_accessor :zone, :is_24h, :time

# Create a Time instance.
#
# @param zone [Integer] Time zone number (1 or 2).
# @param is_24h [Boolean] Toggle 24 hour time.
# @param time [::Time] Time to set.
# @return [Time] Time instance.
def initialize(zone:, is_24h:, time:)
@zone = zone
@is_24h = is_24h
@time = time
end

# Compile packets for a time.
#
# @return [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
def packets
[
[
CPACKET_TIME,
zone,
time.hour,
time.min,
time.month,
time.day,
year_mod_1900,
wday_from_monday,
time.sec,
is_24h_value
].flatten
]
end

private

def year_mod_1900
time.year % 100
end

def wday_from_monday
(time.wday + 6) % 7
end

def is_24h_value
is_24h ? 2 : 1
end
end
end
end
49 changes: 49 additions & 0 deletions spec/lib/timex_datalink_client/protocol_1/time_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

require "spec_helper"

describe TimexDatalinkClient::Protocol1::Time do
let(:zone) { 1 }
let(:is_24h) { false }
let(:time) { Time.new(2015, 10, 21, 19, 28, 32) }

let(:time_instance) do
described_class.new(
zone: zone,
is_24h: is_24h,
time: time
)
end

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

it_behaves_like "CRC-wrapped packets", [
[0x30, 0x01, 0x13, 0x1c, 0x0a, 0x15, 0x0f, 0x02, 0x20, 0x01]
]

context "when zone is 2" do
let(:zone) { 2 }

it_behaves_like "CRC-wrapped packets", [
[0x30, 0x02, 0x13, 0x1c, 0x0a, 0x15, 0x0f, 0x02, 0x20, 0x01]
]
end

context "when is_24h is true" do
let(:is_24h) { true }

it_behaves_like "CRC-wrapped packets", [
[0x30, 0x01, 0x13, 0x1c, 0x0a, 0x15, 0x0f, 0x02, 0x20, 0x02]
]
end

context "when time is 1997-09-19 19:36:55" do
let(:time) { Time.new(1997, 9, 19, 19, 36, 55) }

it_behaves_like "CRC-wrapped packets", [
[0x30, 0x01, 0x13, 0x24, 0x09, 0x13, 0x61, 0x04, 0x37, 0x01]
]
end
end
end
1 change: 1 addition & 0 deletions timex_datalink_client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Gem::Specification.new do |s|
"lib/timex_datalink_client/protocol_1/end.rb",
"lib/timex_datalink_client/protocol_1/start.rb",
"lib/timex_datalink_client/protocol_1/sync.rb",
"lib/timex_datalink_client/protocol_3/time.rb",

"lib/timex_datalink_client/protocol_3/alarm.rb",
"lib/timex_datalink_client/protocol_3/eeprom.rb",
Expand Down

0 comments on commit 09c721a

Please sign in to comment.