lib/artoo/drivers/sphero.rb in artoo-sphero-1.0.0.pre vs lib/artoo/drivers/sphero.rb in artoo-sphero-1.0.0.rc1
- old
+ new
@@ -9,67 +9,49 @@
GREEN = [0, 255, 0]
YELLOW = [255, 255, 0]
BLUE = [0, 0, 255]
WHITE = [255, 255, 255]
- COMMANDS = [:detect_collisions, :clear_collisions, :collisions,
- :power_notifications, :sensor_data, :set_color, :color].freeze
+ COMMANDS = [:roll, :stop, :detect_collisions, :messages, :set_color, :color].freeze
# Starts drives and required connections
def start_driver
begin
detect_collisions
every(interval) do
- handle_collision_events
+ handle_message_events
end
super
rescue Exception => e
Logger.error "Error starting Sphero driver!"
Logger.error e.message
Logger.error e.backtrace.inspect
end
end
- def handle_collision_events
- while i = find_event(::Sphero::Response::CollisionDetected) do
- update_collision(messages.slice!(i))
+ def handle_message_events
+ while not connection.messages.empty? do
+ evt = connection.messages.pop
+ case
+ when evt.is_a?(::Sphero::Response::CollisionDetected)
+ handle_collision_detected(evt)
+ when evt.is_a?(::Sphero::Response::PowerNotification)
+ handle_power_notification(evt)
+ when evt.is_a?(::Sphero::Response::SensorData)
+ handle_sensor_data(evt)
+ end
end
end
- # Publish collision events
- def update_collision(data)
- publish(event_topic_name("collision"), data)
- end
-
# Detects collisions
# @param [Hash] params
def detect_collisions(params={})
connection.configure_collision_detection 0x01, 0x20, 0x20, 0x20, 0x20, 0x50
end
- # Clears collisions
- def clear_collisions
- messages.clear if responses = messages
- end
-
- # @return [CollisionDetected] collision
- def collisions
- matching_response_types messages, ::Sphero::Response::CollisionDetected
- end
-
- # @return [PowerNotification] power notification
- def power_notifications
- matching_response_types messages, ::Sphero::Response::PowerNotification
- end
-
- # @return [SensorData] sensor data
- def sensor_data
- matching_response_types messages, ::Sphero::Response::SensorData
- end
-
# Set color
# @param [Collection] colors
def set_color(*colors)
connection.rgb(*color(*colors))
end
@@ -87,19 +69,22 @@
end
end
private
- def find_event(response_klass)
- messages.index {|m| m.is_a? response_klass}
+ # Publish collision events
+ def handle_collision_detected(data)
+ publish(event_topic_name("collision"), data)
end
- def matching_response_types(responses, respone_klass)
- responses.select { |m| m.is_a? respone_klass } if responses
+ # Publish collision events
+ def handle_power_notification(data)
+ publish(event_topic_name("power"), data)
end
- def messages
- connection.async_messages
+ # Publish collision events
+ def handle_sensor_data(data)
+ publish(event_topic_name("sensor"), data)
end
end
end
end