forked from igkov/bcomp11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttons.c
108 lines (100 loc) · 2.44 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
buttons.c
Ðåàëèçàöèÿ buttons.c. Åñòü 2 ðåæèìà êîìïèëÿöèè ëîãèêè:
1. Îáðàáîòêà êîðîòêèõ íàæàòèé, ïðè äëèòåëüíîì íàæàòèè èäóò ïîâòîðû.
2. Êîðîòêèå è äëèííûå íàæàòèÿ, êàê îòäåëüíûå ñîáûòèÿ.
igorkov / 2017 / igorkov.org/bcomp11v2
*/
#if !defined( WIN32 )
#include <LPC11xx.h>
#endif
#include <string.h>
#include "event.h"
#include "buttons.h"
static int state;
static int last_state[MAX_BUTT];
#if defined( WIN32 )
extern int iMButton;
#define SW1_READ ((iMButton&(1<<0))?1:0)
#define SW2_READ ((iMButton&(1<<1))?1:0)
#else
#define SW1_READ ((LPC_GPIO0->DATA & (1UL<<1))?0:1)
#define SW2_READ ((LPC_GPIO0->DATA & (1UL<<3))?0:1)
#endif
int button_state(int n) {
switch (n) {
case BUTT_SW1:
return SW1_READ;
case BUTT_SW2:
return SW2_READ;
default:
return 0;
}
}
#if ( BUTTONS_SUPPORT_LONG == 0)
static void button_proc(void) {
int n;
for (n=0; n<MAX_BUTT; n++) {
if (button_state(1<<n)) {
if (last_state[n] < -4) { // çàùèòà îò ïîâòîðíîãî íàæàíèÿ (íå áûñòðåå ÷åì ÷åðåç 50ìñ)
last_state[n] = 0;
}
last_state[n]++;
if (last_state[n] == 3) { // òîëüêî óäåðæàíèå â òå÷åíèè 30ìñ.
state |= (1<<n);
} else
if (last_state[n] > 100) { // áîëüøå ñåêóíäû íà÷èíàþòñÿ ïîâòîðû
if (last_state[n]%15 == 0) // çàìåäëåíèå, ïîâòîð êíîïêè íå ÷àùå 150ìñ
state |= (1<<n);
}
} else {
if (last_state[n] > 0)
last_state[n] = 0;
last_state[n]--;
}
}
event_set(button_proc, 10);
}
#else
static void button_proc(void) {
int n;
for (n=0; n<MAX_BUTT; n++) {
if (button_state(1<<(n*2))) {
if (last_state[n] < -4) { // çàùèòà îò ïîâòîðíîãî íàæàíèÿ (íå áûñòðåå ÷åì ÷åðåç 50ìñ)
last_state[n] = 0;
}
if (last_state[n] > 1000) {
// nop
} else
if (last_state[n] > 100) { // áîëüøå ñåêóíäû - äëèò. íàæàòèå
state |= (2<<(n*2));
last_state[n] = 1000;
}
last_state[n]++;
} else {
if (last_state[n] > 100) {
// nop
} else
if (last_state[n] > 4) { // 40ìñ - êîðîòêîå íàæàòèå
state |= (1<<(n*2));
}
if (last_state[n] > 0) {
last_state[n] = 0;
}
last_state[n]--;
}
}
event_set(button_proc, 10);
}
#endif
void button_init(void) {
state = 0;
memset(last_state, 0x00, sizeof(last_state));
event_set(button_proc, 10);
}
int button_read(void) {
int ret;
ret = state;
state = 0; // Ñ ìèíèìàëüíîé âåðîÿòíîñòüþ, ñóùåñòâóåò âîçìîæíîñòü ïðîïóñêà íàæàòèÿ.
return ret;
}