-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
73 lines (61 loc) · 1.48 KB
/
input.cpp
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
#include <atomic>
#include <thread>
#include <unistd.h>
#include "input.h"
extern "C" {
#include <libu8g2arm/gpio.h>
}
const int ROT_S1 = 20;
const int ROT_S2 = 19;
const int ROT_KEY = 18;
const int ROT_PWR = 16;
std::thread input_thread;
std::atomic<InputValue> input_value = InputValue::Unknown;
std::atomic_bool thread_stop = false;
void *input_thread_func() {
int ss1 = 0, skey = 0;
int s1 = 0, s2 = 0, key = 0;
while (!thread_stop) {
s1 = getGPIOValue(ROT_S1);
s2 = getGPIOValue(ROT_S2);
key = getGPIOValue(ROT_KEY);
if (ss1 != s1 && !s1) {
if (s1 != s2) {
input_value.store(InputValue::Up);
} else {
input_value.store(InputValue::Down);
}
}
if (skey != key && !key)
input_value.store(InputValue::Enter);
ss1 = s1;
skey = key;
usleep(5000);
}
return nullptr;
}
InputValue input_get() { return input_value.exchange(InputValue::Unknown); }
void input_wait_enter() {
InputValue i = Unknown;
while (i != Back && i != Enter) {
sleep(0);
i = input_get();
}
}
int input_init() {
exportGPIOPin(ROT_S1);
exportGPIOPin(ROT_S2);
exportGPIOPin(ROT_KEY);
exportGPIOPin(ROT_PWR);
setGPIODirection(ROT_S1, GPIO_IN);
setGPIODirection(ROT_S2, GPIO_IN);
setGPIODirection(ROT_KEY, GPIO_IN);
setGPIODirection(ROT_PWR, GPIO_OUT);
setGPIOValue(ROT_PWR, GPIO_HIGH);
input_thread = std::thread(input_thread_func);
return 1;
}
void input_stop() {
thread_stop.store(true);
input_thread.join();
}