Skip to content

Commit 1ddb0fa

Browse files
committed
camera-flags: initial commit
Signed-off-by: Benn Snyder <[email protected]>
1 parent 89ccc40 commit 1ddb0fa

File tree

6 files changed

+271
-119
lines changed

6 files changed

+271
-119
lines changed

examples/glview.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,21 @@ 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+
}
182197
if (key == '1') {
183198
freenect_set_led(f_dev,LED_GREEN);
184199
}
@@ -348,7 +363,7 @@ void *freenect_threadfunc(void *arg)
348363
freenect_start_depth(f_dev);
349364
freenect_start_video(f_dev);
350365

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

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

include/libfreenect.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ 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+
FREENECT_AUTO_EXPOSURE = 1 << 14,
112+
FREENECT_AUTO_WHITE_BALANCE = 1 << 1,
113+
FREENECT_RAW_COLOR = 1 << 4,
114+
} freenect_flag;
115+
116+
/// Possible values for setting each `freenect_flag`
117+
typedef enum {
118+
FREENECT_ON = 1,
119+
FREENECT_OFF = 0,
120+
} freenect_flag_value;
121+
109122
/// Structure to give information about the width, height, bitrate,
110123
/// framerate, and buffer size of a frame in a particular mode, as
111124
/// well as the total number of bytes needed to hold a single frame.
@@ -614,6 +627,16 @@ FREENECTAPI freenect_frame_mode freenect_find_depth_mode(freenect_resolution res
614627
*/
615628
FREENECTAPI int freenect_set_depth_mode(freenect_device* dev, const freenect_frame_mode mode);
616629

630+
/**
631+
* Enables or disables the specified flag.
632+
*
633+
* @param flag Feature to set
634+
* @param value `FREENECT_ON` or `FREENECT_OFF`
635+
*
636+
* @return 0 on success, < 0 if error
637+
*/
638+
FREENECTAPI int freenect_set_flag(freenect_device *dev, freenect_flag flag, freenect_flag_value value);
639+
617640
#ifdef __cplusplus
618641
}
619642
#endif

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: 1 addition & 116 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;

0 commit comments

Comments
 (0)