【搬运】关于蓝牙手柄与M5StickC的连接-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2326|回复: 0

【搬运】关于蓝牙手柄与M5StickC的连接

[复制链接]
发表于 2019-11-7 20:26 | 显示全部楼层 |阅读模式
     项目地址:https://github.com/bitbank2/esp32_gamepad/blob/master/examples/rfcomm_test/     截至目前有许多兼容M5StickC的底座产品出现,如果想充分利用它们意味着你需要多一台M5StickC配合摇杆来作为遥控器进行控制。当然为了简化使用难度,官方都做了UIFlow的相关适配,可以快速的进行程序编写。如果你本来有一个蓝牙手柄又不想再额外购入一台设备,那么接下来的示例也许可以好好参考一下,常见的蓝牙模式分为经典SPP蓝牙和BLE蓝牙,作者使用SPP蓝牙进行通讯减少了UUID和服务特征的注册,减少了繁琐的注册流程,降低代码的复杂程度。注:以下代码中包含<bb_spi_lcd.h>来控制屏幕,支持M5StickC和T-DISPLAY,该库可通过Arduino管理器安装。此外作者在setup阶段用了不常用的goto的语法来判断条件扫描设备,虽然在很多读物中并不提倡此做法,但是有时候仍是一个解决问题的好办法。esp32_gamepad.h可以在上述地址中下载

[mw_shl_code=arduino,true]#include <esp32_gamepad.h>
#include <bb_spi_lcd.h>
#ifdef ARDUINO_M5Stick_C
#include <M5StickC.h>
#endif

// Uncomment this line if compiling for the TTGO T-Display
//#define TTGO_T_DISPLAY

#ifdef TTGO_T_DISPLAY
int GetButtons(void)
{
static int iOldState;
int iState, iPressed;

  iState = 0;
  if (digitalRead(0) == LOW)
    iState |= 1;
  if (digitalRead(35) == LOW)
    iState |= 2;
      
  // Test button bits for which ones changed from LOW to HIGH
  iPressed = (iState ^ iOldState) & iState; // tells us which ones just changed to 1
  iOldState = iState;
  iPressed = iPressed | (iState << 8); // prepare combined state
  return iPressed;
} /* GetButtons() */
#endif

#ifdef ARDUINO_M5Stick_C
int GetButtons(void)
{
static int iOldState;
int iState, iPressed;

  iState = 0;
  if (digitalRead(39) == LOW)
    iState |= 1;
  if (digitalRead(37) == LOW)
    iState |= 2;
      
  // Test button bits for which ones changed from LOW to HIGH
  iPressed = (iState ^ iOldState) & iState; // tells us which ones just changed to 1
  iOldState = iState;
  iPressed = iPressed | (iState << 8); // prepare combined state
  return iPressed;
} /* GetButtons() */
#endif

// Mike Rankin's Color Coincell V2
#if !defined ( ARDUINO_M5Stick_C ) && !defined( TTGO_T_DISPLAY )
// Touch Pad1 = GPIO27, T7
// Touch Pad2 = GPIO12, T5
// Touch Pad3 = GPIO15, T3
// Touch Pad4 = GPIO2 , T2
#define BUTTON_THRESHOLD 98
uint8_t iButtons[4] = {T7,T5,T3,T2};
int GetButtons(void)
{
static int iOldState;
int iState, iPressed;
int i, j;
int iCounts[4] = {0};

  iState = 0;
  // remove the 'flicker' of the buttons
  for (j=0; j<5; j++)
  {
    for (i=0; i<4; i++)
    {
      if (touchRead(iButtons) < BUTTON_THRESHOLD)
        iCounts++;
    }
    delay(4);
  }
  for (i=0; i<4; i++)
    if (iCounts == 5)
       iState |= (1<<i);
      
    // Test button bits for which ones changed from LOW to HIGH
    iPressed = (iState ^ iOldState) & iState; // tells us which ones just changed to 1
    iOldState = iState;
    iPressed = iPressed | (iState << 8); // prepare combined state
    return iPressed;
} /* GetButtons() */
#endif // !TTGO_T_DISPLAY && !M5StickC

SS_GAMEPAD gp;
uint8_t address[6];
volatile bool bChanged;

void SS_Callback(int iEvent, SS_GAMEPAD *pGamepad)
{
  bChanged = 1;
  memcpy(&gp, pGamepad, sizeof(SS_GAMEPAD));
} /* SS_Callback() */

void setup() {
int i;
uint8_t devAddr[6];
int bDone = 0;

#ifdef TTGO_T_DISPLAY
  spilcdInit(LCD_ST7789_135, 0, 0, 0, 32000000, 5, 16, -1, 4, -1, 19, 18); // TTGO T-Display pin numbering, 40Mhz
  pinMode(0, INPUT_PULLUP);
  pinMode(35, INPUT_PULLUP);
#endif
#ifdef ARDUINO_M5Stick_C
  M5.begin();
  M5.Axp.ScreenBreath(11); // turn on backlight
  spilcdInit(LCD_ST7735S, 0, 1, 0, 40000000, 5, 23, 18, -1, 19, 15, 13); // M5StickC pin numbering
#endif
// Mike Rankin's Color coincell board v2
#if !defined(ARDUINO_M5Stick_C) && !defined(TTGO_T_DISPLAY)
  spilcdInit(LCD_ST7735S_B, 1, 1, 0, 32000000, 4, 21, 22, 26, -1, 23, 18); // Mike Rankin's color coin cell pin numbering
#endif
   spilcdSetOrientation(LCD_ORIENTATION_ROTATED);

  SS_Init();
  SS_RegisterCallback(SS_Callback);
try_again:
  spilcdFill(0,1);
  spilcdWriteString(0,0,(char *)"Starting discovery...", 0xffff, 0, FONT_NORMAL, 1);
  if (SS_Scan(6, devAddr))
  {   
    spilcdWriteString(0,16,(char *)"SS found!", 0x6e0, 0, FONT_STRETCHED, 1);
    if (SS_Connect(devAddr))
    {
      spilcdWriteString(0,32,(char *)"Connected!", 0x6e0, 0, FONT_STRETCHED, 1);
      bDone = 1;
      delay(1000);
    }
    else
      spilcdWriteString(0,32,(char *)"Not Connected", 0xf800, 0, FONT_STRETCHED, 1);
  }
  else
  {
    spilcdWriteString(0,16,(char *)"Not found", 0xf800, 0, FONT_STRETCHED, 1);
  }
  if (!bDone)
  {
    spilcdWriteString(0,64,(char *)"press any key to continue", 0x7ff, 0, FONT_SMALL, 1);
    while (GetButtons() == 0)
    {
     
    }
    goto try_again;
  }
} // setup()

void loop() {
char szTemp[32];

  spilcdFill(0,1);
  while(1)
  {
    sprintf(szTemp,"B 0x%04x", gp.u16Buttons);
    spilcdWriteString(0,0,szTemp, 0xffff,0,FONT_STRETCHED, 1);
    sprintf(szTemp,"L%04d,%04d", gp.iLJoyX,gp.iLJoyY);
    spilcdWriteString(0,24,szTemp, 0xf81f,0,FONT_STRETCHED, 1);
    sprintf(szTemp,"R%04d,%04d", gp.iRJoyX,gp.iRJoyY);
    spilcdWriteString(0,48,szTemp, 0x6e0,0,FONT_STRETCHED, 1);
  }
} /* loop() */[/mw_shl_code]





您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-29 01:18 , Processed in 0.098784 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表