-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffb_handshake.pio
47 lines (35 loc) · 1.31 KB
/
ffb_handshake.pio
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
; SET pin 0 should be mapped to your LED GPIO
.program ffb_handshake
set y, 6 ; 7 groups of pulses in handshake;
; set y=6 to loop 7 times
main_loop:
pull ; Pull delay and pulse count into OSR
out x, 16 ; x = Delay between pulses, in 0.1 ms units
wait_between_pulses:
nop [8]
jmp x-- wait_between_pulses
; Send trigger pulses. 1 cycle = 10 us; 1 ms = 100 cycles
out x, 16 ; x = Pulse count
send_pulse:
set pins, 1 [1] ; 20 ms; adapt-ffb joy uses 50 ms,
; but 20 is more reliable
set pins, 0 [16] ; 180 ms including the jmp below
jmp x-- send_pulse
jmp y-- main_loop
% c-sdk {
#include "hardware/clocks.h"
// Set up the FFB handshake program. Uses only the trigger pin.
void ffb_handshake_program_init(
PIO pio, uint sm, uint offset, uint freq, uint pin_trigger
)
{
pio_sm_config c = ffb_handshake_program_get_default_config(offset);
sm_config_set_set_pins(&c, pin_trigger, 1);
// Connect PIO to the trigger pin, and set its direction
pio_gpio_init(pio, pin_trigger);
pio_sm_set_consecutive_pindirs(pio, sm, pin_trigger, 1, true);
float div = clock_get_hz(clk_sys) / freq;
sm_config_set_clkdiv(&c, div);
pio_sm_init(pio, sm, offset, &c);
}
%}