Skip to content

Instantly share code, notes, and snippets.

@jonasjohansson
Last active October 29, 2019 16:50
Show Gist options
  • Save jonasjohansson/66174784677d6f1ea646ff5308f24ddb to your computer and use it in GitHub Desktop.
Save jonasjohansson/66174784677d6f1ea646ff5308f24ddb to your computer and use it in GitHub Desktop.
const SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');
const robot = require('robotjs');
const parser = new Readline();
SerialPort.list(function(err, ports) {
var arduinos = ports.filter(isArduino);
if (arduinos.length !== 0) {
connect(
arduinos[0].comName,
9600
);
} else {
console.log('No Arduino detected…');
}
});
function isArduino(port) {
pm = port['manufacturer'];
return pm !== undefined && pm.includes('arduino');
}
function connect(comName, baudRate) {
console.log(`Opening port ${comName} at ${baudRate}…`);
var port = new SerialPort(comName, { baudRate: baudRate });
port.pipe(parser);
}
const lookup = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'alt', 'ctrl', 'space', 'shift', 'enter', 'space', 'up', 'down', 'left', 'right'];
var keys = [];
parser.on('data', line => {
line = line.trim();
var key = line.substr(1);
var first = line[0];
if (first === '$') {
if (lookup.includes(key) && !keys.includes(key)) {
pressKey(key, 1);
keys.push(key);
}
}
if (first === '!') {
keys = keys.filter(k => k !== key);
}
});
function pressKey(key, count) {
console.log(`key: ${key}`);
for (var i = 0; i < count; i++) {
robot.keyToggle(key, 'down');
}
for (var i = 0; i < count; i++) {
robot.keyToggle(key, 'up');
}
}
{
"name": "serial2keyboard",
"version": "1.0.0",
"description": "Translates serial commands into keypress",
"main": "main.js",
"dependencies": {
"robotjs": "^0.5.1",
"serialport": "^7.1.5"
},
"author": "Jonas Johansson",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment