GY-63_MS5611/libraries/rotaryDecoder/rotaryDecoder.h
2021-05-17 16:29:53 +02:00

58 lines
1.3 KiB
C++

#pragma once
//
// FILE: rotaryDecoder.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// DATE: 2021-05-08
// PURPOSE: rotary decoder library for Arduino
// URL: https://github.com/RobTillaart/rotaryDecoder
#include "Arduino.h"
#include "Wire.h"
#define ROTARY_DECODER_LIB_VERSION (F("0.1.0"))
class rotaryDecoder
{
public:
explicit rotaryDecoder(const int8_t address, TwoWire *wire = &Wire);
#if defined (ESP8266) || defined(ESP32)
bool begin(uint8_t sda, uint8_t scl, uint8_t cnt = 4);
#endif
bool begin(uint8_t cnt = 4);
bool isConnected();
void readInitialState();
// for polling version,
// checkChange is bit faster than a call to update
// so usefull 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 => + ++ +++
int32_t getValue(uint8_t re) { return _encoder[re]; };
void setValue(uint8_t re, int32_t val = 0) { _encoder[re] = val; };
// DEBUG
uint8_t getLastPosition(uint8_t re) { return _lastPos[re]; };
private:
uint8_t _cnt = 0;
uint8_t _lastVal = 0;
uint8_t _lastPos[4] = { 0,0,0,0 };
int32_t _encoder[4] = { 0,0,0,0 };
uint8_t _read8();
uint8_t _address;
TwoWire * _wire;
};
// -- END OF FILE --