class Myo::Band

Public Class Methods

new(socket) click to toggle source

messg [“event”, {“myo”=>0, “timestamp”=>“52593461767”, “type”=>“paired”, “version”=>[1, 1, 5, 2]}] messg [“event”, {“myo”=>0, “timestamp”=>“56817670098”, “type”=>“connected”, “version”=>[1, 1, 5, 2]}] messg [“event”, {“arm”=>“left”, “myo”=>0, “timestamp”=>“56908739636”, “type”=>“arm_synced”, “x_direction”=>“toward_wrist”}]

# File lib/myo/band.rb, line 7
def initialize(socket)
  @socket    = socket
  @events    = {}
  @callbacks = {}
end

Public Instance Methods

on_connect(event, &block) click to toggle source
# File lib/myo/band.rb, line 13
def on_connect(event, &block)
end
run() click to toggle source
# File lib/myo/band.rb, line 16
def run
  EM.run do
    conn = EventMachine::WebSocketClient.connect(@socket)
    conn.callback do
      conn.send_msg "Hello!"
      conn.send_msg "done"
    end

    conn.errback do |e|
      puts "Got error: #{e}"
    end

    conn.stream do |msg|
      conn.close_connection if msg.data == "done"

      message = JSON.parse(msg.data)[1]
      case message['type']
      when 'paired'
        puts "MYO PAIRED"
        puts "msg: #{msg.inspect}"
      when 'connected'
        puts 'MYO CONNECTED'
        puts "msg: #{msg.inspect}"
      when 'orientation'
        puts "msg: #{msg.inspect}"
      end
    end

    conn.disconnect do
      puts "gone"
      EM::stop_event_loop
    end
  end
end