|
|
|
const { SerialPort } = require('serialport')
|
|
|
|
const express = require('express')
|
|
|
|
const app = express()
|
|
|
|
const { init, translate, circle, rectangle, to, label } = require("./hpgl.js");
|
|
|
|
const tmi = require('tmi.js');
|
|
|
|
|
|
|
|
const client = new tmi.Client({
|
|
|
|
channels: ['twitchplaysplotter']
|
|
|
|
});
|
|
|
|
|
|
|
|
client.connect();
|
|
|
|
|
|
|
|
const port = new SerialPort({
|
|
|
|
path: '/dev/ttyUSB0',
|
|
|
|
baudRate: 9600,
|
|
|
|
})
|
|
|
|
|
|
|
|
const toPlotter = (message) => {
|
|
|
|
console.log("i will write to plotter ", message);
|
|
|
|
|
|
|
|
port.write(message, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return console.log('Error on write: ', err.message)
|
|
|
|
}
|
|
|
|
console.log('message written: ', message)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open errors will be emitted as an error event
|
|
|
|
port.on('error', function (err) {
|
|
|
|
console.log('Error: ', err.message)
|
|
|
|
})
|
|
|
|
|
|
|
|
port.on("open", function () {
|
|
|
|
console.log('open');
|
|
|
|
port.on('data', function (data) {
|
|
|
|
console.log(data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// GET /text?message={message}
|
|
|
|
app.post('/text', (req, res) => {
|
|
|
|
toPlotter(`SI0.5,0.8;LB${req.query.message};`);
|
|
|
|
res.send(`SI0.5,0.8;LB${req.query.message};`)
|
|
|
|
});
|
|
|
|
|
|
|
|
// GET /direct?message={message}
|
|
|
|
app.post('/direct', (req, res) => {
|
|
|
|
toPlotter(`${req.query.message}`);
|
|
|
|
res.send(`${req.query.message}`)
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use(express.static('public'))
|
|
|
|
|
|
|
|
app.listen(3000, () => {
|
|
|
|
console.log(`Example app listening on port`)
|
|
|
|
})
|
|
|
|
|
|
|
|
client.on('message', (channel, tags, message, self) => {
|
|
|
|
const distance = 3;
|
|
|
|
|
|
|
|
console.log(`${tags['display-name']}: ${message}`);
|
|
|
|
|
|
|
|
if (message.toLowerCase().startsWith("lb")) {
|
|
|
|
var words = message.toLowerCase().replace("lb", "");
|
|
|
|
toPlotter(label(words));
|
|
|
|
} else {
|
|
|
|
switch (message.toLowerCase()) {
|
|
|
|
case 'init':
|
|
|
|
toPlotter(`PU;`);
|
|
|
|
break;
|
|
|
|
case 'pu':
|
|
|
|
toPlotter(`PU;`);
|
|
|
|
break;
|
|
|
|
case 'pd':
|
|
|
|
toPlotter(`PD;`);
|
|
|
|
break;
|
|
|
|
case 'up':
|
|
|
|
toPlotter(translate(distance * -1, 0));
|
|
|
|
break;
|
|
|
|
case 'down':
|
|
|
|
toPlotter(translate(0, distance));
|
|
|
|
break;
|
|
|
|
case 'left':
|
|
|
|
toPlotter(translate(distance * -1, 0));
|
|
|
|
break;
|
|
|
|
case 'right':
|
|
|
|
toPlotter(translate(0, distance));
|
|
|
|
break;
|
|
|
|
case 'right':
|
|
|
|
toPlotter(translate(0, distance));
|
|
|
|
break;
|
|
|
|
case 'rectangle':
|
|
|
|
toPlotter(rectangle(distance));
|
|
|
|
break;
|
|
|
|
case 'circle':
|
|
|
|
toPlotter(circle(distance));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.log("Message not recognized");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|