summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/rk27xx/hm801/button-hm801.c
blob: d0323e6f4d33940e59da7df13ef4e31ef5e2b395 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2011 Andrew Ryabinin
 *
 * 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 "adc.h"

enum keyboard_type_t {
    KEYBOARD_V1,
    KEYBOARD_V2,
};

static enum keyboard_type_t kbd_type;

void button_init_device(void) {
    /* setup button gpio as input */
    GPIO_PCCON &= ~(POWEROFF_BUTTON);

    /* identify keyboard type */
    SCU_IOMUXB_CON &= ~(1<<2);
    GPIO_PCCON |= (1<<4);
    if (GPIO_PCDR & (1<<4)) {
        kbd_type = KEYBOARD_V1;
    } else {
        kbd_type = KEYBOARD_V2;
    }
}

static int button_read_device_v1(void) {
    int adc_val = adc_read(ADC_BUTTONS);
    int button =  0;

    if (adc_val < 480) { /* middle */
        if (adc_val < 200) { /* 0 - 200 */
            if (adc_val < 30) {
                button = BUTTON_UP;
            } else {  /* 30 - 200 */
                button =  BUTTON_RIGHT;
            }
        } else { /* 200 - 480 */
            if (adc_val < 370) { /* 200 - 370 */
                button =  BUTTON_SELECT;
            } else { /* 370 - 480 */
                button =  BUTTON_DOWN;
            }
        }
    } else { /*  > 480 */
        if (adc_val < 690) { /* 480 - 690 */
            if (adc_val < 580) { /* 480 - 580 */
                button =  BUTTON_LEFT;
            } else { /* 580 - 690 */
                button =  BUTTON_NEXT;
            }
        } else { /* > 680 */
            if (adc_val < 840) { /* 680 - 840 */
                button =  BUTTON_PREV;
            } else {
                if (adc_val < 920) { /* 840 - 920 */
                    button =  BUTTON_PLAY;
                }
            }
        }
    }
    return button | (GPIO_PCDR & POWEROFF_BUTTON);
}

static int button_read_device_v2(void) {
    int adc_val = adc_read(ADC_BUTTONS);
    int adc_val2 = adc_read(ADC_EXTRA);
    int button =  0;

    /* Buttons on front panel */
    if (adc_val < 520) { /* middle */
        if (adc_val < 360) { /* 0 - 360 */
            if (adc_val < 40) { /* 0 - 40 */
                button |= BUTTON_UP;
            } else { /* 40 - 360 */
                button |= BUTTON_RIGHT;
            }
        } else { /* 360 - 520 */
            button |= BUTTON_SELECT;
        }
    } else { /* >= 520 */
        if (adc_val < 770) { /* 520 - 770 */
            if (adc_val < 640) { /* 520 - 640 */
                button |= BUTTON_DOWN;
            } else { /* 640 - 770 */
                button |= BUTTON_LEFT;
            }
        }
    }

    /* Buttons on top */
    if (adc_val2 < 400) { /* 0 - 400 */
        if (adc_val2 < 120) { /* 0 - 120 */
            button |= BUTTON_NEXT;
        } else { /* 120 - 400 */
            button |= BUTTON_PREV;
        }
    } else { /* >= 400 */
        if (adc_val2 < 560) { /* 400 - 560 */
            button |= BUTTON_PLAY;
        }
    }
    return button | (GPIO_PCDR & POWEROFF_BUTTON);
}

int button_read_device(void) {
    if (kbd_type == KEYBOARD_V1) {
        return button_read_device_v1();
    } else {
        return button_read_device_v2();
    }
}