2015-07-08 11:39:24 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "touch.hpp"
|
|
|
|
|
2016-07-27 18:30:43 -04:00
|
|
|
#include "portapack_persistent_memory.hpp"
|
|
|
|
using namespace portapack;
|
|
|
|
|
2016-07-27 17:04:04 -04:00
|
|
|
#include "utility.hpp"
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
namespace touch {
|
|
|
|
|
2016-07-26 18:13:07 -04:00
|
|
|
Metrics calculate_metrics(const Frame& frame) {
|
2015-07-08 11:39:24 -04:00
|
|
|
/* TODO: Yikes! M0 doesn't have floating point, so this code is
|
|
|
|
* expensive! On the other hand, it seems to be working well (and
|
|
|
|
* fast *enough*?), so maybe leave it alone at least for now.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const auto x_max = frame.x.xp;
|
|
|
|
const auto x_min = frame.x.xn;
|
|
|
|
const auto x_range = x_max - x_min;
|
2016-07-27 14:14:11 -04:00
|
|
|
const float x_position = (frame.x.yp + frame.x.yn) * 0.5f;
|
|
|
|
const float x_norm = (x_position - x_min) / x_range;
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
const auto y_max = frame.y.yn;
|
|
|
|
const auto y_min = frame.y.yp;
|
|
|
|
const auto y_range = y_max - y_min;
|
2016-07-27 14:14:11 -04:00
|
|
|
const float y_position = (frame.y.xp + frame.y.xn) * 0.5f;
|
|
|
|
const float y_norm = (y_position - y_min) / y_range;
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
const auto z_max = frame.pressure.yp;
|
|
|
|
const auto z_min = frame.pressure.xn;
|
|
|
|
const auto z_range = z_max - z_min;
|
|
|
|
const auto z1_position = frame.pressure.xp;
|
|
|
|
const float z1_norm = float(z1_position - z_min) / z_range;
|
|
|
|
const auto z2_position = frame.pressure.yn;
|
|
|
|
const float z2_norm = float(z2_position - z_min) / z_range;
|
|
|
|
|
|
|
|
const float r_x_plate = 330.0f;
|
|
|
|
//const float r_y_plate = 600.0f;
|
|
|
|
const float r_touch = r_x_plate * x_norm * (z2_norm / z1_norm - 1.0f);
|
|
|
|
|
|
|
|
return {
|
|
|
|
.x = x_norm,
|
|
|
|
.y = y_norm,
|
|
|
|
.r = r_touch,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-27 17:01:20 -04:00
|
|
|
ui::Point Calibration::translate(const DigitizerPoint& p) const {
|
|
|
|
static constexpr range_t<int32_t> x_range { 0, 240 - 1 };
|
|
|
|
static constexpr range_t<int32_t> y_range { 0, 320 - 1 };
|
|
|
|
|
|
|
|
const int32_t x = (a * p.x + b * p.y + c) / k;
|
|
|
|
const int32_t y = (d * p.x + e * p.y + f) / k;
|
|
|
|
const auto x_clipped = x_range.clip(x);
|
|
|
|
const auto y_clipped = y_range.clip(y);
|
2016-11-29 13:13:56 -05:00
|
|
|
return { x_clipped, y_clipped };
|
2016-07-27 17:01:20 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 19:44:19 -05:00
|
|
|
void Manager::feed(const Frame& frame) {
|
2015-07-08 11:39:24 -04:00
|
|
|
// touch_debounce.feed(touch_raw);
|
|
|
|
const auto touch_raw = frame.touch;
|
|
|
|
//const auto touch_stable = touch_debounce.state();
|
|
|
|
const auto touch_stable = frame.touch;
|
2023-05-03 04:47:43 -04:00
|
|
|
bool touch_down_pressure = false;
|
|
|
|
bool touch_up_pressure = false;
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
// Only feed coordinate averaging if there's a touch.
|
|
|
|
// TODO: Separate threshold to gate coordinates for filtering?
|
|
|
|
if( touch_raw ) {
|
|
|
|
const auto metrics = calculate_metrics(frame);
|
|
|
|
|
2023-05-03 05:18:37 -04:00
|
|
|
constexpr float r_touch_down_threshold = 3200.0f;
|
|
|
|
constexpr float r_touch_up_threshold = r_touch_down_threshold * 2.0f;
|
|
|
|
|
|
|
|
touch_down_pressure = (metrics.r < r_touch_down_threshold);
|
|
|
|
touch_up_pressure = (metrics.r < r_touch_up_threshold);
|
2023-05-03 04:47:43 -04:00
|
|
|
|
|
|
|
if( touch_down_pressure ) {
|
2016-07-27 17:08:02 -04:00
|
|
|
filter_x.feed(metrics.x * 1024);
|
|
|
|
filter_y.feed(metrics.y * 1024);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
filter_x.reset();
|
|
|
|
filter_y.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(state) {
|
|
|
|
case State::NoTouch:
|
2023-05-03 04:47:43 -04:00
|
|
|
if( touch_stable && touch_down_pressure && !persistent_memory::disable_touchscreen()) {
|
2015-07-08 11:39:24 -04:00
|
|
|
if( point_stable() ) {
|
|
|
|
state = State::TouchDetected;
|
|
|
|
touch_started();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case State::TouchDetected:
|
2023-05-03 04:47:43 -04:00
|
|
|
if( touch_stable && touch_up_pressure ) {
|
2015-07-08 11:39:24 -04:00
|
|
|
touch_moved();
|
|
|
|
} else {
|
|
|
|
state = State::NoTouch;
|
|
|
|
touch_ended();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
state = State::NoTouch;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 17:08:02 -04:00
|
|
|
ui::Point Manager::filtered_point() const {
|
2016-07-27 18:30:43 -04:00
|
|
|
return persistent_memory::touch_calibration().translate({ filter_x.value(), filter_y.value() });
|
2016-07-27 17:08:02 -04:00
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
} /* namespace touch */
|