Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
132 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 "s5l8701.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;
138 theseven 41
static int collect IBSS_ATTR;
42
static int lastdiff IBSS_ATTR;
132 theseven 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);
198 theseven 52
        DEBUGF("Got clickwheel packet");
132 theseven 53
        uint32_t mode = enter_critical_section();
54
        uint32_t data = clickwheel_packet;
55
        leave_critical_section(mode);
198 theseven 56
        DEBUGF("Acquired clickwheel packet: %08X", data);
132 theseven 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
 
198 theseven 63
            DEBUGF("This is a change packet, button state: %02X, position: %02d, touched: %d",
64
                   newbuttons, newpos, newtouched);
132 theseven 65
            int buttonschanged = oldbuttons ^ newbuttons;
198 theseven 66
            DEBUGF("Changed buttons: %02X", buttonschanged);
132 theseven 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);
138 theseven 78
                int distance = newpos - oldpos;
198 theseven 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
                }
132 theseven 90
                else packets++;
138 theseven 91
                lastdiff = distance;
92
                if (packets > 200) packets = 200;
132 theseven 93
                if (distance < -48) distance += 96;
94
                else if (distance > 48) distance -= 96;
198 theseven 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);
138 theseven 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;
198 theseven 103
                DEBUGF("Wheel moved %d steps (%d left)", data, collect);
132 theseven 104
            }
105
            else if (oldtouched)
106
            {
198 theseven 107
                DEBUGF("Wheel was untouched");
132 theseven 108
                button_send_event(WHEEL_POSITION, 0, newpos);
109
                button_send_event(WHEEL_UNTOUCH, 0, newpos);
138 theseven 110
                collect = 0;
111
                packets = 0;
112
                lastdiff = 0;
132 theseven 113
            }
114
 
115
            oldbuttons = newbuttons;
116
            oldpos = newpos;
117
            oldtouched = newtouched;
118
            lastpacket = USEC_TIMER;
119
        }
198 theseven 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
        }
132 theseven 125
    }
126
}
127
 
128
 
129
void clickwheel_init()
130
{
131
    wakeup_init(&clickwheel_wakeup);
132
    oldtouched = false;
133
    oldbuttons = 0;
134
    lastpacket = 0;
138 theseven 135
    collect = 0;
136
    lastdiff = 0;
132 theseven 137
    INTMSK |= 1 << IRQ_WHEEL;
138
    PWRCON(1) &= ~1;
139
    PCON15 = (PCON15 & ~0xFFFF0000) | 0x22220000;
140
    PUNK15 = 0xF0;
141
    WHEEL08 = 0x3A980;
142
    WHEEL00 = 0x280000;
143
    WHEEL10 = 3;
144
    PCON10 = (PCON10 & ~0xFF0) | 0x10;
145
    PDAT10 |= 2;
146
    WHEELTX = 0x8000023A;
147
    WHEEL04 |= 1;
148
    PDAT10 &= ~2;
149
    thread_create("Clickwheel dispatcher", clickwheel_thread, clickwheel_stack,
150
                  sizeof(clickwheel_stack), OS_THREAD, 200, true);
151
}
152
 
153
void INT_WHEEL(void) ICODE_ATTR;
154
void INT_WHEEL()
155
{
156
    uint32_t events = WHEELINT;
157
    if (events & 4) WHEELINT = 4;
158
    if (events & 2) WHEELINT = 2;
159
    if (events & 1)
160
    {
161
        clickwheel_packet = WHEELRX;
162
        wakeup_signal(&clickwheel_wakeup);
163
        WHEELINT = 1;
164
    }
165
}
166
 
167
uint32_t clickwheel_get_state()
168
{
169
    return (oldtouched << 15) | (oldpos << 8) | oldbuttons;
170
}