├── .npmignore ├── docs ├── api │ ├── assets │ │ ├── images │ │ │ ├── icons.png │ │ │ ├── icons@2x.png │ │ │ ├── widgets.png │ │ │ └── widgets@2x.png │ │ └── js │ │ │ └── search.js │ ├── interfaces │ │ ├── dualshock4touchpad.html │ │ ├── dualshock4touchpadtouch.html │ │ ├── dualshock4state.html │ │ └── dualshock4analogstate.html │ ├── globals.html │ ├── enums │ │ └── dualshock4interface.html │ ├── index.html │ └── classes │ │ ├── dualshock4rumble.html │ │ ├── dualshock4lightbar.html │ │ └── dualshock4.html ├── index.html └── demo.539144ec.css ├── demo ├── index.js ├── index.html └── Demo.vue ├── src ├── util │ ├── normalize.ts │ └── colorConversion.ts ├── rumble.ts ├── lightbar.ts ├── state.ts └── index.ts ├── rollup.config.js ├── LICENSE ├── package.json ├── .gitignore ├── README.md └── tsconfig.json /.npmignore: -------------------------------------------------------------------------------- 1 | .cache 2 | demo 3 | docs 4 | src 5 | .vscode 6 | rollup.config.js 7 | tsconfig.json 8 | -------------------------------------------------------------------------------- /docs/api/assets/images/icons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheBITLINK/WebHID-DS4/HEAD/docs/api/assets/images/icons.png -------------------------------------------------------------------------------- /docs/api/assets/images/icons@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheBITLINK/WebHID-DS4/HEAD/docs/api/assets/images/icons@2x.png -------------------------------------------------------------------------------- /docs/api/assets/images/widgets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheBITLINK/WebHID-DS4/HEAD/docs/api/assets/images/widgets.png -------------------------------------------------------------------------------- /docs/api/assets/images/widgets@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheBITLINK/WebHID-DS4/HEAD/docs/api/assets/images/widgets@2x.png -------------------------------------------------------------------------------- /demo/index.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import Demo from './Demo.vue' 3 | 4 | new Vue({ 5 | el: '#app', 6 | render: (h) => h(Demo) 7 | }) 8 | -------------------------------------------------------------------------------- /src/util/normalize.ts: -------------------------------------------------------------------------------- 1 | export function normalizeThumbstick (input : number, deadZone = 0) { 2 | const rel = (input - 128) / 128 3 | if (Math.abs(rel) <= deadZone) return 0 4 | return Math.min(1, Math.max(-1, rel)) 5 | } 6 | 7 | export function normalizeTrigger (input : number, deadZone = 0) { 8 | return Math.min(1, Math.max(deadZone, input / 255)) 9 | } 10 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import typescript from 'rollup-plugin-typescript2' 2 | import commonjs from 'rollup-plugin-commonjs' 3 | import resolve from 'rollup-plugin-node-resolve' 4 | 5 | export default { 6 | input: './src/index.ts', 7 | output: [ 8 | { file: 'dist/webhid-ds4.js', format: 'cjs' }, 9 | { file: 'dist/webhid-ds4.esm.js', format: 'esm' } 10 | ], 11 | plugins: [ 12 | typescript(), 13 | commonjs({ extensions: ['.js', '.ts'] }), 14 | resolve({ preferBuiltins: false }) 15 | ] 16 | } 17 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 |
Generated using TypeDoc
215 |Generated using TypeDoc
200 |Generated using TypeDoc
235 |Generated using TypeDoc
243 |A browser API for Sony DualShock 4 controllers built over the WebHID API.
69 |Provides high level APIs to access most of the controller's features over USB and Bluetooth.
70 | 71 |npm install --save webhid-ds4
import { DualShock4 } from 'webhid-ds4'
98 |
99 | // The WebHID device can only be requested upon user interaction
100 | document.getElementById('connectButton').addEventListener('click', async () => {
101 | const DS4 = new DualShock4()
102 | // This will request the WebHID device and initialize the controller
103 | await DS4.init()
104 | // Define a custom lightbar color
105 | await DS4.lightbar.setColorRGB(170, 255, 0)
106 | // The state object is updated periodically with the current controller state
107 | function logInputs () {
108 | requestAnimationFrame(logInputs)
109 | document.getElementById('triangle').innerText = `Triangle Button: ${DS4.state.buttons.triangle}`
110 | document.getElementById('circle').innerText = `Circle Button: ${DS4.state.buttons.circle}`
111 | document.getElementById('cross').innerText = `Cross Button: ${DS4.state.buttons.cross}`
112 | document.getElementById('square').innerText = `Square Button: ${DS4.state.buttons.square}`
113 |
114 | document.getElementById('leftStick').innerText = `Left Stick: ${DS4.state.axes.leftStickX}, ${DS4.state.axes.leftStickY}`
115 | document.getElementById('rightStick').innerText = `Right Stick: ${DS4.state.axes.rightStickX}, ${DS4.state.axes.rightStickY}`
116 | }
117 | logInputs()
118 | })
119 |
120 | API Documentation is available here
123 |Generated using TypeDoc
229 |Generated using TypeDoc
291 |Generated using TypeDoc
299 |Generated using TypeDoc
342 |Generated using TypeDoc
369 |Generated using TypeDoc
418 |