Skip to content

Commit 0aaac6b

Browse files
committed
tests: Add D-PAD buttons and timeout on release-wait loops.
1 parent 2d80e7b commit 0aaac6b

1 file changed

Lines changed: 119 additions & 1 deletion

File tree

tests/scenarios/board_buttons.yaml

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ tests:
1414
from time import sleep_ms, ticks_ms, ticks_diff
1515
led = Pin("LED_GREEN", Pin.OUT)
1616
btn = Pin("A_BUTTON", Pin.IN, Pin.PULL_UP)
17+
t_rel = ticks_ms()
1718
while btn.value() == 0:
19+
if ticks_diff(ticks_ms(), t_rel) > 3000:
20+
break
1821
sleep_ms(20)
1922
led.on()
2023
detected = False
@@ -38,7 +41,10 @@ tests:
3841
from time import sleep_ms, ticks_ms, ticks_diff
3942
led = Pin("LED_GREEN", Pin.OUT)
4043
btn = Pin("B_BUTTON", Pin.IN, Pin.PULL_UP)
44+
t_rel = ticks_ms()
4145
while btn.value() == 0:
46+
if ticks_diff(ticks_ms(), t_rel) > 3000:
47+
break
4248
sleep_ms(20)
4349
led.on()
4450
detected = False
@@ -62,7 +68,10 @@ tests:
6268
from time import sleep_ms, ticks_ms, ticks_diff
6369
led = Pin("LED_GREEN", Pin.OUT)
6470
btn = Pin("MENU_BUTTON", Pin.IN, Pin.PULL_UP)
71+
t_rel = ticks_ms()
6572
while btn.value() == 0:
73+
if ticks_diff(ticks_ms(), t_rel) > 3000:
74+
break
6675
sleep_ms(20)
6776
led.on()
6877
detected = False
@@ -77,7 +86,107 @@ tests:
7786
expect_true: true
7887
mode: [hardware]
7988

80-
# --- Interrupt tests (one per button) ---
89+
# --- D-PAD buttons via MCP23009E (I2C 0x20, GPIO register 0x09) ---
90+
# Read directly via I2C without importing the driver.
91+
# Bit mapping: UP=bit7, DOWN=bit5, LEFT=bit6, RIGHT=bit4.
92+
93+
- name: "D-PAD UP press (polling)"
94+
action: hardware_script
95+
pre_prompt: "Test polling D-PAD : appuyez sur chaque bouton quand la LED verte s'allume (5s). Commençons par UP."
96+
wait: false
97+
script: |
98+
from machine import Pin, I2C
99+
from time import sleep_ms, ticks_ms, ticks_diff
100+
led = Pin("LED_GREEN", Pin.OUT)
101+
i2c = I2C(1)
102+
bit = 7
103+
led.on()
104+
detected = False
105+
t0 = ticks_ms()
106+
while ticks_diff(ticks_ms(), t0) < 5000:
107+
gpio = i2c.readfrom_mem(0x20, 0x09, 1)[0]
108+
if not (gpio & (1 << bit)):
109+
detected = True
110+
break
111+
sleep_ms(20)
112+
led.off()
113+
result = detected
114+
expect_true: true
115+
mode: [hardware]
116+
117+
- name: "D-PAD DOWN press (polling)"
118+
action: hardware_script
119+
pre_prompt: "Bouton DOWN"
120+
wait: false
121+
script: |
122+
from machine import Pin, I2C
123+
from time import sleep_ms, ticks_ms, ticks_diff
124+
led = Pin("LED_GREEN", Pin.OUT)
125+
i2c = I2C(1)
126+
bit = 5
127+
led.on()
128+
detected = False
129+
t0 = ticks_ms()
130+
while ticks_diff(ticks_ms(), t0) < 5000:
131+
gpio = i2c.readfrom_mem(0x20, 0x09, 1)[0]
132+
if not (gpio & (1 << bit)):
133+
detected = True
134+
break
135+
sleep_ms(20)
136+
led.off()
137+
result = detected
138+
expect_true: true
139+
mode: [hardware]
140+
141+
- name: "D-PAD LEFT press (polling)"
142+
action: hardware_script
143+
pre_prompt: "Bouton LEFT"
144+
wait: false
145+
script: |
146+
from machine import Pin, I2C
147+
from time import sleep_ms, ticks_ms, ticks_diff
148+
led = Pin("LED_GREEN", Pin.OUT)
149+
i2c = I2C(1)
150+
bit = 6
151+
led.on()
152+
detected = False
153+
t0 = ticks_ms()
154+
while ticks_diff(ticks_ms(), t0) < 5000:
155+
gpio = i2c.readfrom_mem(0x20, 0x09, 1)[0]
156+
if not (gpio & (1 << bit)):
157+
detected = True
158+
break
159+
sleep_ms(20)
160+
led.off()
161+
result = detected
162+
expect_true: true
163+
mode: [hardware]
164+
165+
- name: "D-PAD RIGHT press (polling)"
166+
action: hardware_script
167+
pre_prompt: "Bouton RIGHT"
168+
wait: false
169+
script: |
170+
from machine import Pin, I2C
171+
from time import sleep_ms, ticks_ms, ticks_diff
172+
led = Pin("LED_GREEN", Pin.OUT)
173+
i2c = I2C(1)
174+
bit = 4
175+
led.on()
176+
detected = False
177+
t0 = ticks_ms()
178+
while ticks_diff(ticks_ms(), t0) < 5000:
179+
gpio = i2c.readfrom_mem(0x20, 0x09, 1)[0]
180+
if not (gpio & (1 << bit)):
181+
detected = True
182+
break
183+
sleep_ms(20)
184+
led.off()
185+
result = detected
186+
expect_true: true
187+
mode: [hardware]
188+
189+
# --- Interrupt tests (GPIO direct buttons) ---
81190
# LED_GREEN signals when the board is ready; uses Pin.irq() to detect press.
82191

83192
- name: "Button A press (interrupt)"
@@ -89,7 +198,10 @@ tests:
89198
from time import sleep_ms, ticks_ms, ticks_diff
90199
led = Pin("LED_GREEN", Pin.OUT)
91200
btn = Pin("A_BUTTON", Pin.IN, Pin.PULL_UP)
201+
t_rel = ticks_ms()
92202
while btn.value() == 0:
203+
if ticks_diff(ticks_ms(), t_rel) > 3000:
204+
break
93205
sleep_ms(20)
94206
detected = False
95207
def on_press(pin):
@@ -117,7 +229,10 @@ tests:
117229
from time import sleep_ms, ticks_ms, ticks_diff
118230
led = Pin("LED_GREEN", Pin.OUT)
119231
btn = Pin("B_BUTTON", Pin.IN, Pin.PULL_UP)
232+
t_rel = ticks_ms()
120233
while btn.value() == 0:
234+
if ticks_diff(ticks_ms(), t_rel) > 3000:
235+
break
121236
sleep_ms(20)
122237
detected = False
123238
def on_press(pin):
@@ -145,7 +260,10 @@ tests:
145260
from time import sleep_ms, ticks_ms, ticks_diff
146261
led = Pin("LED_GREEN", Pin.OUT)
147262
btn = Pin("MENU_BUTTON", Pin.IN, Pin.PULL_UP)
263+
t_rel = ticks_ms()
148264
while btn.value() == 0:
265+
if ticks_diff(ticks_ms(), t_rel) > 3000:
266+
break
149267
sleep_ms(20)
150268
detected = False
151269
def on_press(pin):

0 commit comments

Comments
 (0)