forked from polyfloyd/shady
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peripheral.go
141 lines (126 loc) · 2.89 KB
/
peripheral.go
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package peripheral
import (
"bufio"
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/tarm/serial"
"github.com/billtraill/shady/renderer"
"github.com/billtraill/shady/shadertoy"
)
func init() {
shadertoy.RegisterResourceType("perip_mat4", func(m shadertoy.Mapping, _ shadertoy.GenTexFunc, _ renderer.RenderState) (shadertoy.Resource, error) {
return newMat4Peripheral(m.Name, m.PWD, m.Value)
})
}
var (
periphFile = regexp.MustCompile(`^([^;]+)(\??)$`)
periphSerial = regexp.MustCompile(`^([^;]+);(\d+)(\??)$`)
)
type periphMat4 struct {
uniformName string
currentValue [16]float32
currentValueLock sync.Mutex
closed, loopClosed chan struct{}
}
func newMat4Peripheral(uniformName, pwd, value string) (shadertoy.Resource, error) {
var reader io.ReadCloser
var err error
var failSilent bool
if match := periphFile.FindStringSubmatch(value); match != nil {
failSilent = match[2] != ""
path, err := shadertoy.ResolvePath(pwd, match[1])
if err != nil {
return nil, err
}
var fd *os.File
fd, err = os.Open(path)
if err == nil {
reader = fd
}
} else if match := periphSerial.FindStringSubmatch(value); match != nil {
failSilent = match[3] != ""
baudrate, _ := strconv.Atoi(match[2])
var ser *serial.Port
ser, err = serial.OpenPort(&serial.Config{
Name: match[1],
Baud: baudrate,
ReadTimeout: time.Second * 10,
})
if err == nil {
reader = ser
} else if ser == nil {
err = fmt.Errorf("Serial device %q not found", match[1])
}
}
if err != nil && !failSilent {
return nil, err
}
pr := &periphMat4{
uniformName: uniformName,
currentValue: [16]float32{
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
},
}
if reader == nil {
return pr, nil
}
pr.closed = make(chan struct{})
pr.loopClosed = make(chan struct{})
go func() {
defer reader.Close()
defer close(pr.loopClosed)
br := bufio.NewReader(reader)
for {
select {
case <-pr.closed:
break
default:
}
line, err := br.ReadBytes('\n')
if err != nil {
break
}
spl := strings.Split(string(line), " ")
if len(spl) != 17 || spl[0] != "mat4" {
continue
}
var out [16]float32
for i, v := range spl[1:] {
f, _ := strconv.ParseFloat(v, 32)
out[i] = float32(f)
}
pr.currentValueLock.Lock()
pr.currentValue = out
pr.currentValueLock.Unlock()
}
}()
return pr, nil
}
func (pr *periphMat4) UniformSource() string {
return fmt.Sprintf("uniform mat4 %s;", pr.uniformName)
}
func (pr *periphMat4) PreRender(state renderer.RenderState) {
if loc, ok := state.Uniforms[pr.uniformName]; ok {
pr.currentValueLock.Lock()
gl.UniformMatrix4fv(loc.Location, 1, false, &pr.currentValue[0])
pr.currentValueLock.Unlock()
}
}
func (pr *periphMat4) Close() error {
if pr.closed == nil {
return nil
}
close(pr.closed)
<-pr.loopClosed
return nil
}