Skip to content

Commit c4505cc

Browse files
committed
Merge pull request #335 from OpenKinect/unstable
libfreenect v0.2.0
2 parents ec04a63 + 6631fa1 commit c4505cc

File tree

20 files changed

+450
-144
lines changed

20 files changed

+450
-144
lines changed

examples/CMakeLists.txt

100755100644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ else()
5656
find_package(OpenGL REQUIRED)
5757
find_package(GLUT REQUIRED)
5858

59-
include_directories(${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS} ${USB_INCLUDE_DIRS})
59+
include_directories(${OPENGL_INCLUDE_DIR} ${GLUT_INCLUDE_DIR} ${USB_INCLUDE_DIRS})
6060

6161
target_link_libraries(glview freenect ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${MATH_LIB})
6262
target_link_libraries(regview freenect ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${MATH_LIB})

examples/glview.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,27 @@ void keyPressed(unsigned char key, int x, int y)
179179
freenect_angle = -30;
180180
}
181181
}
182+
if (key == 'e') {
183+
static freenect_flag_value auto_exposure = FREENECT_ON;
184+
freenect_set_flag(f_dev, FREENECT_AUTO_EXPOSURE, auto_exposure);
185+
auto_exposure = !auto_exposure;
186+
}
187+
if (key == 'b') {
188+
static freenect_flag_value white_balance = FREENECT_ON;
189+
freenect_set_flag(f_dev, FREENECT_AUTO_WHITE_BALANCE, white_balance);
190+
white_balance = !white_balance;
191+
}
192+
if (key == 'r') {
193+
static freenect_flag_value raw_color = FREENECT_ON;
194+
freenect_set_flag(f_dev, FREENECT_RAW_COLOR, raw_color);
195+
raw_color = !raw_color;
196+
}
197+
if (key == 'm') {
198+
static freenect_flag_value mirror = FREENECT_ON;
199+
freenect_set_flag(f_dev, FREENECT_MIRROR_DEPTH, mirror);
200+
freenect_set_flag(f_dev, FREENECT_MIRROR_VIDEO, mirror);
201+
mirror = !mirror;
202+
}
182203
if (key == '1') {
183204
freenect_set_led(f_dev,LED_GREEN);
184205
}
@@ -349,6 +370,7 @@ void *freenect_threadfunc(void *arg)
349370
freenect_start_video(f_dev);
350371

351372
printf("'w'-tilt up, 's'-level, 'x'-tilt down, '0'-'6'-select LED mode, 'f'-video format\n");
373+
printf("'e' - auto exposure, 'b' - white balance, 'r' - raw color, 'm' - mirror\n");
352374

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

examples/wavrecord.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ int main(int argc, char** argv) {
9393
}
9494

9595
capture state;
96+
state.samples = 0;
9697
state.logfiles[0] = fopen("channel1.wav", "wb");
9798
state.logfiles[1] = fopen("channel2.wav", "wb");
9899
state.logfiles[2] = fopen("channel3.wav", "wb");

include/libfreenect.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,23 @@ typedef enum {
106106
FREENECT_DEPTH_DUMMY = 2147483647, /**< Dummy value to force enum to be 32 bits wide */
107107
} freenect_depth_format;
108108

109+
/// Enumeration of flags to toggle features with freenect_set_flag()
110+
typedef enum {
111+
// values written to the CMOS register
112+
FREENECT_AUTO_EXPOSURE = 1 << 14,
113+
FREENECT_AUTO_WHITE_BALANCE = 1 << 1,
114+
FREENECT_RAW_COLOR = 1 << 4,
115+
// registers to be written with 0 or 1
116+
FREENECT_MIRROR_DEPTH = 0x0017,
117+
FREENECT_MIRROR_VIDEO = 0x0047,
118+
} freenect_flag;
119+
120+
/// Possible values for setting each `freenect_flag`
121+
typedef enum {
122+
FREENECT_OFF = 0,
123+
FREENECT_ON = 1,
124+
} freenect_flag_value;
125+
109126
/// Structure to give information about the width, height, bitrate,
110127
/// framerate, and buffer size of a frame in a particular mode, as
111128
/// well as the total number of bytes needed to hold a single frame.
@@ -302,6 +319,15 @@ FREENECTAPI int freenect_supported_subdevices(void);
302319
*/
303320
FREENECTAPI void freenect_select_subdevices(freenect_context *ctx, freenect_device_flags subdevs);
304321

