Skip to content

Commit

Permalink
Merge pull request #330 from synthead/replace-serialport-gem-with-uar…
Browse files Browse the repository at this point in the history
…t-gem

Replace serialport gem with uart gem
  • Loading branch information
synthead authored Feb 14, 2025
2 parents 0a98b5e + d225483 commit a0a42d3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PATH
activemodel (~> 7.0.4)
crc (~> 0.4.2)
mdb (~> 0.5.0)
serialport (~> 1.3.2)
uart (~> 1.0.0)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -92,10 +92,12 @@ GEM
rack (>= 1.1)
rubocop (>= 1.33.0, < 2.0)
ruby-progressbar (1.13.0)
serialport (1.3.2)
ruby-termios (1.1.0)
tomlrb (2.0.3)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
uart (1.0.0)
ruby-termios
unicode-display_width (2.4.2)
yard (0.9.34)
yard-junk (0.0.9)
Expand Down
26 changes: 11 additions & 15 deletions lib/timex_datalink_client/notebook_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "serialport"
require "uart"

class TimexDatalinkClient
class NotebookAdapter
Expand Down Expand Up @@ -28,25 +28,21 @@ def initialize(serial_device:, byte_sleep: nil, packet_sleep: nil, verbose: fals
# @param packets [Array<Array<Integer>>] Two-dimensional array of integers that represent bytes.
# @return [void]
def write(packets)
packets.each do |packet|
packet.each do |byte|
printf("%.2X ", byte) if verbose
UART.open(serial_device) do |serial_port|
packets.each do |packet|
packet.each do |byte|
printf("%.2X ", byte) if verbose

serial_port.write(byte.chr)
serial_port.write(byte.chr)

sleep(byte_sleep)
end
sleep(byte_sleep)
end

sleep(packet_sleep)
sleep(packet_sleep)

puts if verbose
puts if verbose
end
end
end

private

def serial_port
@serial_port ||= SerialPort.new(serial_device)
end
end
end
14 changes: 7 additions & 7 deletions spec/lib/timex_datalink_client/notebook_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
]
end

let(:serial_double) { instance_double(SerialPort) }
let(:serial_port_double) { instance_double(File) }

it "writes serial data with correct sleep lengths" do
expect(SerialPort).to receive(:new).with(serial_device).and_return(serial_double)
expect(UART).to receive(:open).with(serial_device).and_yield(serial_port_double)

packets.each do |packet|
packet.each do |byte|
expect(serial_double).to receive(:write).with(byte.chr).ordered
expect(serial_port_double).to receive(:write).with(byte.chr).ordered
expect(notebook_adapter).to receive(:sleep).with(byte_sleep).ordered
end

Expand All @@ -43,8 +43,8 @@
end

it "does not write to console" do
allow(SerialPort).to receive(:new).with(serial_device).and_return(serial_double)
allow(serial_double).to receive(:write)
allow(UART).to receive(:open).with(serial_device).and_yield(serial_port_double)
allow(serial_port_double).to receive(:write)
allow(notebook_adapter).to receive(:sleep)

expect(notebook_adapter).to_not receive(:printf)
Expand All @@ -57,12 +57,12 @@
let(:verbose) { true }

it "writes serial data with console output" do
expect(SerialPort).to receive(:new).with(serial_device).and_return(serial_double)
expect(UART).to receive(:open).with(serial_device).and_yield(serial_port_double)

packets.each do |packet|
packet.each do |byte|
expect(notebook_adapter).to receive(:printf).with("%.2X ", byte).ordered
expect(serial_double).to receive(:write).ordered
expect(serial_port_double).to receive(:write).ordered
expect(notebook_adapter).to receive(:sleep).ordered
end

Expand Down
2 changes: 1 addition & 1 deletion timex_datalink_client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ Gem::Specification.new do |s|
s.add_dependency "activemodel", "~> 7.0.4"
s.add_dependency "crc", "~> 0.4.2"
s.add_dependency "mdb", "~> 0.5.0"
s.add_dependency "serialport", "~> 1.3.2"
s.add_dependency "uart", "~> 1.0.0"
end

0 comments on commit a0a42d3

Please sign in to comment.