Introduce a new demo sketch to visulize the
attenuation and offset constraints of the input cv.
This commit is contained in:
@ -4,25 +4,29 @@
|
||||
* @brief Class for interacting with analog inputs.
|
||||
* @version 0.1
|
||||
* @date 2025-05-23
|
||||
*
|
||||
*
|
||||
* @copyright Copyright (c) 2025
|
||||
*
|
||||
*
|
||||
*/
|
||||
#ifndef ANALOG_INPUT_H
|
||||
#define ANALOG_INPUT_H
|
||||
|
||||
const int MAX_INPUT = (1 << 10) - 1; // Max 10 bit analog read resolution.
|
||||
|
||||
// estimated default calibration value
|
||||
const int CALIBRATED_LOW = -566;
|
||||
const int CALIBRATED_HIGH = 512;
|
||||
|
||||
class AnalogInput {
|
||||
public:
|
||||
AnalogInput() {}
|
||||
~AnalogInput() {}
|
||||
|
||||
/**
|
||||
* Initializes a analog input object.
|
||||
*
|
||||
* @param pin gpio pin for the analog input.
|
||||
*/
|
||||
* Initializes a analog input object.
|
||||
*
|
||||
* @param pin gpio pin for the analog input.
|
||||
*/
|
||||
void Init(uint8_t pin) {
|
||||
pinMode(pin, INPUT);
|
||||
pin_ = pin;
|
||||
@ -30,33 +34,43 @@ class AnalogInput {
|
||||
|
||||
/**
|
||||
* Read the value of the analog input and set instance state.
|
||||
*
|
||||
*
|
||||
*/
|
||||
void Process() {
|
||||
old_read_ = read_;
|
||||
int raw = analogRead(pin_);
|
||||
read_ = map(raw, offset_, MAX_INPUT, low_, high_);
|
||||
read_ = map(raw, 0, MAX_INPUT, low_, high_);
|
||||
read_ = constrain(read_ - offset_, -512, 512);
|
||||
if (inverted_) read_ = -read_;
|
||||
}
|
||||
|
||||
// Set calibration values.
|
||||
|
||||
void AdjustCalibrationLow(int amount) { low_ += amount; }
|
||||
void AdjustCalibrationOffset(int amount) { offset_ -= amount; }
|
||||
|
||||
void AdjustCalibrationHigh(int amount) { high_ += amount; }
|
||||
|
||||
void SetOffset(float percent) { offset_ = -(percent)*512; }
|
||||
|
||||
void SetAttenuation(float percent) {
|
||||
low_ = abs(percent) * CALIBRATED_LOW;
|
||||
high_ = abs(percent) * CALIBRATED_HIGH;
|
||||
inverted_ = percent < 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current value of the analog input within a range of +/-512.
|
||||
*
|
||||
*
|
||||
* @return read value within a range of +/-512.
|
||||
*
|
||||
*
|
||||
*/
|
||||
inline int16_t Read() { return read_; }
|
||||
|
||||
/**
|
||||
* Return the analog read value as voltage.
|
||||
*
|
||||
*
|
||||
* @return A float representing the voltage (-5.0 to +5.0).
|
||||
*
|
||||
*
|
||||
*/
|
||||
inline float Voltage() { return ((read_ / 512.0) * 5.0); }
|
||||
|
||||
@ -66,8 +80,9 @@ class AnalogInput {
|
||||
uint16_t old_read_;
|
||||
// calibration values.
|
||||
int offset_ = 0;
|
||||
int low_ = -512;
|
||||
int high_ = 512;
|
||||
int low_ = CALIBRATED_LOW;
|
||||
int high_ = CALIBRATED_HIGH;
|
||||
bool inverted_ = false;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user