lib/artoo/drivers/wiidriver.rb in artoo-0.4.1 vs lib/artoo/drivers/wiidriver.rb in artoo-0.5.0
- old
+ new
@@ -16,29 +16,25 @@
end
# Starts drives and required connections
def start_driver
begin
- listener = ->(value) { update(value) }
- connection.on("i2c_reply", listener)
-
connection.i2c_config(0)
every(interval) do
connection.i2c_write_request(address, 0x40, 0x00)
- p
connection.i2c_write_request(address, 0x00, 0x00)
- p
connection.i2c_read_request(address, 6)
- p
+
connection.read_and_process
+ handle_events
end
super
rescue Exception => e
- p "start driver"
- p e.message
- p e.backtrace.inspect
+ Logger.error "Error starting wii driver!"
+ Logger.error e.message
+ Logger.error e.backtrace.inspect
end
end
# Get and update data
def update(value)
@@ -48,10 +44,24 @@
end
@data = parse(value)
end
+ def handle_events
+ while i = find_event("i2c_reply") do
+ update(events.slice!(i).data.first)
+ end
+ end
+
protected
+
+ def find_event(name)
+ events.index {|e| e.name == name}
+ end
+
+ def events
+ connection.async_events
+ end
def get_defaults
{}
end