Skip to content

Commit 75ec2a6

Browse files
committed
Add support for near mode (K4W only)
Signed-off-by: Benn Snyder <[email protected]>
1 parent 846b6bf commit 75ec2a6

File tree

9 files changed

+66
-14
lines changed

9 files changed

+66
-14
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ include (SetupDirectories)
4848

4949
set (PROJECT_VER_MAJOR 0)
5050
set (PROJECT_VER_MINOR 5)
51-
set (PROJECT_VER_PATCH 0)
51+
set (PROJECT_VER_PATCH 1)
5252
set (PROJECT_VER
5353
"${PROJECT_VER_MAJOR}.${PROJECT_VER_MINOR}.${PROJECT_VER_PATCH}")
5454
set (PROJECT_APIVER

examples/glview.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ void keyPressed(unsigned char key, int x, int y)
231231
freenect_set_flag(f_dev, FREENECT_MIRROR_VIDEO, mirror);
232232
mirror = mirror ? FREENECT_OFF : FREENECT_ON;
233233
}
234+
if (key == 'n') {
235+
static freenect_flag_value near = FREENECT_ON;
236+
freenect_set_flag(f_dev, FREENECT_NEAR_MODE, near);
237+
near = near ? FREENECT_OFF : FREENECT_ON;
238+
}
234239
if (key == '1') {
235240
freenect_set_led(f_dev,LED_GREEN);
236241
}
@@ -416,7 +421,7 @@ void *freenect_threadfunc(void *arg)
416421

417422
printf("'w' - tilt up, 's' - level, 'x' - tilt down, '0'-'6' - select LED mode \n");
418423
printf("'f' - change video format, 'm' - mirror video, 'o' - rotate video with accelerometer \n");
419-
printf("'e' - auto exposure, 'b' - white balance, 'r' - raw color \n");
424+
printf("'e' - auto exposure, 'b' - white balance, 'r' - raw color, 'n' - near mode (K4W only) \n");
420425

