|
| 1 | +/* |
| 2 | + * This file is part of the OpenKinect Project. http://www.openkinect.org |
| 3 | + * |
| 4 | + * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file |
| 5 | + * for details. |
| 6 | + * |
| 7 | + * This code is licensed to you under the terms of the Apache License, version |
| 8 | + * 2.0, or, at your option, the terms of the GNU General Public License, |
| 9 | + * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, |
| 10 | + * or the following URLs: |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * http://www.gnu.org/licenses/gpl-2.0.txt |
| 13 | + * |
| 14 | + * If you redistribute this file in source form, modified or unmodified, you |
| 15 | + * may: |
| 16 | + * 1) Leave this header intact and distribute it under the same terms, |
| 17 | + * accompanying it with the APACHE20 and GPL20 files, or |
| 18 | + * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or |
| 19 | + * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file |
| 20 | + * In all cases you must keep the copyright notice intact and include a copy |
| 21 | + * of the CONTRIB file. |
| 22 | + * |
| 23 | + * Binary distributions must follow the binary distribution requirements of |
| 24 | + * either License. |
| 25 | + */ |
| 26 | + |
| 27 | +#pragma once |
| 28 | + |
| 29 | +#include <libfreenect.h> |
| 30 | +#include <stdexcept> |
| 31 | +#include <map> |
| 32 | +#include <pthread.h> |
| 33 | + |
| 34 | +namespace Freenect { |
| 35 | + class Noncopyable { |
| 36 | + public: |
| 37 | + Noncopyable() {} |
| 38 | + ~Noncopyable() {} |
| 39 | + private: |
| 40 | + Noncopyable( const Noncopyable& ); |
| 41 | + const Noncopyable& operator=( const Noncopyable& ); |
| 42 | + }; |
| 43 | + |
| 44 | + class FreenectDevice : Noncopyable { |
| 45 | + public: |
| 46 | + FreenectDevice(freenect_context *_ctx, int _index) { |
| 47 | + if(freenect_open_device(_ctx, &m_dev, _index) != 0) throw std::runtime_error("Cannot open Kinect"); |
| 48 | + freenect_set_user(m_dev, this); |
| 49 | + freenect_set_rgb_format(m_dev, FREENECT_FORMAT_RGB); |
| 50 | + freenect_set_depth_format(m_dev, FREENECT_FORMAT_11_BIT); |
| 51 | + freenect_set_depth_callback(m_dev, freenect_depth_callback); |
| 52 | + freenect_set_rgb_callback(m_dev, freenect_rgb_callback); |
| 53 | + } |
| 54 | + ~FreenectDevice() { |
| 55 | + if(freenect_close_device(m_dev) != 0) throw std::runtime_error("Cannot shutdown Kinect"); |
| 56 | + } |
| 57 | + void startRGB() { |
| 58 | + if(freenect_start_rgb(m_dev) != 0) throw std::runtime_error("Cannot start RGB callback"); |
| 59 | + } |
| 60 | + void stopRGB() { |
| 61 | + if(freenect_stop_rgb(m_dev) != 0) throw std::runtime_error("Cannot stop RGB callback"); |
| 62 | + } |
| 63 | + void startDepth() { |
| 64 | + if(freenect_start_depth(m_dev) != 0) throw std::runtime_error("Cannot start depth callback"); |
| 65 | + } |
| 66 | + void stopDepth() { |
| 67 | + if(freenect_stop_depth(m_dev) != 0) throw std::runtime_error("Cannot stop depth callback"); |
| 68 | + } |
| 69 | + void setTiltDegrees(double _angle) { |
| 70 | + if(freenect_set_tilt_degs(m_dev, _angle) != 0) throw std::runtime_error("Cannot set angle in degrees"); |
| 71 | + } |
| 72 | + void setLed(freenect_led_options _option) { |
| 73 | + if(freenect_set_led(m_dev, _option) != 0) throw std::runtime_error("Cannot set led"); |
| 74 | + } |
| 75 | + void getAccelerometers(double &_x, double &_y, double &_z) { |
| 76 | + if(freenect_get_mks_accel(m_dev,&_x, &_y, &_z) != 0) throw std::runtime_error("Cannot get mks accemerometers"); |
| 77 | + } |
| 78 | + void getAccelerometers(int16_t &_x, int16_t &_y, int16_t &_z) { |
| 79 | + if(freenect_get_raw_accel(m_dev,&_x, &_y, &_z) != 0) throw std::runtime_error("Cannot get raw accemerometers"); |
| 80 | + } |
| 81 | + // Do not call directly even in child |
| 82 | + virtual void RGBCallback(freenect_pixel *rgb, uint32_t timestamp) = 0; |
| 83 | + // Do not call directly even in child |
| 84 | + virtual void DepthCallback(freenect_depth *depth, uint32_t timestamp) = 0; |
| 85 | + private: |
| 86 | + freenect_device *m_dev; |
| 87 | + static void freenect_depth_callback(freenect_device *dev, freenect_depth *depth, uint32_t timestamp) { |
| 88 | + FreenectDevice* device = static_cast<FreenectDevice*>(freenect_get_user(dev)); |
| 89 | + device->DepthCallback(depth, timestamp); |
| 90 | + } |
| 91 | + static void freenect_rgb_callback(freenect_device *dev, freenect_pixel *rgb, uint32_t timestamp) { |
| 92 | + FreenectDevice* device = static_cast<FreenectDevice*>(freenect_get_user(dev)); |
| 93 | + device->RGBCallback(rgb, timestamp); |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + template <class T>class Freenect : Noncopyable { |
| 98 | + public: |
| 99 | + Freenect() : m_stop(false) { |
| 100 | + if(freenect_init(&m_ctx, NULL) != 0) throw std::runtime_error("Cannot initialize freenect library"); |
| 101 | + if(pthread_create(&m_thread, NULL, pthread_callback, (void*)this) != 0) throw std::runtime_error("Cannot initialize freenect thread"); |
| 102 | + } |
| 103 | + ~Freenect() { |
| 104 | + for(typename std::map<int, T*>::iterator it = m_devices.begin() ; it != m_devices.end() ; ++it) { |
| 105 | + delete it->second; |
| 106 | + } |
| 107 | + m_stop = true; |
| 108 | + pthread_join(m_thread, NULL); |
| 109 | + if(freenect_shutdown(m_ctx) != 0) throw std::runtime_error("Cannot cleanup freenect library"); |
| 110 | + } |
| 111 | + T& createDevice(int _index) { |
| 112 | + m_devices.insert(std::make_pair<int, T*>(_index, new T(m_ctx, _index))); |
| 113 | + return *(m_devices.at(_index)); |
| 114 | + } |
| 115 | + void deleteDevice(int _index) { |
| 116 | + m_devices.erase(_index); |
| 117 | + } |
| 118 | + int deviceCount() { |
| 119 | + return freenect_num_devices(m_ctx); |
| 120 | + } |
| 121 | + // Do not call directly, thread runs here |
| 122 | + void operator()() { |
| 123 | + while(!m_stop) { |
| 124 | + if(freenect_process_events(m_ctx) != 0) throw std::runtime_error("Cannot process freenect events"); |
| 125 | + } |
| 126 | + } |
| 127 | + static void *pthread_callback(void *user_data) { |
| 128 | + Freenect<T>* freenect = static_cast<Freenect<T>*>(user_data); |
| 129 | + (*freenect)(); |
| 130 | + } |
| 131 | + private: |
| 132 | + freenect_context *m_ctx; |
| 133 | + volatile bool m_stop; |
| 134 | + pthread_t m_thread; |
| 135 | + std::map<int, T*> m_devices; |
| 136 | + }; |
| 137 | + |
| 138 | +} |
| 139 | + |
0 commit comments