Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 17 additions & 25 deletions src/touch/gestures.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,6 @@ static void _reset_state(void)
memset(_state, 0, sizeof(_state));
}

/**
* Prepares the gestures data to be sent with an emitted event
*/
static void _collect_gestures_data(
gestures_detection_state_t* state,
gestures_slider_data_t* slider_data)
{
slider_data->position = state->position_current;
slider_data->diff = state->position_current - state->position_start;
slider_data->velocity = state->velocity_sum;
}

/********************************** GESTURE DETECTION **********************************/

static bool _is_continuous_tap(uint8_t location)
Expand Down Expand Up @@ -194,59 +182,63 @@ static void _gesture_emit_event(uint8_t id, slider_location_t location)
if (!_released_since_new_screen) {
return;
}
gestures_slider_data_t slider_data;
_collect_gestures_data(&_state[location], &slider_data);
event_t event = {.data = &slider_data, .id = id};
event_slider_data_t slider_data = {
.source = location,
.position = _state[location].position_current,
.diff = _state[location].position_current - _state[location].position_start,
.velocity = _state[location].velocity_sum,
};
event_t event = {.id = id, .data = slider_data};
emit_event(&event);
}

static void _emit_continuous_slide_event(void)
{
if (_is_continuous_slide(top_slider)) {
_gesture_emit_event(EVENT_TOP_SLIDE, top_slider);
_gesture_emit_event(EVENT_SLIDE, top_slider);
}
if (_is_continuous_slide(bottom_slider)) {
_gesture_emit_event(EVENT_BOTTOM_SLIDE, bottom_slider);
_gesture_emit_event(EVENT_SLIDE, bottom_slider);
}
}

static void _emit_slide_release_event(void)
{
if (_is_slide_released(top_slider)) {
_gesture_emit_event(EVENT_TOP_SLIDE_RELEASED, top_slider);
_gesture_emit_event(EVENT_SLIDE_RELEASED, top_slider);
}
if (_is_slide_released(bottom_slider)) {
_gesture_emit_event(EVENT_BOTTOM_SLIDE_RELEASED, bottom_slider);
_gesture_emit_event(EVENT_SLIDE_RELEASED, bottom_slider);
}
}

static void _emit_long_tap_event(void)
{
if (_is_long_tap_release(top_slider)) {
_gesture_emit_event(EVENT_TOP_LONG_TAP, top_slider);
_gesture_emit_event(EVENT_LONG_TAP, top_slider);
}
if (_is_long_tap_release(bottom_slider)) {
_gesture_emit_event(EVENT_BOTTOM_LONG_TAP, bottom_slider);
_gesture_emit_event(EVENT_LONG_TAP, bottom_slider);
}
}

static void _emit_short_tap_event(void)
{
if (_is_tap_release(top_slider)) {
_gesture_emit_event(EVENT_TOP_SHORT_TAP, top_slider);
_gesture_emit_event(EVENT_SHORT_TAP, top_slider);
}
if (_is_tap_release(bottom_slider)) {
_gesture_emit_event(EVENT_BOTTOM_SHORT_TAP, bottom_slider);
_gesture_emit_event(EVENT_SHORT_TAP, bottom_slider);
}
}

static void _emit_continuous_tap_event(void)
{
if (_is_continuous_tap(top_slider)) {
_gesture_emit_event(EVENT_TOP_CONTINUOUS_TAP, top_slider);
_gesture_emit_event(EVENT_CONTINUOUS_TAP, top_slider);
}
if (_is_continuous_tap(bottom_slider)) {
_gesture_emit_event(EVENT_BOTTOM_CONTINUOUS_TAP, bottom_slider);
_gesture_emit_event(EVENT_CONTINUOUS_TAP, bottom_slider);
}
}

Expand Down
6 changes: 0 additions & 6 deletions src/touch/gestures.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@
#define SLIDER_POSITION_ONE_THIRD (MAX_SLIDER_POS / 3)
#define SLIDER_POSITION_TWO_THIRD (MAX_SLIDER_POS / 3 * 2)

