Class: Buttplug::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/buttplugrb.rb

Overview

This class creates a Wrapper for your various devices you fetched from listDevices for your controlling pleasure!

Instance Method Summary collapse

Constructor Details

#initialize(client, deviceInfo) ⇒ Device

Creates our Device wrapper for our client

Note: This does create a few functions on the fly. you should check to see if they are available using .methods.include

Arguments:

  • client (Buttplug::Client) - Our buttplugrb client that we are gonna use to control our device

  • deviceInfo (Hash) - Our information that we should have fetched from the list_devices() instance method … should look like:

    {"DeviceName"=>"XBox Compatible Gamepad (XInput)", "DeviceIndex"=>1, "DeviceMessages"=>{"SingleMotorVibrateCmd"=>{}, "VibrateCmd"=>{"FeatureCount"=>2}, "StopDeviceCmd"=>{}}}
    

Returns:

  • Our nicely bundled up device ready to be domminated~



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/buttplugrb.rb', line 217

def initialize(client, deviceInfo)
  #Ok we are gonna expect our deviceInfo to be a Hash so we can do some ... fun things ...
  #{"DeviceName"=>"XBox Compatible Gamepad (XInput)", "DeviceIndex"=>1, "DeviceMessages"=>{"SingleMotorVibrateCmd"=>{}, "VibrateCmd"=>{"FeatureCount"=>2}
  @deviceName=deviceInfo["DeviceName"]
  @deviceIndex=deviceInfo["DeviceIndex"]
  @client=client
  #Ok so we are starting our weird metaProgramming BS here

  if(deviceInfo["DeviceMessages"].keys.include? "VibrateCmd")
    @vibeMotors=deviceInfo["DeviceMessages"]["VibrateCmd"]["FeatureCount"]
    define_singleton_method(:vibrate){|speeds|
      #And now the real fun, we are gonna craft our message!
      id=client.generateID()
      cmd=[{"VibrateCmd"=>{"Id"=>id,"DeviceIndex"=>@deviceIndex,"Speeds"=>[]}}]
      #Ok we arn't gonna really care about how many speeds we are fed in here, we are gonna make sure that our total array isn't empty.
      (0..@vibeMotors-1).each{|i|
        if speeds[i].nil?
          speeds[i]=0
        end 
        cmd[0]["VibrateCmd"]["Speeds"]<<{"Index"=>i,"Speed"=>speeds[i]}
      }
      client.sendMessage([id,cmd.to_json])
    }
    generateActivateAllCommand(:vibrate,@vibeMotors,:vibrateAll)
  end
  if(deviceInfo["DeviceMessages"].keys.include? "LinearCmd")
    @linearActuators=deviceInfo["DeviceMessages"]["LinearCmd"]["FeatureCount"]
    generateArrayedHashCommand({"Duration"=>0, "Position"=>0.0},@linearActuators,"LinearCmd","Vectors",:stroke)
    generateActivateAllCommand(:stroke,@linearActuators,:strokeAll)
  end
  if(deviceInfo["DeviceMessages"].keys.include? "RotateCmd")
    @rotationMotors=deviceInfo["DeviceMessages"]["RotateCmd"]["FeatureCount"]
    generateArrayedHashCommand({"Speed"=>0.0,"Clockwise"=>true},@rotationMotors,"RotateCmd","Rotations",:rotate)
    generateActivateAllCommand(:rotate,@rotationMotors,:rotateAll)
  end
  if(deviceInfo["DeviceMessages"].keys.include? "RawCmd")
  #TODO: Do some stuff here with RawCmd? ... Honestly I don't know what devices would support this ... possibly estim but at the moment 🤷 I have no idea. 🤷 
  #To implement: https://metafetish.github.io/buttplug/generic.html#rawcmd
  end
end

Instance Method Details

#stopDeviceObject

Stops the Device from any current actions that it might be taking.



260
261
262
263
264
# File 'lib/buttplugrb.rb', line 260

def stopDevice
  id=@client.generateID()
  cmd="[{\"StopDeviceCmd\": {\"ID\":#{id},\"DeviceIndex\":#{@deviceIndex}}}]"
  @client.sendMessage([id,cmd])
end