]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/adk/ArduinoBlinkLED/ArduinoBlinkLED.ino
Merge commit 'f6d56675f9f981c5464f0ca7a1fbb0162154e8c5'
[qmk_firmware.git] / tmk_core / protocol / usb_hid / USB_Host_Shield_2.0 / examples / adk / ArduinoBlinkLED / ArduinoBlinkLED.ino
1 // The source for the Android application can be found at the following link: https://github.com/Lauszus/ArduinoBlinkLED
2 // The code for the Android application is heavily based on this guide: http://allaboutee.com/2011/12/31/arduino-adk-board-blink-an-led-with-your-phone-code-and-explanation/ by Miguel
3 #include <adk.h>
4
5 //
6 // CAUTION! WARNING! ATTENTION! VORSICHT! ADVARSEL! ¡CUIDADO! ВНИМАНИЕ!
7 //
8 // Pin 13 is occupied by the SCK pin on various Arduino boards,
9 // including Uno, Duemilanove, etc., so use a different pin for those boards.
10 //
11 // CAUTION! WARNING! ATTENTION! VORSICHT! ADVARSEL! ¡CUIDADO! ВНИМАНИЕ!
12 //
13 #if defined(LED_BUILTIN)
14 #define LED LED_BUILTIN // Use built in LED
15 #else
16 #define LED 9 // Set to something here that makes sense for your board.
17 #endif
18
19
20 // Satisfy IDE, which only needs to see the include statment in the ino.
21 #ifdef dobogusinclude
22 #include <spi4teensy3.h>
23 #include <SPI.h>
24 #endif
25
26 USB Usb;
27 ADK adk(&Usb, "TKJElectronics", // Manufacturer Name
28               "ArduinoBlinkLED", // Model Name
29               "Example sketch for the USB Host Shield", // Description (user-visible string)
30               "1.0", // Version
31               "http://www.tkjelectronics.dk/uploads/ArduinoBlinkLED.apk", // URL (web page to visit if no installed apps support the accessory)
32               "123456789"); // Serial Number (optional)
33
34 uint32_t timer;
35 bool connected;
36
37 void setup() {
38   Serial.begin(115200);
39 #if !defined(__MIPSEL__)
40   while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
41 #endif
42   if (Usb.Init() == -1) {
43     Serial.print("\r\nOSCOKIRQ failed to assert");
44     while (1); // halt
45   }
46   pinMode(LED, OUTPUT);
47   Serial.print("\r\nArduino Blink LED Started");
48 }
49
50 void loop() {
51   Usb.Task();
52
53   if (adk.isReady()) {
54     if (!connected) {
55       connected = true;
56       Serial.print(F("\r\nConnected to accessory"));
57     }
58
59     uint8_t msg[1];
60     uint16_t len = sizeof(msg);
61     uint8_t rcode = adk.RcvData(&len, msg);
62     if (rcode && rcode != hrNAK) {
63       Serial.print(F("\r\nData rcv: "));
64       Serial.print(rcode, HEX);
65     } else if (len > 0) {
66       Serial.print(F("\r\nData Packet: "));
67       Serial.print(msg[0]);
68       digitalWrite(LED, msg[0] ? HIGH : LOW);
69     }
70
71     if (millis() - timer >= 1000) { // Send data every 1s
72       timer = millis();
73       rcode = adk.SndData(sizeof(timer), (uint8_t*)&timer);
74       if (rcode && rcode != hrNAK) {
75         Serial.print(F("\r\nData send: "));
76         Serial.print(rcode, HEX);
77       } else if (rcode != hrNAK) {
78         Serial.print(F("\r\nTimer: "));
79         Serial.print(timer);
80       }
81     }
82   } else {
83     if (connected) {
84       connected = false;
85       Serial.print(F("\r\nDisconnected from accessory"));
86       digitalWrite(LED, LOW);
87     }
88   }
89 }