summaryrefslogtreecommitdiff
path: root/src/ui/include/encoder_input.hpp
blob: e685a7a2337763b1c399a3fdbd74d3151ec53933 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#pragma once

#include <stdint.h>
#include <deque>
#include <memory>

#include "core/lv_group.h"
#include "gpios.hpp"
#include "hal/lv_hal_indev.h"

#include "nvs.hpp"
#include "relative_wheel.hpp"
#include "touchwheel.hpp"

namespace ui {

class Scroller;

/*
 * Main input device abstracting that handles turning lower-level input device
 * drivers into events and LVGL inputs.
 *
 * As far as LVGL is concerned, this class represents an ordinary rotary
 * encoder, supporting only left and right ticks, and clicking.
 */
class EncoderInput {
 public:
  EncoderInput(drivers::IGpios& gpios, drivers::TouchWheel& wheel);

  auto Read(lv_indev_data_t* data) -> void;
  auto registration() -> lv_indev_t* { return registration_; }

  auto mode(drivers::NvsStorage::InputModes mode) { mode_ = mode; }
  auto lock(bool l) -> void { is_locked_ = l; }

 private:
  lv_indev_drv_t driver_;
  lv_indev_t* registration_;

  drivers::IGpios& gpios_;
  drivers::TouchWheel& raw_wheel_;
  std::unique_ptr<drivers::RelativeWheel> relative_wheel_;
  std::unique_ptr<Scroller> scroller_;

  drivers::NvsStorage::InputModes mode_;
  bool is_locked_;

  enum class Keys {
    kVolumeUp,
    kVolumeDown,
    kTouchWheel,
    kTouchWheelCenter,
    kDirectionalUp,
    kDirectionalRight,
    kDirectionalDown,
    kDirectionalLeft,
  };

  std::unordered_map<Keys, uint64_t> touch_time_ms_;
  std::unordered_map<Keys, bool> short_press_fired_;
  std::unordered_map<Keys, bool> long_press_fired_;

  auto HandleKey(Keys key, uint64_t ms, bool clicked) -> void;
  auto ShortPressTrigger(Keys key) -> bool;
  auto ShortPressTriggerRepeating(Keys key, uint64_t ms) -> bool;
  auto LongPressTrigger(Keys key, uint64_t ms) -> bool;
};

class Scroller {
 public:
  Scroller() : last_input_ms_(0), velocity_(0) {}

  auto AddInput(uint64_t, int) -> int;

 private:
  uint64_t last_input_ms_;
  int velocity_;
};

}  // namespace ui