backup diy encoder lib

This commit is contained in:
2023-08-11 16:40:06 +02:00
parent 347e788680
commit ba92d5fa79
6 changed files with 128 additions and 2 deletions

View File

@ -0,0 +1,64 @@
#ifndef ENCODER_H
#define ENCODER_H
#include <Arduino.h>
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

View File

@ -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);
}