summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/rk27xx/ihifi2/button-ihifi.c
blob: 172853a83a088e4ece6f9da4f851f256893ba408 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2016 by Roman Stolyarov
 *
 * 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 "kernel.h"
#include "button.h"
#include "adc.h"
#include "backlight.h"

static bool soft_hold = false;
#ifndef BOOTLOADER
static unsigned hold_counter = 0;
#ifndef IHIFI800
#define HOLDBUTTON gpio_btn
#define HOLDCNTMAX HZ
#else
#define HOLDBUTTON (gpio_btn) && (adc_val > 325) && (adc_val < 480)
#define HOLDCNTMAX (HZ/10)
#endif
#endif

void button_init_device(void) {
    GPIO_PCCON &= ~(1<<1); /* PWR BTN */
    GPIO_PCCON &= ~(1<<7); /* CD */
}

bool button_hold(void)
{
    return soft_hold;
}

int button_read_device(void) {
    int adc_val = adc_read(ADC_BUTTONS);
    int gpio_btn = GPIO_PCDR & (1<<1);

    int button = BUTTON_NONE;

    if (gpio_btn)
        button |= BUTTON_POWER;

#ifndef BOOTLOADER
    if (HOLDBUTTON) {
        if (++hold_counter == HOLDCNTMAX) {
            soft_hold = !soft_hold;
            backlight_hold_changed(soft_hold);
        }
    } else {
        hold_counter = 0;
    }
    if (soft_hold) {
        return (hold_counter <= HOLDCNTMAX) ? BUTTON_NONE : button;
    }
#endif

    if (adc_val < 792) {
        if (adc_val < 480) {
            if (adc_val < 170) {
                if (adc_val < 46) {
                    button |= BUTTON_HOME;  // 0-45
                } else {
                    button |= BUTTON_PLAY;  // 46-169
                }
            } else {
                if (adc_val < 325) {
                    button |= BUTTON_NEXT;  // 170-324
                } else {
                    button |= BUTTON_VOL_UP;// 325-479
                }
            }
        } else {
            if (adc_val < 636) {
                button |= BUTTON_VOL_DOWN;// 480-635
            } else {
                button |= BUTTON_PREV; // 636-791
            }
        }
    }

    return button;
}