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.

128 lines
3.3 KiB
JavaScript

const { SerialPort } = require('serialport')
const express = require('express')
const tmi = require('tmi.js');
const { init, translate, circle, rectangle, to, label } = require("./hpgl.js");
const distance = 3;
const app = express()
const client = new tmi.Client({
channels: ['twitchplaysplotter']
});
client.connect();
const port = new SerialPort({
path: '/dev/ttyUSB0',
baudRate: 9600,
})
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);
});
});
// 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 parseMessage = (message) => {
if (message.toLowerCase().startsWith("lb")) {
var words = message.toLowerCase().replace("lb", "");
toPlotter(label(words));
} else if (message.toLowerCase().startsWith("distance")) {
} 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;
}
}
}
client.on('message', (channel, tags, message, self) => {
console.log(`${tags['display-name']}: ${message}`);
parseMessage(message)
});
var testMessages = ['init','pu','pd','up','down','left','right','right','rectangle','circle','LBhaihai'];
var testIndex = 0;
var interval = setInterval(function () {
console.log("----- about to send a test message: ", testMessages[testIndex]);
parseMessage(testMessages[testIndex]);
if(testIndex < (testMessages.length - 1)) {
testIndex++;
} else {
console.log("completed the test")
clearInterval(interval);
}
}, 1000);