49 lines
826 B
C++
49 lines
826 B
C++
#ifndef HELLIGKEIT_ROTARY_H
|
|
#define HELLIGKEIT_ROTARY_H
|
|
|
|
#include <Arduino.h>
|
|
|
|
class Rotary {
|
|
|
|
public:
|
|
|
|
typedef void (*callback_t)(int delta);
|
|
|
|
private:
|
|
|
|
const uint8_t pinCLK;
|
|
|
|
const uint8_t pinDT;
|
|
|
|
bool lastCLK = false;
|
|
|
|
callback_t callback;
|
|
|
|
public:
|
|
|
|
Rotary(const uint8_t pinCLK, const uint8_t pinDT, callback_t callback) : pinCLK(pinCLK), pinDT(pinDT), callback(callback) {
|
|
//
|
|
}
|
|
|
|
void setup() {
|
|
pinMode(pinCLK, INPUT_PULLUP);
|
|
pinMode(pinDT, INPUT_PULLUP);
|
|
lastCLK = digitalRead(pinCLK) == HIGH;
|
|
}
|
|
|
|
void loop() {
|
|
const bool currentCLK = digitalRead(pinCLK) == HIGH;
|
|
if (currentCLK != lastCLK && currentCLK) {
|
|
if ((digitalRead(pinDT) == HIGH) != currentCLK) {
|
|
callback(+1);
|
|
} else {
|
|
callback(-1);
|
|
}
|
|
}
|
|
lastCLK = currentCLK;
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|