-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCTeensyTouch.cpp
195 lines (162 loc) · 3.62 KB
/
TCTeensyTouch.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "TCAssert.h"
#include "TCModule.h"
#include "TCUtilities.h"
#include "TCTeensyTouch.h"
enum
{
eMaxInterfaces = 4,
ePinCount = 32,
};
struct SSensorInterface
{
ITouchSensor* touchSensor;
};
class CModule_TeensyTouch : public CModule
{
public:
CModule_TeensyTouch(
);
void
Setup(
void);
void
Update(
uint32_t inDeltaTimeUS);
};
static int gInterfaceCount;
static SSensorInterface gInterfaceList[eMaxInterfaces];
static CModule_TeensyTouch gModuleMPR121;
static uint8_t gTouchState[ePinCount];
static uint32_t gLastTouchTime[ePinCount];
static uint32_t gTouchInputBV;
enum
{
eState_WaitingForChangeToTouched,
eState_WaitingForTouchSettle,
eState_WaitingForChangeToRelease,
eState_WaitingForReleaseSettle,
eUpdateTimeUS = 50000,
eSettleTimeMS = 200,
eTouchThreshold = 2000,
};
CModule_TeensyTouch::CModule_TeensyTouch(
)
:
CModule("tytc", 0, eUpdateTimeUS)
{
}
void
CModule_TeensyTouch::Setup(
void)
{
}
void
CModule_TeensyTouch::Update(
uint32_t inDeltaTimeUS)
{
for(int itr = 0; itr < ePinCount; ++itr)
{
if(!(gTouchInputBV & (1 << itr)))
{
continue;
}
int readValue = touchRead(itr);
//DebugMsg(eDbgLevel_Basic, "TTch: %d %d\n", itr, readValue);
bool touched = readValue > eTouchThreshold;
switch(gTouchState[itr])
{
case eState_WaitingForChangeToTouched:
if(touched)
{
gLastTouchTime[itr] = gCurTimeMS;
gTouchState[itr] = eState_WaitingForTouchSettle;
}
break;
case eState_WaitingForTouchSettle:
if(!touched)
{
gTouchState[itr] = eState_WaitingForChangeToTouched;
break;
}
if(gCurTimeMS - gLastTouchTime[itr] >= eSettleTimeMS)
{
gTouchState[itr] = eState_WaitingForChangeToRelease;
DebugMsg(eDbgLevel_Verbose, "TTch: Touched %d\n", itr);
for(int itr2 = 0; itr2 < gInterfaceCount; ++itr2)
{
gInterfaceList[itr2].touchSensor->Touch(itr);
}
}
break;
case eState_WaitingForChangeToRelease:
if(!touched)
{
//DebugMsg(eDbgLevel_Verbose, "TeensyTouch: Going to release settle %d\n", itr);
gLastTouchTime[itr] = gCurTimeMS;
gTouchState[itr] = eState_WaitingForReleaseSettle;
}
break;
case eState_WaitingForReleaseSettle:
if(touched)
{
//DebugMsg(eDbgLevel_Verbose, "MPR121: Going to release waiting %d\n", itr);
gTouchState[itr] = eState_WaitingForChangeToRelease;
break;
}
if(gCurTimeMS - gLastTouchTime[itr] >= eSettleTimeMS)
{
gTouchState[itr] = eState_WaitingForChangeToTouched;
DebugMsg(eDbgLevel_Verbose, "TTch: Release %d\n", itr);
for(int itr2 = 0; itr2 < gInterfaceCount; ++itr2)
{
gInterfaceList[itr2].touchSensor->Release(itr);
}
}
break;
}
}
}
void
TeensyRegisterTouchSensor(
uint32_t inTouchInputBV,
ITouchSensor* inTouchSensor)
{
MAssert(gInterfaceCount < eMaxInterfaces);
gTouchInputBV |= inTouchInputBV;
for(int i = 0; i < gInterfaceCount; ++i)
{
if(gInterfaceList[i].touchSensor == inTouchSensor)
{
// Don't re register
return;
}
}
SSensorInterface* newInterface = gInterfaceList + gInterfaceCount++;
newInterface->touchSensor = inTouchSensor;
}
void
TeensyUnRegisterTouchSensor(
ITouchSensor* inTouchSensor)
{
for(int i = 0; i < gInterfaceCount; ++i)
{
if(gInterfaceList[i].touchSensor == inTouchSensor)
{
memmove(gInterfaceList + i, gInterfaceList + i + 1, (gInterfaceCount - i - 1) * sizeof(SSensorInterface));
--gInterfaceCount;
return;
}
}
}
void
TeensyAddTouchInputs(
uint32_t inTouchInputBV)
{
gTouchInputBV |= inTouchInputBV;
}
void
TeensyRemoveTouchInputs(
uint32_t inTouchInputBV)
{
gTouchInputBV &= ~inTouchInputBV;
}