Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/utils/btn.c
1476 views
1
/*
2
* Copyright (c) 2018 naehrwert
3
* Copyright (c) 2018-2022 CTCaer
4
*
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms and conditions of the GNU General Public License,
7
* version 2, as published by the Free Software Foundation.
8
*
9
* This program is distributed in the hope it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
* more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#include "btn.h"
19
#include <soc/i2c.h>
20
#include <soc/gpio.h>
21
#include <soc/timer.h>
22
#include <soc/t210.h>
23
#include <power/max77620.h>
24
25
u8 btn_read()
26
{
27
u8 res = 0;
28
if (!gpio_read(GPIO_PORT_X, GPIO_PIN_7))
29
res |= BTN_VOL_DOWN;
30
if (!gpio_read(GPIO_PORT_X, GPIO_PIN_6))
31
res |= BTN_VOL_UP;
32
// HOAG can use the GPIO. Icosa/Iowa/AULA cannot. Traces are there but they miss a resistor.
33
if (i2c_recv_byte(I2C_5, MAX77620_I2C_ADDR, MAX77620_REG_ONOFFSTAT) & MAX77620_ONOFFSTAT_EN0)
34
res |= BTN_POWER;
35
return res;
36
}
37
38
u8 btn_read_vol()
39
{
40
u8 res = 0;
41
if (!gpio_read(GPIO_PORT_X, GPIO_PIN_7))
42
res |= BTN_VOL_DOWN;
43
if (!gpio_read(GPIO_PORT_X, GPIO_PIN_6))
44
res |= BTN_VOL_UP;
45
return res;
46
}
47
48
u8 btn_read_home()
49
{
50
return (!gpio_read(GPIO_PORT_Y, GPIO_PIN_1)) ? BTN_HOME : 0;
51
}
52
53
u8 btn_wait()
54
{
55
u8 res = 0, btn = btn_read();
56
bool pwr = false;
57
58
//Power button down, raise a filter.
59
if (btn & BTN_POWER)
60
{
61
pwr = true;
62
btn &= ~BTN_POWER;
63
}
64
65
do
66
{
67
res = btn_read();
68
//Power button up, remove filter.
69
if (!(res & BTN_POWER) && pwr)
70
pwr = false;
71
else if (pwr) //Power button still down.
72
res &= ~BTN_POWER;
73
} while (btn == res);
74
75
return res;
76
}
77
78
u8 btn_wait_timeout(u32 time_ms, u8 mask)
79
{
80
u32 timeout = get_tmr_ms() + time_ms;
81
u8 res = btn_read() & mask;
82
83
while (get_tmr_ms() < timeout)
84
{
85
if (res == mask)
86
break;
87
else
88
res = btn_read() & mask;
89
};
90
91
return res;
92
}
93
94
u8 btn_wait_timeout_single(u32 time_ms, u8 mask)
95
{
96
u8 single_button = mask & BTN_SINGLE;
97
mask &= ~BTN_SINGLE;
98
99
u32 timeout = get_tmr_ms() + time_ms;
100
u8 res = btn_read();
101
102
while (get_tmr_ms() < timeout)
103
{
104
if ((res & mask) == mask)
105
{
106
if (single_button && (res & ~mask)) // Undesired button detected.
107
res = btn_read();
108
else
109
return (res & mask);
110
}
111
else
112
res = btn_read();
113
};
114
115
// Timed out.
116
if (!single_button || !time_ms)
117
return (res & mask);
118
else
119
return 0; // Return no button press if single button requested.
120
}
121
122