Skip to content

Commit

Permalink
Gemify, and begin a test for the mock sensor.
Browse files Browse the repository at this point in the history
  • Loading branch information
evanfarrar committed Sep 18, 2010
1 parent 90e269a commit 1d92310
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Rakefile
@@ -1,3 +1,18 @@
task :default do
sh "bacon --automatic --quiet"
end


begin
require 'jeweler'
Jeweler::Tasks.new do |gemspec|
gemspec.name = "opensprints-comm"
gemspec.summary = "Wrapper for the opensprints race manager hardware"
gemspec.description = "Provides the library for interacting with the arduino firmware and also for mocking it out."
gemspec.email = "luke@opensprints.org"
gemspec.homepage = "http://github.com/lukeorland/opensprints-comm"
gemspec.authors = ["Luke Orland"]
end
rescue LoadError
puts "Jeweler not available. Install it with: gem install jeweler"
end
38 changes: 38 additions & 0 deletions lib/opensprints-comm.rb
@@ -0,0 +1,38 @@
class MockFirmware
def initialize()
@output_stream ||= []
end

def self.open(file, mode)
@@mock ||= self.new()
end

def readline
@output_stream.pop
end

def write(command)
if command =~ /^!a:/
number = get_parameter(command)
if number.to_i >= 2**16
@output_stream << "A:NACK\r\n"
else
@output_stream << "A:#{number}\r\n"
end
else
@output_stream << "NACK\r\n"
end
end

def close

end

def flush

end

def get_parameter(command)
command.gsub(/.*:(.*)\r\n/,'\1')
end
end
74 changes: 74 additions & 0 deletions test/test_mock_firmware.rb
@@ -0,0 +1,74 @@
require 'rubygems'
require 'bacon'
require 'timeout'
require 'lib/opensprints-comm'
include Timeout

filename = ENV['OPENSPRINTS_PORT']||"/dev/ttyUSB0"
serialport = MockFirmware.open(filename, "w+")
serialport.flush
serialport.close
sleep(1.5)

serialport = MockFirmware.open(filename, "w+")
serialport.write "!s\r\n"
serialport.flush
timeout(0.2) {
puts serialport.readline
}

serialport.close

serialport = MockFirmware.open(filename, "w+")

serialport.write "!defaults\r\n"
serialport.flush
timeout(0.2) {
puts serialport.readline
}
serialport.close

def write_command(command)
filename = ENV['OPENSPRINTS_PORT']||"/dev/ttyUSB0"
@serialport = MockFirmware.open(filename, "w+")
@serialport.write command
@serialport.close
@serialport = MockFirmware.open(filename, "w+")
end

def set_to_defaults!
write_command("!defaults\r\n")

timeout(1.1) {
@serialport.readline
}
end

def write_stimulus(stimulus)
write_command(stimulus)
timeout(0.1) {
return @serialport.readline
}
end

describe "The handshake" do
before do
set_to_defaults!
end
describe "with a number 0-65535" do
it "should reply with the given number" do
write_stimulus("!a:0\r\n").should==("A:0\r\n")
write_stimulus("!a:12345\r\n").should==("A:12345\r\n")
write_stimulus("!a:65535\r\n").should==("A:65535\r\n")
end
end

describe "with numbers greater than 65535" do
it "should NACK back" do
write_command("!a:65536\r\n")
timeout(0.1) {
@serialport.readline.should==("A:NACK\r\n")
}
end
end
end

0 comments on commit 1d92310

Please sign in to comment.