Skip to content

Commit

Permalink
Added min/max parameters to potentiometer
Browse files Browse the repository at this point in the history
  • Loading branch information
talbotmcinnis committed Sep 4, 2022
1 parent 43db677 commit e4f8afe
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
3 changes: 3 additions & 0 deletions examples/OneOfEverything/OneOfEverything.ino
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ DcsBios::RotaryEncoder rotaryEncoderExample("MSG_0", "ARG_DEC", "ARG_INC", 1, 2)
DcsBios::RotaryAcceleratedEncoder rotaryAcceleratedEncoderExample("MSG_0", "ARG_DEC", "ARG_INC", "FAST_INC", "FAST_DEC", 1, 2);
// A linear/analog axis on a single pin
DcsBios::Potentiometer potentiometerExample("MSG_0", 1);
// A linear axis control where the physical or electrical range of the input does utilize the full 0 to 1023 range.
DcsBios::Potentiometer clippedPotentiometerExample("MSG_0", false, 256, 768);

// An inverted version of a linear axis control
DcsBios::Potentiometer invertedPotentiometerExample("MSG_0", 1, true);

Expand Down
3 changes: 3 additions & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## vNext
- Added input_min and input_max optional parameters to PotentiometerEWMA to allow a user to calibrate their analog inputs if their physical control does not utilize the full range available to the controller.

## v0.3.7
- Integrate 2 and 3 position Matrix switches, thanks to Dehuman for the starting point!
- Fix STM32 compilation issue. Confirmed working on Bluepill and SparkFun.
Expand Down
17 changes: 12 additions & 5 deletions src/internal/Potentiometers.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ namespace DcsBios {
void pollInput() {
unsigned int state;
if (reverse_)
state = map(analogRead(pin_), 0, 1023, 65535, 0);
state = map(analogRead(pin_), input_min_, input_max_, 65535, 0);
else
state = map(analogRead(pin_), 0, 1023, 0, 65535);
state = map(analogRead(pin_), input_min_, input_max_, 0, 65535);

accumulator += ((float)state - accumulator) / (float)ewma_divisor;
state = (unsigned int)accumulator;

Expand All @@ -40,18 +41,23 @@ namespace DcsBios {
unsigned int lastState_;
float accumulator;
bool reverse_;
unsigned int input_min_;
unsigned int input_max_;

public:
PotentiometerEWMA(const char* msg, char pin, bool reverse = false) :
PotentiometerEWMA(const char* msg, char pin, bool reverse = false, unsigned int input_min = 0, unsigned int input_max = 1023) :
PollingInput(pollIntervalMs) {
msg_ = msg;
pin_ = pin;
reverse_ = reverse;
input_min_ = input_min;
input_max_ = input_max;

pinMode(pin_, INPUT);
if (reverse_)
lastState_ = map(analogRead(pin_), 0, 1023, 65535, 0);
lastState_ = map(analogRead(pin_), input_min_, input_max_, 65535, 0);
else
lastState_ = map(analogRead(pin_), 0, 1023, 0, 65535);
lastState_ = map(analogRead(pin_), input_min_, input_max_, 0, 65535);
}

void SetControl( const char* msg )
Expand All @@ -64,6 +70,7 @@ namespace DcsBios {
this->resetState();
}
};

typedef PotentiometerEWMA<> Potentiometer;
}

Expand Down

0 comments on commit e4f8afe

Please sign in to comment.