-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ls
52 lines (45 loc) · 1.27 KB
/
index.ls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{ Buffer } = require 'buffer'
TEK =
GS: 29
PREFIX:
HIGH:
x: 1 .<<. 5
y: 1 .<<. 5
LOW:
x: 2 .<<. 5
y: 3 .<<. 5
# Takes a plt file as a string and returns an array of octets
module.exports = plt2tek = (input) ->
input = input.toString!.replace /\s/g, ''
octets = input |> extractNodes |> convertNodes
function extractNodes input
input
.split ';'
.map -> it.match /(P[UD])(\d+),(\d+)/
.filter -> it isnt null
.map ->
pinUp = it[1] is 'PU'
[x, y] = it.slice 2, 4
{ pinUp, x, y }
function convertNodes input
input
.map toBytes = (node) ->
octets = if node.pinUp then [ TEK.GS ] else [ ]
octets ++= (valueToBytes 'y', node.y)
octets ++= (valueToBytes 'x', node.x)
.reduce flatten = (collector, item) ->
collector ++ item
function valueToBytes axis, value
# Convert an x or y value into an array of octets
values = split adjust value
high = TEK.PREFIX.HIGH[axis] + values[0]
low = TEK.PREFIX.LOW[axis] + values[1]
[high, low]
function split value
# Split a 10-bit group into two 5-bit groups
high = (value .>>. 5) % 32
low = value % 32
[high, low]
function adjust value
# One point in HPGL is 25 µm (so 1016 units per inch). We assume 90 dpi.
value / 1016 * 90