summaryrefslogtreecommitdiff
path: root/firmware/target/arm/s3c2440/gigabeat-fx/button-meg-fx.c
blob: 0e69cbd420e154fb6774c1c4f312ea1f534defa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 by Linus Nielsen Feltzing
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "config.h"
#include "cpu.h"
#include "system.h"
#include "button.h"
#include "kernel.h"
#include "backlight.h"
#include "adc.h"
#include "backlight-target.h"

static bool headphones_detect;
static bool hold_button        = false;

#define TOUCHPAD_SENS_NORMAL ((1 << 12) | /* right++ */ \
                              (1 <<  7) | /* left++ */ \
                              (1 <<  6) | /* down++*/ \
                              (1 <<  0) | /* up++ */ \
                              (1 <<  3))  /* center */

#define TOUCHPAD_SENS_HIGH   (((1 << 12) | (1 << 11)) | /* right++, right+ */ \
                              ((1 <<  8) | (1 <<  7)) | /* left+, left++ */ \
                              ((1 <<  6) | (1 <<  5)) | /* down++, down+ */ \
                              ((1 <<  1) | (1 <<  0)) | /* up+, up++ */ \
                                           (1 <<  3))   /* Center */

static int touchpad_mask = TOUCHPAD_SENS_NORMAL;

static int const remote_buttons[] =
{
    BUTTON_NONE,        /* Headphones connected - remote disconnected */
    BUTTON_RC_PLAY,
    BUTTON_RC_DSP,
    BUTTON_RC_REW,
    BUTTON_RC_FF,
    BUTTON_RC_VOL_UP,
    BUTTON_RC_VOL_DOWN,
    BUTTON_NONE,        /* Remote control attached - no buttons pressed */
    BUTTON_NONE,        /* Nothing in the headphone socket */
};

void button_init_device(void)
{
    /* Some button inputs need the internal pullups disabled to function */
    GPGUP|=0x031F;
    GPJUP|=0x1FFF;
}

inline bool button_hold(void)
{
    return (GPGDAT & (1 << 15));
}

int button_read_device(void)
{
    int touchpad;
    int buttons;
    static int lastbutton;
    unsigned short remote_adc;
    int btn = BUTTON_NONE;
    bool hold_button_old;

    /* normal buttons */
    hold_button_old = hold_button;
    hold_button = button_hold();

#ifndef BOOTLOADER
    /* give BL notice if HB state chaged */
    if (hold_button != hold_button_old)
        backlight_hold_changed(hold_button);
#endif

    /* See header for ADC values when remote control buttons are pressed */
    /* Only one button can be sensed at a time on the remote. */
    /* Need to filter the remote button because the ADC is so fast */
    remote_adc = adc_read(ADC_HPREMOTE);

    if (remote_adc != ADC_READ_ERROR)
    {
        /* If there is nothing in the headphone socket, the ADC reads high */
        if (remote_adc < 940)
            headphones_detect = true;
        else
            headphones_detect = false;
    }

    btn = remote_buttons[(remote_adc + 64) / 128];
    if (btn != lastbutton)
    {
        /* if the buttons dont agree twice in a row, then its none */
        lastbutton = btn;
        btn = BUTTON_NONE;
        buttonlight_on();
    }

    /* Check for hold first - exit if asserted with no button pressed */
    if (hold_button)
        return btn;

    /* the side buttons - Check before doing all of the work on each bit */
    buttons = GPGDAT & 0x1F;
    if (buttons)
    {
        btn |= buttons;
        buttonlight_on();
    }
    
    /* the touchpad - only watch the lines we actually read */
    touchpad = GPJDAT & touchpad_mask;

    if (touchpad)
    {
        if (touchpad & (1 << 3))
        {
            btn |= BUTTON_SELECT;
            /* Desensitize all but outer detectors and still allow a combo if
             * that's really intended. */
            touchpad &= TOUCHPAD_SENS_NORMAL;
        }

        /* Simply include all lines in checks since "touchpad" has been
         * masked to desired sensitivity already - allows any mask to be
         * used without changing this code. */
        if (touchpad & ((1 << 2) | (1 << 1) | (1 << 0)))
            btn |= BUTTON_UP;

        if (touchpad & ((1 << 12) | (1 << 11) | (1 << 10)))
            btn |= BUTTON_RIGHT;

        if (touchpad & ((1 << 6) | (1 << 5) | (1 << 4)))
            btn |= BUTTON_DOWN;

        if (touchpad & ((1 << 9) | (1 << 8) | (1 << 7)))
            btn |= BUTTON_LEFT;

        buttonlight_on();
    }
    
    return btn;
}

void touchpad_set_sensitivity(int level)
{
    static const int masks[] =
    {
        TOUCHPAD_SENS_NORMAL,
        TOUCHPAD_SENS_HIGH
    };

    if ((unsigned)level >= ARRAYLEN(masks))
        level = 0;

    touchpad_mask = masks[level];
}

bool headphones_inserted(void)
{
    return headphones_detect;
}