-
Notifications
You must be signed in to change notification settings - Fork 0
/
PushbuttonScanner.cpp
93 lines (81 loc) · 2.68 KB
/
PushbuttonScanner.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* PushbuttonScanner.cpp
*
* Created on: Feb 3, 2013
* Author: jpm8766
*/
#include "PushbuttonScanner.h"
PushbuttonScanner::PushbuttonScanner(uintptr_t input_port) :
input_port_handle(input_port) {}
PushbuttonScanner::~PushbuttonScanner() {
}
void* PushbuttonScanner::run() {
char lastRead = in8(input_port_handle);
char intermed;
char depressed;
char released;
int i;
//printf("PushbuttonScanner running\n");
while (!killThread) {
//Wait for there to be a change on the pushbutton inputs.
while ((lastRead & LOW_MASK) == (in8(input_port_handle) & LOW_MASK)) {
usleep(POLL_PERIOD);
}
//when a change is detected, wait for the inputs to settle.
usleep(DEBOUNCE_PERIOD);
//then save a copy of the inputs.
intermed = in8(input_port_handle);
//determine what has occurred by comparing the intermediate and the last read value.
depressed = 0b00000000;
released = 0b00000000;
for(i = 0; i < MASK_ARRAY_SIZE; i++) {
if ((lastRead & MASK_ARRAY[i]) != (intermed & MASK_ARRAY[i])) {
//if that bit has changed...
if((intermed & MASK_ARRAY[i]) == 0) {
//it must have been released
released |= MASK_ARRAY[i];
} else if((intermed & MASK_ARRAY[i]) == MASK_ARRAY[i]) {
//it must have been depressed
depressed |= MASK_ARRAY[i];
} else { /* cant reach this block */ }
}
}
//if buttons were actually released...
if(released != 0) {
//fire the individual events
if ((released & MASK_ARRAY[0])) {
EventCenter::DefaultEventCenter()->sendEvent(evSetReleased);
}
if ((released & MASK_ARRAY[1])) {
EventCenter::DefaultEventCenter()->sendEvent(
evStartStopReleased);
}
if ((released & MASK_ARRAY[2])) {
EventCenter::DefaultEventCenter()->sendEvent(evModeReleased);
}
}
//if buttons were actually depressed...
if(depressed != 0) {
//if its a special combo (all or MODE+START/STOP), fire those events
if((depressed & MASK_ARRAY[0]) && (depressed & MASK_ARRAY[1]) && (depressed & MASK_ARRAY[2])) {
EventCenter::DefaultEventCenter()->sendEvent(evAllDepressed);
} else if(!(depressed & MASK_ARRAY[0]) && (depressed & MASK_ARRAY[1]) && (depressed & MASK_ARRAY[2])) {
EventCenter::DefaultEventCenter()->sendEvent(evModeStartStopDepressed);
} else {
//else fire the individual events
if((depressed & MASK_ARRAY[0])) {
EventCenter::DefaultEventCenter()->sendEvent(evSetDepressed);
}
if((depressed & MASK_ARRAY[1])) {
EventCenter::DefaultEventCenter()->sendEvent(evStartStopDepressed);
}
if((depressed & MASK_ARRAY[2])) {
EventCenter::DefaultEventCenter()->sendEvent(evModeDepressed);
}
}
}
//save the last value read
lastRead = intermed;
}
return NULL;
}