Subversion Repositories freemyipod

Rev

Rev 138 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 138 Rev 198
Line 47... Line 47...
47
{
47
{
48
    int i;
48
    int i;
49
    while (true)
49
    while (true)
50
    {
50
    {
51
        wakeup_wait(&clickwheel_wakeup, TIMEOUT_BLOCK);
51
        wakeup_wait(&clickwheel_wakeup, TIMEOUT_BLOCK);
-
 
52
        DEBUGF("Got clickwheel packet");
52
        uint32_t mode = enter_critical_section();
53
        uint32_t mode = enter_critical_section();
53
        uint32_t data = clickwheel_packet;
54
        uint32_t data = clickwheel_packet;
54
        leave_critical_section(mode);
55
        leave_critical_section(mode);
-
 
56
        DEBUGF("Acquired clickwheel packet: %08X", data);
55
        if ((data & 0x800000FF) == 0x8000001A)
57
        if ((data & 0x800000FF) == 0x8000001A)
56
        {
58
        {
57
            int newbuttons = (data >> 8) & 0x1f;
59
            int newbuttons = (data >> 8) & 0x1f;
58
            int newpos = (data >> 16) & 0xff;
60
            int newpos = (data >> 16) & 0xff;
59
            bool newtouched = (data & 0x40000000) ? true : false;
61
            bool newtouched = (data & 0x40000000) ? true : false;
60
 
62
 
-
 
63
            DEBUGF("This is a change packet, button state: %02X, position: %02d, touched: %d",
-
 
64
                   newbuttons, newpos, newtouched);
61
            int buttonschanged = oldbuttons ^ newbuttons;
65
            int buttonschanged = oldbuttons ^ newbuttons;
-
 
66
            DEBUGF("Changed buttons: %02X", buttonschanged);
62
            for (i = 0; i < 5; i++)
67
            for (i = 0; i < 5; i++)
63
                if ((buttonschanged >> i) & 1)
68
                if ((buttonschanged >> i) & 1)
64
                {
69
                {
65
                    if ((oldbuttons >> i) & 1) button_send_event(BUTTON_RELEASE, i, 0);
70
                    if ((oldbuttons >> i) & 1) button_send_event(BUTTON_RELEASE, i, 0);
66
                    else button_send_event(BUTTON_PRESS, i, 0);
71
                    else button_send_event(BUTTON_PRESS, i, 0);
Line 69... Line 74...
69
            if (newtouched)
74
            if (newtouched)
70
            {
75
            {
71
                if (!oldtouched) button_send_event(WHEEL_TOUCH, 0, newpos);
76
                if (!oldtouched) button_send_event(WHEEL_TOUCH, 0, newpos);
72
                button_send_event(WHEEL_POSITION, 0, newpos);
77
                button_send_event(WHEEL_POSITION, 0, newpos);
73
                int distance = newpos - oldpos;
78
                int distance = newpos - oldpos;
-
 
79
                DEBUGF("Time since last packet: %d microseconds", USEC_TIMER - lastpacket);
74
                if (TIMEOUT_EXPIRED(lastpacket, 200000) || lastdiff * distance < 0) packets = 10;
80
                if (TIMEOUT_EXPIRED(lastpacket, 200000))
-
 
81
                {
-
 
82
                    DEBUGF("Resetting accel due to timeout");
-
 
83
                    packets = 10;
-
 
84
                }
-
 
85
                else if (lastdiff * distance < 0)
-
 
86
                {
-
 
87
                    DEBUGF("Resetting accel due to direction change");
-
 
88
                    packets = 10;
-
 
89
                }
75
                else packets++;
90
                else packets++;
76
                lastdiff = distance;
91
                lastdiff = distance;
77
                if (packets > 200) packets = 200;
92
                if (packets > 200) packets = 200;
78
                if (distance < -48) distance += 96;
93
                if (distance < -48) distance += 96;
79
                else if (distance > 48) distance -= 96;
94
                else if (distance > 48) distance -= 96;
-
 
95
                DEBUGF("Wheel moved %d units without accel", distance);
80
                collect += distance * packets;
96
                DEBUGF("Wheel moved %d units with accel", distance * packets);
81
                button_send_event(WHEEL_MOVED, 0, distance);
97
                button_send_event(WHEEL_MOVED, 0, distance);
-
 
98
                collect += distance * packets;
82
                enum button_event e = collect > 0 ? WHEEL_FORWARD : WHEEL_BACKWARD;
99
                enum button_event e = collect > 0 ? WHEEL_FORWARD : WHEEL_BACKWARD;
83
                int data = (collect > 0 ? collect : -collect) / 128;
100
                int data = (collect > 0 ? collect : -collect) / 128;
84
                if (data) button_send_event(e, 0, data);
101
                if (data) button_send_event(e, 0, data);
85
                collect %= 128;
102
                collect %= 128;
-
 
103
                DEBUGF("Wheel moved %d steps (%d left)", data, collect);
86
            }
104
            }
87
            else if (oldtouched)
105
            else if (oldtouched)
88
            {
106
            {
-
 
107
                DEBUGF("Wheel was untouched");
89
                button_send_event(WHEEL_POSITION, 0, newpos);
108
                button_send_event(WHEEL_POSITION, 0, newpos);
90
                button_send_event(WHEEL_UNTOUCH, 0, newpos);
109
                button_send_event(WHEEL_UNTOUCH, 0, newpos);
91
                collect = 0;
110
                collect = 0;
92
                packets = 0;
111
                packets = 0;
93
                lastdiff = 0;
112
                lastdiff = 0;
Line 96... Line 115...
96
            oldbuttons = newbuttons;
115
            oldbuttons = newbuttons;
97
            oldpos = newpos;
116
            oldpos = newpos;
98
            oldtouched = newtouched;
117
            oldtouched = newtouched;
99
            lastpacket = USEC_TIMER;
118
            lastpacket = USEC_TIMER;
100
        }
119
        }
101
        else if ((data & 0x8000FFFF) == 0x8000023A && (data & 0x1F0000))
120
        else if ((data & 0x8000FFFF) == 0x8000023A)
-
 
121
        {
102
            oldbuttons = (data >> 16) & 0x1F;
122
            if (data & 0x1F0000) oldbuttons = (data >> 16) & 0x1F;
-
 
123
            DEBUGF("This is an init packet, button state: %02X", oldbuttons);
-
 
124
        }
103
    }
125
    }
104
}
126
}
105
 
127
 
106
 
128
 
107
void clickwheel_init()
129
void clickwheel_init()