00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include <stdarg.h>
00031 #include <unistd.h>
00032
00033 #include "freenect_internal.h"
00034
00035 int freenect_init(freenect_context **ctx, freenect_usb_context *usb_ctx)
00036 {
00037 *ctx = malloc(sizeof(freenect_context));
00038 if (!ctx)
00039 return -1;
00040
00041 memset(*ctx, 0, sizeof(freenect_context));
00042
00043 (*ctx)->log_level = LL_WARNING;
00044 return fnusb_init(&(*ctx)->usb, usb_ctx);
00045 }
00046
00048 int
00049 freenect_shutdown (freenect_context *ctx)
00050 {
00051 fnusb_shutdown (&ctx->usb);
00052 return (0);
00053 }
00054
00055 int freenect_process_events(freenect_context *ctx)
00056 {
00057 return fnusb_process_events(&ctx->usb);
00058 }
00059
00063 int
00064 freenect_num_devices (freenect_context *ctx)
00065 {
00066 libusb_device **devs;
00067 ssize_t cnt = libusb_get_device_list (ctx->usb.ctx, &devs);
00068
00069 if (cnt < 0)
00070 return (-1);
00071
00072 int nr = 0, i = 0;
00073 struct libusb_device_descriptor desc;
00074 for (i = 0; i < cnt; ++i)
00075 {
00076 int r = libusb_get_device_descriptor (devs[i], &desc);
00077 if (r < 0)
00078 continue;
00079 if (desc.idVendor == MS_MAGIC_VENDOR && desc.idProduct == MS_MAGIC_CAMERA_PRODUCT)
00080 nr++;
00081 }
00082
00083 libusb_free_device_list (devs, 1);
00084
00085 return (nr);
00086 }
00087
00089 int
00090 freenect_open_device (freenect_context *ctx, freenect_device **dev, int index)
00091 {
00092 int res;
00093 freenect_device *pdev = malloc (sizeof (freenect_device));
00094 if (!pdev)
00095 return -1;
00096
00097 memset (pdev, 0, sizeof (*pdev));
00098
00099 pdev->parent = ctx;
00100
00101 res = fnusb_open_subdevices (pdev, index);
00102
00103 if (res < 0)
00104 {
00105 free (pdev);
00106 return (res);
00107 }
00108 else
00109 {
00110 *dev = pdev;
00111 return (0);
00112 }
00113 }
00114
00116 int
00117 freenect_close_device (freenect_device *dev)
00118 {
00119 libusb_close (dev->usb_cam.dev);
00120 return (0);
00121 }
00122
00123 void freenect_set_user(freenect_device *dev, void *user)
00124 {
00125 dev->user_data = user;
00126 }
00127
00128 void *freenect_get_user(freenect_device *dev)
00129 {
00130 return dev->user_data;
00131 }
00132
00133 void freenect_set_log_level(freenect_context *ctx, freenect_loglevel level)
00134 {
00135 ctx->log_level = level;
00136 }
00137
00138 void freenect_set_log_callback(freenect_context *ctx, freenect_log_cb cb)
00139 {
00140 ctx->log_cb = cb;
00141 }
00142
00143 void fn_log(freenect_context *ctx, freenect_loglevel level, const char *fmt, ...)
00144 {
00145 va_list ap;
00146
00147 if (level > ctx->log_level)
00148 return;
00149
00150 if (ctx->log_cb) {
00151 char msgbuf[1024];
00152
00153 va_start(ap, fmt);
00154 vsnprintf(msgbuf, 1024, fmt, ap);
00155 msgbuf[1023] = 0;
00156 va_end(ap);
00157
00158 ctx->log_cb(ctx, level, msgbuf);
00159 } else {
00160 va_start(ap, fmt);
00161 vfprintf(stderr, fmt, ap);
00162 va_end(ap);
00163 }
00164 }
00165
00166 freenect_raw_device_state* freenect_get_device_state(freenect_device *dev)
00167 {
00168 return &dev->raw_state;
00169 }
00170
00171 int freenect_update_device_state(freenect_device *dev)
00172 {
00173 freenect_context *ctx = dev->parent;
00174 uint8_t buf[10];
00175 uint16_t ux, uy, uz;
00176 int ret = fnusb_control(&dev->usb_motor, 0xC0, 0x32, 0x0, 0x0, buf, 10);
00177 if (ret != 10) {
00178 FN_ERROR("Error in accelerometer reading, libusb_control_transfer returned %d\n", ret);
00179 return ret < 0 ? ret : -1;
00180 }
00181
00182 ux = ((uint16_t)buf[2] << 8) | buf[3];
00183 uy = ((uint16_t)buf[4] << 8) | buf[5];
00184 uz = ((uint16_t)buf[6] << 8) | buf[7];
00185
00186 dev->raw_state.accelerometer_x = (int16_t)ux;
00187 dev->raw_state.accelerometer_y = (int16_t)uy;
00188 dev->raw_state.accelerometer_z = (int16_t)uz;
00189 dev->raw_state.tilt_angle = (int8_t)buf[8];
00190 dev->raw_state.tilt_status = buf[9];
00191
00192 return ret;
00193 }