繁体   English   中英

尝试使用 arduino ide 使用 esp32 制作自定义快捷键键盘

[英]Trying to make a custom shortcut keyboard with esp32 with arduino ide

好的,就像标题一样,我正在尝试使用 esp32 和 Arduino ide 制作一个自定义快捷键键盘,并不断遇到错误,因此如果有人可以为我解决所有问题,我将不胜感激。

这些错误之一是:函数中没有 return 语句返回 non-void [-Werror=return-type]

代码:

#include <BleKeyboard.h>

BleKeyboard bleKeyboard;

const int buttonPin[] = {36, 39, 34, 35, 32, 33};
int pinCount = 6;

int potPin = 2;
int prevPotState = -1;
int potState = -1;
int potTolerance = 1;
long potDebounceDelay = 20;

int buttonState[] = {1, 1, 1, 1, 1, 1};
int prevButtonState[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
long startedPressing[] = {0, 0, 0, 0, 0, 0};
boolean longPressing[] = {false, false, false, false, false, false};

long lastDebounceTime[] = {0, 0, 0, 0, 0, 0, 0}; // 1 more for the pot

long debounceDelay = 0;

boolean testHardware = false;

int keyComb(char key1 = 0, char key2 = 0, char key3 = 0, char key4 = 0) {
  if (key1 != 0) {
    bleKeyboard.press(key1);
  }
  if (key2 != 0) {
    bleKeyboard.press(key2);
  }
  if (key3 != 0) {
    bleKeyboard.press(key3);
  }
  if (key4 != 0) {
    bleKeyboard.press(key4);
  }
  delay(100);
  bleKeyboard.releaseAll();
}

int sendLine(char const * line) {
  bleKeyboard.print(line);
  delay(750);
  keyComb(KEY_RETURN);
}

// Output actions. Probably the only part that you need to change
int outputAction(int currentButton, int typeOfPress = 0) {
  // typeOfPress 1: on push; 2: on release; 3: on long press; 4: on lingering press.
  // actions on release, on long press and lingering press include the action press. Action lingering press cancels action release and long press.

  if (testHardware) {
    bleKeyboard.print(currentButton + 1);
    if (typeOfPress == 1) {
      bleKeyboard.print(" pressed ");
    }
    if (typeOfPress == 2) {
      bleKeyboard.print(" released ");
    }
    if (typeOfPress == 3) {
      bleKeyboard.print(" long ");
    }
    if (typeOfPress == 4) {
      bleKeyboard.print(" lingering ");
    }
    bleKeyboard.print(millis());
    bleKeyboard.print("Pressed: ");
    bleKeyboard.print(lastDebounceTime[currentButton]);
    keyComb(KEY_RETURN);
  } else {

    if (currentButton + 1 == 1) {
      if (typeOfPress == 1) {
        keyComb(KEY_LEFT_CTRL, KEY_LEFT_ALT, KEY_LEFT_SHIFT, 'M');
      }
      if (typeOfPress == 3) {
        keyComb(KEY_LEFT_CTRL, KEY_LEFT_ALT, KEY_LEFT_SHIFT, 'S');
      }
    }
    if (currentButton + 1 == 2) {
      if (typeOfPress == 1) {
        keyComb(KEY_LEFT_CTRL, KEY_LEFT_ALT, KEY_LEFT_SHIFT, 'P');
      }
    }
    if (currentButton + 1 == 3) {
      if (typeOfPress == 1) {
        keyComb(KEY_LEFT_CTRL, KEY_LEFT_ALT, KEY_LEFT_SHIFT, 'M');
      }
    }
    if (currentButton + 1 == 4) {
      if (typeOfPress == 2) {
        keyComb(KEY_LEFT_CTRL, KEY_LEFT_ALT, KEY_LEFT_SHIFT, 'L');
      }
    }
    if (currentButton + 1 == 5) {
      if (typeOfPress == 1) {
        keyComb(KEY_LEFT_CTRL, KEY_LEFT_ALT, KEY_LEFT_SHIFT, 'K');
      }
    }
    if (currentButton + 1 == 6) {
      if (typeOfPress == 1) {
        keyComb(KEY_LEFT_CTRL, KEY_LEFT_ALT, KEY_LEFT_SHIFT, 'N');
      }
    }
  }
}

void setup() {
  Serial.begin(115200);

  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
    pinMode(buttonPin[thisPin], INPUT);
    analogWrite(buttonPin[thisPin], HIGH); // In some versions use INPUT_PULLUP to use the built-in pull up resistor
  }
  bleKeyboard.begin();
}

void loop() {
  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
    buttonState[thisPin] = analogRead(buttonPin[thisPin]);
    // HIGH = state 1 <- button not pressed
    // LOW  = state 0 <- button pressed

    // On longer press
    if ((startedPressing[thisPin] == 0) || ((millis() - startedPressing[thisPin]) <= 1200)) {

      // Debouncing not working properly with current hardware
      //if (((buttonState[thisPin] != prevButtonState[thisPin])) && ((millis() - lastDebounceTime[thisPin]) > debounceDelay)) {
      if ((buttonState[thisPin] != prevButtonState[thisPin])) {

        if (buttonState[thisPin] == 0) {
          // Standard press action
          startedPressing[thisPin] = millis();
          outputAction(thisPin, 1);
        } else {

          if (!longPressing[thisPin]) {
            if ((millis() - startedPressing[thisPin]) < 500) {
              // On release (to avoid standard action if is incompatible with Long or Longer action)
              outputAction(thisPin, 2);
            } else {
              // Long action (+standard action already sent)
              outputAction(thisPin, 3);
            }
          }

          startedPressing[thisPin] = 0;
          longPressing[thisPin] = false;
        }
        lastDebounceTime[thisPin] = millis();
      }
    } else {
      outputAction(thisPin, 4);

      longPressing[thisPin] = true;
      startedPressing[thisPin] = 0;
    }

    prevButtonState[thisPin] = buttonState[thisPin];
  }
  // The pot
  int thisPin = pinCount; // To consider it the last one in the lastDebounceTime array
  potState = (int) (analogRead(potPin) / 6);
  if (prevPotState == -1) {
    prevPotState = potState;
  }
  if (((potState > prevPotState + potTolerance) || (potState < prevPotState - potTolerance))
      && ((millis() - lastDebounceTime[thisPin]) > potDebounceDelay)
     ) {

    if (potState > prevPotState) {
      keyComb(KEY_UP_ARROW);
    } else {
      keyComb(KEY_DOWN_ARROW);
    }
    lastDebounceTime[thisPin] = millis();
    prevPotState = potState;
  }
}

!!!填料!!! hehehuheuheuheuhuehdkl;lbkljdfklbfklvjnbgkljnjbgvnjkvjbkvfnvkljklbjk

从你的函数声明开始,你声明的函数可以在代码中返回一个整数。 但是你不返回一个整数。

对于你的例子,

int sendLine(char const * line) {
  bleKeyboard.print(line);
  delay(750);
  keyComb(KEY_RETURN);
}

您不会在函数 sendLine 中返回整数。

您声明的另一个函数也有同样的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM