Skip to content

Commit

Permalink
Merge pull request #164 from synthead/move-protocol-7-activity-packet…
Browse files Browse the repository at this point in the history
…s-to-activity-class

Add packets class method to Eeprom::Activity
  • Loading branch information
synthead authored Nov 26, 2022
2 parents bcf8d0a + 84060e0 commit 1ea6388
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
33 changes: 33 additions & 0 deletions lib/timex_datalink_client/protocol_7/eeprom/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,39 @@ class Activity
METADATA_BYTES_BASE = 6
METADATA_BYTES_SIZE = 5

PACKETS_TERMINATOR = 0x04

def self.packets(activities)
header(activities) + metadata_and_messages(activities) + [PACKETS_TERMINATOR]
end

def self.header(activities)
[
random_speech(activities),
0,
0,
0,
activities.count,
0
]
end

def self.random_speech(activities)
activities.each_with_index.sum do |activity, activity_index|
activity.random_speech ? 1 << activity_index : 0
end
end

def self.metadata_and_messages(activities)
metadata = activities.each_with_index.map do |activity, activity_index|
activity.metadata_packet(activities.count + activity_index)
end

messages = activities.map { |activity| activity.messages_packet }

(metadata + messages).flatten
end

attr_accessor :time, :messages, :random_speech

# Create an Activity instance.
Expand Down
37 changes: 33 additions & 4 deletions spec/lib/timex_datalink_client/protocol_7/eeprom/activity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,47 @@
let(:messages) { [[0xb1]] }
let(:random_speech) { false }

let(:activity_instance) do
let(:activity) do
described_class.new(
time: time,
messages: messages,
random_speech: random_speech
)
end

describe ".packets" do
let(:activities) { [activity] }

subject(:packets) { described_class.packets(activities) }

it do
should eq([0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x1e, 0x01, 0x0b, 0x00, 0x30, 0xb1, 0xff, 0x00, 0x00, 0x04])
end

context "with two activities" do
let(:activity_2) do
described_class.new(
time: Time.new(0, 1, 1, 2, 30, 0),
messages: messages,
random_speech: true
)
end

let(:activities) { [activity, activity_2] }

it do
should eq [
0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x1e, 0x01, 0x10, 0x00, 0x02, 0x1e, 0x01, 0x15, 0x00, 0x30, 0xb1,
0xff, 0x00, 0x00, 0x30, 0xb1, 0xff, 0x00, 0x00, 0x04
]
end
end
end

describe "#metadata_packet" do
let(:activity_index) { 1 }

subject(:metadata_packet) { activity_instance.metadata_packet(activity_index) }
subject(:metadata_packet) { activity.metadata_packet(activity_index) }

it { should eq([0x01, 0x1e, 0x01, 0x0b, 0x00]) }

Expand All @@ -42,7 +71,7 @@
end

describe "#messages_packet" do
subject(:messages_packet) { activity_instance.messages_packet }
subject(:messages_packet) { activity.messages_packet }

it { should eq([0x30, 0xb1, 0xff, 0x00, 0x00]) }

Expand Down Expand Up @@ -89,7 +118,7 @@
end

describe "#random_speech" do
subject(:random_speech_value) { activity_instance.random_speech }
subject(:random_speech_value) { activity.random_speech }

it { should be_falsey }

Expand Down

0 comments on commit 1ea6388

Please sign in to comment.