base button
This commit is contained in:
63
ble_button.ino
Normal file
63
ble_button.ino
Normal file
@@ -0,0 +1,63 @@
|
||||
#include <HijelHID_BLEKeyboard.h>
|
||||
|
||||
// HijelHID_BLEKeyboard keyboard;
|
||||
HijelHID_BLEKeyboard keyboard("ESP32 Emergency button", "Hijel", 100);
|
||||
|
||||
#ifndef LED_BUILTIN
|
||||
#define LED_BUILTIN 2
|
||||
#endif
|
||||
unsigned long dernierBlink = 0;
|
||||
bool etatLed = false;
|
||||
|
||||
#define BROCHE_BOUTON 4
|
||||
#define DUREE_ANTIREBOND 50 // millisecondes
|
||||
|
||||
int etatBrut = HIGH; // derniere lecture brute de la broche
|
||||
int etatStable = HIGH; // etat confirme apres anti-rebond
|
||||
unsigned long tChangement = 0; // horodatage du dernier changement brut
|
||||
bool arme = true; // true = pret a envoyer une frappe
|
||||
|
||||
void setup() {
|
||||
keyboard.begin();
|
||||
pinMode(BROCHE_BOUTON, INPUT_PULLUP); // pull-up interne : repos = HIGH, appui = LOW
|
||||
Serial.begin(115200);
|
||||
}
|
||||
|
||||
void envoyerFrappe() {
|
||||
if (keyboard.isConnected()) {
|
||||
keyboard.tap(KEY_B);
|
||||
Serial.println("Frappe : OK");
|
||||
// keyboard.print("httpsM!!zzz:youtube:co,!zqtch§v=DLwxrwFCyOs");
|
||||
// keyboard.tap(KEY_RETURN);
|
||||
}
|
||||
else {
|
||||
Serial.println("Connectez l'ESP32 à un périphérique avant d'appuyer sur le bouton");
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
int lecture = digitalRead(BROCHE_BOUTON);
|
||||
|
||||
// 1) Anti-rebond : a chaque changement de la lecture brute, on relance le minuteur
|
||||
if (lecture != etatBrut) {
|
||||
tChangement = millis();
|
||||
etatBrut = lecture;
|
||||
}
|
||||
|
||||
// 2) On ne valide un nouvel etat que s'il est stable depuis assez longtemps
|
||||
if (millis() - tChangement > DUREE_ANTIREBOND) {
|
||||
if (lecture != etatStable) {
|
||||
etatStable = lecture;
|
||||
|
||||
if (etatStable == LOW && arme) {
|
||||
// Front descendant + bouton arme -> une seule frappe
|
||||
envoyerFrappe();
|
||||
arme = false; // verrou : plus rien jusqu'au relachement
|
||||
}
|
||||
else if (etatStable == HIGH) {
|
||||
// Bouton relache -> on rearme pour le prochain appui
|
||||
arme = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user