Skip to content

Commit

Permalink
Moved monkey patches into core_extensions refinement
Browse files Browse the repository at this point in the history
  • Loading branch information
justincinmd committed Mar 25, 2014
1 parent 10cdbe8 commit d92baac
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 46 deletions.
47 changes: 1 addition & 46 deletions lib/ev3.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "ev3/core_extensions"
require "ev3/brick"
require "ev3/commands"
require "ev3/constants"
Expand All @@ -8,50 +9,4 @@ module EV3
def self.reload!
$".grep(/lib\/ev3/).each{ |f| load(f) if File.exists?(f) }
end
end


# TODO: Consider moving these monkey patches into a refinement

class Integer
BYTE_CONVERSION = {1 => 'c', 2 => 'v', 4 => 'V'}

# Convert to EV3 variable data
# see http://python-ev3.org/parameterencoding.html#subpar
def to_ev3_data
[0b1000_0011] + self.to_little_endian_byte_array(4)
end

# Convert the number to an array of little endian bytes
#
# @param [Integer] number_of_bytes that should represent the integer
def to_little_endian_byte_array(number_of_bytes = 4)
raise ArgumentError unless BYTE_CONVERSION.has_key?(number_of_bytes)
[self].pack(BYTE_CONVERSION[number_of_bytes]).bytes
end
end

class Object
# Converts object to an array if it's not one
def arrayify
if self.is_a?(Array)
self
else
[self]
end
end
end

class TrueClass
# Converts to EV3 byte
def to_ev3_data
1
end
end

class FalseClass
# Converts to EV3 byte
def to_ev3_data
0
end
end
2 changes: 2 additions & 0 deletions lib/ev3/commands/base.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module EV3
module Commands
class Base
using EV3::CoreExtensions

attr_accessor :sequence_number

def initialize(local_variables = 0, global_variables = 0)
Expand Down
2 changes: 2 additions & 0 deletions lib/ev3/commands/output.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module EV3
module Commands
class Output < Base
using EV3::CoreExtensions

# Creates a new output
#
# @param [Integer] nos output bit field [0x00..0x0F]
Expand Down
2 changes: 2 additions & 0 deletions lib/ev3/commands/output_polarity.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module EV3
module Commands
class OutputPolarity < Output
using EV3::CoreExtensions

FORWARD = 1
BACKWARD = -1
OPPOSITE = 0
Expand Down
1 change: 1 addition & 0 deletions lib/ev3/commands/output_speed.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module EV3
module Commands
class OutputSpeed < Output
using EV3::CoreExtensions

# Creates a new output speed
#
Expand Down
1 change: 1 addition & 0 deletions lib/ev3/commands/output_stop.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module EV3
module Commands
class OutputStop < Output
using EV3::CoreExtensions

# Creates a new output stop command
#
Expand Down
2 changes: 2 additions & 0 deletions lib/ev3/commands/sound_tone.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module EV3
module Commands
class SoundTone < Base
using EV3::CoreExtensions

# Creates a new sound tone command
#
# @param [Integer] Volume of the sound [0..100]
Expand Down
46 changes: 46 additions & 0 deletions lib/ev3/core_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module EV3
module CoreExtensions
refine Integer do
BYTE_CONVERSION = {1 => 'c', 2 => 'v', 4 => 'V'}

# Convert to EV3 variable data
# see http://python-ev3.org/parameterencoding.html#subpar
def to_ev3_data
[0b1000_0011] + self.to_little_endian_byte_array(4)
end

# Convert the number to an array of little endian bytes
#
# @param [Integer] number_of_bytes that should represent the integer
def to_little_endian_byte_array(number_of_bytes = 4)
raise ArgumentError unless BYTE_CONVERSION.has_key?(number_of_bytes)
[self].pack(BYTE_CONVERSION[number_of_bytes]).bytes
end
end

refine Object do
# Converts object to an array if it's not one
def arrayify
if self.is_a?(Array)
self
else
[self]
end
end
end

refine TrueClass do
# Converts to EV3 byte
def to_ev3_data
1
end
end

refine FalseClass do
# Converts to EV3 byte
def to_ev3_data
0
end
end
end
end

0 comments on commit d92baac

Please sign in to comment.