Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
304 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
427 farthen 6
//    This file is part of emCORE.
304 theseven 7
//
427 farthen 8
//    emCORE is free software: you can redistribute it and/or
304 theseven 9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
427 farthen 13
//    emCORE is distributed in the hope that it will be useful,
304 theseven 14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
427 farthen 19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
304 theseven 20
//
21
//
22
 
391 theseven 23
 
304 theseven 24
#include "global.h"
25
#include "clickwheel.h"
26
#include "button.h"
27
#include "thread.h"
28
#include "timer.h"
29
#include "s5l8702.h"
30
#include "contextswitch.h"
31
 
32
 
33
static struct wakeup clickwheel_wakeup IBSS_ATTR;
34
static volatile uint32_t clickwheel_packet IBSS_ATTR;
35
static uint32_t clickwheel_stack[0x100];
36
static bool oldtouched IBSS_ATTR;
37
static int oldpos IBSS_ATTR;
38
static int oldbuttons IBSS_ATTR;
39
static uint32_t lastpacket IBSS_ATTR;
40
static int packets IBSS_ATTR;
41
static int collect IBSS_ATTR;
42
static int lastdiff IBSS_ATTR;
43
 
44
 
45
void clickwheel_thread(void) ICODE_ATTR;
46
void clickwheel_thread()
47
{
48
    int i;
49
    while (true)
50
    {
51
        wakeup_wait(&clickwheel_wakeup, TIMEOUT_BLOCK);
52
        DEBUGF("Got clickwheel packet");
53
        uint32_t mode = enter_critical_section();
54
        uint32_t data = clickwheel_packet;
55
        leave_critical_section(mode);
56
        DEBUGF("Acquired clickwheel packet: %08X", data);
57
        if ((data & 0x800000FF) == 0x8000001A)
58
        {
59
            int newbuttons = (data >> 8) & 0x1f;
60
            int newpos = (data >> 16) & 0xff;
61
            bool newtouched = (data & 0x40000000) ? true : false;
62
 
63
            DEBUGF("This is a change packet, button state: %02X, position: %02d, touched: %d",
64
                   newbuttons, newpos, newtouched);
65
            int buttonschanged = oldbuttons ^ newbuttons;
66
            DEBUGF("Changed buttons: %02X", buttonschanged);
67
            for (i = 0; i < 5; i++)
68
                if ((buttonschanged >> i) & 1)
69
                {
70
                    if ((oldbuttons >> i) & 1) button_send_event(BUTTON_RELEASE, i, 0);
71
                    else button_send_event(BUTTON_PRESS, i, 0);
72
                }
73
 
74
            if (newtouched)
75
            {
390 theseven 76
                int distance = 0;
304 theseven 77
                if (!oldtouched) button_send_event(WHEEL_TOUCH, 0, newpos);
390 theseven 78
                else distance = newpos - oldpos;
304 theseven 79
                button_send_event(WHEEL_POSITION, 0, newpos);
80
                DEBUGF("Time since last packet: %d microseconds", USEC_TIMER - lastpacket);
81
                if (TIMEOUT_EXPIRED(lastpacket, 200000))
82
                {
83
                    DEBUGF("Resetting accel due to timeout");
84
                    packets = 10;
85
                }
86
                else if (lastdiff * distance < 0)
87
                {
88
                    DEBUGF("Resetting accel due to direction change");
89
                    packets = 10;
90
                }
91
                else packets++;
92
                lastdiff = distance;
93
                if (packets > 200) packets = 200;
94
                if (distance < -48) distance += 96;
95
                else if (distance > 48) distance -= 96;
96
                DEBUGF("Wheel moved %d units without accel", distance);
97
                DEBUGF("Wheel moved %d units with accel", distance * packets);
98
                button_send_event(WHEEL_MOVED, 0, distance);
99
                collect += distance * packets;
100
                enum button_event e = collect > 0 ? WHEEL_FORWARD : WHEEL_BACKWARD;
101
                int data = (collect > 0 ? collect : -collect) / 128;
102
                if (data) button_send_event(e, 0, data);
103
                collect %= 128;
104
                DEBUGF("Wheel moved %d steps (%d left)", data, collect);
105
            }
106
            else if (oldtouched)
107
            {
108
                DEBUGF("Wheel was untouched");
109
                button_send_event(WHEEL_POSITION, 0, newpos);
110
                button_send_event(WHEEL_UNTOUCH, 0, newpos);
111
                collect = 0;
112
                packets = 0;
113
                lastdiff = 0;
114
            }
115
 
116
            oldbuttons = newbuttons;
117
            oldpos = newpos;
118
            oldtouched = newtouched;
119
            lastpacket = USEC_TIMER;
120
        }
121
        else if ((data & 0x8000FFFF) == 0x8000023A)
122
        {
123
            if (data & 0x1F0000) oldbuttons = (data >> 16) & 0x1F;
124
            DEBUGF("This is an init packet, button state: %02X", oldbuttons);
125
        }
126
    }
127
}
128
 
129
 
130
void clickwheel_init()
131
{
132
    wakeup_init(&clickwheel_wakeup);
133
    oldtouched = false;
134
    oldbuttons = 0;
135
    lastpacket = 0;
136
    collect = 0;
137
    lastdiff = 0;
138
    interrupt_enable(IRQ_WHEEL, true);
139
    PUNA(2) &= ~2;
140
    PCON(14) = (PCON(14) & ~0xffff0000) | 0x22220000;
141
    WHEELINT = 7;
142
    WHEEL10 = 7;
143
    WHEEL00 = 0x380000;
144
    WHEEL08 = 0x20000;
145
    do
146
    {
147
        WHEELTX = 0x8001052A;
148
        while (WHEEL0C & 4) yield();
149
        WHEEL04 = 1;
150
        sleep(20000);
151
    }
152
    while (WHEEL0C & 4);
153
    thread_create("Clickwheel dispatcher", clickwheel_thread, clickwheel_stack,
154
                  sizeof(clickwheel_stack), OS_THREAD, 200, true);
155
}
156
 
157
void INT_WHEEL(void) ICODE_ATTR;
158
void INT_WHEEL()
159
{
160
    uint32_t events = WHEELINT;
161
    if (events & 4) WHEELINT = 4;
162
    if (events & 2) WHEELINT = 2;
163
    if (events & 1)
164
    {
165
        clickwheel_packet = WHEELRX;
166
        wakeup_signal(&clickwheel_wakeup);
167
        WHEELINT = 1;
168
    }
169
}
170
 
171
uint32_t clickwheel_get_state()
172
{
173
    return (oldtouched << 15) | (oldpos << 8) | oldbuttons;
174
}