Divided clear/draw

This commit is contained in:
Devine Lu Linvega 2019-01-13 12:12:33 +12:00
parent 1f8eabf588
commit 3392ab87d2
2 changed files with 18 additions and 13 deletions

View File

@ -18,10 +18,10 @@ npm start
## UDP Controls
Dotgrid can be controlled by UDP via the port `49160`. It expects messages of 6 characters.
Dotgrid can be controlled by UDP via the port `49160`. It expects messages up to 6 characters.
- **layer** `0/1/2`
- **type** `l/a/r/c`
- **type** `l/c/r/z` (`l`:line, `c`:clock-wise arc, `r`: cc-wise arc, `z`: close, `.`:clear, `*`: draw)
- **from** `0-z``0-z`
- **to** `0-z``0-z`

View File

@ -4,7 +4,7 @@
// Ex: 1a0156(6 characters 0-9a-z)
// 0 layer[0-2]
// 1 type[larc*]
// 1 type[lcrd*]
// 2 from[0-z][0-z]
// 4 to[0-z][0-z]
@ -17,24 +17,27 @@ function Listener (dotgrid) {
return clamp(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y'].indexOf(c.toLowerCase()), 0, 36) * 15 + 15
}
function operate (data) {
if(!data){ return }
if(!dotgrid.tool.layers[data.layer]){ return }
dotgrid.tool.index = data.layer
dotgrid.tool.addSegment(data.type, [data.from, data.to])
dotgrid.renderer.update()
}
function clear () {
dotgrid.tool.erase()
}
function operate (data) {
if (!data) { return }
if (!dotgrid.tool.layers[data.layer]) { return }
dotgrid.tool.index = data.layer
dotgrid.tool.addSegment(data.type, [data.from, data.to])
}
function draw () {
dotgrid.renderer.update()
}
function parse (msg) {
if (msg.length < 6) { return }
if (msg.length < 2) { return }
const layer = parseInt(msg.substr(0, 1))
const type = { 'l': 'line', 'a': 'arc_c', 'r': 'arc_r', 'c': 'close' }[msg.substr(1, 1).toLowerCase()]
const type = { 'l': 'line', 'c': 'arc_c', 'r': 'arc_r', '*': 'draw' }[msg.substr(1, 1).toLowerCase()]
if (!type) { clear(); return }
if (type === 'draw') { draw(); return }
const from = { x: base36(msg.substr(2, 1)), y: base36(msg.substr(3, 1)) }
const to = { x: base36(msg.substr(4, 1)), y: base36(msg.substr(5, 1)) }
return { layer: layer, type: type, from: from, to: to }
@ -44,6 +47,8 @@ function Listener (dotgrid) {
return v < min ? min : v > max ? max : v
}
// Server
this.server.on('message', (msg, rinfo) => {
// console.log(`Server received UDP message:\n ${msg} from ${rinfo.address}:${rinfo.port}`)
operate(parse(`${msg}`))