Skip to content

Commit 7520e0b

Browse files
committed
upd
1 parent f68452d commit 7520e0b

File tree

13 files changed

+507
-232
lines changed

13 files changed

+507
-232
lines changed

README.md

Lines changed: 217 additions & 51 deletions
Large diffs are not rendered by default.

examples/callback/callback.ino

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55

66
EncButton eb(2, 3, 4);
77

8-
void setup() {
9-
Serial.begin(115200);
10-
}
11-
128
void cb() {
139
Serial.print("callback: ");
1410
switch (eb.action()) {
@@ -53,9 +49,15 @@ void cb() {
5349
Serial.print("release step clicks ");
5450
Serial.println(eb.getClicks());
5551
break;
52+
default: Serial.println();
5653
}
5754
}
5855

56+
void setup() {
57+
Serial.begin(115200);
58+
eb.attach(cb);
59+
}
60+
5961
void loop() {
60-
if (eb.tick()) cb();
62+
eb.tick();
6163
}

examples/demo/demo.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// полное демо
22
#include <Arduino.h>
3-
3+
// #define EB_NO_CALLBACK // отключить обработчик событий attach (экономит 2 байта оперативки)
44
// #define EB_NO_COUNTER // отключить счётчик энкодера (экономит 4 байта оперативки)
55
// #define EB_NO_BUFFER // отключить буферизацию энкодера (экономит 1 байт оперативки)
66

@@ -18,7 +18,8 @@ EncButton eb(2, 3, 4);
1818
void setup() {
1919
Serial.begin(115200);
2020

21-
eb.setButtonLevel(LOW);
21+
// показаны значения по умолчанию
22+
eb.setBtnLevel(LOW);
2223
eb.setClickTimeout(500);
2324
eb.setDebTimeout(50);
2425
eb.setHoldTimeout(500);

examples/isr/isr.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void setup() {
1818
Serial.begin(115200);
1919
attachInterrupt(0, isr, CHANGE);
2020
attachInterrupt(1, isr, CHANGE);
21+
eb.setEncISR(true);
2122
}
2223

2324
void loop() {

keywords.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ VirtButton KEYWORD1
1515

1616
EB_NO_COUNTER KEYWORD1
1717
EB_NO_BUFFER KEYWORD1
18+
EB_NO_CALLBACK KEYWORD1
1819

1920
EB_DEB_TIME KEYWORD1
2021
EB_CLICK_TIME KEYWORD1
@@ -29,7 +30,7 @@ setHoldTimeout KEYWORD2
2930
setStepTimeout KEYWORD2
3031
setClickTimeout KEYWORD2
3132
setDebTimeout KEYWORD2
32-
setButtonLevel KEYWORD2
33+
setBtnLevel KEYWORD2
3334
reset KEYWORD2
3435
clear KEYWORD2
3536
press KEYWORD2
@@ -47,15 +48,18 @@ timeout KEYWORD2
4748
waiting KEYWORD2
4849
busy KEYWORD2
4950
action KEYWORD2
51+
attach KEYWORD2
52+
53+
pressISR KEYWORD2
5054

5155
setEncReverse KEYWORD2
5256
setEncType KEYWORD2
5357
initEnc KEYWORD2
5458

5559
setFastTimeout KEYWORD2
56-
holdEncButton KEYWORD2
57-
toggleEncButton KEYWORD2
60+
setEncISR KEYWORD2
5861
turn KEYWORD2
62+
turnH KEYWORD2
5963
right KEYWORD2
6064
left KEYWORD2
6165
rightH KEYWORD2

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=EncButton
2-
version=3.0.1
2+
version=3.1
33
author=AlexGyver <[email protected]>
44
maintainer=AlexGyver <[email protected]>
55
sentence=Light and powerful library for button and encoder operation for Arduino
6-
paragraph=Debounce, click count, hold, step hold mode and many more. Ьaximum possibilities for button and encoder
6+
paragraph=Debounce, click count, hold, step hold mode and many more. Maximum possibilities for button and encoder
77
category=Sensors
88
url=https://github.com/GyverLibs/EncButton
99
architectures=*

src/EncButton.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22
#include <Arduino.h>
33

4+
#include "core/VirtButton.h"
5+
#include "core/VirtEncoder.h"
46
#include "core/Button.h"
57
#include "core/Encoder.h"
68
#include "core/EncButton.h"

src/core/Button.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
// ============= VAR PIN =============
88
class Button : public VirtButton {
99
public:
10-
Button(uint8_t npin = 0, uint8_t mode = INPUT_PULLUP) {
11-
init(npin, mode);
12-
setButtonLevel(LOW);
10+
Button(uint8_t npin = 0, uint8_t mode = INPUT_PULLUP, uint8_t btnLevel = LOW) {
11+
init(npin, mode, btnLevel);
1312
}
1413

1514
// указать пин и его режим работы
16-
void init(uint8_t npin = 0, uint8_t mode = INPUT_PULLUP) {
15+
void init(uint8_t npin = 0, uint8_t mode = INPUT_PULLUP, uint8_t btnLevel = LOW) {
1716
pin = npin;
1817
pinMode(pin, mode);
18+
setBtnLevel(btnLevel);
1919
}
2020

2121
// прочитать текущее значение кнопки (без дебаунса)
@@ -36,14 +36,14 @@ class Button : public VirtButton {
3636
template <uint8_t PIN>
3737
class ButtonT : public VirtButton {
3838
public:
39-
ButtonT(uint8_t mode = INPUT_PULLUP) {
40-
init(mode);
41-
setButtonLevel(LOW);
39+
ButtonT(uint8_t mode = INPUT_PULLUP, uint8_t btnLevel = LOW) {
40+
init(mode, btnLevel);
4241
}
4342

4443
// указать режим работы пина
45-
void init(uint8_t mode = INPUT_PULLUP) {
44+
void init(uint8_t mode = INPUT_PULLUP, uint8_t btnLevel = LOW) {
4645
pinMode(PIN, mode);
46+
setBtnLevel(btnLevel);
4747
}
4848

4949
// прочитать текущее значение кнопки (без дебаунса)

src/core/EncButton.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
class EncButton : public VirtEncButton {
99
public:
1010
// настроить пины (энк, энк, кнопка, pinmode энк, pinmode кнопка)
11-
EncButton(uint8_t encA = 0, uint8_t encB = 0, uint8_t btn = 0, uint8_t modeEnc = INPUT, uint8_t modeBtn = INPUT_PULLUP) {
12-
init(encA, encB, btn, modeEnc, modeBtn);
13-
setButtonLevel(LOW);
11+
EncButton(uint8_t encA = 0, uint8_t encB = 0, uint8_t btn = 0, uint8_t modeEnc = INPUT, uint8_t modeBtn = INPUT_PULLUP, uint8_t btnLevel = LOW) {
12+
init(encA, encB, btn, modeEnc, modeBtn, btnLevel);
1413
}
1514

1615
// настроить пины (энк, энк, кнопка, pinmode энк, pinmode кнопка)
17-
void init(uint8_t encA = 0, uint8_t encB = 0, uint8_t btn = 0, uint8_t modeEnc = INPUT, uint8_t modeBtn = INPUT_PULLUP) {
16+
void init(uint8_t encA = 0, uint8_t encB = 0, uint8_t btn = 0, uint8_t modeEnc = INPUT, uint8_t modeBtn = INPUT_PULLUP, uint8_t btnLevel = LOW) {
1817
e0 = encA;
1918
e1 = encB;
2019
b = btn;
2120
pinMode(e0, modeEnc);
2221
pinMode(e1, modeEnc);
2322
pinMode(b, modeBtn);
23+
setBtnLevel(btnLevel);
2424
initEnc(readEnc());
2525
}
2626

@@ -32,7 +32,8 @@ class EncButton : public VirtEncButton {
3232

3333
// функция обработки, вызывать в loop
3434
int8_t tick() {
35-
return VirtEncButton::tick(readEnc(), EBread(b));
35+
if (read_ef(EB_EISR)) return VirtEncButton::tick(EBread(b));
36+
else return VirtEncButton::tick(readEnc(), EBread(b));
3637
}
3738

3839
// ====================== READ ======================
@@ -56,16 +57,16 @@ template <uint8_t ENCA, uint8_t ENCB, uint8_t BTN>
5657
class EncButtonT : public VirtEncButton {
5758
public:
5859
// настроить пины (энк, энк, кнопка, pinmode энк, pinmode кнопка)
59-
EncButtonT(uint8_t modeEnc = INPUT, uint8_t modeBtn = INPUT_PULLUP) {
60-
init(modeEnc, modeBtn);
61-
setButtonLevel(LOW);
60+
EncButtonT(uint8_t modeEnc = INPUT, uint8_t modeBtn = INPUT_PULLUP, uint8_t btnLevel = LOW) {
61+
init(modeEnc, modeBtn, btnLevel);
6262
}
6363

6464
// настроить пины (pinmode энк, pinmode кнопка)
65-
void init(uint8_t modeEnc = INPUT, uint8_t modeBtn = INPUT_PULLUP) {
65+
void init(uint8_t modeEnc = INPUT, uint8_t modeBtn = INPUT_PULLUP, uint8_t btnLevel = LOW) {
6666
pinMode(ENCA, modeEnc);
6767
pinMode(ENCB, modeEnc);
6868
pinMode(BTN, modeBtn);
69+
setBtnLevel(btnLevel);
6970
initEnc(readEnc());
7071
}
7172

@@ -77,7 +78,8 @@ class EncButtonT : public VirtEncButton {
7778

7879
// функция обработки, вызывать в loop
7980
int8_t tick() {
80-
return VirtEncButton::tick(readEnc(), EBread(BTN));
81+
if (read_ef(EB_EISR)) return VirtEncButton::tick(EBread(BTN));
82+
else return VirtEncButton::tick(readEnc(), EBread(BTN));
8183
}
8284

8385
// ====================== READ ======================

src/core/Encoder.h

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,37 @@
88
class Encoder : public VirtEncoder {
99
public:
1010
// указать пины и их режим работы
11-
Encoder(uint8_t npina = 0, uint8_t npinb = 0, uint8_t mode = INPUT) {
12-
init(npina, npinb, mode);
11+
Encoder(uint8_t encA = 0, uint8_t encB = 0, uint8_t mode = INPUT) {
12+
init(encA, encB, mode);
1313
}
1414

1515
// указать пины и их режим работы
16-
void init(uint8_t npina = 0, uint8_t npinb = 0, uint8_t mode = INPUT) {
17-
encA = npina;
18-
encB = npinb;
19-
pinMode(encA, mode);
20-
pinMode(encB, mode);
16+
void init(uint8_t encA = 0, uint8_t encB = 0, uint8_t mode = INPUT) {
17+
e0 = encA;
18+
e1 = encB;
19+
pinMode(e0, mode);
20+
pinMode(e1, mode);
21+
initEnc(readEnc());
2122
}
2223

2324
// функция обработки для вызова в прерывании энкодера
2425
int8_t tickISR() {
25-
return VirtEncoder::tickISR(EBread(encA), EBread(encB));
26+
return VirtEncoder::tickISR(readEnc());
2627
}
2728

2829
// функция обработки, вызывать в loop
2930
int8_t tick() {
30-
return VirtEncoder::tick(EBread(encA), EBread(encB));
31+
if (read_ef(EB_EISR)) return VirtEncoder::tick();
32+
else return VirtEncoder::tick(readEnc());
3133
}
3234

3335
private:
34-
uint8_t encA, encB;
36+
uint8_t e0, e1;
37+
38+
// прочитать значение энкодера
39+
int8_t readEnc() {
40+
return EBread(e0) | (EBread(e1) << 1);
41+
}
3542
};
3643

3744
// ============= TEMPLATE PIN =============
@@ -47,16 +54,23 @@ class EncoderT : public VirtEncoder {
4754
void init(uint8_t mode = INPUT) {
4855
pinMode(ENCA, mode);
4956
pinMode(ENCB, mode);
57+
initEnc(readEnc());
5058
}
5159

5260
// функция обработки для вызова в прерывании энкодера
5361
int8_t tickISR() {
54-
return VirtEncoder::tickISR(EBread(ENCA), EBread(ENCB));
62+
return VirtEncoder::tickISR(readEnc());
5563
}
5664

5765
// функция обработки, вызывать в loop
5866
int8_t tick() {
59-
return VirtEncoder::tick(EBread(ENCA), EBread(ENCB));
67+
if (read_ef(EB_EISR)) return VirtEncoder::tick();
68+
else return VirtEncoder::tick(readEnc());
69+
}
70+
71+
// прочитать значение энкодера
72+
int8_t readEnc() {
73+
return EBread(ENCA) | (EBread(ENCB) << 1);
6074
}
6175

6276
private:

0 commit comments

Comments
 (0)