github.com/martinohmann/rfoutlet@v1.2.1-0.20220707195255-8a66aa411105/pkg/gpio/gpio.go (about) 1 package gpio 2 3 import "github.com/warthog618/gpiod" 4 5 // Closer is something that can be closed. 6 type Closer interface { 7 // Close closes the thing. 8 Close() error 9 } 10 11 // Watcher watches a pin. 12 type Watcher interface { 13 Closer 14 // Watch returns a channel of observed RisingEdge and FallingEdge pin 15 // events. 16 Watch() <-chan gpiod.LineEvent 17 } 18 19 // OutputPin is a pin that can be written to. 20 type OutputPin interface { 21 Closer 22 // SetValue sets the pin value. 23 SetValue(value int) error 24 } 25 26 // CodeTransmitter defines the interface for a rf code transmitter. 27 type CodeTransmitter interface { 28 Closer 29 // Transmit transmits a code using given protocol and pulse length. 30 // 31 // This method returns immediately. The code is transmitted in the background. 32 // If you need to ensure that a code has been fully transmitted, wait for the 33 // returned channel to be closed. 34 Transmit(code uint64, protocol Protocol, pulseLength uint) <-chan struct{} 35 } 36 37 // CodeReceiver defines the interface for a rf code receiver. 38 type CodeReceiver interface { 39 Closer 40 // Receive blocks until there is a result on the receive channel. 41 Receive() <-chan ReceiveResult 42 } 43 44 // ReceiveResult contains information about a detected code sent by an rf code 45 // transmitter. 46 type ReceiveResult struct { 47 // Code is the detected code. 48 Code uint64 49 50 // BitLength is the detected bit length. 51 BitLength uint 52 53 // PulseLength is the detected pulse length. 54 PulseLength int64 55 56 // Protocol is the detected protocol. The protocol is 1-indexed. 57 Protocol int 58 } 59 60 // FakeWatcher can be used in tests as a Watcher. 61 type FakeWatcher struct { 62 // Events can be used to make the FakeWatcher return arbitrary events. 63 Events chan gpiod.LineEvent 64 65 // Err controls the error returned by Close. 66 Err error 67 68 // Closed indicates whether Close was called or not. 69 Closed bool 70 } 71 72 // NewFakeWatcher creates a new *FakeWatcher that can be used in tests as a 73 // Watcher. 74 func NewFakeWatcher() *FakeWatcher { 75 return &FakeWatcher{ 76 Events: make(chan gpiod.LineEvent), 77 } 78 } 79 80 // Watch implements Watcher. 81 func (w *FakeWatcher) Watch() <-chan gpiod.LineEvent { 82 return w.Events 83 } 84 85 // Close implements Closer. 86 func (w *FakeWatcher) Close() error { 87 close(w.Events) 88 w.Closed = true 89 return w.Err 90 } 91 92 // FakeOutputPin can be used in tests as an OutputPin. 93 type FakeOutputPin struct { 94 // Values holds the sequence of values the were set via SetValue. 95 Values []int 96 97 // Err controls the error returned by Close. 98 Err error 99 100 // Closed indicates whether Close was called or not. 101 Closed bool 102 } 103 104 // NewFakeOutputPin creates a new *FakeOutputPin that can be used in tests as 105 // an OutputPin. 106 func NewFakeOutputPin() *FakeOutputPin { 107 return &FakeOutputPin{ 108 Values: make([]int, 0), 109 } 110 } 111 112 // SetValue implements OutputPin. 113 func (p *FakeOutputPin) SetValue(value int) error { 114 p.Values = append(p.Values, value) 115 return p.Err 116 } 117 118 // Close implements Closer. 119 func (p *FakeOutputPin) Close() error { 120 p.Closed = true 121 return p.Err 122 }