-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
rotaryDecoder.h
65 lines (45 loc) · 1.38 KB
/
rotaryDecoder.h
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
#pragma once
//
// FILE: rotaryDecoder.h
// AUTHOR: Rob Tillaart
// VERSION: 0.3.1
// DATE: 2021-05-08
// PURPOSE: Arduino library for rotary decoder
// URL: https://github.com/RobTillaart/rotaryDecoder
#include "Arduino.h"
#include "Wire.h"
#define ROTARY_DECODER_LIB_VERSION (F("0.3.1"))
class rotaryDecoder
{
public:
explicit rotaryDecoder(const int8_t address, TwoWire *wire = &Wire);
bool begin(uint8_t count = 4);
bool isConnected();
uint8_t readInitialState();
// for polling version,
// checkChange is bit faster than a call to update
// so useful if there are only a few updates
bool checkChange();
// read and update the counters
bool update(); // assumes two directions => +1 and -1
bool updateSingle(); // assumes single direction => + ++ +++
// re = rotary encoder
// returns 0, false if re > 3.
int32_t getValue(uint8_t re);
bool setValue(uint8_t re, int32_t value = 0);
// READ - WRITE interface
uint8_t read1(uint8_t pin);
bool write1(uint8_t pin, uint8_t value);
uint8_t read8();
bool write8(uint8_t value);
// DEBUG
uint8_t getLastPosition(uint8_t re);
protected:
uint8_t _count = 0;
uint8_t _lastValue = 0;
uint8_t _lastPos[4] = { 0, 0, 0, 0 };
int32_t _encoder[4] = { 0, 0, 0, 0 };
uint8_t _address;
TwoWire * _wire;
};
// -- END OF FILE --