roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
basic_touch.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4
6#include "roo_logging.h"
7#include "roo_time.h"
8
9namespace roo_display {
10
11// Base class for touch drivers. Takes care of capping hardware sampling
12// frequency, data smoothing, and spurious gaps in the readout.
13template <int max_touch_points>
15 public:
16 struct Config {
17 // If a request comes within this interval from a previous read, the results
18 // of the previous read are reported. This setting reduces the maximum
19 // device sampling frequency.
21
22 // If there is no touch detected for up to this many ms, we will still
23 // report that the pad is touched, with previously reported coordinates.
24 // This helps overcome brief 'lost-contact' touch gaps, which can throw off
25 // gesture detection algorithms.
27
28 // Controls exponential smoothing. In (0, 1], where 0 corresponds to no
29 // smoothing, and 1 corresponds to infinite smoothing. The value represents
30 // weight of the old sample at dt = 10ms behind the new sample.
32 };
33
35 : config_(std::move(config)),
36 detection_timestamp_(roo_time::Uptime::Start()),
37 touch_points_(),
38 points_touched_(0) {}
39
40 virtual ~BasicTouchDevice() = default;
41
43
44 protected:
45 virtual int readTouch(TouchPoint* points) = 0;
46
47 private:
48 TouchResult pushResult(TouchPoint* points, int max_points) {
49 size_t point_count =
50 std::min(points_touched_, std::min(max_points, max_touch_points));
51 for (size_t i = 0; i < point_count; ++i) {
52 points[i] = touch_points_[i];
53 }
54 return TouchResult(detection_timestamp_, points_touched_);
55 }
56
57 Config config_;
58 roo_time::Uptime detection_timestamp_;
59 TouchPoint touch_points_[max_touch_points];
60 int points_touched_;
61};
62
63template <int max_touch_points>
65 int max_points) {
66 roo_time::Uptime now = roo_time::Uptime::Now();
67 roo_time::Duration dt = now - detection_timestamp_;
68 if (dt < roo_time::Millis(config_.min_sampling_interval_ms)) {
69 // Immediately return the last result without attempting to scan.
70 return pushResult(points, max_points);
71 }
73 int points_touched = readTouch(readout);
76 if (points_touched <= 0) {
77 if (dt < roo_time::Millis(config_.touch_intertia_ms)) {
78 // We did not detect touch, but the latest confirmed touch was not long
79 // ago so we report that one anyway, but do not update the
80 // detection_timestamp_ to reflect that we're reporting a stale value.
81 return pushResult(points, max_points);
82 }
83 // Report definitive no touch.
84 detection_timestamp_ = now;
85 points_touched_ = 0;
86 return pushResult(points, max_points);
87 }
90 }
91 // Touch has been detected. Need to smooth the values and report it.
92 float alpha = 1 - pow(config_.smoothing_factor, dt.inMicros() / 10000.0);
93 for (int i = 0; i < points_touched; i++) {
95 p.vx = 0;
96 p.vy = 0;
97 // Identify a previous touch coordinate, if any.
98 for (int j = 0; j < points_touched_; ++j) {
99 if (touch_points_[j].id == p.id) {
100 TouchPoint& prev = touch_points_[j];
101 // Match! Smooth out the value, and calculate velocity.
102 int16_t x = prev.x * (1 - alpha) + p.x * alpha;
103 int16_t y = prev.y * (1 - alpha) + p.y * alpha;
104 int16_t z = prev.z * (1 - alpha) + p.z * alpha;
105 if (dt > roo_time::Micros(0)) {
106 // Velocity in pixels / s.
107 p.vx = 1000000LL * (x - prev.x) / dt.inMicros();
108 p.vy = 1000000LL * (y - prev.y) / dt.inMicros();
109 }
110 p.x = x;
111 p.y = y;
112 p.z = z;
113 continue;
114 }
115 }
116 }
117 // Copy over and report.
118 detection_timestamp_ = now;
119 points_touched_ = points_touched;
120 std::copy(&readout[0], &readout[points_touched], touch_points_);
121 return pushResult(points, max_points);
122}
123
124} // namespace roo_display
virtual int readTouch(TouchPoint *points)=0
TouchResult getTouch(TouchPoint *points, int max_points) override
Read the current touch state.
Definition basic_touch.h:64
BasicTouchDevice(Config config)
Definition basic_touch.h:34
virtual ~BasicTouchDevice()=default
Touch controller interface.
Definition device.h:428
Defines 140 opaque HTML named colors.
A single touch point returned by a touch controller.
Definition device.h:390
Metadata for a touch sampling result.
Definition device.h:415