Gavin Dinubilo
Published

Pinoccio Mouse

Control your Mac computer using Pinoccio, a few parts, and Node.js

Full instructions provided933
Pinoccio Mouse

Things used in this project

Hardware components

2-Axis Joystick
×1
Breadboard (generic)
Breadboard (generic)
×1
Push Buttons
×2
Wire
×1
Pinoccio Scout
×1

Story

Read more

Code

IMG_1052.JPG

JavaScript
var Pinoccio = require("pinoccio-io");
var five = require('johnny-five');

// NodObjC Bridge to call Objective C Commands to control Mac Mouse Cursor
var $ = require('NodObjC');
// To  Move the Mouse We need the Cocoa Objective C Framework
$.framework('Cocoa');
var pool = $.NSAutoreleasePool('alloc')('init');

IMG_1053.JPG

JavaScript
// Create The Pinoccio Board   
var board = new five.Board({
    io: new Pinoccio({
        token: '{{Your Pinoccio API Token}}',
        troop: '{{Your Pinoccio Troop Number}}',
        scout: '{{Pinoccio Scout Number}}'
    }).on('error', function(err) {
        console.log('error> ', err);
    })
});

// Create the point Variables
var ptX = 500;
var ptY = 500;

// Speed of the Mouse Cursor
var speed = 20;

IMG_1052.JPG

JavaScript
// Initialize The Board 
board.on('ready', function() {
    console.log("ready");

    // Initialize the Push Buttons
    var leftClick = new five.Button({
        pin: "D3",
        invert: true
    });
    var rightClick = new five.Button({
        pin: "D2",
        invert: true
    });

    // Initialize the Joystick
    joystick = new five.Joystick({
        pins: ["A0", "A1"],
        freq: 50
    });
    // Inject the Joystick to the Board
    board.repl.inject({
        joystick: joystick
    });

    // Joystick Event API
    joystick.on("axismove", function(err, timestamp) {
        if (this.fixed.x < 0.85 && ptX > 0) {
            ptX -= speed * (1 - this.fixed.x);
        } else if (this.fixed.x > 0.97 && ptX < 1480) {
            ptX += speed * this.fixed.x;
        }
        if (this.fixed.y < 0.85 && ptY < 880) {
            ptY += speed * (1 - this.fixed.y);
        } else if (this.fixed.y > 0.97 && ptY > 0) {
            ptY -= speed * this.fixed.y;
        }

        // Log the X and Y coordinates of the Mouse, along with the Joystick Values
        console.log("LR:", this.fixed.x, "X: ", ptX);
        console.log("UD:", this.fixed.y, "Y: ", ptY);

        // Move the Mouse
        var moveEvent = $.CGEventCreateMouseEvent(null, $.kCGEventMouseMoved, $.CGPointMake(ptX, ptY), $.kCGMouseButtonLeft);
        $.CGEventPost($.kCGHIDEventTap, moveEvent);
    });

    // Detect if Left Push Button is Pressed
    leftClick.on("down", function() {
        var clickDown = $.CGEventCreateMouseEvent(null, $.kCGEventLeftMouseDown, $.CGPointMake(ptX, ptY), $.kCGMouseButtonLeft);
        $.CGEventPost($.kCGHIDEventTap, clickDown);
        var clickUp = $.CGEventCreateMouseEvent(null, $.kCGEventLeftMouseUp, $.CGPointMake(ptX, ptY), $.kCGMouseButtonLeft);
        $.CGEventPost($.kCGHIDEventTap, clickUp);
    });

    //Detect if Right Push is Pressed
    rightClick.on("down", function() {
        var clickDown = $.CGEventCreateMouseEvent(null, $.kCGEventRightMouseDown, $.CGPointMake(ptX, ptY), $.kCGEventRightMouseDown);
        $.CGEventPost($.kCGHIDEventTap, clickDown);
        var clickUp = $.CGEventCreateMouseEvent(null, $.kCGEventRightMouseUp, $.CGPointMake(ptX, ptY), $.kCGEventRightMouseDown);
        $.CGEventPost($.kCGHIDEventTap, clickUp);
    });
});

pool('drain');

Credits

Gavin Dinubilo

Gavin Dinubilo

7 projects • 8 followers

Comments