Skip to content

Instantly share code, notes, and snippets.

@avipars
Created November 13, 2023 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avipars/bb2a69a549a34e8f3cd1e6bed50777d0 to your computer and use it in GitHub Desktop.
Save avipars/bb2a69a549a34e8f3cd1e6bed50777d0 to your computer and use it in GitHub Desktop.
Daydream VR
var noble = require("noble");
var robot = require("robotjs");
var begMovement = [];
var initPos = [];
var moving = false;
var clicked = false;
robot.setMouseDelay(20);
noble.on('stateChange', function(state) {
if (state === 'poweredOn') {
noble.startScanning();
} else {
noble.stopScanning();
}
});
noble.on('discover', function(peripheral) {
if (true) {
peripheral.connect(function(err) {
peripheral.discoverServices(['fe55'], function(err, services) {
services.forEach(function(service) {
service.discoverCharacteristics(['0000000110001000800000805f9b34fb'], function(err, characteristics) {
characteristics.forEach(function(characteristic) {
characteristic.subscribe();
characteristic.on('data', function(data, isNotification) {
var ab = new ArrayBuffer(data.length);
var view = new Uint8Array(ab);
for (var i = 0; i < data.length; ++i) {
view[i] = data[i];
}
dv = new DataView(ab);
x = ((((dv.getUint8(16) & 0x1F) << 3 | (dv.getUint8(17) & 0xE0) >> 5) / 255.0) - 0.1)/ 0.8;
y = ((((dv.getUint8(17) & 0x1F) << 3 | (dv.getUint8(18) & 0xE0) >> 5) / 255.0) - 0.1)/ 0.8;
if(!moving) {
if((x !== -0.125 || y !== -0.125)) {
moving = true;
}
begMovement = [x,y];
initPos = [robot.getMousePos().x,robot.getMousePos().y];
}
else {
if(x == -0.125 && y == -0.125) {
moving = false;
begMovement = [x,y];
initPos = [robot.getMousePos().x,robot.getMousePos().y];
}
else {
robot.moveMouse(initPos[0] + ((x-begMovement[0])*Math.floor(robot.getScreenSize().width)/2),initPos[1] + (y-begMovement[1])*Math.floor(robot.getScreenSize().height)/2);
}
}
//Is the touchpad being pressed down?
click = (dv.getUint8(18) & 0x1) > 0;
if(!clicked && click) {
robot.mouseClick();
}
else if(clicked && !click) {
clicked = false;
}
//Is the app button being pressed down?
app = (dv.getUint8(18) & 0x4) > 0;
if(!clicked && app) {
clicked = true;
robot.mouseClick();
}
else if(clicked && !app) {
clicked = false;
}
//Is the volume up button being pressed down?
up = (dv.getUint8(18) & 0x10) > 0;
if(!clicked && up) {
clicked = true;
robot.scrollMouse(0,5);
}
else if(clicked && !up) {
clicked = false;
}
//Is the volume down button being pressed down?
down = (dv.getUint8(18) & 0x8) > 0;
if(!clicked && down) {
clicked = true;
robot.scrollMouse(0,-5);
}
else if(clicked && !down) {
clicked = false;
}
})
})
})
})
})
})
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment