/
resin_map.go
179 lines (154 loc) · 3.96 KB
/
resin_map.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// Copyright (c) 2020 Jason S. McMullan <jason.mcmullan@gmail.com>
//
package main
import (
"bufio"
"fmt"
"os"
"runtime"
"sort"
"strconv"
"strings"
"github.com/ezrec/uv3dp"
)
// Resin stores information about resin properties
type Resin struct {
Name string
uv3dp.Exposure
uv3dp.Bottom
}
var ResinMap = map[string](*Resin){}
var ResinConfigPath string
func chituboxPath(suffix string) string {
if runtime.GOOS == "windows" {
return os.Getenv("LOCALAPPDATA") + strings.ReplaceAll("/ChiTuBox/"+suffix, "/", "\\")
} else {
return os.Getenv("HOME") + "/.config/ChiTuBox/" + suffix
}
}
func setExposureFromDefault(exp *uv3dp.Exposure, def uv3dp.Exposure) {
if exp.LightOnTime < 0 {
exp.LightOnTime = def.LightOnTime
}
if exp.LightOffTime < 0 {
exp.LightOffTime = def.LightOffTime
}
if exp.LiftHeight < 0 {
exp.LiftHeight = def.LiftHeight
}
if exp.LiftSpeed < 0 {
exp.LiftSpeed = def.LiftSpeed
}
if exp.RetractHeight < 0 {
exp.RetractHeight = def.RetractHeight
}
if exp.RetractSpeed < 0 {
exp.RetractSpeed = def.RetractSpeed
}
}
// init initializes the resin map from the ChiTuBox config
func init() {
ResinConfigPath = chituboxPath("machine/0.cfg")
reader, err := os.Open(ResinConfigPath)
if err != nil {
// This is fine.
return
}
defer reader.Close()
defExposure := uv3dp.Exposure{
LightPWM: 255,
LightOnTime: -1,
LightOffTime: -1,
LiftHeight: -1,
LiftSpeed: -1,
RetractHeight: -1,
RetractSpeed: -1}
defResin := &Resin{
Name: "",
Exposure: defExposure,
Bottom: uv3dp.Bottom{Count: -1, Exposure: defExposure},
}
// Set reasonable defaults
defResin.Exposure.LightOnTime = 6.0
defResin.Exposure.LightOffTime = 0
defResin.Bottom.Count = 8
defResin.Bottom.Exposure.LightOnTime = 50.0
defResin.Bottom.Exposure.LightOffTime = 0.0
defResin.Bottom.Exposure.LiftSpeed = 65.0
defResin.Bottom.Exposure.LiftHeight = 5.0
defResin.Bottom.Exposure.RetractSpeed = 150.0
defResin.Bottom.Exposure.RetractHeight = 5.0
ResinMap[""] = defResin
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "@@") {
l := strings.SplitN(line[2:], "@@", 2)
name := l[0]
av := strings.SplitN(l[1], ":", 2)
attr := av[0]
val := av[1]
resin, ok := ResinMap[name]
if !ok {
resin = &Resin{
Name: name,
Exposure: defExposure,
Bottom: uv3dp.Bottom{Count: -1, Exposure: defExposure},
}
}
iVal64, _ := strconv.ParseInt(val, 10, 32)
fVal64, _ := strconv.ParseFloat(val, 32)
iVal := int(iVal64)
fVal := float32(fVal64)
switch attr {
case "bottomLayCount":
resin.Bottom.Count = iVal
case "bottomLayerCount":
resin.Bottom.Count = iVal
case "bottomLayerLiftSpeed":
resin.Bottom.Exposure.LiftSpeed = fVal
case "bottomLayExposureTime":
resin.Bottom.Exposure.LightOnTime = fVal
case "bottomLightOffTime":
resin.Bottom.Exposure.LightOffTime = fVal
case "normalExposureTime":
resin.Exposure.LightOnTime = fVal
case "normalLayerLiftSpeed":
resin.Exposure.LiftSpeed = fVal
default:
// Ignored
}
ResinMap[name] = resin
}
}
defResin, ok := ResinMap[""]
if ok {
delete(ResinMap, "")
setExposureFromDefault(&defResin.Exposure, defResin.Bottom.Exposure)
for _, resin := range ResinMap {
if resin.Bottom.Count < 0 {
resin.Bottom.Count = defResin.Bottom.Count
}
setExposureFromDefault(&resin.Exposure, defResin.Exposure)
setExposureFromDefault(&resin.Bottom.Exposure, defResin.Bottom.Exposure)
}
}
}
func PrintResins() {
fmt.Fprintln(os.Stderr)
fmt.Fprintf(os.Stderr, "Known resins: (from %v)\n", ResinConfigPath)
fmt.Fprintln(os.Stderr)
keys := []string{}
for key := range ResinMap {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
item := ResinMap[key]
fmt.Fprintf(os.Stderr, " %-40s bottom %v layers, %v; nominal %v\n", key,
item.Bottom.Count,
item.Bottom.Exposure.LightOnTime,
item.Exposure.LightOnTime)
}
}