Ответсообщение недоступно
#include <BleGamepad.h>
BleGamepad bleGamepad(
"ESP32 BeamNG Shifter",
"ESP32",
100
);
// Пины
const uint8_t buttonPins[] = { 13, 14, 25, 26, 27, 32, 33, 5, 4 };
const uint8_t BUTTON_COUNT = 9;
//сцепление
const uint8_t CLUTCH_PIN = 5; //GPIO 5
const uint8_t CLUTCH_BTN_ID = 8;
int activeGearIndex = -1; // Индекс текущей передачи
//антидребезг сцепления
int lastClutchReading = HIGH;
unsigned long lastClutchDebounceTime = 0;
unsigned long clutchDebounceDelay = 50;
void setup() {
Serial.begin(115200);
// Настройка пинов
for (uint8_t i = 0; i < BUTTON_COUNT; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
BleGamepadConfiguration config;
config.setButtonCount(BUTTON_COUNT);
config.setAutoReport(true);
bleGamepad.begin(&config);
Serial.println("Система готова. GPIO 5 (Button 8) — Сцепление.");
}
void loop() {
if (!bleGamepad.isConnected()) return;
unsigned long currentMillis = millis();
// логика сцепы (GPIO 5)
int reading = digitalRead(CLUTCH_PIN); // Читаем gpio 5
if (reading != lastClutchReading) {
lastClutchDebounceTime = currentMillis;
lastClutchReading = reading;
}
if ((currentMillis - lastClutchDebounceTime) > clutchDebounceDelay) {
bool isClutchPressed = (reading == LOW);
if (isClutchPressed) {
// Если сцепление нажато, а передача еще не включена
if (!bleGamepad.isPressed(CLUTCH_BTN_ID)) {
bleGamepad.press(CLUTCH_BTN_ID);
Serial.println("Сцепление (GPIO 5): НАЖАТО");
// СБРОС ПЕРЕДАЧИ при нажатии сцепления
if (activeGearIndex != -1) {
bleGamepad.release(activeGearIndex + 1);
Serial.print("Сброс передачи номер: ");
Serial.println(activeGearIndex + 1);
activeGearIndex = -1;
}
}
}
else {
// Если сцепление отпущено
if (bleGamepad.isPressed(CLUTCH_BTN_ID)) {
bleGamepad.release(CLUTCH_BTN_ID);
Serial.println("Сцепление (GPIO 5): ОТПУЩЕНО");
}
}
}
// 2. Логика передач
for (uint8_t i = 0; i < BUTTON_COUNT; i++) {
// Сразу пропускаем gpio5 т.к. это сцепа
if (buttonPins[i] == CLUTCH_PIN) continue;
// Если кнопка передачи нажата
if (digitalRead(buttonPins[i]) == LOW) {
// Еслипередача еще не включена
if (activeGearIndex != i) {
// Прост антидребезг
delay(50);
if (digitalRead(buttonPins[i]) == LOW) { // Все еще нажата?
// Выключаем старую передачу если была
if (activeGearIndex != -1) {
bleGamepad.release(activeGearIndex + 1);
}
// Включаем новую
activeGearIndex = i;
bleGamepad.press(activeGearIndex + 1);
Serial.print("Включена передача (Button ");
Serial.print(activeGearIndex + 1);
Serial.println(")");
}
}
}
}
}