From 2aee2a94ea53620e3da7f976875ca4101eec780d Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 17 Jun 2022 11:58:04 +0200 Subject: tracker/wii: clean up a little --- tracker-wii/wii_camera.cpp | 61 ++++++++++++++--------------- tracker-wii/wii_camera.h | 40 ++++++++----------- tracker-wii/wii_point_extractor.cpp | 77 ++++++++++++++++++------------------- 3 files changed, 82 insertions(+), 96 deletions(-) (limited to 'tracker-wii') diff --git a/tracker-wii/wii_camera.cpp b/tracker-wii/wii_camera.cpp index 98d180bf..1e05a11c 100644 --- a/tracker-wii/wii_camera.cpp +++ b/tracker-wii/wii_camera.cpp @@ -15,11 +15,9 @@ #include "wii_camera.h" #include "wii_frame.hpp" -#include "compat/sleep.hpp" #include "compat/math-imports.hpp" #include - #include using namespace pt_module; @@ -41,46 +39,46 @@ WIICamera::~WIICamera() QString WIICamera::get_desired_name() const { - return desired_name; + return QStringLiteral("Wii"); } QString WIICamera::get_active_name() const { - return active_name; + return get_desired_name(); } void WIICamera::show_camera_settings() { - } WIICamera::result WIICamera::get_info() const { - if (cam_info.res_x == 0 || cam_info.res_y == 0) - return result(false, pt_camera_info()); - return result(true, cam_info); + if (cam_info.res_x == 0 || cam_info.res_y == 0) + return result(false, pt_camera_info()); + return result(true, cam_info); } WIICamera::result WIICamera::get_frame(pt_frame& frame_) { - cv::Mat& frame = frame_.as()->mat; + cv::Mat& frame = frame_.as()->mat; struct wii_info& wii = frame_.as()->wii; + const wii_camera_status new_frame = get_frame(frame); - const wii_camera_status new_frame = get_frame(frame); //create a fake blank frame - frame = cv::Mat(cam_info.res_x, cam_info.res_y, CV_8UC3, cv::Scalar(0, 0, 0)); + frame.create(cam_info.res_x, cam_info.res_y, CV_8UC3); + frame.setTo({0}); wii.status = new_frame; - switch (new_frame) - { + switch (new_frame) + { case wii_cam_data_change: - get_status(wii); - get_points(wii); + get_status(wii); + get_points(wii); break; case wii_cam_data_no_change: return result(false, cam_info); - default: - break; + default: + break; } return result(true, cam_info); @@ -88,30 +86,28 @@ WIICamera::result WIICamera::get_frame(pt_frame& frame_) bool WIICamera::start(const pt_settings&) { + if (m_pDev) + return true; m_pDev = std::make_unique(); m_pDev->ChangedCallback = on_state_change; m_pDev->CallbackTriggerFlags = (state_change_flags)(CONNECTED | EXTENSION_CHANGED | MOTIONPLUS_CHANGED); - return true; + return true; } void WIICamera::stop() { - desired_name = QString(); - active_name = QString(); - cam_info = pt_camera_info(); - cam_desired = pt_camera_info(); - onExit = true; - - if (m_pDev) - { - m_pDev->ChangedCallback = nullptr; - m_pDev->Disconnect(); - m_pDev = nullptr; - } - - Beep(1000, 200); + if (!m_pDev) + return; + + cam_info = {}; + cam_desired = {}; + pitch_ = 0; roll_ = 0; + + m_pDev->ChangedCallback = nullptr; + m_pDev->Disconnect(); + m_pDev = nullptr; } #ifdef __MINGW32__ @@ -149,7 +145,6 @@ wii_camera_status WIICamera::pair() while (ibtidx < max_devices && BluetoothFindNextRadio(&bt_param, hbtlist + ibtidx)); BluetoothFindRadioClose(hbt); - int i; bool error = false; for (i = 0; i < ibtidx; i++) diff --git a/tracker-wii/wii_camera.h b/tracker-wii/wii_camera.h index 038e25e7..3a7993aa 100644 --- a/tracker-wii/wii_camera.h +++ b/tracker-wii/wii_camera.h @@ -27,47 +27,39 @@ namespace pt_module { struct WIICamera final : pt_camera { - WIICamera(const QString& module_name); - ~WIICamera() override; + WIICamera(const QString& module_name); + ~WIICamera() override; - bool start(const pt_settings&) override; - void stop() override; + bool start(const pt_settings&) override; + void stop() override; - result get_frame(pt_frame& Frame) override; - result get_info() const override; + result get_frame(pt_frame& Frame) override; + result get_info() const override; - pt_camera_info get_desired() const override { return cam_desired; } - QString get_desired_name() const override; - QString get_active_name() const override; + pt_camera_info get_desired() const override { return cam_desired; } + QString get_desired_name() const override; + QString get_active_name() const override; - void set_fov(f x) override { (void) x; } - void show_camera_settings() override; + void set_fov(f x) override { (void) x; } + void show_camera_settings() override; private: std::unique_ptr m_pDev; static void on_state_change(wiimote &remote, state_change_flags changed, const wiimote_state &new_state); - bool onExit = false; wii_camera_status pair(); wii_camera_status get_frame(cv::Mat& Frame); bool get_points(struct wii_info& wii); void get_status(struct wii_info& wii); - double dt_mean = 0; + pt_camera_info cam_info; + pt_camera_info cam_desired; + int pitch_ = 0, roll_ = 0; + pt_settings s; - Timer t; - - pt_camera_info cam_info; - pt_camera_info cam_desired; - QString desired_name, active_name; - - pt_settings s; - - int pitch_ = 0, roll_ = 0; - - static constexpr inline double dt_eps = 1./384; + static constexpr inline double dt_eps = 1./384; }; } // ns pt_module diff --git a/tracker-wii/wii_point_extractor.cpp b/tracker-wii/wii_point_extractor.cpp index 6f86da1d..4f1f92b9 100644 --- a/tracker-wii/wii_point_extractor.cpp +++ b/tracker-wii/wii_point_extractor.cpp @@ -27,7 +27,6 @@ using namespace pt_module; WIIPointExtractor::WIIPointExtractor(const QString& module_name) : s(module_name) { - } //define a temp draw function @@ -35,8 +34,8 @@ void WIIPointExtractor::draw_point(cv::Mat& preview_frame, const vec2& p, const { static constexpr int len = 9; - cv::Point p2(iround(p[0] * preview_frame.cols + preview_frame.cols / 2.f), - iround(-p[1] * preview_frame.cols + preview_frame.rows / 2.f)); + cv::Point p2(iround(p[0] * preview_frame.cols + preview_frame.cols / 2.f), + iround(-p[1] * preview_frame.cols + preview_frame.rows / 2.f)); cv::line(preview_frame, cv::Point(p2.x - len, p2.y), @@ -48,7 +47,7 @@ void WIIPointExtractor::draw_point(cv::Mat& preview_frame, const vec2& p, const cv::Point(p2.x, p2.y + len), color, thickness); -}; +} void WIIPointExtractor::draw_points(cv::Mat& preview_frame, const struct wii_info& wii) { @@ -75,11 +74,11 @@ void WIIPointExtractor::draw_points(cv::Mat& preview_frame, const struct wii_inf void WIIPointExtractor::draw_bg(cv::Mat& preview_frame, const struct wii_info& wii) { //draw battery status - cv::line(preview_frame, - cv::Point(0, 0), - cv::Point(preview_frame.cols*wii.BatteryPercent / 100, 0), - (wii.bBatteryDrained ? cv::Scalar(255, 0, 0) : cv::Scalar(0, 140, 0)), - 2); + cv::line(preview_frame, + cv::Point(0, 0), + cv::Point(preview_frame.cols*wii.BatteryPercent / 100, 0), + (wii.bBatteryDrained ? cv::Scalar(255, 0, 0) : cv::Scalar(0, 140, 0)), + 2); //draw horizon int pdelta = iround(preview_frame.rows / 4.f * tan(wii.Pitch * pi / 180.f)); @@ -100,38 +99,38 @@ void WIIPointExtractor::extract_points(const pt_frame& frame_, const struct wii_info& wii = frame_.as_const()->wii; cv::Mat& preview_frame = *preview_frame_.as(); - map_points(wii, points); - if (preview_visible && wii.status == wii_cam_data_change) - { - draw_bg(preview_frame, wii); - draw_points(preview_frame, wii); - } + map_points(wii, points); + if (preview_visible && wii.status == wii_cam_data_change) + { + draw_bg(preview_frame, wii); + draw_points(preview_frame, wii); + } } bool WIIPointExtractor::map_points(const struct wii_info& wii, std::vector& points) { - constexpr int W = 1024; - constexpr int H = 768; - points.reserve(4); - points.clear(); - - for (unsigned index = 0; index < 4; index++) // NOLINT(modernize-loop-convert) - { - const struct wii_info_points &dot = wii.Points[index]; - if (dot.bvis) { - //qDebug() << "wii:" << dot.RawX << "+" << dot.RawY; - //anti-clockwise rotate the 2D point - const float RX = W - dot.ux; - const float RY = H - dot.uy; - //vec2 dt((dot.RawX - W / 2.0f) / W, -(dot.RawY - H / 2.0f) / W); - //vec2 dt((RX - W / 2.0f) / W, -(RY - H / 2.0f) / W); - //vec2 dt((2.0f*RX - W) / W, -(2.0f*RY - H ) / W); - vec2 dt; - std::tie(dt[0], dt[1]) = to_screen_pos(RX, RY, W, H); - - points.push_back(dt); - } - } - - return points.size() >= PointModel::N_POINTS; + constexpr int W = 1024; + constexpr int H = 768; + points.reserve(4); + points.clear(); + + for (unsigned index = 0; index < 4; index++) // NOLINT(modernize-loop-convert) + { + const struct wii_info_points &dot = wii.Points[index]; + if (dot.bvis) { + //qDebug() << "wii:" << dot.RawX << "+" << dot.RawY; + //anti-clockwise rotate the 2D point + const float RX = W - dot.ux; + const float RY = H - dot.uy; + //vec2 dt((dot.RawX - W / 2.0f) / W, -(dot.RawY - H / 2.0f) / W); + //vec2 dt((RX - W / 2.0f) / W, -(RY - H / 2.0f) / W); + //vec2 dt((2.0f*RX - W) / W, -(2.0f*RY - H ) / W); + vec2 dt; + std::tie(dt[0], dt[1]) = to_screen_pos(RX, RY, W, H); + + points.push_back(dt); + } + } + + return points.size() >= PointModel::N_POINTS; } -- cgit v1.2.3