Skip to content
This repository was archived by the owner on Dec 24, 2024. It is now read-only.

Commit d5b2965

Browse files
committed
eda: introduce config type
1 parent 58dbb21 commit d5b2965

File tree

4 files changed

+135
-128
lines changed

4 files changed

+135
-128
lines changed

eda/cfg.go

+120
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,130 @@ package eda
66

77
import (
88
"fmt"
9+
"path/filepath"
10+
"time"
911

1012
"github.com/go-lpc/mim/conddb"
1113
)
1214

15+
type Option func(*config)
16+
17+
func WithThreshold(v uint32) Option {
18+
return func(cfg *config) {
19+
cfg.daq.delta = v
20+
}
21+
}
22+
23+
func WithRFMMask(v uint32) Option {
24+
return func(cfg *config) {
25+
cfg.daq.rfm = v
26+
}
27+
}
28+
29+
func WithRShaper(v uint32) Option {
30+
return func(cfg *config) {
31+
cfg.hr.rshaper = v
32+
}
33+
}
34+
35+
func WithCShaper(v uint32) Option {
36+
return func(cfg *config) {
37+
cfg.hr.cshaper = v
38+
}
39+
}
40+
41+
func WithDevSHM(dir string) Option {
42+
return func(cfg *config) {
43+
cfg.run.dir = dir
44+
}
45+
}
46+
47+
func WithCtlAddr(addr string) Option {
48+
return func(cfg *config) {
49+
cfg.ctl.addr = addr
50+
}
51+
}
52+
53+
func WithConfigDir(dir string) Option {
54+
return func(cfg *config) {
55+
if dir == "" {
56+
return
57+
}
58+
cfg.mode = "csv"
59+
cfg.hr.fname = filepath.Join(dir, "conf_base.csv")
60+
cfg.daq.fname = filepath.Join(dir, "dac_floor_4rfm.csv")
61+
cfg.preamp.fname = filepath.Join(dir, "pa_gain_4rfm.csv")
62+
cfg.mask.fname = filepath.Join(dir, "mask_4rfm.csv")
63+
}
64+
}
65+
66+
func WithDAQMode(mode string) Option {
67+
return func(cfg *config) {
68+
cfg.daq.mode = mode
69+
}
70+
}
71+
72+
func WithResetBCID(timeout time.Duration) Option {
73+
return func(cfg *config) {
74+
cfg.daq.timeout = timeout
75+
}
76+
}
77+
78+
type config struct {
79+
mode string // csv or db
80+
ctl struct {
81+
addr string // addr+port to eda-ctl
82+
}
83+
84+
hr struct {
85+
fname string
86+
rshaper uint32 // resistance shaper
87+
cshaper uint32 // capacity shaper
88+
89+
db dbConfig // configuration from tmv-db
90+
91+
buf [szCfgHR]byte
92+
data []byte
93+
}
94+
95+
daq struct {
96+
mode string // dcc, noise or inj
97+
fname string
98+
floor [nRFM * nHR * 3]uint32
99+
delta uint32 // delta threshold
100+
rfm uint32 // RFM ON mask
101+
102+
addrs []string // [addr:port]s for sending DIF data
103+
104+
timeout time.Duration // timeout for reset-BCID
105+
}
106+
107+
preamp struct {
108+
fname string
109+
gains [nRFM * nHR * nChans]uint32
110+
}
111+
112+
mask struct {
113+
fname string
114+
table [nRFM * nHR * nChans]uint32
115+
}
116+
117+
run struct {
118+
dir string
119+
}
120+
}
121+
122+
func newConfig() config {
123+
cfg := config{
124+
mode: "db",
125+
}
126+
cfg.hr.db = newDbConfig()
127+
cfg.hr.cshaper = 3
128+
cfg.daq.mode = "dcc"
129+
cfg.hr.data = cfg.hr.buf[4:]
130+
return cfg
131+
}
132+
13133
// dbConfig holds the configuration from the TMVDb
14134
// for each of the RFMs.
15135
type dbConfig struct {

eda/cfg_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestCompareConfig(t *testing.T) {
8181
}
8282

8383
func testCfgWithDB(dev *Device, asics []conddb.ASIC, rshaper uint32, rfms []int) error {
84-
WithRShaper(rshaper)(dev)
84+
WithRShaper(rshaper)(&dev.cfg)
8585
dev.cfg.hr.cshaper = 3
8686
dev.cfg.hr.data = dev.cfg.hr.buf[4:]
8787
dev.cfg.hr.db = newDbConfig()
@@ -144,9 +144,9 @@ func testCfgWithDB(dev *Device, asics []conddb.ASIC, rshaper uint32, rfms []int)
144144
}
145145

146146
func testCfgWithCSV(dev *Device, thresh, rshaper uint32, rfms []int) error {
147-
WithConfigDir("testdata")(dev)
148-
WithThreshold(thresh)(dev)
149-
WithRShaper(rshaper)(dev)
147+
WithConfigDir("testdata")(&dev.cfg)
148+
WithThreshold(thresh)(&dev.cfg)
149+
WithRShaper(rshaper)(&dev.cfg)
150150
dev.cfg.hr.db = newDbConfig()
151151
dev.cfg.hr.cshaper = 3
152152
dev.cfg.hr.data = dev.cfg.hr.buf[4:]

eda/device.go

+10-123
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"net"
1313
"os"
1414
"path"
15-
"path/filepath"
1615
"strconv"
1716
"time"
1817

@@ -96,49 +95,7 @@ type Device struct {
9695
}
9796
}
9897

99-
cfg struct {
100-
mode string // csv or db
101-
ctl struct {
102-
addr string // addr+port to eda-ctl
103-
}
104-
105-
hr struct {
106-
fname string
107-
rshaper uint32 // resistance shaper
108-
cshaper uint32 // capacity shaper
109-
110-
db dbConfig // configuration from tmv-db
111-
112-
buf [szCfgHR]byte
113-
data []byte
114-
}
115-
116-
daq struct {
117-
mode string // dcc, noise or inj
118-
fname string
119-
floor [nRFM * nHR * 3]uint32
120-
delta uint32 // delta threshold
121-
rfm uint32 // RFM ON mask
122-
123-
addrs []string // [addr:port]s for sending DIF data
124-
125-
timeout time.Duration // timeout for reset-BCID
126-
}
127-
128-
preamp struct {
129-
fname string
130-
gains [nRFM * nHR * nChans]uint32
131-
}
132-
133-
mask struct {
134-
fname string
135-
table [nRFM * nHR * nChans]uint32
136-
}
137-
138-
run struct {
139-
dir string
140-
}
141-
}
98+
cfg config
14299

143100
daq struct {
144101
rfm []rfmSink // DIF data sink, one per RFM
@@ -161,70 +118,6 @@ type rfmSink struct {
161118

162119
func (sink *rfmSink) valid() bool { return sink.id != 0 }
163120

164-
type Option func(*Device)
165-
166-
func WithThreshold(v uint32) Option {
167-
return func(dev *Device) {
168-
dev.cfg.daq.delta = v
169-
}
170-
}
171-
172-
func WithRFMMask(v uint32) Option {
173-
return func(dev *Device) {
174-
dev.cfg.daq.rfm = v
175-
}
176-
}
177-
178-
func WithRShaper(v uint32) Option {
179-
return func(dev *Device) {
180-
dev.cfg.hr.rshaper = v
181-
}
182-
}
183-
184-
func WithCShaper(v uint32) Option {
185-
return func(dev *Device) {
186-
dev.cfg.hr.cshaper = v
187-
}
188-
}
189-
190-
func WithDevSHM(dir string) Option {
191-
return func(dev *Device) {
192-
dev.cfg.run.dir = dir
193-
}
194-
}
195-
196-
func WithCtlAddr(addr string) Option {
197-
return func(dev *Device) {
198-
dev.cfg.ctl.addr = addr
199-
}
200-
}
201-
202-
func WithConfigDir(dir string) Option {
203-
return func(dev *Device) {
204-
if dir == "" {
205-
return
206-
}
207-
dev.cfg.mode = "csv"
208-
dev.cfg.hr.fname = filepath.Join(dir, "conf_base.csv")
209-
dev.cfg.daq.fname = filepath.Join(dir, "dac_floor_4rfm.csv")
210-
dev.cfg.preamp.fname = filepath.Join(dir, "pa_gain_4rfm.csv")
211-
dev.cfg.mask.fname = filepath.Join(dir, "mask_4rfm.csv")
212-
}
213-
}
214-
215-
func WithDAQMode(mode string) Option {
216-
return func(dev *Device) {
217-
dev.cfg.daq.mode = mode
218-
dev.msg.Printf("using daq mode=%q", mode)
219-
}
220-
}
221-
222-
func WithResetBCID(timeout time.Duration) Option {
223-
return func(dev *Device) {
224-
dev.cfg.daq.timeout = timeout
225-
}
226-
}
227-
228121
func newDevice(devmem, odir, devshm string, opts ...Option) (*Device, error) {
229122
mem, err := os.OpenFile(devmem, os.O_RDWR|os.O_SYNC, 0666)
230123
if err != nil {
@@ -240,19 +133,15 @@ func newDevice(devmem, odir, devshm string, opts ...Option) (*Device, error) {
240133
msg: log.New(os.Stdout, "eda: ", 0),
241134
dir: odir,
242135
buf: make([]byte, 4),
136+
cfg: newConfig(),
243137
}
244138
dev.mem.fd = mem
245-
dev.cfg.mode = "db"
246-
dev.cfg.hr.db = newDbConfig()
247-
dev.cfg.hr.cshaper = 3
248-
dev.cfg.daq.mode = "dcc"
249-
dev.cfg.hr.data = dev.cfg.hr.buf[4:]
250139

251-
WithResetBCID(10 * time.Second)(dev)
252-
WithDevSHM(devshm)(dev)
140+
WithResetBCID(10 * time.Second)(&dev.cfg)
141+
WithDevSHM(devshm)(&dev.cfg)
253142

254143
for _, opt := range opts {
255-
opt(dev)
144+
opt(&dev.cfg)
256145
}
257146

258147
// setup RFMs indices from provided mask
@@ -305,20 +194,18 @@ func NewDevice(fname string, odir string, opts ...Option) (*Device, error) {
305194
msg: log.New(os.Stdout, "eda: ", 0),
306195
dir: odir,
307196
buf: make([]byte, 4),
197+
cfg: newConfig(),
308198
}
309199
dev.mem.fd = mem
310-
WithResetBCID(10 * time.Second)(dev)
311-
WithConfigDir("/dev/shm/config_base")(dev)
312-
WithDevSHM("/dev/shm")(dev)
200+
WithResetBCID(10 * time.Second)(&dev.cfg)
201+
WithConfigDir("/dev/shm/config_base")(&dev.cfg)
202+
WithDevSHM("/dev/shm")(&dev.cfg)
313203

314204
dev.cfg.mode = "db"
315-
dev.cfg.hr.db = newDbConfig()
316-
dev.cfg.hr.cshaper = 3
317205
dev.cfg.daq.mode = "dcc"
318-
dev.cfg.hr.data = dev.cfg.hr.buf[4:]
319206

320207
for _, opt := range opts {
321-
opt(dev)
208+
opt(&dev.cfg)
322209
}
323210

324211
// setup RFMs indices from provided mask

eda/server_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestServer(t *testing.T) {
5353

5454
srv, err := newServer(
5555
addr, odir, fdev.mem, fdev.shm,
56-
func(dev *Device) { dev.cfg.mode = "db" },
56+
func(cfg *config) { cfg.mode = "db" },
5757
WithRFMMask(1<<1), // dummy
5858
)
5959
if err != nil {

0 commit comments

Comments
 (0)