WIP osc transfer

rob
Jurij Podgoršek 2021-08-11 14:39:34 +02:00
parent fa1e522a7e
commit 65edb120d4
3 changed files with 2068 additions and 26 deletions

2032
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,7 @@
"dependencies": {
"express": "^4.17.1",
"nouislider": "14.6.2",
"osc": "^2.4.2",
"three": "0.120.1",
"ws": "^7.3.1"
},

View File

@ -2,8 +2,11 @@ const express = require('express')
const http = require('http')
const WebSocket = require('ws')
const port = 6676
const osc = require('osc')
const port = 6676
// Vzemi iz argumenta
const tty = process.argv[2]
const include_files = [
'/anim.js',
@ -16,11 +19,20 @@ const include_files = [
const app = express();
const server = http.Server(app);
app.get('/', function(req, res) {
// Odprti serijski OSC link
const scon = new osc.SerialPort({
devicePath: tty
})
scon.on('error', e => {
console.log('tty error', e)
})
scon.open()
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.get('/ctl', function(req, res) {
app.get('/ctl', (req, res) => {
res.sendFile(__dirname + '/control.html');
});
@ -40,9 +52,42 @@ server.listen(port, () => console.log('listening on *:' + port))
// Websocket init
const wss = new WebSocket.Server({ server })
wss.on('connection', function (ws) {
const sendAll = (msg, wss, ws = null) => {
wss.clients.forEach( client => {
if (client != ws) {
client.send(msg)
}
})
}
console.log('kontekt')
// Debug osc
scon.on('osc', msg => {
console.log('tty osc', msg.address, ...msg.args)
})
wss.on('connection', function (ws) {
const oscWS = new osc.WebSocketPort({
socket: ws,
metadata: true
});
// Arduino OSC gre v web
scon.on('osc', msg => {
switch(msg.address) {
case '/gyro/':
const [gx, gy, gz] = msg.args
sendAll('adjust:rotx:' + gx, wss)
sendAll('adjust:roty:' + gy, wss)
sendAll('adjust:rotz:' + gz, wss)
break;
case '/accel/':
const [ax, ay, az] = msg.args
sendAll('adjust:rotx:' + ax, wss)
sendAll('adjust:roty:' + ay, wss)
sendAll('adjust:rotz:' + az, wss)
break;
}
})
ws.on('message', (msg) => {
//console.log('got msg', msg)
@ -52,11 +97,7 @@ wss.on('connection', function (ws) {
// Broadcast adjust msg
switch (cmd) {
case 'adjust':
wss.clients.forEach( client => {
if (client != ws) {
client.send(msg)
}
})
sendAll(msg, wss, ws)
}
})
})