-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariables.go
More file actions
132 lines (110 loc) · 2.54 KB
/
Copy pathvariables.go
File metadata and controls
132 lines (110 loc) · 2.54 KB
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
package m1
import (
"encoding/binary"
"errors"
"fmt"
"reflect"
"github.com/ysmilda/m1-go/internals/m1binary"
"github.com/ysmilda/m1-go/modules/res"
"github.com/ysmilda/m1-go/modules/svi"
)
type Variable struct {
Name string
svi.Variable
initialized bool
module res.ModuleNumber
target *Target
}
// NewVariable creates a new variable on the given module with the given name.
func NewVariable(target *Target, module res.ModuleNumber, name string) (*Variable, error) {
v := &Variable{
Name: name,
module: module,
target: target,
}
err := v.initialize()
if err != nil {
return nil, err
}
return v, nil
}
func (v *Variable) initialize() error {
reply, err := v.target.SVI.GetExtendedAddress(v.module, svi.GetExtendedAddressCall{
Name: v.Name,
})
if err != nil {
return err
}
v.initialized = true
v.Address = reply.Address
v.Format = reply.Format
v.Length = reply.Length
return nil
}
func (v Variable) String() string {
t := v.GetGoDataType()
rw := ""
if v.IsReadable() {
rw += "r"
}
if v.IsWritable() {
rw += "w"
}
if v.IsBlock() {
return fmt.Sprintf(
"%s: %T[%d] (%s)",
v.Name, reflect.ValueOf(t).Elem().Interface(), v.GetArrayLength(), rw,
)
}
return fmt.Sprintf("%s: %T (%s)", v.Name, reflect.ValueOf(t).Elem().Interface(), rw)
}
func (v *Variable) SetValue(value any) error {
t := v.GetGoDataType()
if reflect.TypeOf(value) != reflect.TypeOf(v.GetGoDataType()).Elem() {
return fmt.Errorf("expected %T, got %T", reflect.ValueOf(t).Elem().Interface(), value)
}
if !v.initialized {
err := v.initialize()
if err != nil {
return err
}
}
if v.IsBlock() {
// TODO: extended call
return errors.New("setvalue not yet implemented for block values")
}
buf, err := m1binary.Encode(value)
if err != nil {
return err
}
buf = append(buf, make([]byte, 4-len(buf))...)
_, err = v.target.SVI.SetValue(v.module, svi.SetValueCall{
Address: v.Address,
Value: binary.LittleEndian.Uint32(buf),
})
return err
}
func (v *Variable) GetValue() (any, error) {
if !v.initialized {
err := v.initialize()
if err != nil {
return nil, err
}
}
if v.IsBlock() {
// TODO: extended call
return nil, errors.New("getvalue not yet implemented for block values")
}
reply, err := v.target.SVI.GetValue(v.module, svi.GetValueCall{
Address: v.Address,
})
if err != nil {
return nil, err
}
t := v.GetGoDataType()
_, err = m1binary.Decode(binary.LittleEndian.AppendUint32(nil, reply.Value), &t)
if err != nil {
return nil, err
}
return reflect.ValueOf(t).Elem().Interface(), nil
}