usb.c 861 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <string.h>
  2. #include "py/runtime.h"
  3. #include "Arduino.h"
  4. #include "usb.h"
  5. #include "usb_serial.h"
  6. bool usb_vcp_is_connected(void)
  7. {
  8. return usb_configuration && (usb_cdc_line_rtsdtr & (USB_SERIAL_DTR | USB_SERIAL_RTS));
  9. }
  10. bool usb_vcp_is_enabled(void)
  11. {
  12. return true;
  13. }
  14. int usb_vcp_rx_num(void) {
  15. return usb_serial_available();
  16. }
  17. int usb_vcp_recv_byte(uint8_t *ptr)
  18. {
  19. int ch = usb_serial_getchar();
  20. if (ch < 0) {
  21. return 0;
  22. }
  23. *ptr = ch;
  24. return 1;
  25. }
  26. void usb_vcp_send_str(const char* str)
  27. {
  28. usb_vcp_send_strn(str, strlen(str));
  29. }
  30. void usb_vcp_send_strn(const char* str, int len)
  31. {
  32. usb_serial_write(str, len);
  33. }
  34. void usb_vcp_send_strn_cooked(const char *str, int len)
  35. {
  36. for (const char *top = str + len; str < top; str++) {
  37. if (*str == '\n') {
  38. usb_serial_putchar('\r');
  39. }
  40. usb_serial_putchar(*str);
  41. }
  42. }