Skip to content

Commit

Permalink
Merge pull request #5231 from openframeworks/bugfix-ofxkinect
Browse files Browse the repository at this point in the history
Bugfix ofxKinect / latest libfreenect
  • Loading branch information
ofTheo committed Aug 26, 2016
2 parents 79ac0d6 + 6a00143 commit 3ae20b6
Show file tree
Hide file tree
Showing 19 changed files with 852 additions and 823 deletions.
31 changes: 25 additions & 6 deletions addons/ofxKinect/libs/libfreenect/include/libfreenect.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#ifdef _WIN32
#include <winsock.h>
#else
#define BUILD_AUDIO
#include <sys/time.h>
#endif

Expand Down Expand Up @@ -65,10 +64,10 @@ typedef enum {
/// A struct used in enumeration to give access to serial numbers, so you can
/// open a particular device by serial rather than depending on index. This
/// is most useful if you have more than one Kinect.
struct freenect_device_attributes;
struct freenect_device_attributes {
struct freenect_device_attributes *next; /**< Next device in the linked list */
const char* camera_serial; /**< Serial number of this device's camera subdevice */
struct freenect_device_attributes
{
struct freenect_device_attributes *next; // Next device in the linked list
const char* camera_serial; // Serial number of camera or audio subdevice
};

/// Enumeration of available resolutions.
Expand Down Expand Up @@ -116,6 +115,7 @@ typedef enum {
// arbitrary bitfields to support flag combination
FREENECT_MIRROR_DEPTH = 1 << 16,
FREENECT_MIRROR_VIDEO = 1 << 17,
FREENECT_NEAR_MODE = 1 << 18, // K4W only
} freenect_flag;

/// Possible values for setting each `freenect_flag`
Expand Down Expand Up @@ -670,6 +670,25 @@ FREENECTAPI int freenect_set_depth_mode(freenect_device* dev, const freenect_fra
*/
FREENECTAPI int freenect_set_flag(freenect_device *dev, freenect_flag flag, freenect_flag_value value);

/**
* Returns the brightness of the IR sensor.
*
* @param dev Device to get IR brightness
*
* @return IR brightness value in the range 1 - 50, < 0 if error
*/
FREENECTAPI int freenect_get_ir_brightness(freenect_device *dev);

/**
* Sets the brightness of the IR sensor.
* Note that this does not change the intensity of the IR projector.
*
* @param dev Device to set IR brightness
* @param brighness Brightness value in range 1 - 50
*
* @return 0 on success, < 0 if error
*/
FREENECTAPI int freenect_set_ir_brightness(freenect_device *dev, uint16_t brightness);

/**
* Allows the user to specify a pointer to the audio firmware in memory for the Xbox 360 Kinect
Expand All @@ -692,4 +711,4 @@ FREENECTAPI void freenect_set_fw_address_k4w(freenect_context * ctx, unsigned ch

#ifdef __cplusplus
}
#endif
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ FREENECTAPI int freenect_destroy_registration(freenect_registration* reg);
FREENECTAPI void freenect_camera_to_world(freenect_device* dev,
int cx, int cy, int wz, double* wx, double* wy);

// helper function to map one FREENECT_VIDEO_RGB image to a FREENECT_DEPTH_MM
// image (inverse mapping to FREENECT_DEPTH_REGISTERED, which is depth -> RGB)
FREENECTAPI void freenect_map_rgb_to_depth( freenect_device* dev,
uint16_t* depth_mm, uint8_t* rgb_raw, uint8_t* rgb_registered );

#ifdef __cplusplus
}
#endif
30 changes: 0 additions & 30 deletions addons/ofxKinect/libs/libfreenect/platform/windows/stdbool.h

This file was deleted.

24 changes: 19 additions & 5 deletions addons/ofxKinect/libs/libfreenect/platform/windows/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,29 @@
#pragma once

#include <stdint.h>
#include <windows.h>

// MinGW defines _SSIZE_T_ in sys/types.h when it defines ssize_t to be a long.
// MinGW defines _SSIZE_T_DEFINED in sys/types.h when it defines ssize_t to be a long.
// Redefining it causes an error.
// MSVC does not define this.
#ifndef _SSIZE_T_
#define _SSIZE_T_
#ifndef _SSIZE_T_DEFINED
#define _SSIZE_T_DEFINED
#ifdef _WIN64
typedef __int64 ssize_t;
#else
typedef long ssize_t;
typedef int ssize_t;
#endif
#endif // _SSIZE_T_
#endif // _SSIZE_T_DEFINED


static void usleep(__int64 usec)
{
// Convert to 100 nanosecond interval, negative for relative time.
LARGE_INTEGER ft;
ft.QuadPart = -(10 * usec);

HANDLE timer = CreateWaitableTimer(NULL, TRUE, NULL);
SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
WaitForSingleObject(timer, INFINITE);
CloseHandle(timer);
}
3 changes: 0 additions & 3 deletions addons/ofxKinect/libs/libfreenect/src/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <string.h>
#include <stdlib.h>

#ifdef BUILD_AUDIO

static void prepare_iso_out_data(freenect_device* dev, uint8_t* buffer) {
audio_stream* stream = &dev->audio;
Expand Down Expand Up @@ -239,5 +238,3 @@ int freenect_stop_audio(freenect_device* dev) {

return ret;
}

#endif
16 changes: 12 additions & 4 deletions addons/ofxKinect/libs/libfreenect/src/cameras.c
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,6 @@ static int freenect_fetch_zero_plane_info(freenect_device *dev)
int freenect_start_depth(freenect_device *dev)
{
freenect_context *ctx = dev->parent;
int res;

if (dev->depth.running)
return -1;
Expand Down Expand Up @@ -869,7 +868,12 @@ int freenect_start_depth(freenect_device *dev)
return -1;
}

res = fnusb_start_iso(&dev->usb_cam, &dev->depth_isoc, depth_process, 0x82, NUM_XFERS, PKTS_PER_XFER, DEPTH_PKTBUF);
const unsigned char depth_endpoint = 0x82;
int packet_size = fnusb_get_max_iso_packet_size(&dev->usb_cam, depth_endpoint, DEPTH_PKTBUF);

FN_INFO("[Stream 70] Negotiated packet size %d\n", packet_size);

int res = fnusb_start_iso(&dev->usb_cam, &dev->depth_isoc, depth_process, depth_endpoint, NUM_XFERS, PKTS_PER_XFER, packet_size);
if (res < 0)
return res;

Expand Down Expand Up @@ -901,7 +905,6 @@ int freenect_start_depth(freenect_device *dev)
int freenect_start_video(freenect_device *dev)
{
freenect_context *ctx = dev->parent;
int res;

if (dev->video.running)
return -1;
Expand Down Expand Up @@ -1015,7 +1018,12 @@ int freenect_start_video(freenect_device *dev)
break;
}

res = fnusb_start_iso(&dev->usb_cam, &dev->video_isoc, video_process, 0x81, NUM_XFERS, PKTS_PER_XFER, VIDEO_PKTBUF);
const unsigned char video_endpoint = 0x81;
int packet_size = fnusb_get_max_iso_packet_size(&dev->usb_cam, video_endpoint, VIDEO_PKTBUF);

FN_INFO("[Stream 80] Negotiated packet size %d\n", packet_size);

int res = fnusb_start_iso(&dev->usb_cam, &dev->video_isoc, video_process, video_endpoint, NUM_XFERS, PKTS_PER_XFER, packet_size);
if (res < 0)
return res;

Expand Down
32 changes: 10 additions & 22 deletions addons/ofxKinect/libs/libfreenect/src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@
#include "freenect_internal.h"
#include "registration.h"
#include "cameras.h"
#ifdef BUILD_AUDIO
#include "loader.h"
#endif


FREENECTAPI int freenect_init(freenect_context **ctx, freenect_usb_context *usb_ctx)
{
Expand All @@ -49,11 +48,7 @@ FREENECTAPI int freenect_init(freenect_context **ctx, freenect_usb_context *usb_
memset(*ctx, 0, sizeof(freenect_context));

(*ctx)->log_level = LL_WARNING;
(*ctx)->enabled_subdevices = (freenect_device_flags)(FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA
#ifdef BUILD_AUDIO
| FREENECT_DEVICE_AUDIO
#endif
);
(*ctx)->enabled_subdevices = (freenect_device_flags)(FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA);
res = fnusb_init(&(*ctx)->usb, usb_ctx);
if (res < 0) {
free(*ctx);
Expand Down Expand Up @@ -94,13 +89,11 @@ FREENECTAPI int freenect_process_events_timeout(freenect_context *ctx, struct ti
freenect_stop_video(dev);
freenect_stop_depth(dev);
}
#ifdef BUILD_AUDIO
if (dev->usb_audio.device_dead) {
FN_ERROR("USB audio marked dead, stopping streams\n");
res = -1; // Or something else to tell the user that the device just vanished.
freenect_stop_audio(dev);
}
#endif
dev = dev->next;
}
return res;
Expand Down Expand Up @@ -132,23 +125,18 @@ FREENECTAPI void freenect_free_device_attributes(struct freenect_device_attribut
return;
}

FREENECTAPI int freenect_supported_subdevices(void) {
#ifdef BUILD_AUDIO
FREENECTAPI int freenect_supported_subdevices(void)
{
return FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA | FREENECT_DEVICE_AUDIO;
#else
return FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA;
#endif
}

FREENECTAPI void freenect_select_subdevices(freenect_context *ctx, freenect_device_flags subdevs) {
ctx->enabled_subdevices = (freenect_device_flags)(subdevs & (FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA
#ifdef BUILD_AUDIO
| FREENECT_DEVICE_AUDIO
#endif
));
FREENECTAPI void freenect_select_subdevices(freenect_context *ctx, freenect_device_flags subdevs)
{
ctx->enabled_subdevices = (freenect_device_flags)(subdevs & (FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA | FREENECT_DEVICE_AUDIO));
}

FREENECTAPI freenect_device_flags freenect_enabled_subdevices(freenect_context *ctx) {
FREENECTAPI freenect_device_flags freenect_enabled_subdevices(freenect_context *ctx)
{
return ctx->enabled_subdevices;
}

Expand Down Expand Up @@ -200,7 +188,7 @@ FREENECTAPI int freenect_open_device_by_camera_serial(freenect_context *ctx, fre
int count = fnusb_list_device_attributes(&ctx->usb, &attrlist);
if (count < 0) {
FN_ERROR("freenect_open_device_by_camera_serial: Couldn't enumerate serial numbers\n");
return -1;
return count;
}
int index = 0;
for(item = attrlist ; item != NULL; item = item->next , index++) {
Expand Down
68 changes: 65 additions & 3 deletions addons/ofxKinect/libs/libfreenect/src/flags.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@
*/

#include <string.h> // for memcpy
#include <unistd.h> // for usleep
#include "freenect_internal.h"
#include "flags.h"


// freenect_set_flag is the only function exposed in libfreenect.h
// The rest are available internally via #include flags.h

FN_INTERNAL int register_for_flag(int flag)
{
switch(flag)
Expand All @@ -47,6 +45,34 @@ FN_INTERNAL int register_for_flag(int flag)

int freenect_set_flag(freenect_device *dev, freenect_flag flag, freenect_flag_value value)
{
freenect_context *ctx = dev->parent;

if (flag == FREENECT_NEAR_MODE)
{
if (dev->usb_cam.PID != PID_K4W_CAMERA)
{
FN_WARNING("Near mode is only supported by K4W");
return -1;
}

if (value == FREENECT_ON)
{
int ret = write_register(dev, 0x0015, 0x0007);
if (ret < 0)
return ret;
usleep(100000);
return write_register(dev, 0x02EF, 0x0000);
}
else
{
int ret = write_register(dev, 0x0015, 0x001E);
if (ret < 0)
return ret;
usleep(100000);
return write_register(dev, 0x02EF, 0x0190);
}
}

if (flag >= (1 << 16))
{
int reg = register_for_flag(flag);
Expand All @@ -67,6 +93,42 @@ int freenect_set_flag(freenect_device *dev, freenect_flag flag, freenect_flag_va
return write_cmos_register(dev, 0x0106, cmos_value);
}

int freenect_get_ir_brightness(freenect_device *dev)
{
freenect_context *ctx = dev->parent;

const uint16_t brightness = read_register(dev, 0x15);
if (brightness == UINT16_MAX)
{
FN_WARNING("Failed to get IR brightness!");
return -1;
}

return brightness;
}

int freenect_set_ir_brightness(freenect_device *dev, uint16_t brightness)
{
freenect_context *ctx = dev->parent;

if (brightness < 1)
{
brightness = 1;
}
if (brightness > 50)
{
brightness = 50;
}

const int ret = write_register(dev, 0x15, brightness);
if (ret < 0)
{
FN_WARNING("Failed to set IR brightness");
}

return ret;
}

typedef struct {
uint8_t magic[2];
uint16_t len;
Expand Down
Loading

0 comments on commit 3ae20b6

Please sign in to comment.