diff --git a/device/device.go b/device/device.go index f1a359d6..b918aba4 100644 --- a/device/device.go +++ b/device/device.go @@ -31,6 +31,7 @@ package device import ( "fmt" "io" + "errors" "bitbucket.org/ausocean/av/revid/config" ) @@ -89,12 +90,14 @@ type ManualInput struct { // NewManualInput provides a new ManualInput. func NewManualInput() *ManualInput { - r, w := io.Pipe() - return &ManualInput{reader: r, writer: w} + return &ManualInput{} } // Read reads from the manual input and puts the bytes into p. func (m *ManualInput) Read(p []byte) (int, error) { + if !m.isRunning { + return 0, errors.New("manual input has not been started, can't read") + } return m.reader.Read(p) } @@ -109,6 +112,7 @@ func (m *ManualInput) Set(c config.Config) error { return nil } // to satisfy the Device interface. func (m *ManualInput) Start() error { m.isRunning = true + m.reader, m.writer = io.Pipe() return nil } @@ -131,5 +135,8 @@ func (m *ManualInput) IsRunning() bool { return m.isRunning } // Write writes p to the ManualInput's writer side of its pipe. func (m *ManualInput) Write(p []byte) (int, error) { + if !m.isRunning { + return 0, errors.New("manual input has not been started, can't write") + } return m.writer.Write(p) }