backup diy encoder lib
This commit is contained in:
parent
347e788680
commit
ba92d5fa79
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"cmake.configureOnOpen": true
|
||||||
|
}
|
@ -18,4 +18,8 @@ build_flags =
|
|||||||
|
|
||||||
-D EXAMPLE_STRING=\"Pouet\"
|
-D EXAMPLE_STRING=\"Pouet\"
|
||||||
|
|
||||||
-D DEBUG
|
-D DEBUG
|
||||||
|
|
||||||
|
-D ENCODER_SWITCH=D5
|
||||||
|
-D ENCODER_DT=D6
|
||||||
|
-D ENCODER_CLK=D7
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
#ifndef PROGRAM_H
|
#ifndef PROGRAM_H
|
||||||
#define PROGRAM_H
|
#define PROGRAM_H
|
||||||
|
|
||||||
#include "Arduino.h"
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
#include "Encoder.h"
|
||||||
|
|
||||||
class Program {
|
class Program {
|
||||||
public:
|
public:
|
||||||
@ -14,6 +16,11 @@ public:
|
|||||||
* Program main loop
|
* Program main loop
|
||||||
*/
|
*/
|
||||||
void loop();
|
void loop();
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* Encoder object
|
||||||
|
*/
|
||||||
|
Encoder* encoder;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
64
lib/Encoder/include/Encoder.h
Normal file
64
lib/Encoder/include/Encoder.h
Normal 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
|
45
lib/Encoder/src/Encoder.cpp
Normal file
45
lib/Encoder/src/Encoder.cpp
Normal 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);
|
||||||
|
}
|
@ -3,6 +3,9 @@
|
|||||||
Program::Program() {
|
Program::Program() {
|
||||||
// Startup
|
// Startup
|
||||||
Serial.begin(MONITOR_SPEED);
|
Serial.begin(MONITOR_SPEED);
|
||||||
|
|
||||||
|
this->encoder = new Encoder(ENCODER_SWITCH, ENCODER_DT, ENCODER_CLK);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Program::loop() {
|
void Program::loop() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user