summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/as3525/sansa-fuzev2/button-fuzev2.c
blob: 5bd8afebc127f1b854b62c2586b0e9bf2b39c282 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2010 by Thomas Martitz
 *
 * 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 "system.h"
#include "button.h"
#include "backlight.h"

/*
 * TODO: Scrollwheel!
 */
 
#ifdef HAS_BUTTON_HOLD
static bool hold_button = false;
#endif
void button_init_device(void)
{
}

/*
 * Get button pressed from hardware
 */

int button_read_device(void)
{
    int btn = 0;
    volatile int delay;
    static bool hold_button_old = false;
    static long power_counter = 0;
    unsigned gpiod = GPIOD_DATA;
    unsigned gpioa_dir = GPIOA_DIR;
    unsigned gpiod6;
    for(delay = 500; delay; delay--) nop;
    CCU_IO &= ~(1<<12);
    for(delay=8;delay;delay--) nop;
    GPIOB_DIR |= 1<<3;
    GPIOB_PIN(3) = 1<<3;
    GPIOC_DIR = 0;
    GPIOB_DIR &= ~(1<<1);
    GPIOB_DIR |= 1<<0;
    GPIOB_PIN(0) = 1;
    for(delay = 500; delay; delay--)
        nop;
    gpiod6 = GPIOD_PIN(6);
    GPIOB_PIN(0) = 0;
    for(delay = 240; delay; delay--)
        nop;
    GPIOD_DIR = 0xff;
    GPIOA_DIR &= ~(1<<6|1<<7);
    GPIOD_DATA = 0;
    GPIOD_DIR = 0;
    if (GPIOC_PIN(1) & 1<<1)
        btn |= BUTTON_DOWN;
    if (GPIOC_PIN(2) & 1<<2)
        btn |= BUTTON_UP;
    if (GPIOC_PIN(3) & 1<<3)
        btn |= BUTTON_LEFT;
    if (GPIOC_PIN(4) & 1<<4)
        btn |= BUTTON_SELECT;
    if (GPIOC_PIN(5) & 1<<5)
        btn |= BUTTON_RIGHT;
    if (GPIOB_PIN(1) & 1<<1)
        btn |= BUTTON_HOME;
    if (gpiod6 & 1<<6)
    {   /* power/hold is on the same pin. we know it's hold if the bit isn't
         * set now anymore */
        if (GPIOD_PIN(6) & 1<<6)
        {
            hold_button = false;
            btn |= BUTTON_POWER;
        }
        else
        {
            hold_button = true;
        }
    }

    GPIOD_DIR = 0xff;
    GPIOD_DATA = gpiod;
    GPIOA_DIR = gpioa_dir;
    GPIOD_DIR = 0;
    CCU_IO |= 1<<12;
#ifdef HAS_BUTTON_HOLD
    /* light handling */
    if (hold_button != hold_button_old)
    {
        hold_button_old = hold_button;
        backlight_hold_changed(hold_button);
    }
    if (hold_button)
    {
        power_counter = HZ;
        return 0;
    }
    /* read power, but not if hold button was just released, since
     * you basically always hit power due to the slider mechanism after releasing
     * (fuze only)
     */
    else if (power_counter > 0)
    {
        power_counter--;
        btn &= ~BUTTON_POWER;
    }
#endif
    return btn;
}

#ifdef HAS_BUTTON_HOLD
bool button_hold(void)
{
    return hold_button;
}
#endif