-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspk_mRotaryEncoder.h
More file actions
64 lines (49 loc) · 1.83 KB
/
spk_mRotaryEncoder.h
File metadata and controls
64 lines (49 loc) · 1.83 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
// *SPARK D-FUSER
// A project by Toby Harris
// Copyright *spark audio-visual 2012
//
// spkRotaryEncoder extends mRotaryEncoder to return the change on pot state since last queried
// This allows the encoder to be polled when the host program is ready, and return info suitable for driving a TVOne style menu
// Importantly to driving such a menu, it will ignore any further rotation after the switch is pressed.
#include "mRotaryEncoder.h"
class SPKRotaryEncoder : public mRotaryEncoder {
public:
bool hasPressed();
int getChange();
int getPos(); // This would be a Get() override, but its not virtual. We use this instead to correct for positions-per-detent
SPKRotaryEncoder(PinName pinA, PinName pinB, PinName pinSW, PinMode pullMode=PullUp, int debounceTime_us=1000);
private:
void onPress();
bool m_hasPressed;
int m_positionOld;
int m_positionOnPress;
};
SPKRotaryEncoder::SPKRotaryEncoder(PinName pinA, PinName pinB, PinName pinSW, PinMode pullMode, int debounceTime_us) : mRotaryEncoder(pinA, pinB, pinSW, pullMode, debounceTime_us)
{
attachSW(this,&SPKRotaryEncoder::onPress);
}
bool SPKRotaryEncoder::hasPressed()
{
bool hasPressed = m_hasPressed;
m_hasPressed = false;
return hasPressed;
}
int SPKRotaryEncoder::getChange()
{
int positionEnc = this->getPos();
int positionToUse = m_hasPressed ? m_positionOnPress : positionEnc;
int change = positionToUse - m_positionOld;
m_positionOld = positionEnc;
return change;
}
int SPKRotaryEncoder::getPos()
{
int positionEnc = this->Get();
int positionsPerDetent = 2;
return positionEnc / positionsPerDetent;
}
void SPKRotaryEncoder::onPress()
{
m_positionOnPress = this->getPos();
m_hasPressed = true;
}