VibHub-Browser: Difference between revisions

From VibHub Wiki
Jump to navigation Jump to search
Jasx (talk | contribs)
Created page with "The library can be clone from [https://github.com/JasXSL/VibHub-Browser here]."
 
Jasx (talk | contribs)
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
The library can be clone from [https://github.com/JasXSL/VibHub-Browser here].
The library can be cloned from [https://github.com/JasXSL/VibHub-Browser here].
 
=== Setup ===
Start by importing the library:
 
<code>import {default as VH, VhProgram as Program, VhStage as Stage} from 'vh-socket.js';</code>
 
Next you'll want to extend VH with a class of your own.
 
<code>class App extends VH{</code>
 
Constructor:<pre>
constructor(...args){
super(...args);
}
</pre>All done for our VibHub manager class! Let's set all ports on our device to max!<pre>
(async () => {
const manager = new App();
manager.onDeviceOnline = function(device){
device.set(1); // Sets all ports on the device to max intensity
};
await manager.addDevice("<your device ID>");
})();
</pre>
 
== VhSocket ==
VhSocket is the main vibhub manager, allowing you to manage multiple devices from your app!
 
Versions:
 
* m - 2026 VibHub Micro
* o - Original VibHub 4x AA battery version.
 
=== Overridable ===
These methods are meant to be overridden by your extended class or on the fly.
{| class="wikitable"
|+
!Method
!Versions
!Args
!Async
!Description
|-
|onDeviceOnline
|m,o
|VhDevice device
|
|Raised when a device has come online.
|-
|onDeviceOffline
|m,o
|VhDevice device
|
|Raised when a device has gone offline.
|-
|onDeviceBattery
|m
|VhDevice device
|
|Battery status has been updated
|-
|onCustomMessage
|m
|string device_ID, string socket_ID, var data
|
|Raised when a custom message has been received (advanced).
|-
|onConnected
|m,o
|
|
|Raised when the app itself has connected to a VibHub server.
|-
|onDisconnected
|m,o
|
|
|Raised when the app itself has disconnected from the VibHub server.
|-
|onTick
|m,o
|
|
|Raised n times per second (based on what you passed to constructor).
|}
 
=== Methods ===
{| class="wikitable"
!Method
!Versions
!Args
!Returns
!Async
!Description
|-
|constructor
|m,o
|string appName, string server = "https://vibhub.io", int port = 443, int fps = 30, bool autoHighRes
|VhSocket
|
|Library constructor.
- appName can be anything you'd like to call your app (mainly used for debugging.
- server is the server to connect to.
- port is the port (hurdur). Usually 443 (SSL) or 80
- fps sets a max limit on how often we can send messages to the device. I don't recommend setting this to more than 60.
- autoHighRes tries to automatically enable High Res mode if the device supports it. High Res gives you more granular control.
|-
|begin
|m,o
|
|this
|Yes
|Starts the app, connecting via socket.io and binding events.
|-
|isConnected
|m,o
|
|bool
|
|Returns true if we're connected to the server.
|-
|setName
|m,o
|
|bool
|Yes
|Sends the app name to the server (auto handled in begin). Returns true on success.
|-
|handleNewIndex
|m,o
|array devices
|
|Yes
|Internal handler for the server refreshing our managed devices.
|-
|handleDeviceOnline
|m,o
|array data
|
|
|Internal handler for device data received. Data is an array of: string id, string socketID, var metadata
|-
|handleDeviceOffline
|m,o
|data
|
|
|Internal handler for device disconnect. Data is an array of: string id
|-
|handleDeviceBattery
|m
|data
|
|
|Internal handler for device battery updates. Data is an object with {string id, bool batteryLow, int mv(millivolts), int xv(max millivolts)}
|-
|addDevice
|m,o
|string deviceID
|VhDevice device
|Yes
|Requires an active connection. Tells the server that you want permissions to manage this device by id. Permissions are NOT lost if the device disconnects and reconnects. Permissions ARE lost if your app disconnects.
|-
|remDevice
|m,o
|VhDevice device
|
|Yes
|Requires an active connection. Tells the server that you no longer want to manage a device. Note that devices are automatically disowned when you disconnect the app. But it's good practice to call this if you want to swap devices.
|-
|wipeDevices
|m,o
|
|
|Yes
|Removes all devices from your app.
|-
|getDevice
|m,o
|string deviceID
|VhDevice device OR undefined
|
|Gets a maintained device by ID if it exists, otherwise returns false.
|-
|sendPWM
|m,o
|VhDevice device
|
|
|Updates the vibration value of a device. Use VhDevice set (listed below) instead, as it automatically prevents overloading the device with too many requests.
|-
|sendSingleChannelPWM
|m,o
|VhDevice device, int channel...
|
|
|Same as sendPWM but for specific ports. Use VhDevice set instead.
|-
|sendProgram
|m,o
|VhDevice device, VhProgram program
|
|
|Sends a program. Use VhDevice sendProgram instead.
|-
|getBattery
|m
|VhDevice device
|
|
|Requests a battery update. Use VhDevice getBattery instead. If successful, the onDeviceBattery event will be raised.
|-
|sendCustomMessage
|m
|VhDevice device, var data
|
|
|Emits a dCustom event (advanced).
|-
|tick
|m,o
|
|
|
|Used internally to rate-limit updates and prevent overloading your device with requests.
|-
|_addScript
|m,o
|
|
|
|Used internally to make sure socket.io exists in the browser.
|}
 
== VhDevice ==
A VhDevice is an object representation of a physical device (your VibHub).
 
Capability names:
 
* p : PWM batch updates (all devices support this)
* ps : PWM specific port updates (all devices support this)
* ph : PWM specific high res updates
* vib : REST programs over websockets (all devices support this)
* sb : Battery status updates
* app_offline : The device can tell when the app disconnects
* dCustom : Can handle custom tasks from app
* aCustom : Can send custom data to app
* h : int, how many bits the device supports in high res mode. Usually 10 if high res is supported.
 
{| class="wikitable"
!Method
!Args
!Returns
!Async
!Description
|-
|constructor
|string deviceId, VhSocket parent
|VhDevice
|
|Constructor. Do not call this directly, use addDevice in VhSocket instead.
|-
|setHighRes
|bool on
|this
|
|Toggles high res mode. In high res mode you gain more granularity to your controls (if your device supports it). Note that you can enable autoHighRes in the VhSocket constructor to automatically manage high res. The only drawback to high res is that you need to send a bit more data.
|-
|isHighRes
|
|bool isHighres
|
|Returns true if your device supports high res. Note that the device must be connected first.
|-
|loadMeta
|obj data
|
|
|Used internally to load device metadata. Currently supports:
- string version: Firmware Version
- string custom: Custom data (only used if you customize your firmware)
- string hwversion: Hardware info
- obj capabilities: See Capabilities above
|-
|getChannelval
|int channel
|int val
|
|Used internally to recalculate a float intensity to an integer based on supported device resolution.
|-
|calcMinPwm
|float input
|float output
|
|Used internally to convert a 0-1 value to a range where 0 is where the device just starts to spin, and 1 being max power.
|-
|hasCapPwmBasic
|
|boolean has
|
|Check if device has this capability
|-
|hasCapPwmSpecific
|
|boolean has
|
|Check if device has this capability
|-
|hasCapPrograms
|
|boolean has
|
|Check if device has this capability
|-
|hasCapOffline
|
|boolean has
|
|Check if device has this capability
|-
|hasCustomToDevice
|
|boolean has
|
|Check if device has this capability
|-
|hasCustomToApp
|
|boolean has
|
|Check if device has this capability
|-
|hasBatteryStatus
|
|boolean has
|
|Check if device has this capability
|-
|getHiResBits
|
|int bits
|
|Returns nr bits the device supports for updates.
|-
|getCapabilityNames
|
|array names
|
|Returns a readable array of capability names for the device.
|-
|sendPWM
|
|
|
|Used internally to update the PWM. Use set instead of this.
|-
|sendSingleChannelPWM
|channel...
|
|
|Used internally to update single channel PWM. Use set instead of this.
|-
|sendProgram
|VhProgram program
|
|
|Send a [[REST]] program to run on the device.
|-
|getBattery
|
|
|
|Attempts to fetch battery status. On success, the VhSocket event onDeviceBattery will be raised.
|-
|changed
|bool stash
|
|
|Used internally to prevent messages from being sent each frame if changes have been detected.
|-
|stashChange
|
|
|
|Used internally.
|-
|set
|float val, int channel = -1
|
|
|Sets PWM (vibration intensity) between 0 and 1. If channel is less than 0, all channels will be affected. Otherwise only the specified channel.
|-
|setMinPwm
|amt = 0
|
|
|Sets a minimum value for the device turning on. This is useful since many vibrators don't turn on until 60% power or so. To get this value, keep min PWM to 0 and slowly crank up the intensity until it starts vibrating. Then use that value for 0.
|-
|remove
|
|
|yes
|Removes this device from the parent VhSocket
|}
 
== VhProgram ==
VhProgram lets you build REST type programs to send to the device. Programs are beneficial if you want to send things like pulses, or ramp vibrations up/down instead of having direct input control.
{| class="wikitable"
!Method
!Args
!Returns
!Async
!Description
|-
|constructor
|array ports/int port, repeats = 0
|VhProgram
|
|Ports: Ports to run program on (0 indexed). Can also be an integer to control 1 port.
Repeats: Nr repeats. Use -1 for infinity.
|-
|setPorts
|array ports/int port
|
|
|Lets you change the ports on the fly. Useful if you want to reuse programs.
|-
|addStage
|VhStage ...
|
|
|Add one or more VhStage objects to the program.
|-
|export
|VhDevice device
|object data
|
|Flattens the program into a generic object, with values automatically converted to integers based on device min PWM and high res capabilities.
|}
 
== VhStage ==
VhStage describes a program stage, setting a value at time.
 
* Supported easing types: In, Out, InOut, None.
* Supported easing algorithms: Linear, Sine, Quad, Cubic, Quart, Quint, Back, Bounce, Circular, Elastic, Exponential, None.
 
{| class="wikitable"
!Method
!Args
!Returns
!Async
!Description
|-
|constructor
|obj settings
|VhStage
|
|Settings object supports the following keys:
- float intensity: A value between 0 and 1. OR a VhRandObject.
- int duration : Duration in milliseconds. OR a VhRandObject.
- string easing : Easing method. Default "Linear.None"
- int repeats : Nr of times to repeat this stage. OR a VhRandObject.
- bool yoyo : Requires repeats. Makes repeats alternate back and forth.
|-
|exportFloatOrRand
|var value, VhDevice device
|obj randObj OR float
|
|Used internally when flattening to a generic object.
|-
|export
|VhDevice device
|obj data
|
|Flattens the stage into a generic object.
|}
 
== VhRandObject ==
VhRandObject can be used in a VhStage to add randomness to intensity, duration, and/or repeats.
{| class="wikitable"
!Method
!Args
!Returns
!Async
!Description
|-
|constructor
|obj settings
|VhRandObject
|
|Settings object supports the following keys:
- float min: A value between 0 and 1
 
- float max: A value between 0 and 1
 
- bool absolute: When true, the min/max values won't be adjusted based on device min val and resolution. Useful for repeats.
 
- int offset: Primarily used with yoyo and multi.
 
- int multi: Primarily used with program repeats with yoyo. Multiplies the output value. Such as if you wanted yoyo back and forth 1-3 times you'd set absolute to true, multi to 2, min to 0, max to 2, and offset to 1.
|-
|exportFloatOrRand
|var value, VhDevice device
|obj randObj OR float
|
|Used internally when flattening to a generic object.
|-
|export
|VhDevice device
|obj data
|
|Flattens the stage into a generic object.
|}

Latest revision as of 13:16, 29 January 2026

The library can be cloned from here.

Setup

Start by importing the library:

import {default as VH, VhProgram as Program, VhStage as Stage} from 'vh-socket.js';

Next you'll want to extend VH with a class of your own.

class App extends VH{

Constructor:

constructor(...args){
	super(...args);
}

All done for our VibHub manager class! Let's set all ports on our device to max!

(async () => { const manager = new App(); manager.onDeviceOnline = function(device){ device.set(1); // Sets all ports on the device to max intensity }; await manager.addDevice("<your device ID>"); })();

VhSocket

VhSocket is the main vibhub manager, allowing you to manage multiple devices from your app!

Versions:

  • m - 2026 VibHub Micro
  • o - Original VibHub 4x AA battery version.

Overridable

These methods are meant to be overridden by your extended class or on the fly.

Method Versions Args Async Description
onDeviceOnline m,o VhDevice device Raised when a device has come online.
onDeviceOffline m,o VhDevice device Raised when a device has gone offline.
onDeviceBattery m VhDevice device Battery status has been updated
onCustomMessage m string device_ID, string socket_ID, var data Raised when a custom message has been received (advanced).
onConnected m,o Raised when the app itself has connected to a VibHub server.
onDisconnected m,o Raised when the app itself has disconnected from the VibHub server.
onTick m,o Raised n times per second (based on what you passed to constructor).

Methods

Method Versions Args Returns Async Description
constructor m,o string appName, string server = "https://vibhub.io", int port = 443, int fps = 30, bool autoHighRes VhSocket Library constructor.

- appName can be anything you'd like to call your app (mainly used for debugging. - server is the server to connect to. - port is the port (hurdur). Usually 443 (SSL) or 80 - fps sets a max limit on how often we can send messages to the device. I don't recommend setting this to more than 60. - autoHighRes tries to automatically enable High Res mode if the device supports it. High Res gives you more granular control.

begin m,o this Yes Starts the app, connecting via socket.io and binding events.
isConnected m,o bool Returns true if we're connected to the server.
setName m,o bool Yes Sends the app name to the server (auto handled in begin). Returns true on success.
handleNewIndex m,o array devices Yes Internal handler for the server refreshing our managed devices.
handleDeviceOnline m,o array data Internal handler for device data received. Data is an array of: string id, string socketID, var metadata
handleDeviceOffline m,o data Internal handler for device disconnect. Data is an array of: string id
handleDeviceBattery m data Internal handler for device battery updates. Data is an object with {string id, bool batteryLow, int mv(millivolts), int xv(max millivolts)}
addDevice m,o string deviceID VhDevice device Yes Requires an active connection. Tells the server that you want permissions to manage this device by id. Permissions are NOT lost if the device disconnects and reconnects. Permissions ARE lost if your app disconnects.
remDevice m,o VhDevice device Yes Requires an active connection. Tells the server that you no longer want to manage a device. Note that devices are automatically disowned when you disconnect the app. But it's good practice to call this if you want to swap devices.
wipeDevices m,o Yes Removes all devices from your app.
getDevice m,o string deviceID VhDevice device OR undefined Gets a maintained device by ID if it exists, otherwise returns false.
sendPWM m,o VhDevice device Updates the vibration value of a device. Use VhDevice set (listed below) instead, as it automatically prevents overloading the device with too many requests.
sendSingleChannelPWM m,o VhDevice device, int channel... Same as sendPWM but for specific ports. Use VhDevice set instead.
sendProgram m,o VhDevice device, VhProgram program Sends a program. Use VhDevice sendProgram instead.
getBattery m VhDevice device Requests a battery update. Use VhDevice getBattery instead. If successful, the onDeviceBattery event will be raised.
sendCustomMessage m VhDevice device, var data Emits a dCustom event (advanced).
tick m,o Used internally to rate-limit updates and prevent overloading your device with requests.
_addScript m,o Used internally to make sure socket.io exists in the browser.

VhDevice

A VhDevice is an object representation of a physical device (your VibHub).

Capability names:

  • p : PWM batch updates (all devices support this)
  • ps : PWM specific port updates (all devices support this)
  • ph : PWM specific high res updates
  • vib : REST programs over websockets (all devices support this)
  • sb : Battery status updates
  • app_offline : The device can tell when the app disconnects
  • dCustom : Can handle custom tasks from app
  • aCustom : Can send custom data to app
  • h : int, how many bits the device supports in high res mode. Usually 10 if high res is supported.
Method Args Returns Async Description
constructor string deviceId, VhSocket parent VhDevice Constructor. Do not call this directly, use addDevice in VhSocket instead.
setHighRes bool on this Toggles high res mode. In high res mode you gain more granularity to your controls (if your device supports it). Note that you can enable autoHighRes in the VhSocket constructor to automatically manage high res. The only drawback to high res is that you need to send a bit more data.
isHighRes bool isHighres Returns true if your device supports high res. Note that the device must be connected first.
loadMeta obj data Used internally to load device metadata. Currently supports:

- string version: Firmware Version - string custom: Custom data (only used if you customize your firmware) - string hwversion: Hardware info - obj capabilities: See Capabilities above

getChannelval int channel int val Used internally to recalculate a float intensity to an integer based on supported device resolution.
calcMinPwm float input float output Used internally to convert a 0-1 value to a range where 0 is where the device just starts to spin, and 1 being max power.
hasCapPwmBasic boolean has Check if device has this capability
hasCapPwmSpecific boolean has Check if device has this capability
hasCapPrograms boolean has Check if device has this capability
hasCapOffline boolean has Check if device has this capability
hasCustomToDevice boolean has Check if device has this capability
hasCustomToApp boolean has Check if device has this capability
hasBatteryStatus boolean has Check if device has this capability
getHiResBits int bits Returns nr bits the device supports for updates.
getCapabilityNames array names Returns a readable array of capability names for the device.
sendPWM Used internally to update the PWM. Use set instead of this.
sendSingleChannelPWM channel... Used internally to update single channel PWM. Use set instead of this.
sendProgram VhProgram program Send a REST program to run on the device.
getBattery Attempts to fetch battery status. On success, the VhSocket event onDeviceBattery will be raised.
changed bool stash Used internally to prevent messages from being sent each frame if changes have been detected.
stashChange Used internally.
set float val, int channel = -1 Sets PWM (vibration intensity) between 0 and 1. If channel is less than 0, all channels will be affected. Otherwise only the specified channel.
setMinPwm amt = 0 Sets a minimum value for the device turning on. This is useful since many vibrators don't turn on until 60% power or so. To get this value, keep min PWM to 0 and slowly crank up the intensity until it starts vibrating. Then use that value for 0.
remove yes Removes this device from the parent VhSocket

VhProgram

VhProgram lets you build REST type programs to send to the device. Programs are beneficial if you want to send things like pulses, or ramp vibrations up/down instead of having direct input control.

Method Args Returns Async Description
constructor array ports/int port, repeats = 0 VhProgram Ports: Ports to run program on (0 indexed). Can also be an integer to control 1 port.

Repeats: Nr repeats. Use -1 for infinity.

setPorts array ports/int port Lets you change the ports on the fly. Useful if you want to reuse programs.
addStage VhStage ... Add one or more VhStage objects to the program.
export VhDevice device object data Flattens the program into a generic object, with values automatically converted to integers based on device min PWM and high res capabilities.

VhStage

VhStage describes a program stage, setting a value at time.

  • Supported easing types: In, Out, InOut, None.
  • Supported easing algorithms: Linear, Sine, Quad, Cubic, Quart, Quint, Back, Bounce, Circular, Elastic, Exponential, None.
Method Args Returns Async Description
constructor obj settings VhStage Settings object supports the following keys:

- float intensity: A value between 0 and 1. OR a VhRandObject. - int duration : Duration in milliseconds. OR a VhRandObject. - string easing : Easing method. Default "Linear.None" - int repeats : Nr of times to repeat this stage. OR a VhRandObject. - bool yoyo : Requires repeats. Makes repeats alternate back and forth.

exportFloatOrRand var value, VhDevice device obj randObj OR float Used internally when flattening to a generic object.
export VhDevice device obj data Flattens the stage into a generic object.

VhRandObject

VhRandObject can be used in a VhStage to add randomness to intensity, duration, and/or repeats.

Method Args Returns Async Description
constructor obj settings VhRandObject Settings object supports the following keys:

- float min: A value between 0 and 1

- float max: A value between 0 and 1

- bool absolute: When true, the min/max values won't be adjusted based on device min val and resolution. Useful for repeats.

- int offset: Primarily used with yoyo and multi.

- int multi: Primarily used with program repeats with yoyo. Multiplies the output value. Such as if you wanted yoyo back and forth 1-3 times you'd set absolute to true, multi to 2, min to 0, max to 2, and offset to 1.

exportFloatOrRand var value, VhDevice device obj randObj OR float Used internally when flattening to a generic object.
export VhDevice device obj data Flattens the stage into a generic object.