322+
/**
323+
* Returns the devices that are enabled after calls to freenect_open_device()
324+
* On newer kinects the motor and audio are automatically disabled for now
325+
*
326+
* @param ctx Context to set future subdevice selection for
327+
* @return Flags representing the subdevices that were actually opened (see freenect_device_flags)
328+
*/
329+
FREENECTAPI freenect_device_flags freenect_enabled_subdevices(freenect_context *ctx);
330+
305331
/**
306332
* Opens a kinect device via a context. Index specifies the index of
307333
* the device on the current state of the bus. Bus resets may cause
@@ -614,6 +640,16 @@ FREENECTAPI freenect_frame_mode freenect_find_depth_mode(freenect_resolution res
614640
*/
615641
FREENECTAPI int freenect_set_depth_mode(freenect_device* dev, const freenect_frame_mode mode);
616642

643+
/**
644+
* Enables or disables the specified flag.
645+
*
646+
* @param flag Feature to set
647+
* @param value `FREENECT_OFF` or `FREENECT_ON`
648+
*
649+
* @return 0 on success, < 0 if error
650+
*/
651+
FREENECTAPI int freenect_set_flag(freenect_device *dev, freenect_flag flag, freenect_flag_value value);
652+
617653
#ifdef __cplusplus
618654
}
619655
#endif

platform/windows/libusb10emu/libusb-1.0/libusb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ int libusb_get_string_descriptor(libusb_device_handle *dev_handle, uint8_t desc_
7777
int libusb_get_string_descriptor_ascii(libusb_device_handle *dev_handle, uint8_t desc_index, unsigned char *data, int length);
7878

7979
int libusb_set_configuration(libusb_device_handle *dev, int configuration);
80+
int libusb_set_interface_alt_setting(libusb_device_handle *dev,int interface_number,int alternate_setting);
8081
int libusb_claim_interface(libusb_device_handle* dev, int interface_number);
8182
int libusb_release_interface(libusb_device_handle* dev, int interface_number);
8283

platform/windows/libusb10emu/libusb-1.0/libusbemu.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,17 @@ int libusb_set_configuration(libusb_device_handle *dev, int configuration)
302302
return 0;
303303
}
304304

305+
int libusb_set_interface_alt_setting(libusb_device_handle *dev, int interface_number,int alternate_setting){
306+
RAIIMutex lock (dev->dev->ctx->mutex);
307+
if (0 != usb_set_altinterface(dev->handle, alternate_setting))
308+
{
309+
LIBUSBEMU_ERROR_LIBUSBWIN32();
310+
return(LIBUSB_ERROR_OTHER);
311+
}
312+
313+
return(0);
314+
}
315+
305316
int libusb_claim_interface(libusb_device_handle* dev, int interface_number)
306317
{
307318
RAIIMutex lock (dev->dev->ctx->mutex);

src/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ set(CMAKE_C_FLAGS "-Wall")
88

99
include_directories(${LIBUSB_1_INCLUDE_DIRS})
1010
IF(WIN32)
11-
LIST(APPEND SRC core.c tilt.c cameras.c usb_libusb10.c registration.c ../platform/windows/libusb10emu/libusb-1.0/libusbemu.cpp ../platform/windows/libusb10emu/libusb-1.0/failguard.cpp)
11+
LIST(APPEND SRC core.c tilt.c cameras.c flags.c usb_libusb10.c registration.c ../platform/windows/libusb10emu/libusb-1.0/libusbemu.cpp ../platform/windows/libusb10emu/libusb-1.0/failguard.cpp)
1212
set_source_files_properties(${SRC} PROPERTIES LANGUAGE CXX)
1313
ELSE(WIN32)
14-
LIST(APPEND SRC core.c tilt.c cameras.c usb_libusb10.c registration.c)
14+
LIST(APPEND SRC core.c tilt.c cameras.c flags.c usb_libusb10.c registration.c)
1515
ENDIF(WIN32)
1616

1717
IF(BUILD_AUDIO)

src/cameras.c

Lines changed: 4 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "freenect_internal.h"
3333
#include "registration.h"
3434
#include "cameras.h"
35+
#include "flags.h"
3536

3637
#define MAKE_RESERVED(res, fmt) (uint32_t)(((res & 0xff) << 8) | (((fmt & 0xff))))
3738
#define RESERVED_TO_RESOLUTION(reserved) (freenect_resolution)((reserved >> 8) & 0xff)
@@ -650,122 +651,6 @@ static void video_process(freenect_device *dev, uint8_t *pkt, int len)
650651
dev->video_cb(dev, dev->video.proc_buf, dev->video.timestamp);
651652
}
652653

