-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPIOPin.cpp
More file actions
58 lines (47 loc) · 1.18 KB
/
GPIOPin.cpp
File metadata and controls
58 lines (47 loc) · 1.18 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
#include "GPIOPin.hpp"
#include <functional>
#include <chrono>
#include <cassert>
#include <wiringPi.h>
using std::function;
using std::chrono::duration_cast;
using std::chrono::microseconds;
using std::chrono::steady_clock;
bool GPIOPin::wiringPi_initialized = false;
GPIOPin::GPIOPin(unsigned int number) : number(number)
{
if (!wiringPi_initialized) {
wiringPiSetupGpio();
wiringPi_initialized = true;
}
}
GPIOPin *GPIOPin::instance = nullptr;
void GPIOPin::setPullDown() {
pullUpDnControl(number, PUD_DOWN);
}
void GPIOPin::setOutput() {
pinMode(number, OUTPUT);
}
void GPIOPin::setHigh() {
digitalWrite(number, HIGH);
}
void GPIOPin::setLow() {
digitalWrite(number, LOW);
}
void GPIOPin::ISR() {
if (instance) {
instance->interruptHandler();
}
}
void GPIOPin::interruptHandler() {
if (this->edgeHandler) {
this->edgeHandler(duration_cast<microseconds>(steady_clock::now().time_since_epoch()));
}
}
void GPIOPin::setEdgeHandler(function<void(microseconds)> edgeHandler)
{
assert(instance == nullptr);
instance = this;
this->edgeHandler = edgeHandler;
wiringPiISR(number, INT_EDGE_BOTH, &GPIOPin::ISR);
}