Skip to content

Commit a8de511

Browse files
add RotaryEncoderMCP23017ButtonESP32
1 parent 10b3677 commit a8de511

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
RotaryEncoderMCP23017ButtonESP32.h - ESP32 version of rotary encoder with
3+
internal push button controlled by MCP23017 multiplexer.
4+
*/
5+
6+
#include "RotaryEncoderMCP23017ButtonESP32.h"
7+
8+
// declared here so handler below can access
9+
Method _btnCallback;
10+
//_btnCallback.callback2IntArg(button->getId(), eventType);
11+
12+
RotaryEncoderPushButtonESP32::RotaryEncoderPushButtonESP32(
13+
uint8_t index,
14+
int a_pin,
15+
int b_pin,
16+
int btn_pin,
17+
Method btn_callback,
18+
Method encoder_callback,
19+
MultiPlexer_MCP23017* mcp
20+
) {
21+
_pinA = a_pin;
22+
_pinB = b_pin;
23+
_btnPin = btn_pin;
24+
_btnCallback = btn_callback;
25+
_encoderCallback = encoder_callback;
26+
27+
_encoder = new ESP32Encoder();
28+
_button = new Button_MCP23017(_btnPin, MCP_PORT::A, mcp, _btnCallback);
29+
}
30+
31+
void RotaryEncoderPushButtonESP32::begin() {
32+
// encoder
33+
_encoder->attachSingleEdge(_pinA, _pinB);
34+
35+
// button
36+
_button->begin();
37+
}
38+
39+
void RotaryEncoderPushButtonESP32::loop() {
40+
// button
41+
_button->loop();
42+
43+
// encoder
44+
_value = getPosition();
45+
if (_position != _value) {
46+
if (_value > _position) {
47+
rotation = EventType::ROTARY_CW;
48+
} else {
49+
rotation = EventType::ROTARY_CCW;
50+
}
51+
_position = _value;
52+
53+
// notify others
54+
_encoderCallback.callback2IntArg(_position, rotation);
55+
}
56+
}
57+
58+
int64_t RotaryEncoderPushButtonESP32::getPosition() {
59+
return _encoder->getCount();
60+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
RotaryEncoderMCP23017ButtonESP32.h - ESP32 version of rotary encoder with
3+
internal push button controlled by MCP23017 multiplexer.
4+
*/
5+
6+
#ifndef RotaryEncoderMCP23017ButtonESP32_h
7+
#define RotaryEncoderMCP23017ButtonESP32_h
8+
9+
#include <Arduino.h>
10+
#include <Method.h>
11+
#include <ESP32Encoder.h>
12+
#include <Button_MCP23017.h>
13+
#include <MultiPlexer_MCP23017.h>
14+
15+
class RotaryEncoderMCP23017ButtonESP32
16+
{
17+
public:
18+
RotaryEncoderMCP23017ButtonESP32(
19+
uint8_t index,
20+
int a_pin,
21+
int b_pin,
22+
int btn_pin,
23+
Method btn_callback,
24+
Method encoder_callback,
25+
MultiPlexer_MCP23017* mcp
26+
);
27+
void begin();
28+
void loop();
29+
int64_t getPosition();
30+
31+
int rotation;
32+
33+
enum EventType {
34+
ROTARY_CW = 1 << 3,
35+
ROTARY_CCW = 1 << 4,
36+
};
37+
38+
private:
39+
Button_MCP23017* _button;
40+
ESP32Encoder* _encoder;
41+
int64_t _position = 0;
42+
int64_t _value = 0;
43+
int _pinA;
44+
int _pinB;
45+
int _btnPin;
46+
47+
// callbacks
48+
Method _encoderCallback;
49+
};
50+
51+
#endif

0 commit comments

Comments
 (0)