Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gemify, and begin a test for the mock sensor.
- Loading branch information
1 parent
90e269a
commit 1d92310
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |