#include <Encoder.h> // https://www.pjrc.com/teensy/td_libs_Encoder.html
#include <Toggle.h> // https://github.com/Dlloydev/Toggle
class Codeur {
Encoder encodeur;
char plus, moins;
long valeurPrecedente = 0;
public:
Codeur(const byte dtPin, const byte clkPin, const char p, const char m):
encodeur(dtPin, clkPin), plus(p), moins(m) {}
void begin() {
encodeur.write(0);
}
void check() {
long v = encodeur.read() >> 2; // on divise par 4 car l'encdeur envoie 4 ticks par clicks
if (v != valeurPrecedente) {
// la valeur a changé
if (v > valeurPrecedente) {
for (long i = 0; i < v - valeurPrecedente; i++) Serial.print(plus);
} else {
for (long i = 0; i < valeurPrecedente - v; i++) Serial.print(moins);
}
valeurPrecedente = v;
}
}
};
class Bouton {
public:
Toggle bouton;
const byte pin;
char down, up;
public:
Bouton(const byte p, const char d, const char u) : bouton(p), pin(p), down(d), up(u) {};
void begin() {
bouton.begin(pin);
}
void check() {
bouton.poll();
if (bouton.onPress()) Serial.print(down);
if (bouton.onRelease()) Serial.print(up);
}
};
Codeur encodeurs[] = {{3, 2, 'a', 'z'}, {6, 5, 'e', 'r'}, {9, 8, 't', 'y'}};
Bouton boutons[] = {{4, 'q', 's'}, {7, 'd', 'f'}, {10, 'g', 'h'}, {11, 'j', 'k'}, {12, 'l', 'm'}, {13, 'w', 'x'}};
void setup() {
for (Codeur& c : encodeurs) c.begin();
for (Bouton& b : boutons) b.begin();
Serial.begin(115200);
Serial.println(F("PRET!"));
}
void loop() {
for (Codeur& c : encodeurs) c.check();
for (Bouton& b : boutons) b.check();
}