esp32cam 蓝牙键盘的模拟

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>

//https://gitcode.net/mirrors/T-vK/ESP32-BLE-Keyboard?utm_source=csdn_github_accelerator
#include <BleKeyboard.h>
BleKeyboard bleKeyboard;

struct keys {
    int cross[2] = {14,15};
    int vertical[3] = {2,13,12};
    int crossNum=2;
    int verticalNum=3;
};
struct keys mykeys;

void initKey() {
    for(int i=0; i<mykeys.crossNum; i++) {
        pinMode(mykeys.cross[i], INPUT_PULLUP);
    }

    for(int i=0; i<mykeys.verticalNum; i++) {
        pinMode(mykeys.vertical[i], OUTPUT);
    }
    Serial.println("keys work!");
}

/**
 * 所有竖都输出高电平
 * */
void setVerticalAllHigh(){
  for(int i = 0; i < mykeys.verticalNum; i++) {
      digitalWrite(mykeys.vertical[i], HIGH);
  }
}


/**
 * 指定竖都输出低电平
 * */
void setVerticalLow(int num) {
    digitalWrite(mykeys.vertical[num], LOW);
}


/**
 * 按键处理函数, 输入对应的按键, 分别处理不同的方法
 */
void handleKey(int i) {
  if(i==0) {
    Serial.print("Left Ctr + S to save!!!");
    Serial.println("Sending Ctrl+Alt+Delete...");
    bleKeyboard.press(KEY_MEDIA_WWW_HOME);
    // bleKeyboard.press(KEY_LEFT_CTRL);
//    bleKeyboard.write('s');  // press and release the 'A' key
    bleKeyboard.releaseAll();
  } else if(i==1) {
    bleKeyboard.write('1');
    bleKeyboard.write('3');
    bleKeyboard.write('1');
    bleKeyboard.write('4');
//    Serial.print("change table");
//    Serial.println("Sending Ctrl+Alt+Delete...");
//    bleKeyboard.press(KEY_LEFT_ALT);
//    bleKeyboard.press(KEY_TAB);
//    bleKeyboard.releaseAll();
  } else if(i==2) {
    Serial.println("KEY_MEDIA_VOLUME_DOWN");
    bleKeyboard.press(KEY_MEDIA_VOLUME_DOWN);
    bleKeyboard.releaseAll();    
  } else if(i==3) {
    Serial.print("change desk");
    bleKeyboard.press(KEY_LEFT_CTRL);
    bleKeyboard.press(KEY_RIGHT_GUI);
    bleKeyboard.press(KEY_LEFT_ARROW);
    bleKeyboard.releaseAll();
  } else if(i==4) {
    Serial.print("change desk");
    bleKeyboard.press(KEY_LEFT_CTRL);
    bleKeyboard.press(KEY_RIGHT_GUI);
    bleKeyboard.press(KEY_RIGHT_ARROW);
    bleKeyboard.releaseAll();
} else if(i==5) {
    Serial.println("KEY_MEDIA_VOLUME_UP");
    bleKeyboard.press(KEY_MEDIA_VOLUME_UP);
    bleKeyboard.releaseAll();    
  }
  
}


/**
 * 扫描键盘, 打印每次扫描到的按键序号
 */
int scanKeysFlag=0; // 用于每次按下去延时300ms, 因为扫描频率是100ms
void scanKeys() {
    for(int i=0; i<mykeys.verticalNum; i++) {
        setVerticalAllHigh();
        setVerticalLow(i);
        for(int j=0; j<mykeys.crossNum; j++) {
            if(scanKeysFlag<7 && digitalRead(mykeys.cross[j]) == LOW) {
                scanKeysFlag=10;
                // 二维转一维
                Serial.println(j*mykeys.verticalNum+i);
                handleKey(j*mykeys.verticalNum+i);
            }
        }
    }
}



void setup() {
  Serial.begin(115200);
  // 设置按键管脚上拉输入模式, 默认为高电平, 触发读取后是低电平 
  Serial.println("Starting BLE work!");
  bleKeyboard.begin();
  initKey();   
}


// 每个任务的轮询延长时间
class Flasher_task {
  long OnTime;
  unsigned long previousMillis;  // will store last time LED was updated
  void (*func)();
public:
  Flasher_task(void (*task)(), long on) {
    OnTime = on;
    previousMillis = 0;
    func = task;
  }
  void Update() {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= OnTime) {
      previousMillis = currentMillis;  // Remember the time
      if(scanKeysFlag>0) scanKeysFlag--;
      func();
    }
  }
};

Flasher_task task1(scanKeys,100); // 让oled和mpu6050不停轮询更新数据

void loop() {
  task1.Update();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落子无悔!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>