2016-12-20 20:25:22 +08:00
|
|
|
package aio
|
|
|
|
|
2017-04-02 23:55:49 +08:00
|
|
|
import "sync"
|
|
|
|
|
2016-12-20 20:25:22 +08:00
|
|
|
type aioTestAdaptor struct {
|
2022-03-25 01:40:26 +08:00
|
|
|
name string
|
|
|
|
port string
|
|
|
|
mtx sync.Mutex
|
|
|
|
testAdaptorAnalogRead func() (val int, err error)
|
|
|
|
testAdaptorAnalogWrite func(val int) (err error)
|
|
|
|
written []int
|
2016-12-20 20:25:22 +08:00
|
|
|
}
|
|
|
|
|
2017-04-02 23:55:49 +08:00
|
|
|
func (t *aioTestAdaptor) TestAdaptorAnalogRead(f func() (val int, err error)) {
|
|
|
|
t.mtx.Lock()
|
|
|
|
defer t.mtx.Unlock()
|
|
|
|
t.testAdaptorAnalogRead = f
|
2016-12-20 20:25:22 +08:00
|
|
|
}
|
|
|
|
|
2022-03-25 01:40:26 +08:00
|
|
|
func (t *aioTestAdaptor) TestAdaptorAnalogWrite(f func(val int) (err error)) {
|
|
|
|
t.mtx.Lock()
|
|
|
|
defer t.mtx.Unlock()
|
|
|
|
t.testAdaptorAnalogWrite = f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *aioTestAdaptor) AnalogRead(pin string) (val int, err error) {
|
2017-04-02 23:55:49 +08:00
|
|
|
t.mtx.Lock()
|
|
|
|
defer t.mtx.Unlock()
|
|
|
|
return t.testAdaptorAnalogRead()
|
2016-12-20 20:25:22 +08:00
|
|
|
}
|
2022-03-25 01:40:26 +08:00
|
|
|
|
|
|
|
func (t *aioTestAdaptor) AnalogWrite(pin string, val int) (err error) {
|
|
|
|
t.mtx.Lock()
|
|
|
|
defer t.mtx.Unlock()
|
|
|
|
t.written = append(t.written, val)
|
|
|
|
return t.testAdaptorAnalogWrite(val)
|
|
|
|
}
|
|
|
|
|
2016-12-20 20:25:22 +08:00
|
|
|
func (t *aioTestAdaptor) Connect() (err error) { return }
|
|
|
|
func (t *aioTestAdaptor) Finalize() (err error) { return }
|
|
|
|
func (t *aioTestAdaptor) Name() string { return t.name }
|
|
|
|
func (t *aioTestAdaptor) SetName(n string) { t.name = n }
|
|
|
|
func (t *aioTestAdaptor) Port() string { return t.port }
|
|
|
|
|
|
|
|
func newAioTestAdaptor() *aioTestAdaptor {
|
|
|
|
return &aioTestAdaptor{
|
|
|
|
port: "/dev/null",
|
2017-04-02 23:55:49 +08:00
|
|
|
testAdaptorAnalogRead: func() (val int, err error) {
|
|
|
|
return 99, nil
|
|
|
|
},
|
2022-03-25 01:40:26 +08:00
|
|
|
testAdaptorAnalogWrite: func(val int) (err error) {
|
|
|
|
return nil
|
|
|
|
},
|
2016-12-20 20:25:22 +08:00
|
|
|
}
|
|
|
|
}
|