small refactor

master
vitrinekast 10 months ago
parent 70020cfd75
commit 5a6ea2337f

140
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}`);
switch (message.toLowerCase()) { if (message.toLowerCase().startsWith("lb")) {
case 'pu': var words = message.toLowerCase().replace("lb", "");
console.log("Performing a PU"); toPlotter(label(words));
toPlotter(initMessage); } else {
toPlotter(`PU;`); switch (message.toLowerCase()) {
break; case 'init':
case 'pd': toPlotter(`PU;`);
toPlotter(initMessage); break;
toPlotter(`PD;`); case 'pu':
break; toPlotter(`PU;`);
case 'up': break;
toPlotter(initMessage); case 'pd':
console.log("Performing a move UP"); toPlotter(`PD;`);
toPlotter(`PR0,100;`); break;
break; case 'up':
case 'down': toPlotter(translate(distance * -1, 0));
toPlotter(initMessage); break;
toPlotter(`PR0,-100;`); case 'down':
break; toPlotter(translate(0, distance));
case 'left': break;
toPlotter(initMessage); case 'left':
console.log("Performing a move LEFT"); toPlotter(translate(distance * -1, 0));
toPlotter(`PR-100,0;`); break;
break; case 'right':
case 'right': toPlotter(translate(0, distance));
toPlotter(initMessage); break;
console.log("Performing a move RIGHT"); case 'right':
toPlotter(`PR100,0;`); toPlotter(translate(0, distance));
break; break;
default: case 'rectangle':
toPlotter(initMessage); toPlotter(rectangle(distance));
console.log("Message not recognized"); break;
break; case 'circle':
toPlotter(circle(distance));
break;
default:
console.log("Message not recognized");
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