653-
typedef struct {
654-
uint8_t magic[2];
655-
uint16_t len;
656-
uint16_t cmd;
657-
uint16_t tag;
658-
} cam_hdr;
659-
660-
static int send_cmd(freenect_device *dev, uint16_t cmd, void *cmdbuf, unsigned int cmd_len, void *replybuf, int reply_len)
661-
{
662-
freenect_context *ctx = dev->parent;
663-
int res, actual_len;
664-
uint8_t obuf[0x400];
665-
uint8_t ibuf[0x200];
666-
cam_hdr *chdr = (cam_hdr*)obuf;
667-
cam_hdr *rhdr = (cam_hdr*)ibuf;
668-
669-
if (cmd_len & 1 || cmd_len > (0x400 - sizeof(*chdr))) {
670-
FN_ERROR("send_cmd: Invalid command length (0x%x)\n", cmd_len);
671-
return -1;
672-
}
673-
674-
chdr->magic[0] = 0x47;
675-
chdr->magic[1] = 0x4d;
676-
chdr->cmd = fn_le16(cmd);
677-
chdr->tag = fn_le16(dev->cam_tag);
678-
chdr->len = fn_le16(cmd_len / 2);
679-
680-
memcpy(obuf+sizeof(*chdr), cmdbuf, cmd_len);
681-
682-
res = fnusb_control(&dev->usb_cam, 0x40, 0, 0, 0, obuf, cmd_len + sizeof(*chdr));
683-
FN_SPEW("Control cmd=%04x tag=%04x len=%04x: %d\n", cmd, dev->cam_tag, cmd_len, res);
684-
if (res < 0) {
685-
FN_ERROR("send_cmd: Output control transfer failed (%d)\n", res);
686-
return res;
687-
}
688-
689-
do {
690-
actual_len = fnusb_control(&dev->usb_cam, 0xc0, 0, 0, 0, ibuf, 0x200);
691-
FN_FLOOD("actual_len: %d\n", actual_len);
692-
} while ((actual_len == 0) || (actual_len == 0x200));
693-
FN_SPEW("Control reply: %d\n", res);
694-
if (actual_len < (int)sizeof(*rhdr)) {
695-
FN_ERROR("send_cmd: Input control transfer failed (%d)\n", res);
696-
return res;
697-
}
698-
actual_len -= sizeof(*rhdr);
699-
700-
if (rhdr->magic[0] != 0x52 || rhdr->magic[1] != 0x42) {
701-
FN_ERROR("send_cmd: Bad magic %02x %02x\n", rhdr->magic[0], rhdr->magic[1]);
702-
return -1;
703-
}
704-
if (rhdr->cmd != chdr->cmd) {
705-
FN_ERROR("send_cmd: Bad cmd %02x != %02x\n", rhdr->cmd, chdr->cmd);
706-
return -1;
707-
}
708-
if (rhdr->tag != chdr->tag) {
709-
FN_ERROR("send_cmd: Bad tag %04x != %04x\n", rhdr->tag, chdr->tag);
710-
return -1;
711-
}
712-
if (fn_le16(rhdr->len) != (actual_len/2)) {
713-
FN_ERROR("send_cmd: Bad len %04x != %04x\n", fn_le16(rhdr->len), (int)(actual_len/2));
714-
return -1;
715-
}
716-
717-
if (actual_len > reply_len) {
718-
FN_WARNING("send_cmd: Data buffer is %d bytes long, but got %d bytes\n", reply_len, actual_len);
719-
memcpy(replybuf, ibuf+sizeof(*rhdr), reply_len);
720-
} else {
721-
memcpy(replybuf, ibuf+sizeof(*rhdr), actual_len);
722-
}
723-
724-
dev->cam_tag++;
725-
726-
return actual_len;
727-
}
728-
729-
static int write_register(freenect_device *dev, uint16_t reg, uint16_t data)
730-
{
731-
freenect_context *ctx = dev->parent;
732-
uint16_t reply[2];
733-
uint16_t cmd[2];
734-
int res;
735-
736-
cmd[0] = fn_le16(reg);
737-
cmd[1] = fn_le16(data);
738-
739-
FN_DEBUG("Write Reg 0x%04x <= 0x%02x\n", reg, data);
740-
res = send_cmd(dev, 0x03, cmd, 4, reply, 4);
741-
if (res < 0)
742-
return res;
743-
if (res != 2) {
744-
FN_WARNING("send_cmd returned %d [%04x %04x], 0000 expected\n", res, reply[0], reply[1]);
745-
}
746-
return 0;
747-
}
748-
749-
// This function is here for completeness. We don't actually use it for anything right now.
750-
static uint16_t read_register(freenect_device *dev, uint16_t reg)
751-
{
752-
freenect_context *ctx = dev->parent;
753-
uint16_t reply[2];
754-
uint16_t cmd;
755-
int res;
756-
757-
cmd = fn_le16(reg);
758-
759-
FN_DEBUG("Read Reg 0x%04x =>\n", reg);
760-
res = send_cmd(dev, 0x02, &cmd, 2, reply, 4);
761-
if (res < 0)
762-
FN_ERROR("read_register: send_cmd() failed: %d\n", res);
763-
if (res != 4)
764-
FN_WARNING("send_cmd returned %d [%04x %04x], 0000 expected\n", res, reply[0], reply[1]);
765-
766-
return reply[1];
767-
}
768-
769654
static int freenect_fetch_reg_info(freenect_device *dev)
770655
{
771656
freenect_context *ctx = dev->parent;
@@ -904,9 +789,9 @@ static int freenect_fetch_zero_plane_info(freenect_device *dev)
904789
uint16_t cmd[5] = {0}; // Offset is the only field in this command, and it's 0
905790

906791
int res;
907-
res = send_cmd(dev, 0x04, cmd, 10, reply, 322); //OPCODE_GET_FIXED_PARAMS = 4,
908-
if (res != 322) {
909-
FN_ERROR("freenect_fetch_zero_plane_info: send_cmd read %d bytes (expected 322)\n", res);
792+
res = send_cmd(dev, 0x04, cmd, 10, reply, ctx->zero_plane_res); //OPCODE_GET_FIXED_PARAMS = 4,
793+
if (res != ctx->zero_plane_res) {
794+
FN_ERROR("freenect_fetch_zero_plane_info: send_cmd read %d bytes (expected %d)\n", res,ctx->zero_plane_res);
910795
return -1;
911796
}
912797

src/core.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ FREENECTAPI void freenect_select_subdevices(freenect_context *ctx, freenect_devi
148148
));
149149
}
150150

151+
FREENECTAPI freenect_device_flags freenect_enabled_subdevices(freenect_context *ctx) {
152+
return ctx->enabled_subdevices;
153+
}
154+
151155
FREENECTAPI int freenect_open_device(freenect_context *ctx, freenect_device **dev, int index)
152156
{
153157
int res;

0 commit comments

Comments
 (0)