Skip to content

Commit c5ce0c5

Browse files
committed
gestures: Simplify some repetitive code
1 parent 2fa485b commit c5ce0c5

File tree

2 files changed

+17
-54
lines changed

2 files changed

+17
-54
lines changed

src/touch/gestures.c

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ static void _reset_state(void)
146146

147147
/********************************** GESTURE DETECTION **********************************/
148148

149+
typedef bool (*gesture_detect_fn)(uint8_t location);
150+
149151
static bool _is_continuous_tap(uint8_t location)
150152
{
151153
return _state[location].max_slide_travel < TAP_SLIDE_TOLERANCE &&
@@ -192,55 +194,13 @@ static void _gesture_emit_event(uint8_t id, slider_location_t location)
192194
emit_event(&event);
193195
}
194196

195-
static void _emit_continuous_slide_event(void)
196-
{
197-
if (_is_continuous_slide(top_slider)) {
198-
_gesture_emit_event(EVENT_SLIDE, top_slider);
199-
}
200-
if (_is_continuous_slide(bottom_slider)) {
201-
_gesture_emit_event(EVENT_SLIDE, bottom_slider);
202-
}
203-
}
204-
205-
static void _emit_slide_release_event(void)
206-
{
207-
if (_is_slide_released(top_slider)) {
208-
_gesture_emit_event(EVENT_SLIDE_RELEASED, top_slider);
209-
}
210-
if (_is_slide_released(bottom_slider)) {
211-
_gesture_emit_event(EVENT_SLIDE_RELEASED, bottom_slider);
212-
}
213-
}
214-
215-
static void _emit_long_tap_event(void)
216-
{
217-
if (_is_long_tap_release(top_slider)) {
218-
_gesture_emit_event(EVENT_LONG_TAP, top_slider);
219-
}
220-
if (_is_long_tap_release(bottom_slider)) {
221-
_gesture_emit_event(EVENT_LONG_TAP, bottom_slider);
222-
}
223-
}
224-
225-
static void _emit_short_tap_event(void)
226-
{
227-
if (_is_tap_release(top_slider)) {
228-
_gesture_emit_event(EVENT_SHORT_TAP, top_slider);
229-
}
230-
if (_is_tap_release(bottom_slider)) {
231-
_gesture_emit_event(EVENT_SHORT_TAP, bottom_slider);
232-
}
233-
}
234-
235-
static void _emit_continuous_tap_event(void)
236-
{
237-
if (_is_continuous_tap(top_slider)) {
238-
_gesture_emit_event(EVENT_CONTINUOUS_TAP, top_slider);
239-
}
240-
if (_is_continuous_tap(bottom_slider)) {
241-
_gesture_emit_event(EVENT_CONTINUOUS_TAP, bottom_slider);
242-
}
243-
}
197+
static gesture_detect_fn _emit_event_detect_fns[EVENT_ENUM_MAX] = {
198+
[EVENT_SLIDE] = _is_continuous_slide,
199+
[EVENT_SLIDE_RELEASED] = _is_slide_released,
200+
[EVENT_CONTINUOUS_TAP] = _is_continuous_tap,
201+
[EVENT_LONG_TAP] = _is_long_tap_release,
202+
[EVENT_SHORT_TAP] = _is_tap_release,
203+
};
244204

245205
/********************************** MEASURE, DETECT and CALLBACK **********************************/
246206

@@ -263,11 +223,13 @@ static void _measure_and_emit(void)
263223
}
264224

265225
if (gesture_detected) {
266-
_emit_continuous_slide_event();
267-
_emit_slide_release_event();
268-
_emit_long_tap_event();
269-
_emit_short_tap_event();
270-
_emit_continuous_tap_event();
226+
for (int location = 0; location < TOUCH_NUM_SLIDERS; location++) {
227+
for (int event_idx = 0; event_idx < EVENT_ENUM_MAX; event_idx++) {
228+
if (_emit_event_detect_fns[event_idx](location)) {
229+
_gesture_emit_event(event_idx, location);
230+
}
231+
}
232+
}
271233
}
272234

273235
bool both_sliders_released_or_inactive = true;

src/ui/event.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ enum {
3131
EVENT_CONTINUOUS_TAP,
3232
EVENT_LONG_TAP,
3333
EVENT_SHORT_TAP,
34+
EVENT_ENUM_MAX, // MAX must always be last, indicates the number of items in the enumration
3435
};
3536

3637
typedef struct {

0 commit comments

Comments
 (0)