-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttonmonitorthread.cpp
110 lines (86 loc) · 2.87 KB
/
buttonmonitorthread.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "buttonmonitorthread.h"
#include <unistd.h>
#include <syslog.h>
#include <stdint.h>
#define PIFACE_HWADDR 0
ButtonMonitorThread::ButtonMonitorThread(QObject *parent) :
QThread(NULL)
{
moveToThread(this);
connect((QObject *)parent, SIGNAL(destroyed()), this, SLOT(quit()));
debounceTimer.setInterval(20);
debounceTimer.setSingleShot(true);
connect(&debounceTimer, SIGNAL(timeout()), this, SLOT(onDebounce()));
pressTimer.setInterval(50);
pressTimer.setSingleShot(true);
connect(&pressTimer, SIGNAL(timeout()), this, SLOT(onReadAttempt()));
connect(this, SIGNAL(tryReading()), &pressTimer, SLOT(start()));
}
ButtonMonitorThread::~ButtonMonitorThread()
{
pifacedigital_close(PIFACE_HWADDR);
}
void ButtonMonitorThread::run()
{
openButtons();
exec();
}
void ButtonMonitorThread::openButtons()
{
for( int i=0; i < NUM_BUTTONS; i++ ) {
m_aLastButtonState[i] = false;
}
// Open buttons here (connect to PiFace)
pifacedigital_open(PIFACE_HWADDR);
if( pifacedigital_enable_interrupts() ) {
qDebug("Could not enable interrupts. Try running using sudo to enable PiFaceDigital interrupts.\n");
syslog(LOG_ERR, "[ButtonMonitorThread] Could not enable interrupts.");
}
emit tryReading();
}
void ButtonMonitorThread::setOutputState( int iOutput, bool bState )
{
qDebug("Setting output %d to %d", iOutput, (int)bState);
pifacedigital_digital_write(iOutput, bState);
}
void ButtonMonitorThread::splitInputs( uint8_t inputs )
{
m_bCurrentButtonState[0] = !(inputs & 0x1);
m_bCurrentButtonState[1] = !(inputs & 0x2);
m_bCurrentButtonState[2] = !(inputs & 0x4);
m_bCurrentButtonState[3] = !(inputs & 0x8);
m_bCurrentButtonState[4] = !(inputs & 0x10);
m_bCurrentButtonState[5] = !(inputs & 0x20);
m_bCurrentButtonState[6] = !(inputs & 0x40);
m_bCurrentButtonState[7] = !(inputs & 0x80);
}
void ButtonMonitorThread::onDebounce()
{
uint8_t inputs = pifacedigital_read_reg(0x13, PIFACE_HWADDR);
splitInputs( inputs );
qDebug("Debounced Inputs: 0x%x\n", inputs);
for( int i=0; i < 8; i++ ) {
if ( m_bNeedsDebounced[i] ) {
if( m_bCurrentButtonState[i] != m_aLastButtonState[i] ) {
m_aLastButtonState[i] = m_bCurrentButtonState[i];
emit buttonChange(i, m_bCurrentButtonState[i]);
}
m_bNeedsDebounced[i] = false;
}
}
emit tryReading();
}
void ButtonMonitorThread::onReadAttempt()
{
bool bDebounce = false;
uint8_t inputs = pifacedigital_read_reg(0x13, PIFACE_HWADDR);
splitInputs( inputs );
for( int i=0; i < 8; i++ ) {
if( m_bCurrentButtonState[i] != m_aLastButtonState[i] ) {
m_bNeedsDebounced[i] = true;
bDebounce = true;
}
}
if ( bDebounce ) debounceTimer.start();
else emit tryReading();
}