diff --git a/anim.js b/anim.js index 40bab12..938febb 100644 --- a/anim.js +++ b/anim.js @@ -379,7 +379,8 @@ var oscCallbacks = { dqZ = euler[2]; } ], - /* + /* Ne uporabljamo vec + '/gyro/': [ function (args) { var [gx, gy, gz] = args.map(getVal); diff --git a/scosc2.scd b/scosc2.scd index 92783b5..f6442e6 100644 --- a/scosc2.scd +++ b/scosc2.scd @@ -96,7 +96,7 @@ o = OSCFunc({ ) // ENDWW - +OSCFunc.trace(true); diff --git a/server.js b/server.js index c79a6a9..ecf1935 100644 --- a/server.js +++ b/server.js @@ -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)