class WiimoteSps

Public Class Methods

new(device_id: 'wiimote', sps_address: nil, sps_port: 59000, led_xor: true) click to toggle source
Calls superclass method
# File lib/wiimote_sps.rb, line 12
def initialize(device_id: 'wiimote', sps_address: nil, sps_port: 59000, 
               led_xor: true)

  raise 'WiimoteSps: Please provide a SPS addres' unless sps_address
  super()
  
  @device_id, @led_xor = device_id, led_xor

  @pub = @sub = SPSSub.new address: sps_address, callback: self

  @pub.notice @device_id + '/info: wiimote initialized'

end

Public Instance Methods

activate() click to toggle source
Calls superclass method
# File lib/wiimote_sps.rb, line 26
def activate

  Thread.new do      
    @sub.subscribe topic: 'wiimote/output/#'
  end
      
  super()

end
notify(m) click to toggle source
# File lib/wiimote_sps.rb, line 83
def notify(m)
  @pub.notice @device_id + '/input: ' + m    
end
on_btn_1_up(wm) click to toggle source
# File lib/wiimote_sps.rb, line 88
def on_btn_1_up(wm)      notify 'button 1 pressed'     end
on_btn_2_up(wm) click to toggle source
# File lib/wiimote_sps.rb, line 87
def on_btn_2_up(wm)      notify 'button 2 pressed'     end
on_btn_a_press(wm) click to toggle source
# File lib/wiimote_sps.rb, line 92
def on_btn_a_press(wm)      notify 'button a pressed'     end
on_btn_b_down(wm) click to toggle source
# File lib/wiimote_sps.rb, line 91
def on_btn_b_down(wm)   end
on_btn_b_press(wm) click to toggle source
# File lib/wiimote_sps.rb, line 89
def on_btn_b_press(wm)   notify 'button b pressed'     end
on_btn_b_up(wm) click to toggle source
# File lib/wiimote_sps.rb, line 90
def on_btn_b_up(wm)     end
on_btn_down_press(wm) click to toggle source
# File lib/wiimote_sps.rb, line 97
def on_btn_down_press(wm)   notify 'button down pressed'  end
on_btn_home_press(wm) click to toggle source
# File lib/wiimote_sps.rb, line 94
def on_btn_home_press(wm)   notify 'button home pressed'  end
on_btn_left_press(wm) click to toggle source
# File lib/wiimote_sps.rb, line 95
def on_btn_left_press(wm)   notify 'button left pressed'  end
on_btn_minus_press(wm) click to toggle source
# File lib/wiimote_sps.rb, line 93
def on_btn_minus_press(wm)  notify 'button minus pressed' end
on_btn_plus_press(wm) click to toggle source
# File lib/wiimote_sps.rb, line 99
def on_btn_plus_press(wm)   notify 'button plus pressed'  end
on_btn_right_press(wm) click to toggle source
# File lib/wiimote_sps.rb, line 96
def on_btn_right_press(wm)  notify 'button right pressed' end
on_btn_up_press(wm) click to toggle source
# File lib/wiimote_sps.rb, line 98
def on_btn_up_press(wm)     notify 'button up pressed'    end
on_deactivated() click to toggle source
# File lib/wiimote_sps.rb, line 101
def on_deactivated()
  EventMachine.stop
end
ontopic(topic, msg) click to toggle source
# File lib/wiimote_sps.rb, line 36
def ontopic(topic, msg)

  case topic
  when /rumble$/
   
    case msg
    when /^(buzz|vibrate|rumble|duration)/
      
      r = msg.match(/duration\s([\d\.]+)/)
      
      duration = r ? r.captures.first.to_f : 0.3
      
      self.rumble = true;  sleep duration;  self.rumble = false
      
    end      

  when /led$/

    r = msg.match(/(\d+)\s*(on|off|blink|stop)\s*([\d\.]+)?(?:\s*duration\s)?([\d\.]+)?/)

    if r then
      index, state, seconds, raw_duration = r.captures
      duration = raw_duration ? raw_duration.to_f : nil

      a = case state

        when 'on'
          [:on, duration]

        when 'off'
          [:off]
          
        when 'blink'
          seconds = seconds ? seconds.to_f : 0.5
          [:blink, seconds, duration: duration]
          
        when 'stop'
          [:off]

      end
      
      @led.each {|x| x.send :off} if @led_xor
      @led[index.to_i].send(*a) if index.to_i < @led.length
    end
  end   
end