Repository navigation
Expand file tree
/
Copy pathTrackballComponent.cpp
More file actions
123 lines (108 loc) · 3.42 KB
/
Copy pathTrackballComponent.cpp
File metadata and controls
123 lines (108 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "TrackballComponent.h"
#include "Trackball.h"
#include "DekiInput.h"
// The trackball needs deki-gpio, which installing deki-input does not bring: a
// project without it (a desktop one, typically) builds this step as a stub.
#if __has_include("DekiGPIO.h")
#define DEKI_INPUT_HAS_GPIO 1
#include "DekiGPIO.h" // from deki-gpio
#endif
#include <deki/Engine.h>
#include <deki/LogSystem.h>
#include <memory>
namespace DekiInput
{
#if DEKI_INPUT_HAS_GPIO
void TrackballComponent::Setup(SetupCallback onComplete)
{
// Like the keyboard: a control that is not there is reported, and the
// boot carries on.
DekiGpio::IDekiGPIO* gpio = DekiGpio::DekiGPIO::GetCurrent();
if (!gpio)
{
DEKI_LOG_WARNING("TrackballComponent: this platform has no GPIO backend; no trackball");
if (onComplete)
{
onComplete(true);
}
return;
}
const int pins[4] = { upPin, downPin, leftPin, rightPin };
for (int pin : pins)
{
// The ball's lines rest high and pulse low, so each step is one
// falling edge.
if (pin >= 0 && !gpio->CountEdges(pin, DekiGpio::Edge::Falling, DekiGpio::Pull::Up))
{
DEKI_LOG_ERROR("TrackballComponent: cannot count edges on GPIO %d; no trackball", pin);
if (onComplete)
{
onComplete(true);
}
return;
}
}
if (clickPin >= 0)
{
gpio->SetInput(clickPin, clickActiveLow ? DekiGpio::Pull::Up : DekiGpio::Pull::Down);
}
const int up = upPin, down = downPin, left = leftPin, right = rightPin, click = clickPin;
const bool activeLow = clickActiveLow;
auto ball = std::make_unique<Trackball>(
[gpio, up, down, left, right, click, activeLow]()
{
Trackball::Sample s;
if (up >= 0)
{
s.up = gpio->TakeEdges(up);
}
if (down >= 0)
{
s.down = gpio->TakeEdges(down);
}
if (left >= 0)
{
s.left = gpio->TakeEdges(left);
}
if (right >= 0)
{
s.right = gpio->TakeEdges(right);
}
if (click >= 0)
{
s.pressed = gpio->Read(click) != activeLow;
}
return s;
});
ball->SetMode(mode == TrackballMode::Pointer ? Trackball::Mode::Pointer : Trackball::Mode::Keys);
ball->SetPixelsPerStep(pixelsPerStep);
// The screen: the framebuffer, sized once the display is set up.
ball->SetPointerAreaSource(
[](int32_t& w, int32_t& h)
{
auto& engine = Deki::Engine::GetInstance();
w = engine.GetScreenWidth();
h = engine.GetScreenHeight();
});
if (ball->Initialize())
{
::DekiInput::DekiInput::SetInput(std::move(ball), "Trackball");
DEKI_LOG_INFO("TrackballComponent: trackball on GPIO %d/%d/%d/%d, click %d, as %s", up, down, left, right,
click, mode == TrackballMode::Pointer ? "a pointer" : "arrow keys");
}
if (onComplete)
{
onComplete(true);
}
}
#else
void TrackballComponent::Setup(SetupCallback onComplete)
{
DEKI_LOG_ERROR("TrackballComponent: this build has no deki-gpio, so there is no trackball; install deki-gpio");
if (onComplete)
{
onComplete(true);
}
}
#endif
} // namespace DekiInput