nx_input_gpio_test.nx source
↩ module page · 54 lines · 2170 B
1// nx_input_gpio_test.nx -- smoke for nx_input_gpio.
2
3import "nx_syscalls.nx"
4import "nx_input_gpio.nx"
5
6func main() -> i64 {
7 if NX_BTN_N_BUTTONS != 6 { return 1 }
8 if NX_BE_N_EDGES != 4 { return 2 }
9 if nx_btn_is_valid(NX_BTN_UP) != 1 { return 3 }
10 if nx_btn_is_valid(NX_BTN_ACTION_B) != 1 { return 4 }
11 if nx_btn_is_valid(6) != 0 { return 5 }
12 if nx_be_edge_is_valid(NX_BE_PRESSED) != 1 { return 6 }
13 if nx_be_edge_is_valid(4) != 0 { return 7 }
14
15 // 10ms debounce, 500ms held threshold
16 let g: *NxInputGpio = nx_input_gpio_new(10000, 500000)
17 if g.debounce_us != 10000 { return 8 }
18
19 // Initial state: not pressed
20 if nx_input_gpio_is_pressed(g, NX_BTN_UP) != 0 { return 9 }
21
22 // First poll raw=1 at t=1000 -- since_last_change=1000 < 10000us debounce -> NONE
23 let v0: nx_int = nx_input_gpio_poll(g, NX_BTN_UP, 1, 1000)
24 if v0 != NX_BE_NONE { return 10 }
25 if nx_input_gpio_is_pressed(g, NX_BTN_UP) != 0 { return 11 }
26
27 // Poll at t=15000 (past 10ms debounce) -> PRESSED commits
28 let v1: nx_int = nx_input_gpio_poll(g, NX_BTN_UP, 1, 15000)
29 if v1 != NX_BE_PRESSED { return 12 }
30 if nx_input_gpio_is_pressed(g, NX_BTN_UP) != 1 { return 13 }
31
32 // After held_threshold_us (500_000): HELD
33 let v3: nx_int = nx_input_gpio_poll(g, NX_BTN_UP, 1, 600000)
34 if v3 != NX_BE_HELD { return 14 }
35
36 // Release: raw=0 commits after debounce
37 let v4: nx_int = nx_input_gpio_poll(g, NX_BTN_UP, 0, 700000)
38 if v4 != NX_BE_RELEASED { return 15 }
39 if nx_input_gpio_is_pressed(g, NX_BTN_UP) != 0 { return 16 }
40 if nx_input_gpio_held_us(g, NX_BTN_UP, 800000) != 0 { return 17 } // not pressed
41
42 // Bad button
43 if nx_input_gpio_poll(g, 99, 1, 1000) != NX_GPIO_ERR_BAD_BTN { return 18 }
44 if nx_input_gpio_is_pressed(g, 99) != 0 { return 19 }
45
46 // Different buttons independent
47 nx_input_gpio_poll(g, NX_BTN_LEFT, 1, 800000)
48 nx_input_gpio_poll(g, NX_BTN_RIGHT, 1, 800000)
49 if nx_input_gpio_is_pressed(g, NX_BTN_LEFT) != 1 { return 20 }
50 if nx_input_gpio_is_pressed(g, NX_BTN_RIGHT) != 1 { return 21 }
51 if nx_input_gpio_is_pressed(g, NX_BTN_UP) != 0 { return 22 }
52
53 return 0
54}