const { SerialPort } = require('serialport') const express = require('express') const bodyParser = require('body-parser') const app = express() const movement = 1000; import hpgl from "./hpgl.js"; console.log(hpgl()); // Create a port const port = new SerialPort({ path: '/dev/ttyUSB0', baudRate: 9600, }) // app.use(bodyParser.json()); 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); }); }); app.post('/init', (req, res) => { toPlotter(`IN;SC0,0,100,0,100;SP1;PA60,60;PU`) res.send('Initialise the plotter') }) app.post('/pen-up', (req, res) => { toPlotter(`PU;`); res.send("Sending a message to the plotter about PU;",) }); app.post('/pen-down', (req, res) => { toPlotter(`PD;`); res.send("Sending a message to the plotter about PD;",) }); app.post('/move-left', (req, res) => { toPlotter(`PR-${movement},0;`); res.send("Sending a message to the plotter about PR-10,0;",) }); app.post('/move-right', (req, res) => { toPlotter(`PR${movement},0;`); res.send("Sending a message to the plotter about PR10,0;",) }); app.post('/move-up', (req, res) => { toPlotter(`PR0,${movement},0;`); res.send("Sending a message to the plotter about PR0,10;",) }); app.post('/move-down', (req, res) => { toPlotter(`PR0,-${movement};`); res.send("Sending a message to the plotter about PR0,-10;",) }); // 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`) }) const tmi = require('tmi.js'); const client = new tmi.Client({ channels: ['twitchplaysplotter'] }); client.connect(); let penIsUp = false; var initMessage = "IN;SC0,0,100,0,100;"; client.on('message', (channel, tags, message, self) => { console.log("\n"); console.log(`${tags['display-name']}: ${message}`); switch (message.toLowerCase()) { case 'pu': console.log("Performing a PU"); toPlotter(initMessage); toPlotter(`PU;`); break; case 'pd': toPlotter(initMessage); toPlotter(`PD;`); break; case 'up': toPlotter(initMessage); console.log("Performing a move UP"); toPlotter(`PR0,100;`); break; case 'down': toPlotter(initMessage); toPlotter(`PR0,-100;`); break; case 'left': toPlotter(initMessage); console.log("Performing a move LEFT"); toPlotter(`PR-100,0;`); break; case 'right': toPlotter(initMessage); console.log("Performing a move RIGHT"); toPlotter(`PR100,0;`); break; default: toPlotter(initMessage); console.log("Message not recognized"); break; } });