|
| 1 | +// Simple example showing multiple devices, one at a time, over USB. |
| 2 | +// Released to the public domain 2025 by Earle F. Philhower, III |
| 3 | + |
| 4 | +#include <USB.h> |
| 5 | +#include <Keyboard.h> |
| 6 | +#include <Mouse.h> |
| 7 | + |
| 8 | +String readline() { |
| 9 | + String s; |
| 10 | + while (true) { |
| 11 | + int c = Serial.read(); |
| 12 | + if ((c == '\r') || (c == '\n')) { |
| 13 | + return s; |
| 14 | + } |
| 15 | + if (c >= 1) { |
| 16 | + s += (char)c; |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +void setup() { |
| 22 | + Serial.begin(); |
| 23 | + pinMode(LED_BUILTIN, OUTPUT); |
| 24 | + digitalWrite(LED_BUILTIN, LOW); |
| 25 | + delay(5000); |
| 26 | + Serial.println("USB morphing device demo"); |
| 27 | + Serial.println("Examine the `lsusb -v` or Windows Device Manager to see the changing USB devices"); |
| 28 | + Serial.println("Wait for the LED on to press BOOTSEL in these demos.\r\n\r\n"); |
| 29 | + Serial.println("First we'll become a keyboard."); |
| 30 | + Serial.println("What should we type when you hit BOOTSEL?"); |
| 31 | + String toType = readline(); |
| 32 | + Serial.println("OK, I'll disconnect from the USB and reappear as a keyboard."); |
| 33 | + Serial.println("Press BOOTSEL to type your message and disconnect the keyboard and reappear."); |
| 34 | + // Ensure the messages get sent then disconnect and turn into a keyboard |
| 35 | + Serial.flush(); |
| 36 | + delay(5000); |
| 37 | + |
| 38 | + Serial.end(); |
| 39 | + Keyboard.begin(); |
| 40 | + delay(3000); // Give the computer time to recognize the change |
| 41 | + digitalWrite(LED_BUILTIN, HIGH); |
| 42 | + while (!BOOTSEL) { |
| 43 | + delay(10); |
| 44 | + } |
| 45 | + Keyboard.print(toType); |
| 46 | + delay(100); // Ensure PC has eaten all keys |
| 47 | + while (BOOTSEL) { |
| 48 | + delay(10); |
| 49 | + } |
| 50 | + digitalWrite(LED_BUILTIN, LOW); |
| 51 | + |
| 52 | + Keyboard.end(); |
| 53 | + Serial.begin(115200); |
| 54 | + delay(5000); // Give PC time to reconnect |
| 55 | + Serial.println("Now I'll become a mouse and move down diagonally when you press BOOTSEL..."); |
| 56 | + |
| 57 | + // Ensure the messages get sent then disconnect and turn into a keyboard |
| 58 | + Serial.flush(); |
| 59 | + delay(5000); |
| 60 | + |
| 61 | + Serial.end(); |
| 62 | + Mouse.begin(); |
| 63 | + delay(3000); // Give the computer time to recognize the change |
| 64 | + digitalWrite(LED_BUILTIN, HIGH); |
| 65 | + while (!BOOTSEL) { |
| 66 | + delay(10); |
| 67 | + } |
| 68 | + for (int i = 0; i < 30; i++) { |
| 69 | + Mouse.move(3, 3, 0); |
| 70 | + delay(10); |
| 71 | + } |
| 72 | + delay(100); |
| 73 | + while (BOOTSEL) { |
| 74 | + delay(10); |
| 75 | + } |
| 76 | + digitalWrite(LED_BUILTIN, HIGH); |
| 77 | + |
| 78 | + Mouse.end(); |
| 79 | + Serial.begin(115200); |
| 80 | + delay(5000); // Again. give PC time to set device back up |
| 81 | + Serial.println("Now I'm back to just a serial port. The end."); |
| 82 | +} |
| 83 | + |
| 84 | +void loop() { |
| 85 | +} |
0 commit comments