You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.3 KiB

  1. #include <cdcacm.h>
  2. #include <usbhub.h>
  3. #include "pgmstrings.h"
  4. // Satisfy the IDE, which needs to see the include statment in the ino too.
  5. #ifdef dobogusinclude
  6. #include <spi4teensy3.h>
  7. #include <SPI.h>
  8. #endif
  9. class ACMAsyncOper : public CDCAsyncOper
  10. {
  11. public:
  12. uint8_t OnInit(ACM *pacm);
  13. };
  14. uint8_t ACMAsyncOper::OnInit(ACM *pacm)
  15. {
  16. uint8_t rcode;
  17. // Set DTR = 1 RTS=1
  18. rcode = pacm->SetControlLineState(3);
  19. if (rcode)
  20. {
  21. ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode);
  22. return rcode;
  23. }
  24. LINE_CODING lc;
  25. lc.dwDTERate = 115200;
  26. lc.bCharFormat = 0;
  27. lc.bParityType = 0;
  28. lc.bDataBits = 8;
  29. rcode = pacm->SetLineCoding(&lc);
  30. if (rcode)
  31. ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode);
  32. return rcode;
  33. }
  34. USB Usb;
  35. //USBHub Hub(&Usb);
  36. ACMAsyncOper AsyncOper;
  37. ACM Acm(&Usb, &AsyncOper);
  38. void setup()
  39. {
  40. Serial.begin( 115200 );
  41. #if !defined(__MIPSEL__)
  42. while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  43. #endif
  44. Serial.println("Start");
  45. if (Usb.Init() == -1)
  46. Serial.println("OSCOKIRQ failed to assert");
  47. delay( 200 );
  48. }
  49. void loop()
  50. {
  51. Usb.Task();
  52. if( Acm.isReady()) {
  53. uint8_t rcode;
  54. /* reading the keyboard */
  55. if(Serial.available()) {
  56. uint8_t data= Serial.read();
  57. /* sending to the phone */
  58. rcode = Acm.SndData(1, &data);
  59. if (rcode)
  60. ErrorMessage<uint8_t>(PSTR("SndData"), rcode);
  61. }//if(Serial.available()...
  62. delay(50);
  63. /* reading the phone */
  64. /* buffer size must be greater or equal to max.packet size */
  65. /* it it set to 64 (largest possible max.packet size) here, can be tuned down
  66. for particular endpoint */
  67. uint8_t buf[64];
  68. uint16_t rcvd = 64;
  69. rcode = Acm.RcvData(&rcvd, buf);
  70. if (rcode && rcode != hrNAK)
  71. ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
  72. if( rcvd ) { //more than zero bytes received
  73. for(uint16_t i=0; i < rcvd; i++ ) {
  74. Serial.print((char)buf[i]); //printing on the screen
  75. }
  76. }
  77. delay(10);
  78. }//if( Usb.getUsbTaskState() == USB_STATE_RUNNING..
  79. }