You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.2 KiB
JavaScript

const { SerialPort } = require('serialport')
const express = require('express')
const tmi = require('tmi.js');
const { init, translate, circle, rectangle, to, label } = require("./hpgl.js");
const distance = 3;
const app = express()
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(3001, () => {
console.log(`Example app listening on port`)
})
const parseMessage = (message) => {
if (message.toLowerCase().startsWith("lb")) {
var words = message.toLowerCase().replace("lb", "");
toPlotter(label(words));
} else if (message.toLowerCase().startsWith("distance")) {
} 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;
}
}
}
client.on('message', (channel, tags, message, self) => {
console.log(`${tags['display-name']}: ${message}`);
parseMessage(message)
});
const readline = require('node:readline').createInterface({
input: process.stdin,
output: process.stdout,
});
const askQuestion = message => {
console.log(`sending ${message}! \n`);
readline.question(`What do you want the plotter to do? \n`, askQuestion);
parseMessage(message)
}
readline.question(`What do you want the plotter to do?`, askQuestion);