Podpri vec naprav (nekoliko grdo)

kegel
Jurij Podgoršek 2022-05-27 12:34:22 +02:00
parent 0272c5851f
commit c5d19367f8
1 changed files with 42 additions and 32 deletions

View File

@ -14,6 +14,8 @@ const DEBUG = {
// MIDI out
const MIDI = true
// Serial baud rate
const BAUDRATE = 115200
@ -31,16 +33,11 @@ const fs = require('fs')
const midi = require('midi')
// Vzemi iz argumenta
const tty = process.argv[2]
// Seznam povezanih tty naprav
const tty = process.argv.splice(2)
let eulerRotation = [0, 0, 0]
let baudrate = parseInt(process.argv[3])
if (!baudrate) {
baudrate = 115200
}
const include_files = [
'/anim.js',
'/control.js',
@ -55,8 +52,8 @@ const include_files = [
const app = express();
const server = http.Server(app);
// Odprti serijski OSC link
let scon = null
// Odprti serijski OSC linki
let serijskePovezave = {}
let mo = null
if (MIDI) {
@ -68,50 +65,56 @@ if (MIDI) {
mo.openVirtualPort("kegel")
}
function openSerial() {
console.log('opening ', tty, baudrate)
function openSerial(ttyAddr) {
console.log('opening ', ttyAddr, BAUDRATE)
scon = new osc.SerialPort({
devicePath: tty,
bitrate: baudrate,
serijskePovezave[ttyAddr] = new osc.SerialPort({
devicePath: ttyAddr,
bitrate: BAUDRATE,
autoOpen: true,
useSLIP: true
})
const scon = serijskePovezave[ttyAddr]
scon.on('open', e => {
console.log('serial connection opened')
console.log(`serial connection ${ttyAddr} opened`)
//console.log(scon)
})
scon.on('error', e => {
console.error('tty error', e)
console.error(`tty ${ttyAddr} error`, e)
//scon.close()
})
scon.on('close', e => {
console.warn('serial connection closed, restarting in 1 second')
setTimeout(openSerial, 1000)
console.warn(`serial connection ${ttyAddr} closed, restarting in 1 second`)
setTimeout(() => { openSerial(ttyAddr) }, 1000)
})
// Arduino OSC gre v web
scon.on('message', msg => {
// Debug incoming osc
if (DEBUG.osc) {
console.log('osc msg', msg)
}
const index = Object.keys(serijskePovezave).indexOf(ttyAddr)
const prepend = `/ww/${index}`
sendAll(msg, null, null, osclients)
// Debug incoming serial osc
if (DEBUG.osc) {
console.log('osc SERIAL msg', msg, ttyAddr, prepend)
}
sendAll(msg, null, null, osclients, prepend)
})
scon.open()
if (scon._closeCode) {
scon = null
console.log('restarting serial connection')
console.log('restarting serial connection ', ttyAddr)
setTimeout(openSerial, 1000)
}
}
if (tty) {
openSerial(baudrate)
if (tty.length > 0) {
tty.forEach((ttyAddr) => {
openSerial(ttyAddr);
});
}
app.get('/', (req, res) => {
@ -221,7 +224,7 @@ function eulerFromQuaternion(quaternion) {
return euler;
}
const sendAll = (msg, info, oscWS, osclients) => {
const sendAll = (msg, info, oscWS, osclients, prepend = '') => {
// Reset euler rotation to 0
if (msg.address == '/keys') {
if (msg.args[0] && msg.args[1] && msg.args[2] && msg.args[3]) {
@ -235,7 +238,7 @@ const sendAll = (msg, info, oscWS, osclients) => {
sendAll({
address: '/eulerDiff',
args: euler
}, info, oscWS, osclients)
}, info, oscWS, osclients, prepend)
eulerRotation[0] += euler[0]
eulerRotation[1] += euler[1]
@ -244,13 +247,20 @@ const sendAll = (msg, info, oscWS, osclients) => {
sendAll({
address: '/euler',
args: eulerRotation
}, info, oscWS, osclients)
}, info, oscWS, osclients, prepend)
}
// Add prepend
let sendMsg = {
address: `${prepend}${msg.address}`,
args: msg.args
}
// OSC relay
osclients.forEach( client => {
if (client && oscWS != client) {
// console.log("sending", msg, info)
client.send(msg)
// console.log("OSC RELAY", sendMsg, info, prepend)
client.send(sendMsg)
}
})
@ -258,7 +268,7 @@ const sendAll = (msg, info, oscWS, osclients) => {
if (DEBUG.udp) {
console.log("UDP SEND", msg)
}
scudp.send(msg)
scudp.send(sendMsg)
}
}