Guest User

Untitled

a guest
Jul 5th, 2017
1,630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var noble = require("noble");
  2. var robot = require("robotjs");
  3. var begMovement = [];
  4. var initPos = [];
  5. var moving = false;
  6. var clicked = false;
  7.  
  8. robot.setMouseDelay(20);
  9.  
  10. noble.on('stateChange', function(state) {
  11.   if (state === 'poweredOn') {
  12.     noble.startScanning();
  13.   } else {
  14.     noble.stopScanning();
  15.   }
  16. });
  17.  
  18. noble.on('discover', function(peripheral) {
  19.   if (true) {
  20.     peripheral.connect(function(err) {
  21.       peripheral.discoverServices(['fe55'], function(err, services) {
  22.         services.forEach(function(service) {
  23.           service.discoverCharacteristics(['0000000110001000800000805f9b34fb'], function(err, characteristics) {
  24.             characteristics.forEach(function(characteristic) {
  25.               characteristic.subscribe();
  26.               characteristic.on('data', function(data, isNotification) {
  27.                 var ab = new ArrayBuffer(data.length);
  28.                 var view = new Uint8Array(ab);
  29.                 for (var i = 0; i < data.length; ++i) {
  30.                   view[i] = data[i];
  31.                 }
  32.                 dv = new DataView(ab);
  33.                 x = ((((dv.getUint8(16) & 0x1F) << 3 | (dv.getUint8(17) & 0xE0) >> 5) / 255.0) - 0.1)/ 0.8;
  34.                 y = ((((dv.getUint8(17) & 0x1F) << 3 | (dv.getUint8(18) & 0xE0) >> 5) / 255.0) - 0.1)/ 0.8;
  35.                 if(!moving) {
  36.                   if((x !== -0.125 || y !== -0.125)) {
  37.                     moving = true;
  38.                   }
  39.                   begMovement = [x,y];
  40.                   initPos = [robot.getMousePos().x,robot.getMousePos().y];
  41.                 }
  42.                 else {
  43.                   if(x == -0.125 && y == -0.125) {
  44.                     moving = false;
  45.                     begMovement = [x,y];
  46.                     initPos = [robot.getMousePos().x,robot.getMousePos().y];
  47.                   }
  48.                   else {
  49.                     robot.moveMouse(initPos[0] + ((x-begMovement[0])*Math.floor(robot.getScreenSize().width)/2),initPos[1] + (y-begMovement[1])*Math.floor(robot.getScreenSize().height)/2);
  50.                   }
  51.                 }
  52.  
  53.                 //Is the touchpad being pressed down?
  54.                 click = (dv.getUint8(18) & 0x1) > 0;
  55.                 if(!clicked && click) {
  56.                   robot.mouseClick();
  57.                   }
  58.                 else if(clicked && !click) {
  59.                   clicked = false;
  60.                 }
  61.  
  62.                 //Is the app button being pressed down?
  63.                 app = (dv.getUint8(18) & 0x4) > 0;
  64.                 if(!clicked && app) {
  65.                   clicked = true;
  66.                   robot.mouseClick();
  67.                 }
  68.                 else if(clicked && !app) {
  69.                   clicked = false;
  70.                 }
  71.  
  72.                 //Is the volume up button being pressed down?
  73.                 up = (dv.getUint8(18) & 0x10) > 0;
  74.                 if(!clicked && up) {
  75.                   clicked = true;
  76.                   robot.scrollMouse(0,5);
  77.                 }
  78.                 else if(clicked && !up) {
  79.                   clicked = false;
  80.                 }
  81.  
  82.                 //Is the volume down button being pressed down?
  83.                 down = (dv.getUint8(18) & 0x8) > 0;
  84.                 if(!clicked && down) {
  85.                   clicked = true;
  86.                   robot.scrollMouse(0,-5);
  87.                 }
  88.                 else if(clicked && !down) {
  89.                   clicked = false;
  90.                 }
  91.               })
  92.             })
  93.           })
  94.         })
  95.       })
  96.     })
  97.   }
  98. });
Advertisement
Add Comment
Please, Sign In to add comment