summaryrefslogtreecommitdiff
path: root/tek.go
blob: 34c3bb6b2055b86f055f5a6e44da8a9e503f9d99 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main

import (
	"io"
	"os"
)

const (
	height = 3072
	width  = 4096
)

type Out struct {
	io.Writer
	hix, hiy, lox, loy, eb byte
	xterm                  bool
}

func (o *Out) escString(s string) {
	o.Write([]byte{27})
	o.Write([]byte(s))
}

func (o *Out) writeByte(b ...byte) {
	o.Write(b)
}

func NewOut(w io.Writer) *Out {
	return &Out{
		Writer: w,
		xterm:  os.Getenv("TERM") == "xterm",
	}
}

func (o Out) Enable() {
	if o.xterm {
		o.escString("[?38h")
		o.writeByte(27, 12) // Tek Page
	}
}

func (o Out) Disable() {
	if o.xterm {
		o.writeByte(31)    // Text mode
		o.writeByte(27, 3) // VT Page
	}
}

func (o Out) PenUp() {
	o.writeByte(29, 7)
}

func (o Out) PenDown() {
	o.writeByte(29)
}

func limit(val, max int) int {
	if val < 0 {
		return 0
	}
	if val >= max {
		return max - 1
	}
	return val
}

func (o *Out) Plot(x, y int) {
	x = limit(x, width)
	y = limit(y, height)

	hiy := byte(y>>7) & 0x1f
	loy := byte(y>>2) & 0x1f
	hix := byte(x>>7) & 0x1f
	lox := byte(x>>2) & 0x1f
	eb := byte(x&3) | (byte(y&3) << 2)

	if hiy != o.hiy {
		o.writeByte(hiy | 0x20)
	}
	if eb != o.eb {
		o.writeByte(eb | 0x60)
	}
	if eb != o.eb || loy != o.loy || hix != o.hix {
		o.writeByte(loy | 0x60)
	}
	if hix != o.hix {
		o.writeByte(hix | 0x20)
	}
	o.writeByte(lox | 0x40)
	o.hix = hix
	o.hiy = hiy
	o.lox = lox
	o.loy = loy
	o.eb = eb
}