Skip to main content

Seeed Studio XIAO SAMD21 作为 USB 设备(TinyUSB)

本教程介绍如何借助 TinyUSB 库将 Seeed Studio XIAO SAMD21 用作 USB 客户端。它允许 Seeed Studio XIAO SAMD21 用于 HID 设备,如键盘、鼠标等。

此功能依赖于 Adafruit TinyUSB Library for Arduino。该库已在 Seeed Studio XIAO SAMD21Wio Terminal(SAMD51) 上测试并正常工作。

安装 Adafruit TinyUSB Library for Arduino

note

由于"Adafruit TinyUSB Library for Arduino"库的重大更新,V1.0.0 及以上版本无法与 Seeed Studio XIAO SAMD21 一起使用,如果您需要使用此库,请使用 V0.10.5 版本的库。

  1. 访问 Adafruit TinyUSB Library for Arduino 仓库并将整个仓库下载到您的本地驱动器。

  2. 现在,可以将库安装到 Arduino IDE 中。打开 Arduino IDE,点击 sketch -> Include Library -> Add .ZIP Library,然后选择您刚刚下载的 Adafruit_TinyUSB_Arduino 文件。

pir

简单示例代码

TinyUSB 库提供了许多示例,这里我们可以导航到 Files -> Examples -> Adafruit TinyUSB Library -> HID -> hid_mouse 查看一个简单的鼠标示例。将一个按钮连接到 Seeed Studio XIAO SAMD21 的 D0 引脚,并在代码中进行配置(#28),如下所示:

note

确保您已将 ArduinoCore-samd 更新到最新版本以避免编译错误。

/*********************************************************************
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

MIT license, check LICENSE for more information
Copyright (c) 2019 Ha Thach for Adafruit Industries
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/

#include "Adafruit_TinyUSB.h"

/* This sketch demonstrates USB HID mouse
* Press button pin will move
* - mouse toward bottom right of monitor
*
* Depending on the board, the button pin
* and its active state (when pressed) are different
*/
#if defined ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS
const int pin = 4; // Left Button
bool activeState = true;
#elif defined ARDUINO_NRF52840_FEATHER
const int pin = 7; // UserSw
bool activeState = false;
#else
const int pin = 0;
bool activeState = true;
#endif


// HID report descriptor using TinyUSB's template
// Single Report (no ID) descriptor
uint8_t const desc_hid_report[] =
{
TUD_HID_REPORT_DESC_MOUSE()
};

// USB HID object
Adafruit_USBD_HID usb_hid;

// the setup function runs once when you press reset or power the board
void setup()
{
// Set up button, pullup opposite to active state
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);

usb_hid.setPollInterval(2);
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));

usb_hid.begin();

Serial.begin(115200);

// wait until device mounted
while( !USBDevice.mounted() ) delay(1);

Serial.println("Adafruit TinyUSB HID Mouse example");
}

void loop()
{
// poll gpio once each 10 ms
delay(10);

// Whether button is pressed
bool btn_pressed = (digitalRead(pin) == activeState);

// nothing to do if button is not pressed
if (!btn_pressed) return;

// Remote wakeup
if ( USBDevice.suspended() )
{
// Wake up host if we are in suspend mode
// and REMOTE_WAKEUP feature is enabled by host
USBDevice.remoteWakeup();
}

if ( usb_hid.ready() )
{
int8_t const delta = 5;
usb_hid.mouseMove(0, delta, delta); // no ID: right + down
}
}

技术支持与产品讨论

感谢您选择我们的产品!我们在这里为您提供不同的支持,以确保您使用我们产品的体验尽可能顺畅。我们提供多种沟通渠道,以满足不同的偏好和需求。

Loading Comments...