Skip to content

Commit

Permalink
most example works with ch32: hid,msc,midi though mouse_ramdisk does …
Browse files Browse the repository at this point in the history
…not.
  • Loading branch information
hathach committed Jun 7, 2024
1 parent 2e8f1b5 commit 2179aeb
Show file tree
Hide file tree
Showing 19 changed files with 487 additions and 633 deletions.
21 changes: 17 additions & 4 deletions examples/CDC/no_serial/no_serial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ int led = LED_BUILTIN;

void setup()
{
// Manual begin() is required on core without built-in support e.g. mbed rp2040
if (!TinyUSBDevice.isInitialized()) {
TinyUSBDevice.begin(0);
}

// clear configuration will remove all USB interfaces including CDC (Serial)
TinyUSBDevice.clearConfiguration();

Expand All @@ -31,8 +36,16 @@ void setup()

void loop()
{
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
#ifdef TINYUSB_NEED_POLLING_TASK
// Manual call tud_task since it isn't called by Core's background
TinyUSBDevice.task();
#endif

// toggle LED
static uint32_t ms = 0;
static uint8_t led_state = 0;
if (millis() - ms > 1000) {
ms = millis();
digitalWrite(LED_BUILTIN, 1-led_state);
}
}
Empty file.
Empty file.
200 changes: 0 additions & 200 deletions examples/Composite/mouse_external_flash/mouse_external_flash.ino

This file was deleted.

86 changes: 49 additions & 37 deletions examples/Composite/mouse_ramdisk/mouse_ramdisk.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
// 8KB is the smallest size that windows allow to mount
#define DISK_BLOCK_NUM 16
#define DISK_BLOCK_SIZE 512

#include "ramdisk.h"

Adafruit_USBD_MSC usb_msc;
Expand All @@ -41,37 +42,41 @@ uint8_t const desc_hid_report[] = {
Adafruit_USBD_HID usb_hid;

#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY)
const int pin = 4; // Left Button
bool activeState = true;
const int pin = 4; // Left Button
bool activeState = true;

#elif defined(ARDUINO_FUNHOUSE_ESP32S2)
const int pin = BUTTON_DOWN;
bool activeState = true;
const int pin = BUTTON_DOWN;
bool activeState = true;

#elif defined PIN_BUTTON1
const int pin = PIN_BUTTON1;
bool activeState = false;
const int pin = PIN_BUTTON1;
bool activeState = false;

#elif defined(ARDUINO_ARCH_ESP32)
const int pin = 0;
bool activeState = false;
const int pin = 0;
bool activeState = false;

#elif defined(ARDUINO_ARCH_RP2040)
const int pin = D0;
bool activeState = false;

#else
const int pin = 12;
bool activeState = false;
const int pin = A0;
bool activeState = false;
#endif


// the setup function runs once when you press reset or power the board
void setup()
{
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
// Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
TinyUSB_Device_Init(0);
#endif
void setup() {
// Manual begin() is required on core without built-in support e.g. mbed rp2040
if (!TinyUSBDevice.isInitialized()) {
TinyUSBDevice.begin(0);
}

// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
usb_msc.setID("Adafruit", "Mass Storage", "1.0");

// Set disk size
usb_msc.setCapacity(DISK_BLOCK_NUM, DISK_BLOCK_SIZE);

Expand All @@ -80,7 +85,7 @@ void setup()

// Set Lun ready (RAM disk is always ready)
usb_msc.setUnitReady(true);

usb_msc.begin();

// Set up button
Expand All @@ -93,32 +98,23 @@ void setup()
usb_hid.begin();

Serial.begin(115200);
while( !TinyUSBDevice.mounted() ) delay(1); // wait for native usb

Serial.println("Adafruit TinyUSB Mouse + Mass Storage (ramdisk) example");
}

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

void process_hid() {
// button is active low
uint32_t const btn = (digitalRead(pin) == activeState);

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

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

Expand All @@ -128,11 +124,29 @@ void loop()
}
}

void loop() {
#ifdef TINYUSB_NEED_POLLING_TASK
// Manual call tud_task since it isn't called by Core's background
TinyUSBDevice.task();
#endif

// not enumerated()/mounted() yet: nothing to do
if (!TinyUSBDevice.mounted()) {
return;
}

// poll gpio once each 10 ms
static uint32_t ms = 0;
if (millis() - ms > 10) {
ms = millis();
process_hid();
}
}

// Callback invoked when received READ10 command.
// Copy disk's data to buffer (up to bufsize) and
// return number of copied bytes (must be multiple of block size)
int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
{
int32_t msc_read_cb(uint32_t lba, void* buffer, uint32_t bufsize) {
uint8_t const* addr = msc_disk[lba];
memcpy(buffer, addr, bufsize);

Expand All @@ -142,8 +156,7 @@ int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
// Callback invoked when received WRITE10 command.
// Process data in buffer to disk's storage and
// return number of written bytes (must be multiple of block size)
int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
{
int32_t msc_write_cb(uint32_t lba, uint8_t* buffer, uint32_t bufsize) {
uint8_t* addr = msc_disk[lba];
memcpy(addr, buffer, bufsize);

Expand All @@ -152,7 +165,6 @@ int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)

// Callback invoked when WRITE10 command is completed (status received and accepted by host).
// used to flush any pending cache.
void msc_flush_cb (void)
{
void msc_flush_cb(void) {
// nothing to do
}
Loading

0 comments on commit 2179aeb

Please sign in to comment.