-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRotaryEncoder.cpp
More file actions
56 lines (52 loc) · 1.5 KB
/
Copy pathRotaryEncoder.cpp
File metadata and controls
56 lines (52 loc) · 1.5 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
#include "RotaryEncoder.h"
#include <pico/stdlib.h>
#include <functional>
RotaryEncoder::RotaryEncoder(int aPin, int bPin, int swPin) :
Driver({Pin(aPin, FN_INPUT_ISR, GPIO_IRQ_EDGE_FALL),
Pin(bPin, FN_INPUT),
Pin(bPin, FN_INPUT_ISR, GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE)}),
_position(0),
_direction(1)
{
// TODO: Add pull option to Pin class
gpio_set_pulls(this->aPin().pin(), true, false);
gpio_set_pulls(this->swPin().pin(), true, false);
this->aPin().setCallback(std::bind(&RotaryEncoder::rotaryEventHandler, this, std::placeholders::_1, std::placeholders::_2));
this->swPin().setCallback(std::bind(&RotaryEncoder::switchEventHandler, this, std::placeholders::_1, std::placeholders::_2));
}
void RotaryEncoder::rotaryEventHandler(int gpio, uint32_t events)
{
if (this->bPin().get())
{
this->_position += this->_direction;
}
else
{
this->_position -= this->_direction;
}
if (this->rotaryCallback)
{
// Notify user
this->rotaryCallback(this->_position);
}
}
void RotaryEncoder::switchEventHandler(int gpio, uint32_t events)
{
if (this->switchCallback)
{
if (events & GPIO_IRQ_EDGE_FALL)
{
// Button pressed
this->switchCallback(true);
}
else if (events & GPIO_IRQ_EDGE_RISE)
{
// Button released
this->switchCallback(false);
}
else
{
// Unexpected
}
}
}