summaryrefslogtreecommitdiff
path: root/src/tangara/input/input_touch_dpad.cpp
blob: 9275b5106c08ebc63b3bf791c5ee6fa9dab709a9 (plain)
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
/*
 * Copyright 2024 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include "input/input_touch_dpad.hpp"

#include <cstdint>

#include "hal/lv_hal_indev.h"

#include "drivers/haptics.hpp"
#include "drivers/touchwheel.hpp"

#include "events/event_queue.hpp"
#include "input/input_device.hpp"
#include "input/input_hook_actions.hpp"
#include "input/input_touch_dpad.hpp"

namespace input {

TouchDPad::TouchDPad(drivers::TouchWheel& wheel)
    : wheel_(wheel),
      centre_("centre", actions::select(), {}, {}, {}),
      up_("up", actions::scrollUp()),
      right_("right", {}),
      down_("down", actions::scrollDown()),
      left_("left", actions::goBack()) {}

auto TouchDPad::read(lv_indev_data_t* data) -> void {
  wheel_.Update();
  auto wheel_data = wheel_.GetTouchWheelData();

  centre_.update(wheel_data.is_button_touched, data);

  up_.update(
      wheel_data.is_wheel_touched &&
          drivers::TouchWheel::isAngleWithin(wheel_data.wheel_position, 0, 32),
      data);
  right_.update(
      wheel_data.is_wheel_touched && drivers::TouchWheel::isAngleWithin(
                                         wheel_data.wheel_position, 192, 32),
      data);
  down_.update(
      wheel_data.is_wheel_touched && drivers::TouchWheel::isAngleWithin(
                                         wheel_data.wheel_position, 128, 32),
      data);
  left_.update(
      wheel_data.is_wheel_touched &&
          drivers::TouchWheel::isAngleWithin(wheel_data.wheel_position, 64, 32),
      data);
}

auto TouchDPad::name() -> std::string {
  return "dpad";
}

auto TouchDPad::triggers()
    -> std::vector<std::reference_wrapper<TriggerHooks>> {
  return {centre_, up_, right_, down_, left_};
}

}  // namespace input