421426
while (!die && freenect_process_events(f_ctx) >= 0) {
422427
//Throttle the text output

include/libfreenect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ typedef enum {
115115
// arbitrary bitfields to support flag combination
116116
FREENECT_MIRROR_DEPTH = 1 << 16,
117117
FREENECT_MIRROR_VIDEO = 1 << 17,
118+
FREENECT_NEAR_MODE = 1 << 18, // K4W only
118119
} freenect_flag;
119120

120121
/// Possible values for setting each `freenect_flag`

platform/windows/unistd.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#pragma once
2828

2929
#include <stdint.h>
30+
#include <windows.h>
3031

3132
// MinGW defines _SSIZE_T_DEFINED in sys/types.h when it defines ssize_t to be a long.
3233
// Redefining it causes an error.
@@ -35,3 +36,16 @@
3536
#define _SSIZE_T_DEFINED
3637
typedef long ssize_t;
3738
#endif // _SSIZE_T_DEFINED
39+
40+
41+
void usleep(__int64 usec)
42+
{
43+
// Convert to 100 nanosecond interval, negative for relative time.
44+
LARGE_INTEGER ft;
45+
ft.QuadPart = -(10 * usec);
46+
47+
HANDLE timer = CreateWaitableTimer(NULL, TRUE, NULL);
48+
SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
49+
WaitForSingleObject(timer, INFINITE);
50+
CloseHandle(timer);
51+
}

src/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ FREENECTAPI int freenect_open_device_by_camera_serial(freenect_context *ctx, fre
188188
int count = fnusb_list_device_attributes(&ctx->usb, &attrlist);
189189
if (count < 0) {
190190
FN_ERROR("freenect_open_device_by_camera_serial: Couldn't enumerate serial numbers\n");
191-
return -1;
191+
return count;
192192
}
193193
int index = 0;
194194
for(item = attrlist ; item != NULL; item = item->next , index++) {

src/flags.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include <string.h> // for memcpy
28+
#include <unistd.h> // for usleep
2829
#include "freenect_internal.h"
2930
#include "flags.h"
3031

@@ -47,6 +48,34 @@ FN_INTERNAL int register_for_flag(int flag)
4748

4849
int freenect_set_flag(freenect_device *dev, freenect_flag flag, freenect_flag_value value)
4950
{
51+
freenect_context *ctx = dev->parent;
52+
53+
if (flag == FREENECT_NEAR_MODE)
54+
{
55+
if (dev->usb_cam.PID != PID_K4W_CAMERA)
56+
{
57+
FN_WARNING("Near mode is only supported by K4W");
58+
return -1;
59+
}
60+
61+
if (value == FREENECT_ON)
62+
{
63+
int ret = write_register(dev, 0x0015, 0x0007);
64+
if (ret < 0)
65+
return ret;
66+
usleep(100000);
67+
return write_register(dev, 0x02EF, 0x0000);
68+
}
69+
else
70+
{
71+
int ret = write_register(dev, 0x0015, 0x001E);
72+
if (ret < 0)
73+
return ret;
74+
usleep(100000);
75+
return write_register(dev, 0x02EF, 0x0190);
76+
}
77+
}
78+
5079
if (flag >= (1 << 16))
5180
{
5281
int reg = register_for_flag(flag);

src/freenect_internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,6 @@ struct _freenect_device {
253253
fnusb_dev usb_motor;
254254
freenect_raw_tilt_state raw_state;
255255

256-
int device_does_motor_control_with_audio;
257-
int motor_control_with_audio_enabled;
256+
int device_does_motor_control_with_audio;
257+
int motor_control_with_audio_enabled;
258258
};

src/usb_libusb10.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
291291
// If the index given by the user matches our camera index
292292
if (nr_cam == index)
293293
{
294+
dev->usb_cam.VID = desc.idVendor;
295+
dev->usb_cam.PID = desc.idProduct;
296+
294297
res = libusb_open (devs[i], &dev->usb_cam.dev);
295298
if (res < 0 || !dev->usb_cam.dev)
296299
{
@@ -416,6 +419,9 @@ FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
416419
// If the index given by the user matches our camera index
417420
if (nr_mot == index)
418421
{
422+
dev->usb_motor.VID = desc.idVendor;
423+
dev->usb_motor.PID = desc.idProduct;
424+
419425
res = libusb_open (devs[i], &dev->usb_motor.dev);
420426
if (res < 0 || !dev->usb_motor.dev)
421427
{
@@ -444,6 +450,9 @@ FN_INTERNAL int fnusb_open_subdevices(freenect_device *dev, int index)
444450
// If the index given by the user matches our audio index
445451
if (nr_audio == index)
446452
{
453+
dev->usb_audio.VID = desc.idVendor;
454+
dev->usb_audio.PID = desc.idProduct;
455+
447456
res = libusb_open (devs[i], &dev->usb_audio.dev);
448457
if (res < 0 || !dev->usb_audio.dev)
449458
{

src/usb_libusb10.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,7 @@
3131

3232
#if defined(__APPLE__)
3333
/*
34-
From Github Issue 22 by Roefer -
35-
https://github.com/OpenKinect/libfreenect/issues/#issue/22
36-
37-
The current implementation still does not reach 30 Hz on MacOS. This
38-
is due to bad scheduling of USB transfers in libusb (Ed Note: libusb
39-
1.0.8). A fix can be found at
40-
http://www.informatik.uni-bremen.de/~roefer/libusb/libusb-osx-kinect.diff
41-
42-
(Ed Note: patch applies to libusb repo at 7da756e09fd)
34+
https://github.com/OpenKinect/libfreenect/issues/22 (@Roefer)
4335
4436
In camera.c, I use PKTS_PER_XFER = 128, NUM_XFERS = 4. There are a
4537
few rules: PKTS_PER_XFER * NUM_XFERS <= 1000, PKTS_PER_XFER % 8 == 0.
@@ -69,6 +61,8 @@ typedef struct {
6961
freenect_device *parent; //so we can go up from the libusb userdata
7062
libusb_device_handle *dev;
7163
int device_dead; // set to 1 when the underlying libusb_device_handle vanishes (ie, Kinect was unplugged)
64+
int VID;
65+
int PID;
7266
} fnusb_dev;
7367

7468
typedef struct {

0 commit comments

Comments
 (0)