rob
Jurij Podgoršek 2022-03-04 20:57:24 +01:00
parent 2ea68dcc8d
commit bf2a878cd5
3 changed files with 27 additions and 49 deletions

View File

@ -379,7 +379,8 @@ var oscCallbacks = {
dqZ = euler[2];
}
],
/*
/* Ne uporabljamo vec
'/gyro/': [
function (args) {
var [gx, gy, gz] = args.map(getVal);

View File

@ -96,7 +96,7 @@ o = OSCFunc({
)
// ENDWW
OSCFunc.trace(true);

View File

@ -8,7 +8,7 @@ const fs = require('fs')
const port = 6676
// Vzemi iz argumenta
const tty = process.argv[2]
const baudrate = parseInt(process.argv[3])
const baudrate = parseInt(process.argv[3]) || 115200
const include_files = [
'/anim.js',
@ -45,15 +45,12 @@ function getMsg(line) {
return null
}
function openSerial(bitrate) {
function openSerial() {
console.log('opening ', tty)
if (!bitrate) {
bitrate = 115200
}
scon = new osc.SerialPort({
devicePath: tty,
bitrate: bitrate,
bitrate: baudrate,
autoOpen: true,
useSLIP: true
})
@ -64,26 +61,21 @@ function openSerial(bitrate) {
})
scon.on('error', e => {
console.error('tty error', e)
/*
if (e.match(/cannot open/)) {
scon = null
setTimeout(openSerial, 1000)
scon.close()
}
*/
})
scon.on('close', e => {
console.warn('serial connection closed, restarting in 1 second')
setTimeout(openSerial, 1000)
scon = null
})
// Arduino OSC gre v web
scon.on('message', msg => {
console.log('osc msg', msg)
// Debug incoming osc
if (msg.address.indexOf('quaternion') > -1) {
console.log('tty osc', msg.address, ...msg.args.map(val => Math.round(val * 1000) / 1000))
if (DEBUG) {
console.log('osc msg', msg)
}
sendAll(msg, null, null, osclients)
})
@ -93,33 +85,8 @@ function openSerial(bitrate) {
}
}
function openBT() {
console.log('opening bluetooth', tty)
const rl = readline.createInterface({
input: fs.createReadStream(tty)
})
rl.on('line', (line) => {
// Convert line to byte array
var enc = new TextEncoder(); // always utf-8
const arr = enc.encode(line);
try {
const msg = osc.readMessage(arr)
console.log(msg)
sendAll(msg, null, null, osclients)
} catch (e) {
console.warn("msg error", e)
}
})
}
if (tty) {
if (baudrate) {
openSerial(baudrate)
} else {
openBT()
}
openSerial(baudrate)
}
app.get('/', (req, res) => {
@ -150,15 +117,26 @@ server.listen(port, () => console.log('listening on *:' + port))
// Websocket init
const wss = new WebSocket.Server({ server })
const scudp = new osc.UDPPort({
multicastMembership: ['224.0.1.9'],
remotePort: 6696,
multicastTTL: 10,
metadata: true
// Relay multicast to websockets
// @TODO still sends to supercollider? Do we need two sockets?
var dgram = require('dgram');
var sss = dgram.createSocket('udp4');
sss.on('listening', () => {
sss.addMembership('224.0.1.9');
})
sss.bind(6696, '224.0.1.9');
const scudp = new osc.UDPPort({
remotePort: 6698,
metadata: true,
socket: sss
})
scudp.on('open', () => {
console.log("UDP to OSC open")
})
scudp.on('message', (msg) => {
console.log('got UDP msg', msg);
osclients.forEach( client => {
@ -194,7 +172,6 @@ wss.on('connection', function (ws) {
// Vsi OSC sem grejo naprej na kliente OSC
oscWS.on('packet', (packet, info) => {
//console.log('got msg', msg)
// Broadcast adjust msg
const [address, args] = packet
sendAll({ address, args}, info, oscWS, osclients)