New Encoder4 overloads with possibility to add min, max and wrap-around options.

master
Holger Wirtz 6 years ago
parent 36b8b6f9ee
commit 36cdb13133
  1. 59
      Encoder4.h

@ -23,6 +23,7 @@
*/
#include <Encoder.h>
#include "limits.h"
#ifndef ENCODER4_H_INCLUDED
#define ENCODER4_H_INCLUDED
@ -31,10 +32,28 @@ class Encoder4 : public Encoder
{
public:
using Encoder::Encoder;
//using Encoder::Encoder;
Encoder4(uint8_t pin1, uint8_t pin2) : Encoder::Encoder(pin1, pin2)
{
_min = INT_MIN / 4;
_max = INT_MAX / 4;
_wrap_around = false;
}
int32_t read()
{
int32_t p = Encoder::read();
if (p / 4 < _min && _wrap_around == false)
Encoder::write(_min * 4);
else if (p / 4 < _min && _wrap_around == true)
Encoder::write(_max * 4);
else if (p / 4 > _max && _wrap_around == false)
Encoder::write(_max * 4);
else if (p / 4 > _max && _wrap_around == true)
Encoder::write(_min * 4);
return (Encoder::read() / 4);
}
@ -42,6 +61,44 @@ class Encoder4 : public Encoder
{
Encoder::write(p * 4);
}
void write(int32_t p, int32_t min, int32_t max, bool wrap_around=false)
{
_wrap_around=wrap_around;
if (max < min)
{
_min = max;
_max = min;
}
else
{
_min = min;
_max = max;
}
if (p > _max)
p = _max;
else if (p < _min)
p = _min;
Encoder::write(p * 4);
}
int32_t read_min(void)
{
return (_min);
}
int32_t read_max(void)
{
return (_max);
}
private:
int32_t _min;
int32_t _max;
bool _wrap_around;
};
#endif

Loading…
Cancel
Save