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

10 months ago
const { SerialPort } = require('serialport')
const express = require('express')
10 months ago
const tmi = require('tmi.js');
const { init, translate, circle, rectangle, to, label } = require("./hpgl.js");
const distance = 3;
const app = express()
10 months ago
const client = new tmi.Client({
channels: ['twitchplaysplotter']
});
10 months ago
10 months ago
client.connect();
10 months ago
10 months ago
const port = new SerialPort({
path: '/dev/ttyUSB0',
10 months ago
baudRate: 9600,
})
const toPlotter = (message) => {
console.log("i will write to plotter ", message);
10 months ago
10 months ago
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'))
10 months ago
app.listen(3001, () => {
10 months ago
console.log(`Example app listening on port`)
})
const parseMessage = (message) => {
10 months ago
if (message.toLowerCase().startsWith("lb")) {
var words = message.toLowerCase().replace("lb", "");
toPlotter(label(words));
} else if (message.toLowerCase().startsWith("distance")) {
10 months ago
} else {
switch (message.toLowerCase()) {
case 'init':
10 months ago
toPlotter(translate());
10 months ago
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);