Teensy4.1 USBHOST Triple USB device connecting Pgm from TeensyForum_Mr.Kurlt_Thanks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simple test of USB Host Mouse/Keyboard | |
// | |
// This example is in the public domain | |
#include "USBHost_t36.h" | |
#define USBBAUD 115200 | |
uint32_t baud = USBBAUD; | |
uint32_t format = USBHOST_SERIAL_8N1; | |
USBHost myusb; | |
USBHub hub1(myusb); | |
USBHub hub2(myusb); | |
USBHIDParser hid1(myusb); | |
USBHIDParser hid2(myusb); | |
USBHIDParser hid3(myusb); | |
USBSerial userial(myusb); | |
USBSerial_BigBuffer userialb(myusb); | |
USBSerial_BigBuffer userialb2(myusb); | |
USBDriver *drivers[] = {&hub1, &hub2, &hid1, &hid2, &hid3, &userial, &userialb, &userialb2}; | |
#define CNT_DEVICES (sizeof(drivers)/sizeof(drivers[0])) | |
const char * driver_names[CNT_DEVICES] = {"Hub1", "Hub2", "HID1", "HID2", "HID3", "USERIAL1", "USERIALB", "USERIALB2" }; | |
bool driver_active[CNT_DEVICES] = {false, false, false, false}; | |
void setup() | |
{ | |
pinMode(13, OUTPUT); | |
pinMode(2, OUTPUT); | |
pinMode(3, OUTPUT); | |
for (int i = 0; i < 5; i++) { | |
digitalWrite(2, HIGH); | |
delayMicroseconds(50); | |
digitalWrite(2, LOW); | |
delayMicroseconds(50); | |
} | |
while (!Serial && (millis() < 5000)) ; // wait for Arduino Serial Monitor | |
Serial.println("\n\nUSB Host Testing - Serial"); | |
myusb.begin(); | |
Serial1.begin(115200); // We will echo stuff Through Serial1... | |
} | |
void loop() | |
{ | |
digitalWrite(13, !digitalRead(13)); | |
myusb.Task(); | |
updateDeviceList(); | |
if (Serial.available()) { | |
Serial.println("Serial Available"); | |
while (Serial.available()) { | |
int ch = Serial.read(); | |
if (ch == '#') { | |
// Lets see if we have a baud rate specified here... | |
uint32_t new_baud = 0; | |
for (;;) { | |
ch = Serial.read(); | |
if ((ch < '0') || (ch > '9')) | |
break; | |
new_baud = new_baud * 10 + ch - '0'; | |
} | |
// See if the user is specifying a format: 8n1, 7e1, 7e2, 8n2 | |
// Note this is Quick and very dirty code... | |
// | |
if (ch == ',') { | |
char command_line[10]; | |
ch = Serial.read(); | |
while (ch == ' ') Serial.read(); // ignore any spaces. | |
uint8_t cb = 0; | |
while ((ch > ' ') && (cb < sizeof(command_line))) { | |
command_line[cb++] = ch; | |
ch = Serial.read(); | |
} | |
command_line[cb] = '\0'; | |
if (CompareStrings(command_line, "8N1")) format = USBHOST_SERIAL_8N1; | |
else if (CompareStrings(command_line, "8N2")) format = USBHOST_SERIAL_8N2; | |
else if (CompareStrings(command_line, "7E1")) format = USBHOST_SERIAL_7E1; | |
else if (CompareStrings(command_line, "7O1")) format = USBHOST_SERIAL_7O1; | |
} | |
Serial.println("\n*** Set new Baud command ***\n do userial.end()"); | |
digitalWriteFast(2, HIGH); | |
userial.end(); // Do the end statement; | |
digitalWriteFast(2, LOW); | |
if (new_baud) { | |
baud = new_baud; | |
Serial.print(" New Baud: "); | |
Serial.println(baud); | |
Serial.print(" Format: "); | |
Serial.println(format, HEX); | |
digitalWriteFast(3, HIGH); | |
userial.begin(baud, format); | |
digitalWriteFast(3, LOW); | |
Serial.println(" Completed "); | |
Serial1.end(); | |
Serial1.begin(baud, format); | |
} else if (ch == '$') { | |
while (Serial.read() != -1) ; | |
while (Serial.read() == -1) { | |
if (userial)userial.write("USerial"); | |
if (userialb)userialb.write("USerialB"); | |
if (userialb2)userialb2.write("UserialB2"); | |
delay(100); | |
} | |
} else { | |
Serial.println(" New Baud 0 - leave disabled"); | |
} | |
while (Serial.read() != -1); | |
} else { | |
if (userial)userial.write(ch); | |
if (userialb)userialb.write(ch); | |
if (userialb2)userialb2.write(ch); | |
} | |
} | |
} | |
while (Serial1.available()) { | |
// Serial.println("Serial1 Available"); | |
Serial1.write(Serial1.read()); | |
} | |
while (userial.available()) { | |
// Serial.println("USerial Available"); | |
Serial.write(userial.read()); | |
} | |
if (userialb.available()) { | |
Serial.print("SB:"); | |
while (userialb.available()) { | |
// Serial.println("USerialb Available"); | |
Serial.write(userialb.read()); | |
} | |
} | |
if (userialb2.available()) { | |
Serial.print("SB2:"); | |
while (userialb2.available()) { | |
// Serial.println("USerialb Available"); | |
Serial.write(userialb2.read()); | |
} | |
} | |
} | |
void updateDeviceList() { | |
// Print out information about different devices. | |
for (uint8_t i = 0; i < CNT_DEVICES; i++) { | |
if (*drivers[i] != driver_active[i]) { | |
if (driver_active[i]) { | |
Serial.printf("*** Device %s - disconnected ***\n", driver_names[i]); | |
driver_active[i] = false; | |
} else { | |
Serial.printf("*** Device %s %x:%x - connected ***\n", driver_names[i], drivers[i]->idVendor(), drivers[i]->idProduct()); | |
driver_active[i] = true; | |
const uint8_t *psz = drivers[i]->manufacturer(); | |
if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz); | |
psz = drivers[i]->product(); | |
if (psz && *psz) Serial.printf(" product: %s\n", psz); | |
psz = drivers[i]->serialNumber(); | |
if (psz && *psz) Serial.printf(" Serial: %s\n", psz); | |
// If this is a new Serial device. | |
if (drivers[i] == &userial) { | |
// Lets try first outputting something to our USerial to see if it will go out... | |
userial.begin(baud); | |
} | |
if (drivers[i] == &userialb) { | |
// Lets try first outputting something to our USerial to see if it will go out... | |
userialb.begin(baud); | |
} | |
if (drivers[i] == &userialb2) { | |
// Lets try first outputting something to our USerial to see if it will go out... | |
userialb2.begin(baud); | |
} | |
} | |
} | |
} | |
} | |
bool CompareStrings(const char *sz1, const char *sz2) { | |
while (*sz2 != 0) { | |
if (toupper(*sz1) != toupper(*sz2)) | |
return false; | |
sz1++; | |
sz2++; | |
} | |
return true; // end of string so show as match | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment