genericdevice_uarttest.ino 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Abstracted transport for reading and writing data from a UART-based
  3. device such as a TMC2209
  4. Written with help by Claude!
  5. https://claude.ai/chat/335f50b1-3dd8-435e-9139-57ec7ca26a3c (at this time
  6. chats are not shareable :(
  7. */
  8. #include "Adafruit_GenericDevice.h"
  9. /**
  10. * Basic UART device class that demonstrates using GenericDevice with a Stream
  11. * interface. This example shows how to wrap a Stream (like HardwareSerial or
  12. * SoftwareSerial) with read/write callbacks that can be used by BusIO's
  13. * register functions.
  14. */
  15. class UARTDevice {
  16. public:
  17. UARTDevice(Stream *serial) : _serial(serial) {}
  18. // Static callback for writing data to UART
  19. // Called by GenericDevice when data needs to be sent
  20. static bool uart_write(void *thiz, const uint8_t *buffer, size_t len) {
  21. UARTDevice *dev = (UARTDevice *)thiz;
  22. dev->_serial->write(buffer, len);
  23. return true;
  24. }
  25. // Static callback for reading data from UART
  26. // Includes timeout and will return false if not enough data available
  27. static bool uart_read(void *thiz, uint8_t *buffer, size_t len) {
  28. UARTDevice *dev = (UARTDevice *)thiz;
  29. uint16_t timeout = 100;
  30. while (dev->_serial->available() < len && timeout--) {
  31. delay(1);
  32. }
  33. if (timeout == 0) {
  34. return false;
  35. }
  36. for (size_t i = 0; i < len; i++) {
  37. buffer[i] = dev->_serial->read();
  38. }
  39. return true;
  40. }
  41. // Create a GenericDevice instance using our callbacks
  42. Adafruit_GenericDevice *createDevice() {
  43. return new Adafruit_GenericDevice(this, uart_read, uart_write);
  44. }
  45. private:
  46. Stream *_serial; // Underlying Stream instance (HardwareSerial, etc)
  47. };
  48. void setup() {
  49. Serial.begin(115200);
  50. while (!Serial)
  51. ;
  52. delay(100);
  53. Serial.println("Generic Device test!");
  54. // Initialize UART for device communication
  55. Serial1.begin(115200);
  56. // Create UART wrapper and BusIO device
  57. UARTDevice uart(&Serial1);
  58. Adafruit_GenericDevice *device = uart.createDevice();
  59. device->begin();
  60. // Test write/read cycle
  61. uint8_t write_buf[4] = {0x5, 0x0, 0x0, 0x48};
  62. uint8_t read_buf[8];
  63. Serial.println("Writing data...");
  64. if (!device->write(write_buf, 4)) {
  65. Serial.println("Write failed!");
  66. return;
  67. }
  68. Serial.println("Reading response...");
  69. if (!device->read(read_buf, 8)) {
  70. Serial.println("Read failed!");
  71. return;
  72. }
  73. // Print response bytes
  74. Serial.print("Got response: ");
  75. for (int i = 0; i < 8; i++) {
  76. Serial.print("0x");
  77. Serial.print(read_buf[i], HEX);
  78. Serial.print(" ");
  79. }
  80. Serial.println();
  81. }
  82. void loop() { delay(1000); }