-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.c
126 lines (98 loc) · 2.6 KB
/
main.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// ********************* Includes ********************* //
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include "hidapi.h"
#include "rift.h"
// ********************* Defines ********************* //
#define MAX_STR 255
// ********************* Types ********************* //
// ********************* Data ********************* //
// ********************* Prototypes ********************* //
// ********************* Implementation ********************* //
int msleep(long msec)
{
struct timespec ts;
int res;
if (msec < 0)
{
return -1;
}
ts.tv_sec = msec / 1000;
ts.tv_nsec = (msec % 1000) * 1000000;
do
{
res = nanosleep(&ts, &ts);
} while (res);
return res;
}
int kbhit()
{
#define STDIN_FILENO 0
struct timeval tv;
fd_set fds;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
return FD_ISSET(STDIN_FILENO, &fds);
}
int main(int argc, char* argv[])
{
int res;
wchar_t wstr[MAX_STR];
hid_device *handle;
wprintf(L"/// riftDriverPi V1.1 ///\n");
// Initialize hidapi library
hid_init();
// Open device (Oculus Rift CV1)
handle = hid_open(0x2833, 0x0031, NULL);
if( handle == NULL )
{
wprintf(L"hid_open() failed. Oculus Rift CV1 connected? udev rule setup?\n");
goto abort;
}
// Read the Manufacturer String
res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
if( res < 0 ) goto cleanup;
wprintf(L"Manufacturer : %S\n", wstr);
// Read the Product String
res = hid_get_product_string(handle, wstr, MAX_STR);
if( res < 0 ) goto cleanup;
wprintf(L"Product : %S\n", wstr);
// Read the Serial Number String
res = hid_get_serial_number_string(handle, wstr, MAX_STR);
if( res < 0 ) goto cleanup;
wprintf(L"Serial Number : %S\n", wstr);
// Set non-blocking
res = hid_set_nonblocking(handle, 1);
if( res < 0 ) goto cleanup;
wprintf(L"Configured non-blocking transfer\n");
// Turn the CV1 screens on
res = rift_send_enable_components(handle,1);
if( res < 0 ) goto cleanup;
wprintf(L"Screen turned ON\n");
// Send keep alive
while(!kbhit())
{
res = rift_send_keep_alive(handle);
if( res < 0 ) break;
msleep( 1000 );
}
// Turn the CV1 screen off
rift_send_enable_components(handle, 0);
if( res < 0 ) goto cleanup;
wprintf(L"Screen turned OFF\n");
cleanup:
// Close down connection
hid_close(handle);
// Finalize the hidapi library
abort:
hid_exit();
wprintf(L"/// Exit ///\n");
return 0;
}
// EOF