-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpiBus.c
50 lines (39 loc) · 1.14 KB
/
SpiBus.c
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
#include <pthread.h>
#include <stdio.h>
#include <fcntl.h>
#include <poll.h>
#include "SpiBus.h"
#include "spi.h"
#include "Xpad.h"
static int fd = -1;
static int running = 1;
static XpadRumble_t rumble;
typedef struct {
XpadReport_Data_t (*supplier)(void);
void (*callback)(XpadRumble_t *);
} t_supplier_and_callback;
void *readWriteSpi(void *args) {
t_supplier_and_callback *holder = args;
spi_init(fd);
while (running) {
XpadReport_Data_t reportData = (holder->supplier)();
reportData.LENGTH = 0x14;
transfer(fd, (uint8_t *) &reportData, (uint8_t *) &rumble);
(holder->callback)(&rumble);
usleep(1500);
}
}
void startSpiThread(char *file, XpadReport_Data_t (*supplier)(void), void (*callback)(XpadRumble_t *)) {
pthread_t threadId;
fd = open(file, O_RDWR);
printf("\nOpening SPI: %s\n", file);
if (fd == -1) {
printf("SPI file not found or permission denied\n");
return;
}
t_supplier_and_callback arg;
arg.callback = callback;
arg.supplier = supplier;
pthread_create(&threadId, NULL, readWriteSpi, &arg);
pthread_join(threadId, NULL);
}