Subversion Repositories freemyipod

Rev

Go to most recent revision | 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);
52
        uint32_t mode = enter_critical_section();
53
        uint32_t data = clickwheel_packet;
54
        leave_critical_section(mode);
55
        if ((data & 0x800000FF) == 0x8000001A)
56
        {
57
            int newbuttons = (data >> 8) & 0x1f;
58
            int newpos = (data >> 16) & 0xff;
59
            bool newtouched = (data & 0x40000000) ? true : false;
60
 
61
            int buttonschanged = oldbuttons ^ newbuttons;
62
            for (i = 0; i < 5; i++)
63
                if ((buttonschanged >> i) & 1)
64
                {
65
                    if ((oldbuttons >> i) & 1) button_send_event(BUTTON_RELEASE, i, 0);
66
                    else button_send_event(BUTTON_PRESS, i, 0);
67
                }
68
 
69
            if (newtouched)
70
            {
71
                if (!oldtouched) button_send_event(WHEEL_TOUCH, 0, newpos);
72
                button_send_event(WHEEL_POSITION, 0, newpos);
138 theseven 73
                int distance = newpos - oldpos;
74
                if (TIMEOUT_EXPIRED(lastpacket, 200000) || lastdiff * distance < 0) packets = 10;
132 theseven 75
                else packets++;
138 theseven 76
                lastdiff = distance;
77
                if (packets > 200) packets = 200;
132 theseven 78
                if (distance < -48) distance += 96;
79
                else if (distance > 48) distance -= 96;
138 theseven 80
                collect += distance * packets;
132 theseven 81
                button_send_event(WHEEL_MOVED, 0, distance);
138 theseven 82
                enum button_event e = collect > 0 ? WHEEL_FORWARD : WHEEL_BACKWARD;
83
                int data = (collect > 0 ? collect : -collect) / 128;
84
                if (data) button_send_event(e, 0, data);
85
                collect %= 128;
132 theseven 86
            }
87
            else if (oldtouched)
88
            {
89
                button_send_event(WHEEL_POSITION, 0, newpos);
90
                button_send_event(WHEEL_UNTOUCH, 0, newpos);
138 theseven 91
                collect = 0;
92
                packets = 0;
93
                lastdiff = 0;
132 theseven 94
            }
95
 
96
            oldbuttons = newbuttons;
97
            oldpos = newpos;
98
            oldtouched = newtouched;
99
            lastpacket = USEC_TIMER;
100
        }
101
        else if ((data & 0x8000FFFF) == 0x8000023A && (data & 0x1F0000))
102
            oldbuttons = (data >> 16) & 0x1F;
103
    }
104
}
105
 
106
 
107
void clickwheel_init()
108
{
109
    wakeup_init(&clickwheel_wakeup);
110
    oldtouched = false;
111
    oldbuttons = 0;
112
    lastpacket = 0;
138 theseven 113
    collect = 0;
114
    lastdiff = 0;
132 theseven 115
    INTMSK |= 1 << IRQ_WHEEL;
116
    PWRCON(1) &= ~1;
117
    PCON15 = (PCON15 & ~0xFFFF0000) | 0x22220000;
118
    PUNK15 = 0xF0;
119
    WHEEL08 = 0x3A980;
120
    WHEEL00 = 0x280000;
121
    WHEEL10 = 3;
122
    PCON10 = (PCON10 & ~0xFF0) | 0x10;
123
    PDAT10 |= 2;
124
    WHEELTX = 0x8000023A;
125
    WHEEL04 |= 1;
126
    PDAT10 &= ~2;
127
    thread_create("Clickwheel dispatcher", clickwheel_thread, clickwheel_stack,
128
                  sizeof(clickwheel_stack), OS_THREAD, 200, true);
129
}
130
 
131
void INT_WHEEL(void) ICODE_ATTR;
132
void INT_WHEEL()
133
{
134
    uint32_t events = WHEELINT;
135
    if (events & 4) WHEELINT = 4;
136
    if (events & 2) WHEELINT = 2;
137
    if (events & 1)
138
    {
139
        clickwheel_packet = WHEELRX;
140
        wakeup_signal(&clickwheel_wakeup);
141
        WHEELINT = 1;
142
    }
143
}
144
 
145
uint32_t clickwheel_get_state()
146
{
147
    return (oldtouched << 15) | (oldpos << 8) | oldbuttons;
148
}