-
Notifications
You must be signed in to change notification settings - Fork 1
/
reader.c
76 lines (63 loc) · 2.03 KB
/
reader.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/joystick.h>
#include <stdint.h>
#include "joy_driver.h"
#include <errno.h>
/*
reader.c uses the functionality from linux/joystick.h to read data from
joystick input, convert it to a string and write it into a file,
from where caller.c will be able to read it.
*/
int main() {
char* deviceRoute = "/dev/input/js2";
int js = open(deviceRoute, O_RDONLY);
if (js == -1)
{
perror("Couldn't open joystick\n");
return -1;
}
// Infinite loop to read inputs and write them to joyInputs.txt
while(true){
usleep(10000);
struct js_event event;
int bytesRead = read(js, &event, sizeof(event));
if (bytesRead <= 0) return -1;
if(event.type == JS_EVENT_INIT){
printf("Initialized. Event type: %d\n", event.type);
}
if (event.type == JS_EVENT_BUTTON ) {
printf("Button %d's value is: %d\n", event.number, event.value);
FILE *fp = fopen("joyInputs.txt", "w+");
if(fp == NULL){
perror("Error opening files \n");
}
char str1[10], str2[10],str3[10];
sprintf(str1,"%d", event.number);
sprintf(str2,"%d",event.value);
sprintf(str3,"%d",event.type);
fprintf(fp, "%s %s %s", str1, str2, str3);
fclose(fp);
}
if (event.type == JS_EVENT_AXIS ) {
printf("Axis %d's value is: %d\n", event.number, event.value);
FILE *fp = fopen("joyInputs.txt", "w+");
if(fp == NULL){
perror("Error opening files \n");
}
char str1[10], str2[10],str3[10];
sprintf(str1,"%d", event.number);
sprintf(str2,"%d",event.value);
sprintf(str3,"%d",event.type);
fprintf(fp, "%s %s %s", str1, str2, str3);
fclose(fp);
}
}
close(js);
printf("--- |\nend |\n--- |\n");
return 0;
}