diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3a700b2 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "cmake.configureOnOpen": true +} diff --git a/config.ini b/config.ini index 5924152..ae6344a 100644 --- a/config.ini +++ b/config.ini @@ -18,4 +18,8 @@ build_flags = -D EXAMPLE_STRING=\"Pouet\" - -D DEBUG \ No newline at end of file + -D DEBUG + + -D ENCODER_SWITCH=D5 + -D ENCODER_DT=D6 + -D ENCODER_CLK=D7 diff --git a/include/Program.h b/include/Program.h index 66e4a79..39638fc 100644 --- a/include/Program.h +++ b/include/Program.h @@ -1,7 +1,9 @@ #ifndef PROGRAM_H #define PROGRAM_H -#include "Arduino.h" +#include + +#include "Encoder.h" class Program { public: @@ -14,6 +16,11 @@ public: * Program main loop */ void loop(); +private: + /** + * Encoder object + */ + Encoder* encoder; }; #endif diff --git a/lib/Encoder/include/Encoder.h b/lib/Encoder/include/Encoder.h new file mode 100644 index 0000000..b0c30b6 --- /dev/null +++ b/lib/Encoder/include/Encoder.h @@ -0,0 +1,64 @@ +#ifndef ENCODER_H +#define ENCODER_H + +#include + +class Encoder{ +public: + /** + * @brief Construct a new Encoder object with the given pins + * + * @param pinSW encoder switch pin + * @param pinDT encoder DT pin + * @param pinCLK encoder CLK pin + */ + Encoder(int pinSW, int pinDT, int pinCLK); + + /** + * @brief returns the instance of the encoder + */ + static Encoder* getInstance(); + + +private: + + /** + * @brief pinSW is the pin number of the switch + */ + int pinSW; + + /** + * @brief pinDT is the pin number of the DT pin + */ + int pinDT; + + /** + * @brief pinCLK is the pin number of the CLK pin + */ + int pinCLK; + + /** + * @brief swEvent is the event called when the switch is pressed + */ + IRAM_ATTR static void swEvent(); + + /** + * @brief dtEvent is the event called when the encoder is turned + */ + IRAM_ATTR static void dtEvent(); + + /** + * @brief number is the current number of the encoder + */ + int number; + + /** + * @brief instance is the instance of the encoder + */ + static Encoder* instance; + +}; + + + +#endif diff --git a/lib/Encoder/src/Encoder.cpp b/lib/Encoder/src/Encoder.cpp new file mode 100644 index 0000000..8ea77a4 --- /dev/null +++ b/lib/Encoder/src/Encoder.cpp @@ -0,0 +1,45 @@ +#include "../include/Encoder.h" + +Encoder* Encoder::instance = nullptr; + +Encoder* Encoder::getInstance() { + return Encoder::instance; +} + +Encoder::Encoder(int pinSW, int pinDT, int pinCLK) { + this->pinSW = pinSW; + this->pinDT = pinDT; + this->pinCLK = pinCLK; + + pinMode(pinSW, INPUT); + pinMode(pinDT, INPUT); + pinMode(pinCLK, INPUT); + + attachInterrupt(digitalPinToInterrupt(this->pinCLK), Encoder::dtEvent, RISING); + attachInterrupt(digitalPinToInterrupt(this->pinSW), Encoder::swEvent, RISING); + + this->number = 0; + + Encoder::instance = this; +} + +IRAM_ATTR void Encoder::dtEvent() { + Encoder* encoder = Encoder::getInstance(); + if(digitalRead(encoder->pinDT)){ + encoder->number++; + } else { + encoder->number--; + } + Serial.println(encoder->number); + delay(10); +} + +IRAM_ATTR void Encoder::swEvent() { + Serial.print("test :"); + Serial.print(digitalRead(ENCODER_SWITCH)); + Serial.print(" "); + Serial.print(digitalRead(ENCODER_DT)); + Serial.print(" "); + Serial.println(digitalRead(ENCODER_CLK)); + delay(10); +} diff --git a/src/Program.cpp b/src/Program.cpp index e412959..80a5656 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -3,6 +3,9 @@ Program::Program() { // Startup Serial.begin(MONITOR_SPEED); + + this->encoder = new Encoder(ENCODER_SWITCH, ENCODER_DT, ENCODER_CLK); + } void Program::loop() {