-
Notifications
You must be signed in to change notification settings - Fork 0
/
c_enum.go
226 lines (177 loc) · 4.84 KB
/
c_enum.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package eltee
import (
"fmt"
"github.com/eyethereal/go-config"
"github.com/tomseago/go-eltee/api"
"strings"
)
/////////////////////////////////////////////////////////////////////////////////
//
// Control Kind: enum
//
type EnumUpdater struct {
// This is a reference to the slice that is held by the DmxFixture. Thus changes
// made here go directly into the same place that the fixture references without
// a need to copy them.
// Since this control affects a single channel this slice is expected to be
// only 1 element long
channels []byte
}
func (u *EnumUpdater) Update(fc *FixtureControl) {
if u == nil || fc == nil || fc.ControlPoint == nil {
return
}
// TODO: Use the control point WasDirty() method?
value := fc.LensStack.Observe(fc, fc.ControlPoint)
point, ok := value.(EnumPoint)
if !ok {
// not an intensity
return
}
pc, ok := fc.ProfileControl.(*EnumProfileControl)
if !ok {
// attached to the wrong type of control. Bad
return
}
item, degree := point.Option()
// Figure out a value...
if item >= len(pc.options) {
item = len(pc.options) - 1
}
eo := pc.options[item]
var dmxVal byte = 0
if len(eo.VariableName) > 0 {
iVal := fc.Fixture.GetInt(eo.VariableName) + eo.VariableOffset
dmxVal = byte(iVal)
} else {
if len(eo.Values) == 1 {
dmxVal = byte(eo.Values[0])
} else if len(eo.Values) == 2 {
dmxVal = byte(float64(eo.Values[1]-eo.Values[0])*degree + float64(eo.Values[0]))
} else {
// TODO: Map across n divisions
}
}
// Set it
u.channels[0] = dmxVal
}
////////////////
type EnumOption struct {
Name string
Values []int
// An enum option can get it's value entirely from a fixture variable. This
// is used to support the odd bit mapped values for the stinger where
// we want to have toggle controls which control bits in a var on the fixture
// and then an enum reads that var to figure out it's value.
// This concept is either brilliant or insane. Not sure which. It might come
// up with the laser also.
VariableName string
VariableOffset int
}
func NewEnumOption(name string, node *config.AclNode) *EnumOption {
eo := &EnumOption{
Name: name,
Values: make([]int, 0),
VariableName: node.ChildAsString("variable", "name"),
VariableOffset: node.ChildAsInt("variable", "offset"),
}
// If we have a variable name we ignore values
if len(eo.VariableName) > 0 {
return eo
}
// Copy the values
for ix := 0; ix < node.Len(); ix++ {
eo.Values = append(eo.Values, node.AsIntN(ix))
}
return eo
}
func (eo *EnumOption) String() string {
var b strings.Builder
b.WriteString("<'")
b.WriteString(eo.Name)
b.WriteString("' ")
if len(eo.VariableName) > 0 {
b.WriteString("${")
b.WriteString(eo.VariableName)
b.WriteString(" ")
b.WriteString(fmt.Sprintf("%v", eo.VariableOffset))
b.WriteString("}")
} else {
b.WriteString(fmt.Sprintf("%v", eo.Values))
}
b.WriteString(">")
return b.String()
}
func (eo *EnumOption) ToAPI() *api.EnumProfileControlOption {
r := &api.EnumProfileControlOption{
Name: eo.Name,
Values: make([]int32, len(eo.Values)),
VarName: eo.VariableName,
VarOffset: int32(eo.VariableOffset),
}
for ix, val := range eo.Values {
r.Values[ix] = int32(val)
}
return r
}
////////////////
type EnumProfileControl struct {
ProfileControlBase
channelIx int
options []*EnumOption
}
func NewEnumProfileControl(id string, rootNode *config.AclNode) (*EnumProfileControl, error) {
pc := &EnumProfileControl{
ProfileControlBase: MakeProfileControlBase(id, rootNode),
channelIx: rootNode.ChildAsInt("channel"),
options: make([]*EnumOption, 0),
}
if pc.channelIx < 1 {
return nil, fmt.Errorf("Channel index must be > 0")
}
vNode := rootNode.Child("values")
vNode.ForEachOrderedChild(func(nn string, node *config.AclNode) {
val := NewEnumOption(nn, node)
pc.options = append(pc.options, val)
})
return pc, nil
}
func (pc *EnumProfileControl) Id() string {
return pc.id
}
func (pc *EnumProfileControl) Name() string {
return pc.name
}
func (pc *EnumProfileControl) Type() string {
return "enum"
}
func (pc *EnumProfileControl) String() string {
return fmt.Sprintf("Enum %v(%v) %v->%v", pc.name, pc.id, pc.channelIx, pc.options)
}
func (pc *EnumProfileControl) Instantiate(fixture Fixture) *FixtureControl {
dmx, ok := fixture.(*DmxFixture)
if !ok {
return nil
}
updater := &EnumUpdater{
channels: dmx.channels[pc.channelIx-1 : pc.channelIx],
}
fc := NewFixtureControl(pc, updater)
fixture.AttachControl(pc.id, fc)
return fc
}
func (pc *EnumProfileControl) ToAPI() *api.ProfileControl {
aPc := &api.EnumProfileControl{
Id: pc.id,
Name: pc.name,
ChannelIx: int32(pc.channelIx),
Options: make([]*api.EnumProfileControlOption, 0),
}
for _, opt := range pc.options {
aPc.Options = append(aPc.Options, opt.ToAPI())
}
aRet := &api.ProfileControl{
Sub: &api.ProfileControl_Enm{aPc},
}
return aRet
}