查看: 167|回复: 0

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

[复制链接]
  • TA的每日心情
    无聊
    2019-8-12 13:52
  • 签到天数: 1 天

    [LV.1]初来乍到

    发表于 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可以在上述地址中下载

    kittenblock中小学创客名师推荐的图形化编程软件

    #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() */






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

    本版积分规则

    热门推荐

    Blynk与m5stack basic以及uiflow的交互玩法
    Blynk与m5stack basic以及
    Blynk与m5stack basic以及uiflow的交互玩法 [准备工作我们假设玩家已经熟悉下面的准备
    Arduino板接入后端口无法识别
    Arduino板接入后端口无法
    现在的IDE是1.8.10装好IDE后用数据线连接Arduino nano板, 板上红灯亮,但是电脑上并
    旋转编码器的使用
    旋转编码器的使用
    旋转编码器是一个精确旋转测定和点击合二为一的器件,用它来控制舵机比用电位器要精确
    【Arduino】108种传感器模块系列实验(90)--- ULN2003步进电机
    【Arduino】108种传感器模
    37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是
    【原创】全球最小口袋3D打印机mini one直播教程贴
    【原创】全球最小口袋3D打
    最近闲得蛋疼,没事搞个掌上3D打印机,先放效果图吧。 搞了半天,终于能正常打印,
    Copyright   ©2015-2016  Arduino中文社区  Powered by©Discuz!   
    快速回复 返回顶部 返回列表