-
Notifications
You must be signed in to change notification settings - Fork 8
/
buttons.c
87 lines (69 loc) · 2.22 KB
/
buttons.c
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
/*
* Copyright (c) 2019 Maciej Suminski <[email protected]>
*
* This source code is free software; you can redistribute it
* and/or modify it in source code form 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 program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "asf.h"
#include "buttons.h"
#include "command_handlers.h"
//#define BUTTON_ENABLE_INT
#define BUT1_IDX PIO_PA1_IDX
#define BUT2_IDX PIO_PA0_IDX
#define BUT3_IDX PIO_PB0_IDX
#define BUT4_IDX PIO_PB1_IDX
#ifdef BUTTON_ENABLE_INT
#define BUTTON_PIO_ATTR (PIO_INPUT | PIO_PULLUP | PIO_DEBOUNCE | PIO_IT_LOW_LEVEL)
#else
#define BUTTON_PIO_ATTR (PIO_INPUT | PIO_PULLUP | PIO_DEBOUNCE)
#endif
void btn_init(void)
{
pio_configure_pin(BUT1_IDX, BUTTON_PIO_ATTR);
pio_configure_pin(BUT2_IDX, BUTTON_PIO_ATTR);
pio_configure_pin(BUT3_IDX, BUTTON_PIO_ATTR);
pio_configure_pin(BUT4_IDX, BUTTON_PIO_ATTR);
pmc_enable_periph_clk(ID_PIOA);
pmc_enable_periph_clk(ID_PIOB);
}
int btn_state(void)
{
int ret = 0;
if(btn_is_pressed(BUT1))
ret |= BUT1;
if(btn_is_pressed(BUT2))
ret |= BUT2;
if(btn_is_pressed(BUT3))
ret |= BUT3;
if(btn_is_pressed(BUT4))
ret |= BUT4;
return ret;
}
int btn_is_pressed(button_t button)
{
switch(button) {
case BUT1: return !pio_get_pin_value(BUT1_IDX);
case BUT2: return !pio_get_pin_value(BUT2_IDX);
case BUT3: return !pio_get_pin_value(BUT3_IDX);
case BUT4: return !pio_get_pin_value(BUT4_IDX);
}
return 0;
}
void cmd_btn(const uint8_t* data_in, unsigned int input_len) {
(void) data_in;
(void) input_len;
cmd_resp_init(CMD_RESP_OK);
cmd_resp_write(btn_state());
}