项目作者: TheBITLINK

项目描述 :
High Level API for DualShock 4 controllers powered by WebHID
高级语言: TypeScript
项目地址: git://github.com/TheBITLINK/WebHID-DS4.git
创建时间: 2019-11-01T17:51:21Z
项目社区:https://github.com/TheBITLINK/WebHID-DS4

开源协议:MIT License

下载


WebHID-DS4

A browser API for Sony DualShock 4 controllers built over the WebHID API.

Provides high level APIs to access most of the controller’s features over USB and Bluetooth.

Supported Features

  • USB and Bluetooth connectivity
  • Gyro & Accelerometer data
  • Touchpad Input
  • Buttons & Analogs
  • Lightbar RGB values
  • Rumble motors

Known Issues

  • Setting the lightbar colors and rumble intensity over Bluetooth is currently broken.
  • There’s currently no way to detect if a controller has been disconnected.
  • There’s currently no way to detect if a controller has already been connected either.
  • Not all DS4 Models are supported by this library. I only have a CUH-ZCT2U, so that’s what i used to test. Support for third party controllers is not guaranteed.

Installation

npm install --save webhid-ds4

Usage Example

  1. import { DualShock4 } from 'webhid-ds4'
  2. // The WebHID device can only be requested upon user interaction
  3. document.getElementById('connectButton').addEventListener('click', async () => {
  4. const DS4 = new DualShock4()
  5. // This will request the WebHID device and initialize the controller
  6. await DS4.init()
  7. // Define a custom lightbar color
  8. await DS4.lightbar.setColorRGB(170, 255, 0)
  9. // The state object is updated periodically with the current controller state
  10. function logInputs () {
  11. requestAnimationFrame(logInputs)
  12. document.getElementById('triangle').innerText = `Triangle Button: ${DS4.state.buttons.triangle}`
  13. document.getElementById('circle').innerText = `Circle Button: ${DS4.state.buttons.circle}`
  14. document.getElementById('cross').innerText = `Cross Button: ${DS4.state.buttons.cross}`
  15. document.getElementById('square').innerText = `Square Button: ${DS4.state.buttons.square}`
  16. document.getElementById('leftStick').innerText = `Left Stick: ${DS4.state.axes.leftStickX}, ${DS4.state.axes.leftStickY}`
  17. document.getElementById('rightStick').innerText = `Right Stick: ${DS4.state.axes.rightStickX}, ${DS4.state.axes.rightStickY}`
  18. }
  19. logInputs()
  20. })

Documentation

API Documentation is available here