diff --git a/lib/SwitchableEncodeur/include/SwitchableEncodeur.h b/lib/SwitchableEncodeur/include/SwitchableEncodeur.h new file mode 100644 index 0000000..a938568 --- /dev/null +++ b/lib/SwitchableEncodeur/include/SwitchableEncodeur.h @@ -0,0 +1,80 @@ +#ifndef SWITCHABLEENCODEUR_H +#define SWITCHABLEENCODEUR_H + +#include "Encoder.h" + +class SwitchableEncodeur : public Encoder { +public: + + /** + * @brief Construct a new Switchable Encodeur object + * + * @param pin1 ENCODER_DT + * @param pin2 ENCODER_CLK + * @param pinSW ENCODER_SW + * @param nbMenu nombre D'état du system + */ + SwitchableEncodeur(uint8_t pin1, uint8_t pin2, uint8_t pinSW, int nbMenu = 0); + + /** + * @brief reset the menu + */ + void resetMenu(); + + /** + * @brief add one to the menu + */ + void addMenu(); + + /** + * @brief Get the Menu number + * @return int + */ + int getMenu(); + + /** + * @brief Get return the encodeur value + * + * @return int dernière valeur de l'encodeur + */ + int getValue(); + + /** + * @brief update the encodeur value + * + * @return true the value has changed + * @return false the value has not changed + */ + bool update(); + + /** + * @brief reset the encodeur value + */ + void resetValue(); + + static SwitchableEncodeur* getInstance(); + +private: + + static SwitchableEncodeur* instance; + + /** + * @brief menu actuel + */ + int menu; + + /** + * @brief old position of the encoder + */ + int oldPosition = -999; + + /** + * @brief nombre de menu + */ + int nbMenu; + +}; + + + +#endif diff --git a/lib/SwitchableEncodeur/src/SwitchableEncodeur.cpp b/lib/SwitchableEncodeur/src/SwitchableEncodeur.cpp new file mode 100644 index 0000000..2723d59 --- /dev/null +++ b/lib/SwitchableEncodeur/src/SwitchableEncodeur.cpp @@ -0,0 +1,54 @@ +#include "../include/SwitchableEncodeur.h" + +IRAM_ATTR void switchEncoder() { + SwitchableEncodeur::getInstance()->addMenu(); +} + +SwitchableEncodeur* SwitchableEncodeur::instance = nullptr; + +SwitchableEncodeur::SwitchableEncodeur(uint8_t pin1, uint8_t pin2, uint8_t pinSW, int nbMenu): Encoder(pin1, pin2){ + SwitchableEncodeur::instance = this; + this->menu = 0; + this->nbMenu = nbMenu; + pinMode(pinSW, INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(pinSW), switchEncoder, RISING);//FIXME: maybe change to FALLING + this->oldPosition = -999; +} + + +void SwitchableEncodeur::resetMenu() { + this->menu = 0; +} + +void SwitchableEncodeur::addMenu() { + this->menu++; + if (this->menu > this->nbMenu) { + this->menu = 0; + } +} + +int SwitchableEncodeur::getMenu() { + return this->menu; +} + +SwitchableEncodeur* SwitchableEncodeur::getInstance() { + return SwitchableEncodeur::instance; +} + +bool SwitchableEncodeur::update() { + bool sortie = false; + long newPosition = this->read()/4; + if (newPosition != this->oldPosition) { + this->oldPosition = newPosition; + sortie = true; + } + return sortie; +} + +int SwitchableEncodeur::getValue() { + return this->oldPosition; +} + +void SwitchableEncodeur::resetValue() { + this->oldPosition = 0; +}