VibHub-Browser: Difference between revisions

From VibHub Wiki
Jump to navigation Jump to search
Jasx (talk | contribs)
No edit summary
Jasx (talk | contribs)
No edit summary
Line 18: Line 18:
const manager = new App();
const manager = new App();
manager.onDeviceOnline = function(device){
manager.onDeviceOnline = function(device){
device.set(255); // Sets all ports on the device to max intensity (or 1/4 if you enable high res)
device.set(1); // Sets all ports on the device to max intensity
};
};
await manager.addDevice("<your device ID>");
await manager.addDevice("<your device ID>");
Line 25: Line 25:


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


Line 102: Line 104:
- port is the port (hurdur). Usually 443 (SSL) or 80
- 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.
- 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 a resolution of 1024 instead of 256, for more granular control.
- autoHighRes tries to automatically enable High Res mode if the device supports it. High Res gives you more granular control.
|-
|-
|begin
|begin
|m,o
|m,o
|
|
|VhSocket
|this
|Yes
|Yes
|Starts the app, connecting via socket.io and binding events.
|Starts the app, connecting via socket.io and binding events.
Line 229: Line 231:
|
|
|Used internally to make sure socket.io exists in the browser.
|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
|}
|}

Revision as of 12:33, 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