Class: Buttplug::Device
- Inherits:
-
Object
- Object
- Buttplug::Device
- 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
-
#initialize(client, deviceInfo) ⇒ Device
constructor
Creates our Device wrapper for our client.
-
#stopDevice ⇒ Object
Stops the Device from any current actions that it might be taking.
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~
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/buttplugrb.rb', line 151 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
#stopDevice ⇒ Object
Stops the Device from any current actions that it might be taking.
194 195 196 197 198 |
# File 'lib/buttplugrb.rb', line 194 def stopDevice id=@client.generateID() cmd="[{\"StopDeviceCmd\": {\"ID\":#{id},\"DeviceIndex\":#{@deviceIndex}}}]" @client.sendMessage([id,cmd]) end |