2021-05-17 10:29:53 -04:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: rotaryDecoder.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2024-06-24 07:54:24 -04:00
|
|
|
// VERSION: 0.3.1
|
2021-05-17 10:29:53 -04:00
|
|
|
// DATE: 2021-05-08
|
2024-02-23 03:44:55 -05:00
|
|
|
// PURPOSE: Arduino library for rotary decoder
|
2021-05-17 10:29:53 -04:00
|
|
|
// URL: https://github.com/RobTillaart/rotaryDecoder
|
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
#include "Wire.h"
|
|
|
|
|
2024-06-24 07:54:24 -04:00
|
|
|
#define ROTARY_DECODER_LIB_VERSION (F("0.3.1"))
|
2021-05-17 10:29:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
class rotaryDecoder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit rotaryDecoder(const int8_t address, TwoWire *wire = &Wire);
|
|
|
|
|
2021-11-15 15:55:49 -05:00
|
|
|
bool begin(uint8_t count = 4);
|
2021-05-17 10:29:53 -04:00
|
|
|
bool isConnected();
|
|
|
|
|
2024-06-24 07:54:24 -04:00
|
|
|
uint8_t readInitialState();
|
2021-05-17 10:29:53 -04:00
|
|
|
|
2022-11-23 09:19:55 -05:00
|
|
|
// for polling version,
|
|
|
|
// checkChange is bit faster than a call to update
|
|
|
|
// so useful if there are only a few updates
|
2021-05-17 10:29:53 -04:00
|
|
|
bool checkChange();
|
|
|
|
|
2022-11-23 09:19:55 -05:00
|
|
|
// read and update the counters
|
|
|
|
bool update(); // assumes two directions => +1 and -1
|
|
|
|
bool updateSingle(); // assumes single direction => + ++ +++
|
2021-05-17 10:29:53 -04:00
|
|
|
|
2022-11-23 09:19:55 -05:00
|
|
|
// re = rotary encoder
|
2024-06-24 07:54:24 -04:00
|
|
|
// returns 0, false if re > 3.
|
2023-09-22 16:09:36 -04:00
|
|
|
int32_t getValue(uint8_t re);
|
2024-06-24 07:54:24 -04:00
|
|
|
bool setValue(uint8_t re, int32_t value = 0);
|
2021-05-17 10:29:53 -04:00
|
|
|
|
2024-02-23 03:44:55 -05:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
2022-11-23 09:19:55 -05:00
|
|
|
// DEBUG
|
2023-09-22 16:09:36 -04:00
|
|
|
uint8_t getLastPosition(uint8_t re);
|
2021-05-17 10:29:53 -04:00
|
|
|
|
2021-12-27 15:02:58 -05:00
|
|
|
|
2024-02-23 03:44:55 -05:00
|
|
|
protected:
|
2021-11-15 15:55:49 -05:00
|
|
|
uint8_t _count = 0;
|
|
|
|
uint8_t _lastValue = 0;
|
2023-09-22 16:09:36 -04:00
|
|
|
uint8_t _lastPos[4] = { 0, 0, 0, 0 };
|
|
|
|
int32_t _encoder[4] = { 0, 0, 0, 0 };
|
2021-05-17 10:29:53 -04:00
|
|
|
|
|
|
|
uint8_t _address;
|
|
|
|
TwoWire * _wire;
|
|
|
|
};
|
|
|
|
|
2021-12-27 15:02:58 -05:00
|
|
|
|
2022-11-23 09:19:55 -05:00
|
|
|
// -- END OF FILE --
|
2021-12-27 15:02:58 -05:00
|
|
|
|