typedef struct {
int16_t diff;
uint16_t position;
int32_t velocity;
} gestures_slider_data_t;

/**
* Detects a gestures and calls the respective callback.
* @param[in] reset The flag indicates whether the gesture history should be
Expand Down
17 changes: 6 additions & 11 deletions src/ui/components/button.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,13 @@ static void _render(component_t* component)
static void _on_event(const event_t* event, component_t* component)
{
button_data_t* data = (button_data_t*)component->data;
const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data;
if (data->span_over_slider) {
if (event->id ==
((data->location == top_slider) ? EVENT_TOP_SHORT_TAP : EVENT_BOTTOM_SHORT_TAP)) {
if (event->id == EVENT_SHORT_TAP && event->data.source == data->location) {
if (data->span_over_slider) {
data->callback(component);
}
} else {
if (event->id ==
((data->location == top_slider) ? EVENT_TOP_SHORT_TAP : EVENT_BOTTOM_SHORT_TAP) &&
slider_data->position >= component->position.left * MAX_SLIDER_POS / SCREEN_WIDTH &&
slider_data->position <= (component->position.left + component->dimension.width) *
MAX_SLIDER_POS / SCREEN_WIDTH) {
} else if (
event->data.position >= component->position.left * MAX_SLIDER_POS / SCREEN_WIDTH &&
event->data.position <= (component->position.left + component->dimension.width) *
MAX_SLIDER_POS / SCREEN_WIDTH) {
data->callback(component);
}
}
Expand Down
47 changes: 19 additions & 28 deletions src/ui/components/confirm_gesture.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,41 +100,32 @@ static void _render(component_t* component)
*/
static void _on_event(const event_t* event, component_t* component)
{
// Avoid type casting to unknown events
if (event->id != EVENT_TOP_SLIDE_RELEASED && event->id != EVENT_BOTTOM_SLIDE_RELEASED &&
event->id != EVENT_TOP_CONTINUOUS_TAP && event->id != EVENT_BOTTOM_CONTINUOUS_TAP &&
event->id != EVENT_TOP_SHORT_TAP && event->id != EVENT_BOTTOM_SHORT_TAP &&
event->id != EVENT_TOP_LONG_TAP && event->id != EVENT_BOTTOM_LONG_TAP &&
event->id != EVENT_TOP_SLIDE && event->id != EVENT_BOTTOM_SLIDE) {
return;
}
confirm_data_t* data = (confirm_data_t*)component->data;
const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data;

switch (event->id) {
case EVENT_TOP_SLIDE_RELEASED:
data->active_top = false;
break;
case EVENT_TOP_CONTINUOUS_TAP:
if (slider_data->position > SLIDER_POSITION_TWO_THIRD &&
slider_data->position <= MAX_SLIDER_POS) {
data->active_top = true;
case EVENT_SLIDE_RELEASED:
if (event->data.source == top_slider) {
data->active_top = false;
} else {
data->active_bottom = false;
}
break;
case EVENT_TOP_SHORT_TAP:
data->active_top = false;
break;
case EVENT_BOTTOM_SLIDE_RELEASED:
data->active_bottom = false;
break;
case EVENT_BOTTOM_CONTINUOUS_TAP:
if (slider_data->position > SLIDER_POSITION_TWO_THIRD &&
slider_data->position <= MAX_SLIDER_POS) {
data->active_bottom = true;
case EVENT_CONTINUOUS_TAP:
if (event->data.position > SLIDER_POSITION_TWO_THIRD &&
event->data.position <= MAX_SLIDER_POS) {
if (event->data.source == top_slider) {
data->active_top = true;
} else {
data->active_bottom = true;
}
}
break;
case EVENT_BOTTOM_SHORT_TAP:
data->active_bottom = false;
case EVENT_SHORT_TAP:
if (event->data.source == top_slider) {
data->active_top = false;
} else {
data->active_bottom = false;
}
break;
default:
break;
Expand Down
21 changes: 6 additions & 15 deletions src/ui/components/icon_button.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,9 @@ static void _on_event(const event_t* event, component_t* component)

// Return if the slider event is on the wrong slider
switch (event->id) {
case EVENT_TOP_SHORT_TAP:
case EVENT_TOP_CONTINUOUS_TAP:
if (data->location != top_slider) {
data->active = false;
return;
}
break;
case EVENT_BOTTOM_SHORT_TAP:
case EVENT_BOTTOM_CONTINUOUS_TAP:
if (data->location != bottom_slider) {
case EVENT_SHORT_TAP:
case EVENT_CONTINUOUS_TAP:
if (event->data.source != data->location) {
data->active = false;
return;
}
Expand All @@ -127,17 +120,16 @@ static void _on_event(const event_t* event, component_t* component)

// Return if the slider position is away from the button
// Only slider events reach here, so ok to typescast event->data
const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data;
switch (data->type) {
case ICON_BUTTON_CHECK:
case ICON_BUTTON_NEXT:
if (slider_data->position < SLIDER_POSITION_TWO_THIRD) {
if (event->data.position < SLIDER_POSITION_TWO_THIRD) {
data->active = false;
return;
}
break;
case ICON_BUTTON_CROSS:
if (slider_data->position >= SLIDER_POSITION_ONE_THIRD) {
if (event->data.position >= SLIDER_POSITION_ONE_THIRD) {
data->active = false;
return;
}
Expand All @@ -151,8 +143,7 @@ static void _on_event(const event_t* event, component_t* component)

// Call the callback on short tap
switch (event->id) {
case EVENT_TOP_SHORT_TAP:
case EVENT_BOTTOM_SHORT_TAP:
case EVENT_SHORT_TAP:
if (data->callback) {
data->callback(data->user_data);
}
Expand Down
19 changes: 9 additions & 10 deletions src/ui/components/keyboard_switch.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
*/
typedef struct {
keyboard_mode_t mode;
slider_location_t location;
bool active; // Marker is 'active', i.e., touched
// if true, the special chars keyboard mode is available.
bool special_chars;
Expand Down Expand Up @@ -90,18 +89,20 @@ static void _render(component_t* component)
static void _on_event(const event_t* event, component_t* component)
{
keyboard_switch_data_t* ks_data = (keyboard_switch_data_t*)component->data;
const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data;
if (event->data.source != top_slider) {
return;
}
switch (event->id) {
case EVENT_TOP_CONTINUOUS_TAP:
if (ks_data->location == top_slider && slider_data->position > SLIDER_POSITION_ONE_THIRD &&
slider_data->position <= SLIDER_POSITION_TWO_THIRD) {
case EVENT_CONTINUOUS_TAP:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just remove ks_data->location? it is always top_slider, and the event handling wouldn't even work if it was bottom_slider. (separate commit please if you do it)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done as separate commit

if (event->data.position > SLIDER_POSITION_ONE_THIRD &&
event->data.position <= SLIDER_POSITION_TWO_THIRD) {
ks_data->active = true;
break;
}
/* FALLTHROUGH */
case EVENT_TOP_SHORT_TAP:
if (ks_data->location == top_slider && slider_data->position > SLIDER_POSITION_ONE_THIRD &&
slider_data->position <= SLIDER_POSITION_TWO_THIRD) {
case EVENT_SHORT_TAP:
if (event->data.position > SLIDER_POSITION_ONE_THIRD &&
event->data.position <= SLIDER_POSITION_TWO_THIRD) {
ks_data->active = false;
switch (ks_data->mode) {
case LOWER_CASE:
Expand Down Expand Up @@ -144,7 +145,6 @@ static component_functions_t _component_functions = {
/********************************** Create Instance **********************************/

component_t* keyboard_switch_create(
slider_location_t location,
bool special_chars,
bool default_to_digits,
component_t* parent,
Expand All @@ -163,7 +163,6 @@ component_t* keyboard_switch_create(
}
memset(ks_data, 0, sizeof(keyboard_switch_data_t));

ks_data->location = location;
ks_data->mode = default_to_digits ? DIGITS : LOWER_CASE;
ks_data->active = false;
ks_data->special_chars = special_chars;
Expand Down
1 change: 0 additions & 1 deletion src/ui/components/keyboard_switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ typedef enum { LOWER_CASE, UPPER_CASE, DIGITS, SPECIAL_CHARS } keyboard_mode_t;
* @param[in] parent The parent component.
*/
component_t* keyboard_switch_create(
slider_location_t location,
bool special_chars,
bool default_to_digits,
component_t* parent,
Expand Down
19 changes: 9 additions & 10 deletions src/ui/components/label.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,36 +138,35 @@ static void _render(component_t* component)
static void _on_event(const event_t* event, component_t* component)
{
data_t* data = (data_t*)component->data;
if (event->data.source != bottom_slider) {
return;
}
if (data->scrollable) {
switch (event->id) {
case EVENT_BOTTOM_SLIDE: {
const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data;
case EVENT_SLIDE: {
// Variable scroll speed
int16_t margin = SCREEN_WIDTH / 5;
data->slider_position_diff += SIGMOID(slider_data->velocity);
data->slider_position_diff += SIGMOID(event->data.velocity);
data->text_position = data->text_position_last + (int16_t)data->slider_position_diff;
data->text_position = MIN(
component->dimension.width / 2 + margin,
MAX(-margin - component->dimension.width / 2 + SCREEN_WIDTH, data->text_position));
data->slider_position = slider_data->position;
data->slider_position = event->data.position;
data->slider_is_touched = true;
data->slider_was_touched = true;
break;
}

case EVENT_BOTTOM_SLIDE_RELEASED:
case EVENT_SLIDE_RELEASED:
data->text_position_last = data->text_position;
data->slider_position_diff = 0;
data->slider_is_touched = false;
break;

case EVENT_BOTTOM_CONTINUOUS_TAP: {
const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data;
data->slider_position = slider_data->position;
case EVENT_CONTINUOUS_TAP:
data->slider_position = event->data.position;
data->slider_is_touched = true;
data->slider_was_touched = true;
break;
}
default:
break;
}
Expand Down
26 changes: 7 additions & 19 deletions src/ui/components/left_arrow.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,21 @@ static void _render(component_t* component)
static void _on_event(const event_t* event, component_t* component)
{
left_arrow_data_t* data = (left_arrow_data_t*)component->data;
const gestures_slider_data_t* slider_data = (const gestures_slider_data_t*)event->data;
if (event->data.source != data->location) {
return;
}
switch (event->id) {
case EVENT_TOP_SHORT_TAP:
case EVENT_BOTTOM_SHORT_TAP:
if (data->location == top_slider && event->id == EVENT_BOTTOM_SHORT_TAP) {
break;
}
if (data->location == bottom_slider && event->id == EVENT_TOP_SHORT_TAP) {
break;
}
if (slider_data->position <= SLIDER_POSITION_ONE_THIRD) {
case EVENT_SHORT_TAP:
if (event->data.position <= SLIDER_POSITION_ONE_THIRD) {
data->active = false;
if (data->callback) {
data->callback(data->user_data);
}
break;
}
/* FALLTHROUGH */
case EVENT_TOP_CONTINUOUS_TAP:
case EVENT_BOTTOM_CONTINUOUS_TAP:
if (data->location == top_slider && event->id == EVENT_BOTTOM_CONTINUOUS_TAP) {
break;
}
if (data->location == bottom_slider && event->id == EVENT_TOP_CONTINUOUS_TAP) {
break;
}
if (slider_data->position <= SLIDER_POSITION_ONE_THIRD) {
case EVENT_CONTINUOUS_TAP:
if (event->data.position <= SLIDER_POSITION_ONE_THIRD) {
data->active = true;
break;
}
Expand Down
Loading