forked from google/periph
/
pin.go
139 lines (121 loc) · 3.41 KB
/
pin.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2016 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
// Package pin declare well known pins.
//
// pin is about physical pins, not about their logical function.
//
// While not a protocol strictly speaking, these are "well known constants".
package pin
import (
"errors"
"periph.io/x/periph/conn"
)
// These are well known pins.
var (
INVALID *BasicPin // Either floating or invalid pin
GROUND *BasicPin // Ground
V1_8 *BasicPin // 1.8V (filtered)
V2_8 *BasicPin // 2.8V (filtered)
V3_3 *BasicPin // 3.3V (filtered)
V5 *BasicPin // 5V (filtered)
DC_IN *BasicPin // DC IN; this is normally the 5V input
BAT_PLUS *BasicPin // LiPo Battery + connector
)
// Pin is the minimal common interface shared between gpio.PinIO and
// analog.PinIO.
type Pin interface {
conn.Resource
// Name returns the name of the pin.
Name() string
// Number returns the logical pin number or a negative number if the pin is
// not a GPIO, e.g. GROUND, V3_3, etc.
Number() int
// Function returns a user readable string representation of what the pin is
// configured to do. Common case is In and Out but it can be bus specific pin
// name.
//
// Deprecated: Use PinFunc.Func. Will be removed in v4.
Function() string
}
// PinFunc is a supplementary interface that enables specifically querying for
// the pin function.
//
// TODO(maruel): It will be merged into interface Pin for v4.
type PinFunc interface {
// Func returns the pin's current function.
//
// The returned value may be specialized or generalized, depending on the
// actual port. For example it will likely be generalized for ports served
// over USB (like a FT232H with D0 set as SPI_MOSI) but specialized for
// ports on the base board (like a RPi3 with GPIO10 set as SPI0_MOSI).
Func() Func
// SupportedFuncs returns the possible functions this pin support.
//
// Do not mutate the returned slice.
SupportedFuncs() []Func
// SetFunc sets the pin function.
//
// Example use is to reallocate a RPi3's GPIO14 active function between
// UART0_TX and UART1_TX.
SetFunc(f Func) error
}
//
// BasicPin implements Pin as a static pin.
//
// It doesn't have a usable functionality.
type BasicPin struct {
N string
}
// String implements conn.Resource.
func (b *BasicPin) String() string {
return b.N
}
// Halt implements conn.Resource.
func (b *BasicPin) Halt() error {
return nil
}
// Name implements Pin.
func (b *BasicPin) Name() string {
return b.N
}
// Number implements Pin.
//
// Returns -1 as pin number.
func (b *BasicPin) Number() int {
return -1
}
// Function implements Pin.
//
// Returns "" as pin function.
func (b *BasicPin) Function() string {
return ""
}
// Func implements PinFunc.
//
// Returns FuncNone as pin function.
func (b *BasicPin) Func() Func {
return FuncNone
}
// SupportedFuncs implements PinFunc.
//
// Returns nil.
func (b *BasicPin) SupportedFuncs() []Func {
return nil
}
// SetFunc implements PinFunc.
func (b *BasicPin) SetFunc(f Func) error {
return errors.New("pin: can't change static pin function")
}
func init() {
INVALID = &BasicPin{N: "INVALID"}
GROUND = &BasicPin{N: "GROUND"}
V1_8 = &BasicPin{N: "1.8V"}
V2_8 = &BasicPin{N: "2.8V"}
V3_3 = &BasicPin{N: "3.3V"}
V5 = &BasicPin{N: "5V"}
DC_IN = &BasicPin{N: "DC_IN"}
BAT_PLUS = &BasicPin{N: "BAT+"}
}
var _ Pin = INVALID
var _ PinFunc = INVALID