|
1 | 1 | import lvgl as lv # NOQA
|
2 | 2 | import _indev_base
|
| 3 | +import micropython # NOQA |
3 | 4 |
|
4 | 5 |
|
5 |
| -def _remap(value, old_min, old_max, new_min, new_max): |
6 |
| - return int((((value - old_min) * (new_max - new_min)) / (old_max - old_min)) + new_min) |
| 6 | +@micropython.viper |
| 7 | +def _remap(value: int, old_min: int, old_max: int, new_min: int, new_max: int) -> int: |
| 8 | + newrange: int = new_max - new_min |
| 9 | + oldrange: int = old_max - old_min |
| 10 | + step1: int = value - old_min |
| 11 | + step2: int = step1 * newrange |
| 12 | + step3: int = int(step2 / oldrange) |
| 13 | + |
| 14 | + return step3 + new_min |
7 | 15 |
|
8 | 16 |
|
9 | 17 | class PointerDriver(_indev_base.IndevBase):
|
@@ -59,62 +67,58 @@ def _get_coords(self):
|
59 | 67 | # of (state, x, y) or None if no touch even has occured
|
60 | 68 | raise NotImplementedError
|
61 | 69 |
|
| 70 | + def _calc_coords(self, x, y): |
| 71 | + cal = self._cal |
| 72 | + rotation = self._rotation |
| 73 | + left = cal.left |
| 74 | + right = cal.right |
| 75 | + top = cal.top |
| 76 | + bottom = cal.bottom |
| 77 | + |
| 78 | + if left is None: |
| 79 | + left = 0 |
| 80 | + if right is None: |
| 81 | + right = self._orig_width |
| 82 | + if top is None: |
| 83 | + top = 0 |
| 84 | + if bottom is None: |
| 85 | + bottom = self._orig_height |
| 86 | + |
| 87 | + if rotation == lv.DISPLAY_ROTATION._90: # NOQA |
| 88 | + left, right, top, bottom = bottom, top, left, right |
| 89 | + x, y = y, x |
| 90 | + elif rotation == lv.DISPLAY_ROTATION._180: # NOQA |
| 91 | + left, right, top, bottom = right, left, bottom, top |
| 92 | + elif rotation == lv.DISPLAY_ROTATION._270: # NOQA |
| 93 | + left, right, top, bottom = top, bottom, right, left |
| 94 | + x, y = y, x |
| 95 | + |
| 96 | + xpos = _remap(x, left, right, 0, self._width) |
| 97 | + ypos = _remap(y, top, bottom, 0, self._height) |
| 98 | + return xpos, ypos |
| 99 | + |
62 | 100 | def _read(self, drv, data): # NOQA
|
63 | 101 | coords = self._get_coords()
|
64 | 102 |
|
65 |
| - if coords is None: # ignore no touch & multi touch |
66 |
| - if self._current_state != self.RELEASED: |
67 |
| - self._current_state = self.RELEASED |
68 |
| - |
69 |
| - data.point.x = self._last_x |
70 |
| - data.point.y = self._last_y |
71 |
| - data.state = self._current_state |
72 |
| - # print("raw(x={0}, y={1}) point(x={2} y={3})".format(-1, -1, data.point.x, data.point.y)) # NOQA |
73 |
| - data.continue_reading = False |
74 |
| - return |
75 |
| - |
76 |
| - state, x, y = coords |
| 103 | + if coords is None: |
| 104 | + state = self.RELEASED |
| 105 | + x, y = self._last_x, self._last_y |
| 106 | + else: |
| 107 | + state, x, y = coords |
77 | 108 |
|
78 | 109 | if None not in (x, y):
|
79 |
| - cal = self._cal |
80 |
| - rotation = self._rotation |
81 |
| - left = cal.left |
82 |
| - right = cal.right |
83 |
| - top = cal.top |
84 |
| - bottom = cal.bottom |
85 |
| - |
86 |
| - if left is None: |
87 |
| - left = 0 |
88 |
| - if right is None: |
89 |
| - right = self._orig_width |
90 |
| - if top is None: |
91 |
| - top = 0 |
92 |
| - if bottom is None: |
93 |
| - bottom = self._orig_height |
94 |
| - |
95 |
| - if rotation == lv.DISPLAY_ROTATION._0: # NOQA |
96 |
| - xpos = _remap(x, left, right, 0, self._orig_width) |
97 |
| - ypos = _remap(y, top, bottom, 0, self._orig_height) |
98 |
| - elif rotation == lv.DISPLAY_ROTATION._90: # NOQA |
99 |
| - xpos = _remap(y, bottom, top, 0, self._orig_height) |
100 |
| - ypos = _remap(x, left, right, 0, self._orig_width) |
101 |
| - elif rotation == lv.DISPLAY_ROTATION._180: # NOQA |
102 |
| - xpos = _remap(x, right, left, 0, self._orig_width) |
103 |
| - ypos = _remap(y, bottom, top, 0, self._orig_height) |
104 |
| - elif rotation == lv.DISPLAY_ROTATION._270: # NOQA |
105 |
| - xpos = _remap(y, top, bottom, 0, self._orig_height) |
106 |
| - ypos = _remap(x, right, left, 0, self._orig_width) |
| 110 | + self._last_x, self._last_y = x, y |
| 111 | + if coords is None: |
| 112 | + data.continue_reading = False |
107 | 113 | else:
|
108 |
| - raise RuntimeError |
109 |
| - |
110 |
| - self._last_x = xpos |
111 |
| - self._last_y = ypos |
112 |
| - |
113 |
| - data.continue_reading = True |
| 114 | + data.continue_reading = True |
| 115 | + else: |
| 116 | + data.continue_reading = False |
114 | 117 |
|
115 |
| - data.state = self._current_state |
116 |
| - data.point.x = self._last_x |
117 |
| - data.point.y = self._last_y |
| 118 | + data.point.x, data.point.y = ( |
| 119 | + self._calc_coords(self._last_x, self._last_y) |
| 120 | + ) |
| 121 | + data.state = state |
118 | 122 |
|
119 | 123 | def get_vect(self, point):
|
120 | 124 | self._indev_drv.get_vect(point)
|
|
0 commit comments