]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/pl2303/pl2303_xbee_terminal/pl2303_xbee_terminal.ino
lufa: usb-usb: Use LUFA startup instead of cusotom
[max/tmk_keyboard.git] / tmk_core / protocol / usb_hid / USB_Host_Shield_2.0 / examples / pl2303 / pl2303_xbee_terminal / pl2303_xbee_terminal.ino
1 /* Arduino terminal for PL2303 USB to serial converter and XBee radio. */
2 /* Inserts linefeed after carriage return in data sent to and received from Xbee */
3 /* USB support */
4 #include <usbhub.h>
5 /* CDC support */
6 #include <cdcacm.h>
7 #include <cdcprolific.h>
8
9 // Satisfy the IDE, which needs to see the include statment in the ino too.
10 #ifdef dobogusinclude
11 #include <spi4teensy3.h>
12 #include <SPI.h>
13 #endif
14
15 class PLAsyncOper : public CDCAsyncOper
16 {
17 public:
18     uint8_t OnInit(ACM *pacm);
19 };
20
21 uint8_t PLAsyncOper::OnInit(ACM *pacm)
22 {
23     uint8_t rcode;
24
25     // Set DTR = 1
26     rcode = pacm->SetControlLineState(1);
27
28     if (rcode)
29     {
30         ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode);
31         return rcode;
32     }
33
34     LINE_CODING lc;
35     lc.dwDTERate        = 115200;
36     lc.bCharFormat      = 0;
37     lc.bParityType      = 0;
38     lc.bDataBits        = 8;
39
40     rcode = pacm->SetLineCoding(&lc);
41
42     if (rcode)
43         ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode);
44
45     return rcode;
46 }
47 USB     Usb;
48 //USBHub     Hub(&Usb);
49 PLAsyncOper  AsyncOper;
50 PL2303       Pl(&Usb, &AsyncOper);
51
52 void setup()
53 {
54   Serial.begin( 115200 );
55 #if !defined(__MIPSEL__)
56   while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
57 #endif
58   Serial.println("Start");
59
60   if (Usb.Init() == -1)
61       Serial.println("OSCOKIRQ failed to assert");
62
63   delay( 200 );
64 }
65
66 void loop()
67 {
68     Usb.Task();
69
70     if( Usb.getUsbTaskState() == USB_STATE_RUNNING )
71     {
72        uint8_t rcode;
73
74        /* reading the keyboard */
75        if(Serial.available()) {
76          uint8_t data= Serial.read();
77
78          if ( data == '\r' ) {
79            Serial.print("\r\n");  //insert linefeed
80          }
81          else {
82            Serial.print( data );  //echo back to the screen
83          }
84
85          /* sending to the phone */
86          rcode = Pl.SndData(1, &data);
87          if (rcode)
88             ErrorMessage<uint8_t>(PSTR("SndData"), rcode);
89        }//if(Serial.available()...
90
91        delay(50);
92
93         /* reading the converter */
94         /* buffer size must be greater or equal to max.packet size */
95         /* it it set to 64 (largest possible max.packet size) here, can be tuned down
96         for particular endpoint */
97         uint8_t  buf[64];
98         uint16_t rcvd = 64;
99         rcode = Pl.RcvData(&rcvd, buf);
100          if (rcode && rcode != hrNAK)
101             ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
102
103             if( rcvd ) { //more than zero bytes received
104               for(uint16_t i=0; i < rcvd; i++ ) {
105                 if( buf[i] =='\r' ) {
106                   Serial.print("\r\n");  //insert linefeed
107                 }
108                 else {
109                   Serial.print((char)buf[i]); //printing on the screen
110                 }
111               }
112             }
113         delay(10);
114     }//if( Usb.getUsbTaskState() == USB_STATE_RUNNING..
115 }
116
117