1"""
2This test need a set of pins which can be set as inputs and have no external
3pull up or pull down connected.
4GP12 and GP17 must be connected together
5"""
6from machine import Pin
7import os
8
9mch = os.uname().machine
10if "LaunchPad" in mch:
11    pin_map = [
12        "GP24",
13        "GP12",
14        "GP14",
15        "GP15",
16        "GP16",
17        "GP17",
18        "GP28",
19        "GP8",
20        "GP6",
21        "GP30",
22        "GP31",
23        "GP3",
24        "GP0",
25        "GP4",
26        "GP5",
27    ]
28    max_af_idx = 15
29elif "WiPy" in mch:
30    pin_map = [
31        "GP23",
32        "GP24",
33        "GP12",
34        "GP13",
35        "GP14",
36        "GP9",
37        "GP17",
38        "GP28",
39        "GP22",
40        "GP8",
41        "GP30",
42        "GP31",
43        "GP0",
44        "GP4",
45        "GP5",
46    ]
47    max_af_idx = 15
48else:
49    raise Exception("Board not supported!")
50
51# test initial value
52p = Pin("GP12", Pin.IN)
53Pin("GP17", Pin.OUT, value=1)
54print(p() == 1)
55Pin("GP17", Pin.OUT, value=0)
56print(p() == 0)
57
58
59def test_noinit():
60    for p in pin_map:
61        pin = Pin(p)
62        pin.value()
63
64
65def test_pin_read(pull):
66    # enable the pull resistor on all pins, then read the value
67    for p in pin_map:
68        pin = Pin(p, mode=Pin.IN, pull=pull)
69    for p in pin_map:
70        print(pin())
71
72
73def test_pin_af():
74    for p in pin_map:
75        for af in Pin(p).alt_list():
76            if af[1] <= max_af_idx:
77                Pin(p, mode=Pin.ALT, alt=af[1])
78                Pin(p, mode=Pin.ALT_OPEN_DRAIN, alt=af[1])
79
80
81# test un-initialized pins
82test_noinit()
83# test with pull-up and pull-down
84test_pin_read(Pin.PULL_UP)
85test_pin_read(Pin.PULL_DOWN)
86
87# test all constructor combinations
88pin = Pin(pin_map[0])
89pin = Pin(pin_map[0], mode=Pin.IN)
90pin = Pin(pin_map[0], mode=Pin.OUT)
91pin = Pin(pin_map[0], mode=Pin.IN, pull=Pin.PULL_DOWN)
92pin = Pin(pin_map[0], mode=Pin.IN, pull=Pin.PULL_UP)
93pin = Pin(pin_map[0], mode=Pin.OPEN_DRAIN, pull=Pin.PULL_UP)
94pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_DOWN)
95pin = Pin(pin_map[0], mode=Pin.OUT, pull=None)
96pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP)
97pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.LOW_POWER)
98pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.MED_POWER)
99pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.HIGH_POWER)
100pin = Pin(pin_map[0], mode=Pin.OUT, drive=pin.LOW_POWER)
101pin = Pin(pin_map[0], Pin.OUT, Pin.PULL_DOWN)
102pin = Pin(pin_map[0], Pin.ALT, Pin.PULL_UP)
103pin = Pin(pin_map[0], Pin.ALT_OPEN_DRAIN, Pin.PULL_UP)
104test_pin_af()  # try the entire af range on all pins
105
106# test pin init and printing
107pin = Pin(pin_map[0])
108pin.init(mode=Pin.IN)
109print(pin)
110pin.init(Pin.IN, Pin.PULL_DOWN)
111print(pin)
112pin.init(mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.LOW_POWER)
113print(pin)
114pin.init(mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.HIGH_POWER)
115print(pin)
116
117# test value in OUT mode
118pin = Pin(pin_map[0], mode=Pin.OUT)
119pin.value(0)
120pin.toggle()  # test toggle
121print(pin())
122pin.toggle()  # test toggle again
123print(pin())
124# test different value settings
125pin(1)
126print(pin.value())
127pin(0)
128print(pin.value())
129pin.value(1)
130print(pin())
131pin.value(0)
132print(pin())
133
134# test all getters and setters
135pin = Pin(pin_map[0], mode=Pin.OUT)
136# mode
137print(pin.mode() == Pin.OUT)
138pin.mode(Pin.IN)
139print(pin.mode() == Pin.IN)
140# pull
141pin.pull(None)
142print(pin.pull() == None)
143pin.pull(Pin.PULL_DOWN)
144print(pin.pull() == Pin.PULL_DOWN)
145# drive
146pin.drive(Pin.MED_POWER)
147print(pin.drive() == Pin.MED_POWER)
148pin.drive(Pin.HIGH_POWER)
149print(pin.drive() == Pin.HIGH_POWER)
150# id
151print(pin.id() == pin_map[0])
152
153# all the next ones MUST raise
154try:
155    pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.IN)  # incorrect drive value
156except Exception:
157    print("Exception")
158
159try:
160    pin = Pin(pin_map[0], mode=Pin.LOW_POWER, pull=Pin.PULL_UP)  # incorrect mode value
161except Exception:
162    print("Exception")
163
164try:
165    pin = Pin(pin_map[0], mode=Pin.IN, pull=Pin.HIGH_POWER)  # incorrect pull value
166except Exception:
167    print("Exception")
168
169try:
170    pin = Pin("A0", Pin.OUT, Pin.PULL_DOWN)  # incorrect pin id
171except Exception:
172    print("Exception")
173
174try:
175    pin = Pin(pin_map[0], Pin.IN, Pin.PULL_UP, alt=0)  # af specified in GPIO mode
176except Exception:
177    print("Exception")
178
179try:
180    pin = Pin(pin_map[0], Pin.OUT, Pin.PULL_UP, alt=7)  # af specified in GPIO mode
181except Exception:
182    print("Exception")
183
184try:
185    pin = Pin(pin_map[0], Pin.ALT, Pin.PULL_UP, alt=0)  # incorrect af
186except Exception:
187    print("Exception")
188
189try:
190    pin = Pin(pin_map[0], Pin.ALT_OPEN_DRAIN, Pin.PULL_UP, alt=-1)  # incorrect af
191except Exception:
192    print("Exception")
193
194try:
195    pin = Pin(pin_map[0], Pin.ALT_OPEN_DRAIN, Pin.PULL_UP, alt=16)  # incorrect af
196except Exception:
197    print("Exception")
198
199try:
200    pin.mode(Pin.PULL_UP)  # incorrect pin mode
201except Exception:
202    print("Exception")
203
204try:
205    pin.pull(Pin.OUT)  # incorrect pull
206except Exception:
207    print("Exception")
208
209try:
210    pin.drive(Pin.IN)  # incorrect drive strength
211except Exception:
212    print("Exception")
213
214try:
215    pin.id("ABC")  # id cannot be set
216except Exception:
217    print("Exception")
218