아두이노 블루투스 에어마우스 만들기
프로젝트/FPS 게임 컨트롤러

아두이노 블루투스 에어마우스 만들기

반응형

정말 오랜만에 에어마우스 글을 올리는 것 같다.

https://diy-project.tistory.com/12

 

아두이노 에어마우스

공중에서 사용할 수 있는 에어마우스를 일상 생활에서 쓸 일은 그리 많지는 않다. 최근들어 다양한 기능을 지원하는 스마트 티비의 개발과 함께 좀 더 다양한 동작을 할 수있는 에어마우스 형태

diy-project.tistory.com

https://diy-project.tistory.com/19

 

아두이노 FPS 게임 컨트롤러 (오버워치, 더 하우스 오브 더 데드)

전에 아두이노를 이용해 에어마우스를 만들었었다. http://diy-project.tistory.com/12 이 에어마우스를 만든 직후 이를 응용해 FPS 게임컨트롤러를 제작하기 시작했고, 예상보다 오래걸린 약 2주의 제작

diy-project.tistory.com

https://diy-project.tistory.com/57

 

아두이노 FPS 게임 컨트롤러 2.0

시작하기에 앞서 4개월전에 올린 FPS 게임 컨트롤러에 (http://diy-project.tistory.com/19) 대해 많은 분들이 관심을 가져준 것에 대해 감사한다. 많은 분들이 댓글, 메일 등으로 해당 프로젝트에 대한

diy-project.tistory.com

 

사실 기존에는 nRF24L01 모듈을 이용해 RF 수신 방식으로 에어마우스를 구현했으나 송수신기를 모두 만들어야 한다는 점이 가장 큰 문제였다. 그래서 오랜만에 FPS 게임 컨트롤러 3.0도 제작해볼 겸 블루투스 통신이 가능한 에어마우스를 제작해 보기로 했다.

 

우선 아래와 같은 ESP32 보드가 필요하다.

 

일단 정식 명칭은 ESP32 devkitc v4인 듯싶다. 블루투스랑 와이파이 통신을 모두 지원하는데 가격은 3~4달러면 구매가 가능한 가성비가 매우 좋은 보드이다. 아래 링크에서 구매가 가능하다.

 

반응형
 

4910.0₩ 20% OFF|ESP32 개발 보드 ESP32 DevKitC WiFi + 블루투스 초저전력 소비 듀얼 코어 ESP 32|development boa

Smarter Shopping, Better Living! Aliexpress.com

ko.aliexpress.com

 

이제 아래와 같이 회로를 구성한다. (필자는 그림과 좀 달리 VCC를 5V 핀에 연결했다.) 기존 에어마우스 제작 글을 참고하면 좋다.

 

코드는 다음과 같다. 이번 기회에 기존 에어마우스 코드에서 잘못된 부분도 다수 수정했다.

#include <Wire.h>
#include <SPI.h>
#include <SoftwareSerial.h>
#include <BleMouse.h>

uint8_t data[6];
int16_t gyroX, gyroZ;
 
int Sensitivity = 600;
int delayi = 20;

BleMouse bleMouse;

uint32_t timer;
uint8_t i2cData[14];
 
const uint8_t IMUAddress = 0x68;
const uint16_t I2C_TIMEOUT = 1000;
 
uint8_t i2cWrite(uint8_t registerAddress, uint8_t* data, uint8_t length, bool sendStop) {
  Wire.beginTransmission(IMUAddress);
  Wire.write(registerAddress);
  Wire.write(data, length);
  return Wire.endTransmission(sendStop); // Returns 0 on success
}

uint8_t i2cWrite2(uint8_t registerAddress, uint8_t data, bool sendStop) {
  return i2cWrite(registerAddress, &data, 1, sendStop); // Returns 0 on success
}
 
uint8_t i2cRead(uint8_t registerAddress, uint8_t* data, uint8_t nbytes) {
  uint32_t timeOutTimer;
  Wire.beginTransmission(IMUAddress);
  Wire.write(registerAddress);
  if(Wire.endTransmission(false))
    return 1;
  Wire.requestFrom(IMUAddress, nbytes,(uint8_t)true);
  for(uint8_t i = 0; i < nbytes; i++) {
    if(Wire.available())
      data[i] = Wire.read();
    else {
      timeOutTimer = micros();
      while(((micros() - timeOutTimer) < I2C_TIMEOUT) && !Wire.available());
      if(Wire.available())
        data[i] = Wire.read();
      else
        return 2;
    }
  }
  return 0;
}

void setup() {
  Wire.begin();

  i2cData[0] = 7;
  i2cData[1] = 0x00;
  i2cData[3] = 0x00;

  while(i2cWrite(0x19, i2cData, 4, false));
  while(i2cWrite2(0x6B, 0x01, true));
  while(i2cRead(0x75, i2cData, 1));
  delay(100);
  while(i2cRead(0x3B,i2cData,6));
 
  timer = micros();
  Serial.begin(115200);
  bleMouse.begin();
  delay(100);
}

void loop() {
  while(i2cRead(0x3B,i2cData,14));
 
  gyroX = ((i2cData[8] << 8) | i2cData[9]);
  gyroZ = ((i2cData[12] << 8) | i2cData[13]);
 
  gyroX = gyroX / Sensitivity / 1.1  * -1;
  gyroZ = gyroZ / Sensitivity  * -1;

  if(bleMouse.isConnected()){
    Serial.print(gyroX);
    Serial.print("   ");
    Serial.print(gyroZ);
    Serial.print("\r\n");
    bleMouse.move(gyroZ, -gyroX);
  }
  delay(delayi);
}

 

필자는 기본 아두이노 IDE가 아닌 vscode에서 아두이노 코딩 및 컴파일이 가능한 PlatformIO를 사용했다. 때문에 필요한 라이브러리는 아래와 같다.

 

 

일반 아두이노 IDE 사용자의 경우 해당 라이브러리들을 검색해 설치하면 될 것이다. 추가로 softwareserial.h는 ESP32 전용이 존재하고 BLE Mouse 라이브러리는 아래 링크에서 다운로드할 수 있다.

 

https://github.com/T-vK/ESP32-BLE-Mouse

 

GitHub - T-vK/ESP32-BLE-Mouse: Bluetooth LE Mouse library for the ESP32 (Arduino IDE compatible)

Bluetooth LE Mouse library for the ESP32 (Arduino IDE compatible) - GitHub - T-vK/ESP32-BLE-Mouse: Bluetooth LE Mouse library for the ESP32 (Arduino IDE compatible)

github.com

 

참고로 ESP32에 코드를 업로드하는 순간에는 (컴파일 -> 업로드되는 순간) 보드에 boot라는 버튼을 누르고 있어야 업로드가 진행된다. (컴파일 중이나 업로드 중에는 누르지 않아도 된다.)

 

코드가 업로드되면 아래와 같이 "ESP32 Bluetooth Mouse"라는 이름으로 블루투스 장치가 검색된다. 연결하면 바로 마우스로 사용이 가능하다.

 

아래는 작동 영상이다.

 

아직 키보드 및 마우스 클릭 구현은 안되어있는데 아래 키보드, 마우스 콤보 라이브러리를 사용해볼 계획이다.

https://github.com/ServAlex/ESP32-BLE-Combo

 

GitHub - ServAlex/ESP32-BLE-Combo: Bluetooth LE Keyboard/Mouse Combo library for the ESP32 (Arduino IDE compatible)

Bluetooth LE Keyboard/Mouse Combo library for the ESP32 (Arduino IDE compatible) - GitHub - ServAlex/ESP32-BLE-Combo: Bluetooth LE Keyboard/Mouse Combo library for the ESP32 (Arduino IDE compatible)

github.com

 

반응형
    # 테스트용