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