Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
304 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
6
//    This file is part of emBIOS.
7
//
8
//    emBIOS is free software: you can redistribute it and/or
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
//
13
//    emBIOS is distributed in the hope that it will be useful,
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
19
//    with emBIOS.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
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
            {
76
                if (!oldtouched) button_send_event(WHEEL_TOUCH, 0, newpos);
77
                button_send_event(WHEEL_POSITION, 0, newpos);
78
                int distance = newpos - oldpos;
79
                DEBUGF("Time since last packet: %d microseconds", USEC_TIMER - lastpacket);
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
                }
90
                else packets++;
91
                lastdiff = distance;
92
                if (packets > 200) packets = 200;
93
                if (distance < -48) distance += 96;
94
                else if (distance > 48) distance -= 96;
95
                DEBUGF("Wheel moved %d units without accel", distance);
96
                DEBUGF("Wheel moved %d units with accel", distance * packets);
97
                button_send_event(WHEEL_MOVED, 0, distance);
98
                collect += distance * packets;
99
                enum button_event e = collect > 0 ? WHEEL_FORWARD : WHEEL_BACKWARD;
100
                int data = (collect > 0 ? collect : -collect) / 128;
101
                if (data) button_send_event(e, 0, data);
102
                collect %= 128;
103
                DEBUGF("Wheel moved %d steps (%d left)", data, collect);
104
            }
105
            else if (oldtouched)
106
            {
107
                DEBUGF("Wheel was untouched");
108
                button_send_event(WHEEL_POSITION, 0, newpos);
109
                button_send_event(WHEEL_UNTOUCH, 0, newpos);
110
                collect = 0;
111
                packets = 0;
112
                lastdiff = 0;
113
            }
114
 
115
            oldbuttons = newbuttons;
116
            oldpos = newpos;
117
            oldtouched = newtouched;
118
            lastpacket = USEC_TIMER;
119
        }
120
        else if ((data & 0x8000FFFF) == 0x8000023A)
121
        {
122
            if (data & 0x1F0000) oldbuttons = (data >> 16) & 0x1F;
123
            DEBUGF("This is an init packet, button state: %02X", oldbuttons);
124
        }
125
    }
126
}
127
 
128
 
129
void clickwheel_init()
130
{
131
    wakeup_init(&clickwheel_wakeup);
132
    oldtouched = false;
133
    oldbuttons = 0;
134
    lastpacket = 0;
135
    collect = 0;
136
    lastdiff = 0;
137
    PWRCON(0) = 0;
138
    PWRCON(1) = 0;
139
    interrupt_enable(IRQ_WHEEL, true);
140
    PUNA(2) &= ~2;
141
    PCON(14) = (PCON(14) & ~0xffff0000) | 0x22220000;
142
    WHEELINT = 7;
143
    WHEEL10 = 7;
144
    WHEEL00 = 0x380000;
145
    WHEEL08 = 0x20000;
146
    do
147
    {
148
        WHEELTX = 0x8001052A;
149
        while (WHEEL0C & 4) yield();
150
        WHEEL04 = 1;
151
        sleep(20000);
152
    }
153
    while (WHEEL0C & 4);
154
    thread_create("Clickwheel dispatcher", clickwheel_thread, clickwheel_stack,
155
                  sizeof(clickwheel_stack), OS_THREAD, 200, true);
156
}
157
 
158
void INT_WHEEL(void) ICODE_ATTR;
159
void INT_WHEEL()
160
{
161
    uint32_t events = WHEELINT;
162
    if (events & 4) WHEELINT = 4;
163
    if (events & 2) WHEELINT = 2;
164
    if (events & 1)
165
    {
166
        clickwheel_packet = WHEELRX;
167
        wakeup_signal(&clickwheel_wakeup);
168
        WHEELINT = 1;
169
    }
170
}
171
 
172
uint32_t clickwheel_get_state()
173
{
174
    return (oldtouched << 15) | (oldpos << 8) | oldbuttons;
175
}