diff --git a/firmware/src/hw/potentiometer.cpp b/firmware/src/hw/potentiometer.cpp new file mode 100644 index 0000000..0c88a36 --- /dev/null +++ b/firmware/src/hw/potentiometer.cpp @@ -0,0 +1,38 @@ +/**** Includes ****/ +#include "../utils/utils.h" +#include "../utils/interpolate.h" +#include "potentiometer.h" + +using namespace hw; + +/**** Private definitions ****/ +/**** Private constants ****/ +/**** Private variables ****/ +/**** Private function declarations ****/ + +/**** Public function definitions ****/ +hw::Potentiometer::Potentiometer(board::AnalogIn* ain_ch, uint16_t low_deadzone, uint16_t high_deadzone) +{ + this->ain_ch = ain_ch; + this->low_deadzone = low_deadzone; + this->high_deadzone = high_deadzone; + this->percent = 0; +} + +hw::Potentiometer::~Potentiometer(void) +{ + return; +} + +uint8_t hw::Potentiometer::read(void) +{ + // Update input + this->ain_ch->read(); + + // Calculate percent + this->percent = util::interpolate(this->ain_ch->last_read, this->low_deadzone, this->high_deadzone, 0, 100); + + return this->percent; +} + +/**** Private function definitions ****/ diff --git a/firmware/src/hw/potentiometer.h b/firmware/src/hw/potentiometer.h new file mode 100644 index 0000000..5a853cc --- /dev/null +++ b/firmware/src/hw/potentiometer.h @@ -0,0 +1,35 @@ +#ifndef POTENTIOMETER_H_ +#define POTENTIOMETER_H_ + +/**** Includes ****/ +#include +#include "../board/ain.h" + +namespace hw { + +/**** Public definitions ****/ + +class Potentiometer +{ + protected: + board::AnalogIn* ain_ch; + + public: + Potentiometer(board::AnalogIn* ain_ch, uint16_t low_deadzone, uint16_t high_deadzone); + ~Potentiometer(void); + + uint16_t low_deadzone; + uint16_t high_deadzone; + uint8_t percent; + + uint8_t read(void); +}; + +/**** Public function declarations ****/ + +#ifdef TESTING +#endif + +} //namespace + +#endif /* POTENTIOMETER_H_ */ \ No newline at end of file