2016-12-20 13:25:22 +01:00
|
|
|
package aio
|
|
|
|
|
2023-11-23 19:01:42 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
const analogReadReturnValue = 99
|
|
|
|
|
|
|
|
type aioTestBareAdaptor struct{}
|
|
|
|
|
|
|
|
func (t *aioTestBareAdaptor) Connect() error { return nil }
|
|
|
|
func (t *aioTestBareAdaptor) Finalize() error { return nil }
|
|
|
|
func (t *aioTestBareAdaptor) Name() string { return "bare" }
|
|
|
|
func (t *aioTestBareAdaptor) SetName(n string) {}
|
|
|
|
|
|
|
|
type aioTestWritten struct {
|
|
|
|
pin string
|
|
|
|
val int
|
|
|
|
}
|
2017-04-02 17:55:49 +02:00
|
|
|
|
2016-12-20 13:25:22 +01:00
|
|
|
type aioTestAdaptor struct {
|
2023-11-23 19:01:42 +01:00
|
|
|
name string
|
|
|
|
written []aioTestWritten
|
|
|
|
simulateWriteError bool
|
|
|
|
simulateReadError bool
|
|
|
|
port string
|
|
|
|
mtx sync.Mutex
|
|
|
|
analogReadFunc func() (val int, err error)
|
|
|
|
analogWriteFunc func(val int) error
|
2016-12-20 13:25:22 +01:00
|
|
|
}
|
|
|
|
|
2023-10-27 20:46:45 +02:00
|
|
|
func newAioTestAdaptor() *aioTestAdaptor {
|
|
|
|
t := aioTestAdaptor{
|
|
|
|
name: "aio_test_adaptor",
|
|
|
|
port: "/dev/null",
|
2023-11-15 20:51:52 +01:00
|
|
|
analogReadFunc: func() (int, error) {
|
2023-11-23 19:01:42 +01:00
|
|
|
return analogReadReturnValue, nil
|
2023-10-27 20:46:45 +02:00
|
|
|
},
|
2023-11-15 20:51:52 +01:00
|
|
|
analogWriteFunc: func(val int) error {
|
2023-10-27 20:46:45 +02:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2016-12-20 13:25:22 +01:00
|
|
|
|
2023-10-27 20:46:45 +02:00
|
|
|
return &t
|
2022-03-24 18:40:26 +01:00
|
|
|
}
|
|
|
|
|
2023-10-27 20:46:45 +02:00
|
|
|
// AnalogRead capabilities (interface AnalogReader)
|
2023-11-15 20:51:52 +01:00
|
|
|
func (t *aioTestAdaptor) AnalogRead(pin string) (int, error) {
|
2017-04-02 17:55:49 +02:00
|
|
|
t.mtx.Lock()
|
|
|
|
defer t.mtx.Unlock()
|
2023-11-23 19:01:42 +01:00
|
|
|
|
|
|
|
if t.simulateReadError {
|
|
|
|
return 0, fmt.Errorf("read error")
|
|
|
|
}
|
|
|
|
|
2023-10-27 20:46:45 +02:00
|
|
|
return t.analogReadFunc()
|
2016-12-20 13:25:22 +01:00
|
|
|
}
|
2022-03-24 18:40:26 +01:00
|
|
|
|
2023-10-27 20:46:45 +02:00
|
|
|
// AnalogWrite capabilities (interface AnalogWriter)
|
2023-11-15 20:51:52 +01:00
|
|
|
func (t *aioTestAdaptor) AnalogWrite(pin string, val int) error {
|
2022-03-24 18:40:26 +01:00
|
|
|
t.mtx.Lock()
|
|
|
|
defer t.mtx.Unlock()
|
2023-11-23 19:01:42 +01:00
|
|
|
|
|
|
|
if t.simulateWriteError {
|
|
|
|
return fmt.Errorf("write error")
|
|
|
|
}
|
|
|
|
|
|
|
|
w := aioTestWritten{pin: pin, val: val}
|
|
|
|
t.written = append(t.written, w)
|
2023-10-27 20:46:45 +02:00
|
|
|
return t.analogWriteFunc(val)
|
2022-03-24 18:40:26 +01:00
|
|
|
}
|
|
|
|
|
2023-11-15 20:51:52 +01:00
|
|
|
func (t *aioTestAdaptor) Connect() error { return nil }
|
|
|
|
func (t *aioTestAdaptor) Finalize() error { return nil }
|
|
|
|
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 }
|