small refactor

master
vitrinekast 10 months ago
parent 70020cfd75
commit 5a6ea2337f

140
app.js

@ -1,20 +1,20 @@
const { SerialPort } = require('serialport')
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const movement = 1000;
import hpgl from "./hpgl.js";
const { init, translate, circle, rectangle, to, label } = require("./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({
path: '/dev/ttyUSB0',
baudRate: 9600,
})
// app.use(bodyParser.json());
const toPlotter = (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}
app.post('/text', (req, res) => {
@ -97,55 +57,49 @@ 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) => {
const distance = 3;
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;
if (message.toLowerCase().startsWith("lb")) {
var words = message.toLowerCase().replace("lb", "");
toPlotter(label(words));
} 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;
}
}
});

@ -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