-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterruptCapSense.cpp
More file actions
88 lines (75 loc) · 1.75 KB
/
interruptCapSense.cpp
File metadata and controls
88 lines (75 loc) · 1.75 KB
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
/*
Combriat 2020
Manager for interrupted capacitive sensing
*/
#include "interruptCapSense.h"
interruptCapSense::interruptCapSense(int _sendPin, const int _samples, unsigned long _timout, unsigned long _delay)
{
samples = _samples;
sendPin = _sendPin;
timout = _timout; // max time allowed
delay = _delay; // delay to come back to initial state (unload the cap)
times = new unsigned long[samples];
pinMode(sendPin, OUTPUT);
for (int i=0; i<samples;i++) times[i] = 0;
}
interruptCapSense::~interruptCapSense()
{
delete times;
}
bool interruptCapSense::update()
{
bool ret = false;
switch (state)
{
case 0: // initial
digitalWrite(sendPin, HIGH);
last_timing = micros();
ISR_target = 0;
state +=1;
break;
case 1: // waiting for the ISR to fire
if (ISR_target != 0 && ISR_target > last_timing)
{
times[runner] = ISR_target - last_timing;
runner += 1;
if (runner == samples) runner = 0;
ret = true;
state += 1;
digitalWrite(sendPin, LOW);
last_timing = micros();
}
else if (micros() - last_timing > timout)
{
times[runner] = timout;
runner += 1;
if (runner == samples) runner = 0;
ret = true;
state +=1;
digitalWrite(sendPin, LOW);
last_timing = micros();
}
break;
case 2:
if (micros() - last_timing > delay) state = 0;
break;
}
return ret;
}
unsigned long interruptCapSense::getCumul()
{
unsigned long sum = 0;
for (int i=0; i<samples; i++) sum += times[i];
return sum;
}
float interruptCapSense::getAverage()
{
float aver = 0.;
for (int i=0; i<samples; i++) aver += times[i];
aver = aver *1./samples;
return aver;
}
void interruptCapSense::init()
{
while(times[samples-1] == 0) update();
}