m5stack / M5Stack

M5Stack Arduino Library
MIT License
1.19k stars 425 forks source link

CoreS3 Conflict with USB / USBHidKeyboard Libraries #318

Closed richstokes closed 2 weeks ago

richstokes commented 7 months ago

Describe the bug

Trying to have the M5Stack CoreS3 act as a USB keyboard, while using other M5 features, such as the LCD display, results in a conflict when compiling.

To reproduce

This works fine, and will have the M5CoreS3 act as a USB keyboard, sending the 'c' key every 4 seconds:

#include "USB.h"
#include "USBHIDKeyboard.h"
USBHIDKeyboard Keyboard;

void setup() {
  // Remember to set USB mode (not upload mode) to USB-OTG under tools menu
  Keyboard.begin();
  USB.begin();
}

void loop() {
  Keyboard.print("c");
  delay(4000);
}

However attempting to use the M5CoreS3 library at the same time results in compilation errors, e.g:

#include "USB.h"
#include "USBHIDKeyboard.h"
#include <M5CoreS3.h>
USBHIDKeyboard Keyboard;

void setup() {
  // Remember to set USB mode (not upload mode) to USB-OTG under tools menu
  Keyboard.begin();
  USB.begin();
  M5.begin(); // Init M5CoreS3
  M5.Lcd.fillScreen(GREEN);
  delay(500);
}

void loop() {
  Keyboard.print("c");
  delay(4000);
}

Error:

/Users/rich/Documents/Arduino/libraries/M5CoreS3/src/utility/I2C_IMU.cpp: In constructor 'I2C_IMU::I2C_IMU()':
/Users/rich/Documents/Arduino/libraries/M5CoreS3/src/utility/I2C_IMU.cpp:11:5: error: 'USBSerial' was not declared in this scope
     USBSerial.begin(115200);
     ^~~~~~~~~
/Users/rich/Documents/Arduino/libraries/M5CoreS3/src/utility/I2C_IMU.cpp:11:5: note: suggested alternative: 'Serial'
     USBSerial.begin(115200);
     ^~~~~~~~~
     Serial
/Users/rich/Documents/Arduino/libraries/M5CoreS3/src/utility/I2C_IMU.cpp: In member function 'void I2C_IMU::Init()':
/Users/rich/Documents/Arduino/libraries/M5CoreS3/src/utility/I2C_IMU.cpp:99:5: error: 'USBSerial' was not declared in this scope
     USBSerial.printf("Valid BMM150 (Aux) sensor - Chip ID : 0x%x\r\n",
     ^~~~~~~~~
/Users/rich/Documents/Arduino/libraries/M5CoreS3/src/utility/I2C_IMU.cpp:99:5: note: suggested alternative: 'Serial'
     USBSerial.printf("Valid BMM150 (Aux) sensor - Chip ID : 0x%x\r\n",
     ^~~~~~~~~
     Serial
/Users/rich/Documents/Arduino/libraries/M5CoreS3/src/M5CoreS3.cpp: In member function 'void M5CoreS3::begin(bool, bool, bool)':
/Users/rich/Documents/Arduino/libraries/M5CoreS3/src/M5CoreS3.cpp:19:9: error: 'USBSerial' was not declared in this scope
         USBSerial.begin(115200);
         ^~~~~~~~~
/Users/rich/Documents/Arduino/libraries/M5CoreS3/src/M5CoreS3.cpp:19:9: note: suggested alternative: 'Serial'
         USBSerial.begin(115200);
         ^~~~~~~~~
         Serial
/Users/rich/Documents/Arduino/libraries/M5CoreS3/src/M5CoreS3.cpp:25:5: error: 'USBSerial' was not declared in this scope
     USBSerial.printf("APX initial:%d\n", Axp.begin(&Wire1));
     ^~~~~~~~~
/Users/rich/Documents/Arduino/libraries/M5CoreS3/src/M5CoreS3.cpp:25:5: note: suggested alternative: 'Serial'
     USBSerial.printf("APX initial:%d\n", Axp.begin(&Wire1));
     ^~~~~~~~~
     Serial
Multiple libraries were found for "SD.h"
  Used: /Users/rich/Library/Arduino15/packages/m5stack/hardware/esp32/2.0.8/libraries/SD
  Not used: /Users/rich/Library/Arduino15/libraries/SD
exit status 1

Compilation error: exit status 1

Expected behavior

We should be able to have the device act as a USB HID/Keyboard, while being able to use other features of the stack. My goal is to have a device that receives data via wifi and then sends keyboard commands via USB.

Screenshots

No response

Environment

Additional context

No response

Issue checklist

richstokes commented 7 months ago

I think this might be the fix

https://github.com/m5stack/M5CoreS3/issues/50#issuecomment-1812514789