Skip to content

Commit

Permalink
Changed the byte encoding to use short constant format in some cases,
Browse files Browse the repository at this point in the history
and added a few test cases
  • Loading branch information
justincinmd committed Mar 26, 2014
1 parent d92baac commit 2223dcd
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format progress
17 changes: 14 additions & 3 deletions lib/ev3/core_extensions.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
module EV3
module CoreExtensions
refine Integer do
BYTE_CONVERSION = {1 => 'c', 2 => 'v', 4 => 'V'}
BYTE_CONVERSION = {1 => 'c', 2 => 'v', 4 => 'V'}
BYTES_FOLLOWING_ENCODING = {1 => 0b01, 2 => 0b10, 4 => 0b11}

refine Integer do
# Convert to EV3 variable data
# see http://python-ev3.org/parameterencoding.html#subpar
# TODO: Add 1 and 2 byte encoding (http://python-ev3.org/parameterencoding.html)
def to_ev3_data
[0b1000_0011] + self.to_little_endian_byte_array(4)
case self
when 0..31
self
when -32..-1
# Short negative constant
0b0011_1111 & self.to_little_endian_byte_array(1).first
else
# 4-byte variable
[0b1000_0000 | BYTES_FOLLOWING_ENCODING[4]] + self.to_little_endian_byte_array(4)
end
end

# Convert the number to an array of little endian bytes
Expand Down
1 change: 1 addition & 0 deletions ruby-ev3.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ Gem::Specification.new do |spec|

spec.add_development_dependency "bundler", "~> 1.5"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "yard"
end
50 changes: 50 additions & 0 deletions spec/ev3/core_extensions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'rspec'
require 'ev3'

describe EV3::CoreExtensions do
using EV3::CoreExtensions

describe "#to_ev3_data" do
def short_negative(number)
0b0011_1111 & [number].pack('c').bytes.first
end

def long_variable_number(number, byte_count)
bytes_to_follow = EV3::CoreExtensions::BYTES_FOLLOWING_ENCODING[byte_count]
byte_conversion = EV3::CoreExtensions::BYTE_CONVERSION[byte_count]
[0b1000_0000 | bytes_to_follow] + [number].pack(byte_conversion).bytes
end

subject { number.to_ev3_data }

context "when a small constant" do
let(:number) { 0 }
it { should eql(number) }
end

context "when a small constant" do
let(:number) { 31 }
it { should eql(number) }
end

context "when slightly negative" do
let(:number) { -1 }
it { should eql(short_negative(number)) }
end

context "when slightly negative" do
let(:number) { -32 }
it { should eql(short_negative(number)) }
end

context "when large" do
let(:number) { 100_000 }
it { should eql(long_variable_number(number, 4)) }
end

context "when a large negative" do
let(:number) { -100_000 }
it { should eql(long_variable_number(number, 4)) }
end
end
end
17 changes: 17 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# Require this file using `require "spec_helper"` to ensure that it is only
# loaded once.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = 'random'
end

0 comments on commit 2223dcd

Please sign in to comment.