summaryrefslogtreecommitdiff
path: root/firmware/target/arm/as3525/sansa-m200v4/button-m200v4.c
blob: d6bf0acf9b6a4f66d9ac3fbb177de15f57a23c36 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2008 by ??
 *
 * 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 "button.h"
#include "adc.h"

void button_init_device(void)
{
    GPIOA_DIR &= ~((1<<3) | (1<<2) | (1<<1) | (1<<0));  /* A3-A0 is input */
    GPIOA_DIR |= ((1<<6) | (1<<5) | (1<<4)); /* A4-A6 row outputs */
}

/* short delay is needed between raising a colum pin, and reading the row pin. values is arbitraty */
static inline void btn_delay(void) { int i = 5; while(i--) ; }

int button_read_device(void)
{
    int result = BUTTON_NONE;
    if(button_hold())
        return result;
    
    /* direct GPIO connections */
    if (GPIOA_PIN(3))
        result |= BUTTON_POWER;

    /* This is a keypad using A4-A6 as columns and A0-A2 as rows */
    GPIOA_PIN(4) = (1<<4);
    btn_delay();
   
    /* A4A0 is unused */

    if (GPIOA_PIN(1))
        result |= BUTTON_VOL_DOWN;

    if (GPIOA_PIN(2))
        result |= BUTTON_UP;

    GPIOA_PIN(4) = 0x00;  
    
    GPIOA_PIN(5) = (1<<5);
    btn_delay();
    
    if (GPIOA_PIN(0))
        result |= BUTTON_LEFT;

    if (GPIOA_PIN(1))
        result |= BUTTON_SELECT;

    if (GPIOA_PIN(2))
        result |= BUTTON_RIGHT;

    GPIOA_PIN(5) = 0x00;


    GPIOA_PIN(6) = (1<<6);
    btn_delay();
    
    if (GPIOA_PIN(0))
        result |= BUTTON_DOWN;

    if (GPIOA_PIN(1))
        result |= BUTTON_VOL_UP;

    /* hold button is read in button_hold() */
        
    GPIOA_PIN(6) = 0x00;

    return result;
}

bool button_hold(void)
{
    bool ret = false;
    
    GPIOA_PIN(6) = (1<<6);
    if (GPIOA_PIN(2))
        ret = true;
    GPIOA_PIN(6) = 0x00; 
    
    return ret;
}