Project

General

Profile

Revision 91

binary shenanigans to reduce code

View differences:

state_machine.c
7 7
#include "utils.h"
8 8

  
9 9
struct mouse_ev* mouse_get_event(struct packet *pp) {
10

  
11 10
    static struct mouse_ev event;
12
    static struct packet last; // compare consecutive events
13
    static int aux = 1; // first iteration
11
    static uint8_t last = 0;
14 12

  
15 13
    if (pp == NULL)
16 14
        return &event;
17 15

  
18
    // current packet
19
    uint8_t lb_press = pp->bytes[0] & LEFT_BUTTON;
20
    uint8_t rb_press = pp->bytes[0] & RIGHT_BUTTON;
21
    uint8_t mb_press = pp->bytes[0] & MIDDLE_BUTTON;
16
    // current button presses
17
    uint8_t button_presses = pp->bytes[0] & (LEFT_BUTTON | RIGHT_BUTTON | MIDDLE_BUTTON);
22 18
    int16_t delta_x = pp->delta_x;
23 19
    int16_t delta_y = pp->delta_y;
24 20

  
25
    if (aux) { // first event detected
26
        if (lb_press && (rb_press | mb_press) == 0 && delta_x == 0 && delta_y == 0)
27
            event.type = LB_PRESSED;
28
        else if (rb_press && (lb_press | mb_press) == 0 && delta_x == 0 && delta_y == 0)
29
            event.type = RB_PRESSED;
30
        else if ((delta_x || delta_y) && (lb_press | rb_press | mb_press) == 0) {
31
            event.type = MOUSE_MOV;
32
            event.delta_x = delta_x;
33
            event.delta_y = delta_y;
34
        } else
35
            event.type = BUTTON_EV;
36
        aux = 0;
37
    } else {
38
        // last packet
39
        uint8_t last_lb_press = last.bytes[0] & LEFT_BUTTON;
40
        uint8_t last_rb_press = last.bytes[0] & RIGHT_BUTTON;
41
        uint8_t last_mb_press = last.bytes[0] & MIDDLE_BUTTON;
21
    if ((button_presses ^ last) == LEFT_BUTTON && !(last & LEFT_BUTTON)) {
22
        event.type = LB_PRESSED;
23
        last |= LEFT_BUTTON;
42 24

  
43
        if (lb_press && (rb_press | mb_press) == 0 && delta_x == 0 && delta_y == 0 && last_lb_press == 0)
44
            event.type = LB_PRESSED;
45
        else if (rb_press && (lb_press | mb_press) == 0 && delta_x == 0 && delta_y == 0 && last_rb_press == 0)
46
            event.type = RB_PRESSED;
47
        else if ((lb_press | rb_press | mb_press) == 0 && delta_x == 0 && delta_y == 0 && last_lb_press)
48
            event.type = LB_RELEASED;
49
        else if ((lb_press | rb_press | mb_press) == 0 && delta_x == 0 && delta_y == 0 && last_rb_press)
50
            event.type = RB_RELEASED;
51
        else if ((delta_x || delta_y) && (lb_press | rb_press | mb_press) == (last_lb_press | last_rb_press | last_mb_press)) {
52
            event.type = MOUSE_MOV;
53
            event.delta_x = delta_x;
54
            event.delta_y = delta_y;
55
        } else
56
            event.type = BUTTON_EV;
57
    }
25
    } else if ((button_presses ^ last) == RIGHT_BUTTON && !(last & RIGHT_BUTTON)) {
26
        event.type = RB_PRESSED;
27
        last |= RIGHT_BUTTON;
58 28

  
59
    // update last packet for comparison
60
    last = *pp;
29
    } else if ((button_presses ^ last) == LEFT_BUTTON && (last & LEFT_BUTTON)) {
30
        event.type = LB_RELEASED;
31
        last &= ~LEFT_BUTTON;
61 32

  
33
    } else if ((button_presses ^ last) == RIGHT_BUTTON && (last & RIGHT_BUTTON)) {
34
        event.type = RB_RELEASED;
35
        last &= ~RIGHT_BUTTON;
36

  
37
    } else if ((delta_x || delta_y) && (button_presses == last)) {
38
        event.type = MOUSE_MOV;
39

  
40
    } else
41
        event.type = BUTTON_EV;
42

  
43
    event.delta_x = delta_x;
44
    event.delta_y = delta_y;
62 45
    return &event;
63 46
}
64 47

  
......
89 72
                x_length += event->delta_x;
90 73
                y_length += event->delta_y;
91 74
            } else if (event->type == LB_RELEASED) {
92
                if (x_length == 0 || y_length == 0) {
75
                if (y_length < x_length || x_length < x_len) {
93 76
                    state = INITIAL;
94
                    break;
95
                }
96

  
97
                int slope = y_length / x_length;
98

  
99
                if (slope <= 1 || x_length < x_len) {
100
                    state = INITIAL;
101 77
                    x_length = 0;
102 78
                    y_length = 0;
103 79
                    break;
......
139 115
                    state = INITIAL;
140 116
                    break;
141 117
                }
142
                int slope = y_length / x_length;
143 118

  
144
                if (slope >= -1 || x_length < x_len) {
119
                if (y_length > -x_length || x_length < x_len) {
145 120
                    state = INITIAL;
146 121
                    x_length = 0;
147 122
                    y_length = 0;

Also available in: Unified diff