-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsearchLock.cpp
More file actions
95 lines (76 loc) · 1.89 KB
/
searchLock.cpp
File metadata and controls
95 lines (76 loc) · 1.89 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
89
90
91
92
93
94
95
#include <wiringPi.h>
#include <iostream>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <sched.h>
#include <sstream>
using namespace std;
//store data into array
#define MAX_CHANGE_TO_KEEP 30
static int durations[MAX_CHANGE_TO_KEEP];
void log(string a){
cout << a ;
}
string longToString(long mylong){
string mystring;
stringstream mystream;
mystream << mylong;
return mystream.str();
}
string intToString(int myint){
string mystring;
stringstream mystream;
mystream << myint;
return mystream.str();
}
string measure2String(long duration){
string mystring;
stringstream mystream;
mystream << duration;
return mystream.str();
}
void handleInterrupt() {
static unsigned int duration;
static unsigned int changeCount;
static unsigned long lastTime;
static int lockPassed;
long time = micros();
duration = time - lastTime;
//wait for lock
if (duration > 11000 && lockPassed == 0) {
changeCount = 0;
log("Lock started"); cout << endl;
log(measure2String(duration));cout << endl;
//store duration
durations[changeCount++] = duration;
lockPassed = 1;
}
else if (lockPassed != 0) { // REST OF DATA
//log(measure2String(duration, digitalRead(2)));
if (changeCount > MAX_CHANGE_TO_KEEP) { // store 100 change Max
//ended
lockPassed = 0;
changeCount = 0;
log("===============================");
for (int i=0; i < MAX_CHANGE_TO_KEEP; i=i++) {
log(measure2String(durations[i]));
log(" ");
}
cout << endl;
} else {
//store duration
durations[changeCount++] = duration;
}
}
lastTime = time;
}
int main(void) {
if(wiringPiSetup() == -1)
return 0;
//attach interrupt to changes on the pin
wiringPiISR(2, INT_EDGE_BOTH, &handleInterrupt);
//interrupt will handle changes
while(true);
}