@@ -16,44 +16,12 @@ type (
16
16
digitalPinInitializer func (gobot.DigitalPinner ) error
17
17
)
18
18
19
- type digitalPinGpiosForSPI struct {
20
- sclkPin string
21
- ncsPin string
22
- sdoPin string
23
- sdiPin string
24
- }
25
-
26
- type digitalPinsDebouncePin struct {
27
- id string
28
- period time.Duration
29
- }
30
-
31
- type digitalPinsEventOnEdgePin struct {
32
- id string
33
- handler func (lineOffset int , timestamp time.Duration , detectedEdge string , seqno uint32 , lseqno uint32 )
34
- }
35
-
36
- type digitalPinsPollForEdgeDetectionPin struct {
37
- id string
38
- pollInterval time.Duration
39
- pollQuitChan chan struct {}
40
- }
41
-
42
19
// digitalPinsConfiguration contains all changeable attributes of the adaptor.
43
20
type digitalPinsConfiguration struct {
44
- initialize digitalPinInitializer
45
- useSysfs * bool
46
- gpiosForSPI * digitalPinGpiosForSPI
47
- activeLowPins []string
48
- pullDownPins []string
49
- pullUpPins []string
50
- openDrainPins []string
51
- openSourcePins []string
52
- debouncePins []digitalPinsDebouncePin
53
- eventOnFallingEdgePins []digitalPinsEventOnEdgePin
54
- eventOnRisingEdgePins []digitalPinsEventOnEdgePin
55
- eventOnBothEdgesPins []digitalPinsEventOnEdgePin
56
- pollForEdgeDetectionPins []digitalPinsPollForEdgeDetectionPin
21
+ debug bool
22
+ initialize digitalPinInitializer
23
+ systemOptions []system.AccesserOptionApplier
24
+ pinOptions map [string ][]func (gobot.DigitalPinOptioner ) bool
57
25
}
58
26
59
27
// DigitalPinsAdaptor is a adaptor for digital pins, normally used for composition in platforms.
@@ -62,7 +30,6 @@ type DigitalPinsAdaptor struct {
62
30
digitalPinsCfg * digitalPinsConfiguration
63
31
translate digitalPinTranslator
64
32
pins map [string ]gobot.DigitalPinner
65
- pinOptions map [string ][]func (gobot.DigitalPinOptioner ) bool
66
33
mutex sync.Mutex
67
34
}
68
35
@@ -76,15 +43,27 @@ func NewDigitalPinsAdaptor(
76
43
t digitalPinTranslator ,
77
44
opts ... DigitalPinsOptionApplier ,
78
45
) * DigitalPinsAdaptor {
79
- a := & DigitalPinsAdaptor {
80
- sys : sys ,
81
- digitalPinsCfg : & digitalPinsConfiguration {initialize : func (pin gobot.DigitalPinner ) error { return pin .Export () }},
82
- translate : t ,
46
+ a := DigitalPinsAdaptor {
47
+ sys : sys ,
48
+ digitalPinsCfg : & digitalPinsConfiguration {
49
+ initialize : func (pin gobot.DigitalPinner ) error { return pin .Export () },
50
+ pinOptions : make (map [string ][]func (gobot.DigitalPinOptioner ) bool ),
51
+ },
52
+ translate : t ,
83
53
}
54
+
84
55
for _ , o := range opts {
85
56
o .apply (a .digitalPinsCfg )
86
57
}
87
- return a
58
+
59
+ a .sys .AddDigitalPinSupport (a .digitalPinsCfg .systemOptions ... )
60
+
61
+ return & a
62
+ }
63
+
64
+ // WithDigitalPinDebug can be used to switch on debugging for SPI implementation.
65
+ func WithDigitalPinDebug () digitalPinsDebugOption {
66
+ return digitalPinsDebugOption (true )
88
67
}
89
68
90
69
// WithDigitalPinInitializer can be used to substitute the default initializer.
@@ -104,18 +83,6 @@ func WithGpioSysfsAccess() digitalPinsSystemSysfsOption {
104
83
return digitalPinsSystemSysfsOption (true )
105
84
}
106
85
107
- // WithSpiGpioAccess can be used to switch the default SPI implementation to GPIO usage.
108
- func WithSpiGpioAccess (sclkPin , ncsPin , sdoPin , sdiPin string ) digitalPinsForSystemSpiOption {
109
- o := digitalPinsForSystemSpiOption {
110
- sclkPin : sclkPin ,
111
- ncsPin : ncsPin ,
112
- sdoPin : sdoPin ,
113
- sdiPin : sdiPin ,
114
- }
115
-
116
- return o
117
- }
118
-
119
86
// WithGpiosActiveLow prepares the given pins for inverse reaction on next initialize.
120
87
// This is working for inputs and outputs.
121
88
func WithGpiosActiveLow (pin string , otherPins ... string ) digitalPinsActiveLowOption {
@@ -196,69 +163,16 @@ func (a *DigitalPinsAdaptor) Connect() error {
196
163
a .mutex .Lock ()
197
164
defer a .mutex .Unlock ()
198
165
199
- if a .pinOptions != nil {
200
- return fmt .Errorf ("digital pin adaptor already connected, please call Finalize() for re-connect" )
201
- }
202
-
203
- cfg := a .digitalPinsCfg
204
-
205
- if cfg .useSysfs != nil {
206
- if * cfg .useSysfs {
207
- system .WithDigitalPinSysfsAccess ()(a .sys )
208
- } else {
209
- system .WithDigitalPinCdevAccess ()(a .sys )
210
- }
211
- }
212
-
213
- if cfg .gpiosForSPI != nil {
214
- system .WithSpiGpioAccess (a , cfg .gpiosForSPI .sclkPin , cfg .gpiosForSPI .ncsPin , cfg .gpiosForSPI .sdoPin ,
215
- cfg .gpiosForSPI .sdiPin )(a .sys )
166
+ if a .digitalPinsCfg .debug {
167
+ fmt .Println ("connect the digital pins adaptor" )
216
168
}
217
169
218
- a .pinOptions = make (map [string ][]func (gobot.DigitalPinOptioner ) bool )
219
-
220
- for _ , pin := range cfg .activeLowPins {
221
- a .pinOptions [pin ] = append (a .pinOptions [pin ], system .WithPinActiveLow ())
222
- }
223
-
224
- for _ , pin := range cfg .pullDownPins {
225
- a .pinOptions [pin ] = append (a .pinOptions [pin ], system .WithPinPullDown ())
226
- }
227
-
228
- for _ , pin := range cfg .pullUpPins {
229
- a .pinOptions [pin ] = append (a .pinOptions [pin ], system .WithPinPullUp ())
230
- }
231
-
232
- for _ , pin := range cfg .openDrainPins {
233
- a .pinOptions [pin ] = append (a .pinOptions [pin ], system .WithPinOpenDrain ())
234
- }
235
-
236
- for _ , pin := range cfg .openSourcePins {
237
- a .pinOptions [pin ] = append (a .pinOptions [pin ], system .WithPinOpenSource ())
238
- }
239
-
240
- for _ , pin := range cfg .debouncePins {
241
- a .pinOptions [pin .id ] = append (a .pinOptions [pin .id ], system .WithPinDebounce (pin .period ))
242
- }
243
-
244
- for _ , pin := range cfg .eventOnFallingEdgePins {
245
- a .pinOptions [pin .id ] = append (a .pinOptions [pin .id ], system .WithPinEventOnFallingEdge (pin .handler ))
246
- }
247
-
248
- for _ , pin := range cfg .eventOnRisingEdgePins {
249
- a .pinOptions [pin .id ] = append (a .pinOptions [pin .id ], system .WithPinEventOnRisingEdge (pin .handler ))
250
- }
251
-
252
- for _ , pin := range cfg .eventOnBothEdgesPins {
253
- a .pinOptions [pin .id ] = append (a .pinOptions [pin .id ], system .WithPinEventOnBothEdges (pin .handler ))
254
- }
255
-
256
- for _ , pin := range cfg .pollForEdgeDetectionPins {
257
- a .pinOptions [pin .id ] = append (a .pinOptions [pin .id ],
258
- system .WithPinPollForEdgeDetection (pin .pollInterval , pin .pollQuitChan ))
170
+ if a .pins != nil {
171
+ return fmt .Errorf ("digital pin adaptor already connected, please call Finalize() for re-connect" )
259
172
}
260
173
261
174
a .pins = make (map [string ]gobot.DigitalPinner )
175
+
262
176
return nil
263
177
}
264
178
@@ -267,6 +181,10 @@ func (a *DigitalPinsAdaptor) Finalize() error {
267
181
a .mutex .Lock ()
268
182
defer a .mutex .Unlock ()
269
183
184
+ if a .digitalPinsCfg .debug {
185
+ fmt .Println ("finalize the digital pins adaptor" )
186
+ }
187
+
270
188
var err error
271
189
for _ , pin := range a .pins {
272
190
if pin != nil {
@@ -276,7 +194,7 @@ func (a *DigitalPinsAdaptor) Finalize() error {
276
194
}
277
195
}
278
196
a .pins = nil
279
- a . pinOptions = nil
197
+
280
198
return err
281
199
}
282
200
@@ -321,7 +239,7 @@ func (a *DigitalPinsAdaptor) digitalPin(
321
239
return nil , fmt .Errorf ("not connected for pin %s" , id )
322
240
}
323
241
324
- o := append (a .pinOptions [id ], opts ... )
242
+ o := append (a .digitalPinsCfg . pinOptions [id ], opts ... )
325
243
pin := a .pins [id ]
326
244
327
245
if pin == nil {
0 commit comments