-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgpio_fake_collection.go
121 lines (104 loc) · 3.8 KB
/
gpio_fake_collection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/// Author: Bernhard Tittelbach, btittelbach@github (c) 2015
package bbhw
import (
"fmt"
"log"
"sync"
)
// Uses the memory mapped IO to directly interface with AM335x registers.
// Same as FakeGPIO, but part of a collection of GPIOs you can set all at once using database-like transactions.
type FakeGPIOInCollection struct {
FakeGPIO
futureEnable bool
futureState bool
collection *FakeGPIOCollectionFactory
}
// Collection of GPIOs. Records SetState() calls after BeginTransactionRecordSetStates() has been called and delays their effect until EndTransactionApplySetStates() is called.
// Use it to toggle many GPIOs in the very same instant.
type FakeGPIOCollectionFactory struct {
//4 32bit arrays to be copied to register
collection []*FakeGPIOInCollection
record_changes bool
lock sync.Mutex
}
/// ---------- FakeGPIOCollectionFactory ---------------
// Create a collection of GPIOs.
// Doubles as factory for the FakeGPIOInCollection type.
func NewFakeGPIOCollectionFactory() (gpiocf *FakeGPIOCollectionFactory) {
gpiocf = new(FakeGPIOCollectionFactory)
gpiocf.collection = make([]*FakeGPIOInCollection, 0)
return gpiocf
}
// Apply States recorded with BeginTransactionRecordSetStates
func (gpiocf *FakeGPIOCollectionFactory) EndTransactionApplySetStates() {
gpiocf.lock.Lock()
defer gpiocf.lock.Unlock()
for _, gpio := range gpiocf.collection {
if gpio.futureEnable {
gpio.SetStateNow(gpio.futureState)
}
gpio.futureEnable = false
}
gpiocf.record_changes = false
}
// Begin recording calls to SetState for later
func (gpiocf *FakeGPIOCollectionFactory) BeginTransactionRecordSetStates() {
gpiocf.lock.Lock()
defer gpiocf.lock.Unlock()
gpiocf.record_changes = true
}
// slightly more fancy FakeGPIO for debugging.
// takes a name for easy recognition in debugging output and an optional logger (or nil) of your choice,
// thus you could route debug output of different GPIOs to different destinations
func (gpiocf *FakeGPIOCollectionFactory) NewFakeNamedGPIO(name string, direction int, logTarget *log.Logger) (gpio *FakeGPIOInCollection) {
gpio = &FakeGPIOInCollection{FakeGPIO: FakeGPIO{name: name, dir: direction, value: false, logTarget: logTarget}, collection: gpiocf}
gpiocf.lock.Lock()
gpiocf.collection = append(gpiocf.collection, gpio)
gpiocf.lock.Unlock()
return gpio
}
// Same as FakeGPIO but part of a FakeGPIOCollectionFactory
// unfortunately, can't rename this function as it would not be readily interchangeable anymore
func (gpiocf *FakeGPIOCollectionFactory) NewFakeGPIO(number uint, direction int) (gpio *FakeGPIOInCollection) {
return gpiocf.NewFakeNamedGPIO(fmt.Sprintf("FakeGPIOInCollection(%d,%+v)", number, gpiocf), direction, FakeGPIODefaultLogTarget_)
}
func (gpiocf *FakeGPIOCollectionFactory) NewGPIO(number uint, direction int) GPIOControllablePinInCollection {
return gpiocf.NewFakeGPIO(number, direction)
}
/// ------------- FakeGPIOInCollection Methods -------------------
func (gpio *FakeGPIOInCollection) SetStateNow(state bool) error {
if gpio == nil {
panic("gpio == nil")
}
return gpio.FakeGPIO.SetState(state)
}
func (gpio *FakeGPIOInCollection) SetFutureState(state bool) error {
if gpio == nil {
panic("gpio == nil")
}
gpio.futureEnable = true
gpio.futureState = state
return nil
}
func (gpio *FakeGPIOInCollection) GetFutureState() (state_known, state bool, err error) {
if gpio == nil {
panic("gpio == nil")
}
return gpio.futureEnable, gpio.futureState, nil
}
func (gpio *FakeGPIOInCollection) SetState(state bool) error {
if gpio == nil {
panic("gpio == nil")
}
if gpio.collection.record_changes {
return gpio.SetFutureState(state)
} else {
return gpio.SetStateNow(state)
}
}
func (gpio *FakeGPIOInCollection) SetActiveLow(activelow bool) (err error) {
if gpio == nil {
panic("gpio == nil")
}
return gpio.FakeGPIO.SetActiveLow(activelow)
}