summaryrefslogtreecommitdiff
path: root/protocol/usb_hid/USB_Host_Shield_2.0/examples/adk/demokit_20/demokit_20.ino
blob: f65adf57bb77f5683692ba4090ea7cdfddf90d67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <adk.h>
#include <usbhub.h>

// Satisfy IDE, which only needs to see the include statment in the ino.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif

USB Usb;
USBHub hub0(&Usb);
USBHub hub1(&Usb);
ADK adk(&Usb,"Google, Inc.",
            "DemoKit",
            "DemoKit Arduino Board",
            "1.0",
            "http://www.android.com",
            "0000000012345678");
uint8_t  b, b1;


#define  LED1_RED       3
#define  BUTTON1        2

void init_buttons()
{
	pinMode(BUTTON1, INPUT);

	// enable the internal pullups
	digitalWrite(BUTTON1, HIGH);
}

void init_leds()
{
	digitalWrite(LED1_RED, 0);

	pinMode(LED1_RED, OUTPUT);
}

void setup()
{
	Serial.begin(115200);
#if !defined(__MIPSEL__)
  while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
	Serial.println("\r\nADK demo start");

        if (Usb.Init() == -1) {
          Serial.println("OSCOKIRQ failed to assert");
        while(1); //halt
        }//if (Usb.Init() == -1...


	init_leds();
	init_buttons();
	b1 = digitalRead(BUTTON1);
}

void loop()
{
  uint8_t rcode;
  uint8_t msg[3] = { 0x00 };
   Usb.Task();

   if( adk.isReady() == false ) {
     analogWrite(LED1_RED, 255);
     return;
   }
   uint16_t len = sizeof(msg);

   rcode = adk.RcvData(&len, msg);
   if( rcode ) {
     USBTRACE2("Data rcv. :", rcode );
   }
   if(len > 0) {
     USBTRACE("\r\nData Packet.");
    // assumes only one command per packet
    if (msg[0] == 0x2) {
      switch( msg[1] ) {
        case 0:
          analogWrite(LED1_RED, 255 - msg[2]);
          break;
      }//switch( msg[1]...
    }//if (msg[0] == 0x2...
   }//if( len > 0...

   msg[0] = 0x1;

   b = digitalRead(BUTTON1);
   if (b != b1) {
     USBTRACE("\r\nButton state changed");
     msg[1] = 0;
     msg[2] = b ? 0 : 1;
     rcode = adk.SndData( 3, msg );
     if( rcode ) {
       USBTRACE2("Button send: ", rcode );
     }
     b1 = b;
    }//if (b != b1...


      delay( 10 );
}