forked from tweej/HighLatencyGPIO
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cc
More file actions
executable file
·78 lines (53 loc) · 1.87 KB
/
main.cc
File metadata and controls
executable file
·78 lines (53 loc) · 1.87 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
#include "GPIO.hh"
// STL
#include <iostream>
#include <chrono>
#include <unistd.h> // usleep()
using namespace std::chrono;
high_resolution_clock::time_point beg;
duration<double, std::micro> accum;
class Handler
{
public:
void handle(GPIO::Value val)
{
const high_resolution_clock::time_point end = high_resolution_clock::now();
const auto time_span = duration_cast<microseconds>(end - beg);
accum += time_span;
std::cout << "Latency: " << time_span.count() << " microseconds" << std::endl;
}
};
void myisr(GPIO::Value val)
{
const high_resolution_clock::time_point end = high_resolution_clock::now();
const auto time_span = end - beg;
accum += time_span;
std::cout << "Latency: " << time_span.count() << " microseconds" << std::endl;
}
int main()
{
Handler h;
accum = std::chrono::duration<double, std::micro>(0.0);
// Member functions do not take any longer to call than global functions
std::function<void(GPIO::Value)> global(myisr);
std::function<void(GPIO::Value)> handleisr =
std::bind(&Handler::handle, &h, std::placeholders::_1);
{
// Short GPIO 15 (input) to GPIO 27 (output) for the following latency test
GPIO gpio1(27, GPIO::Direction::OUT);
GPIO gpio2(15, GPIO::Edge::RISING, handleisr); // will be destroyed first,
// so no spurious call to handleisr upon
// destruction of GPIO 27
usleep(125000);
const unsigned int nIterations = 50;
for(unsigned int i=0;i<nIterations;++i)
{
beg = high_resolution_clock::now();
gpio1.setValue(GPIO::HIGH);
usleep(31250);
gpio1.setValue(GPIO::LOW);
usleep(31250);
}
std::cout << "Average: " << accum.count()/nIterations << " microseconds " << std::endl;
}
}