small refactor

master
vitrinekast 10 months ago
parent 70020cfd75
commit 5a6ea2337f

104
app.js

@ -1,20 +1,20 @@
const { SerialPort } = require('serialport') const { SerialPort } = require('serialport')
const express = require('express') const express = require('express')
const bodyParser = require('body-parser')
const app = express() const app = express()
const movement = 1000; const { init, translate, circle, rectangle, to, label } = require("./hpgl.js");
import hpgl from "./hpgl.js"; const tmi = require('tmi.js');
const client = new tmi.Client({
channels: ['twitchplaysplotter']
});
client.connect();
console.log(hpgl());
// Create a port
const port = new SerialPort({ const port = new SerialPort({
path: '/dev/ttyUSB0', path: '/dev/ttyUSB0',
baudRate: 9600, baudRate: 9600,
}) })
// app.use(bodyParser.json());
const toPlotter = (message) => { const toPlotter = (message) => {
console.log("i will write to plotter ", message); console.log("i will write to plotter ", message);
@ -38,46 +38,6 @@ port.on("open", function () {
}); });
}); });
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} // GET /text?message={message}
app.post('/text', (req, res) => { app.post('/text', (req, res) => {
@ -97,55 +57,49 @@ app.listen(3000, () => {
console.log(`Example app listening on port`) 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) => { client.on('message', (channel, tags, message, self) => {
const distance = 3;
console.log("\n");
console.log(`${tags['display-name']}: ${message}`); console.log(`${tags['display-name']}: ${message}`);
if (message.toLowerCase().startsWith("lb")) {
var words = message.toLowerCase().replace("lb", "");
toPlotter(label(words));
} else {
switch (message.toLowerCase()) { switch (message.toLowerCase()) {
case 'init':
toPlotter(`PU;`);
break;
case 'pu': case 'pu':
console.log("Performing a PU");
toPlotter(initMessage);
toPlotter(`PU;`); toPlotter(`PU;`);
break; break;
case 'pd': case 'pd':
toPlotter(initMessage);
toPlotter(`PD;`); toPlotter(`PD;`);
break; break;
case 'up': case 'up':
toPlotter(initMessage); toPlotter(translate(distance * -1, 0));
console.log("Performing a move UP");
toPlotter(`PR0,100;`);
break; break;
case 'down': case 'down':
toPlotter(initMessage); toPlotter(translate(0, distance));
toPlotter(`PR0,-100;`);
break; break;
case 'left': case 'left':
toPlotter(initMessage); toPlotter(translate(distance * -1, 0));
console.log("Performing a move LEFT");
toPlotter(`PR-100,0;`);
break; break;
case 'right': case 'right':
toPlotter(initMessage); toPlotter(translate(0, distance));
console.log("Performing a move RIGHT"); break;
toPlotter(`PR100,0;`); case 'right':
toPlotter(translate(0, distance));
break;
case 'rectangle':
toPlotter(rectangle(distance));
break;
case 'circle':
toPlotter(circle(distance));
break; break;
default: default:
toPlotter(initMessage);
console.log("Message not recognized"); console.log("Message not recognized");
break; break;
} }
}
}); });

@ -0,0 +1,72 @@
const state = {
position: {
x: 0,
y: 0
}
}
function init(width = 4000, height = 4000) {
return `IN;IP0,0,${width},${height};SC0,100,0,100;SP1; `
}
function translate(x = 0, y = 0) {
state.position.x += x;
state.position.y += y;
clampPosition();
return `PR${state.position.x},${state.position.y};`
}
function to(x = 0, y = 0) {
state.position.x = x;
state.position.y = y;
clampPosition();
return `PA${state.position.x},${state.position.y};`
}
function circle(radius, resolution = 10) {
return `CT${resolution};PA${state.position.x},${state.position.y};PD;CI${radius};PU;`
}
function clampPosition() {
if (state.position.x > 100) {
console.log("in the borderland");
state.position.x = 100;
} else if (state.position.x < 0) {
console.log("in the borderland");
state.position.x = 0;
}
if (state.position.y > 100) {
console.log("in the borderland");
state.position.y = 100;
} else if (state.position.y < 0) {
console.log("in the borderland");
state.position.y = 0;
}
}
function rectangle(width, height) {
if (!height) {
height = width;
}
return `PD;FT1;RR${width},${height};`
}
function label(string) {
return `DT$,0;SI0.5,0.8;LB${string}$;`;
}
module.exports = {
init,
translate,
circle,
rectangle,
to,
label
}
Loading…
Cancel
Save