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.

103 lines
2.0 KiB

  1. #include <adk.h>
  2. #include <usbhub.h>
  3. // Satisfy IDE, which only needs to see the include statment in the ino.
  4. #ifdef dobogusinclude
  5. #include <spi4teensy3.h>
  6. #include <SPI.h>
  7. #endif
  8. USB Usb;
  9. USBHub hub0(&Usb);
  10. USBHub hub1(&Usb);
  11. ADK adk(&Usb,"Google, Inc.",
  12. "DemoKit",
  13. "DemoKit Arduino Board",
  14. "1.0",
  15. "http://www.android.com",
  16. "0000000012345678");
  17. uint8_t b, b1;
  18. #define LED1_RED 3
  19. #define BUTTON1 2
  20. void init_buttons()
  21. {
  22. pinMode(BUTTON1, INPUT);
  23. // enable the internal pullups
  24. digitalWrite(BUTTON1, HIGH);
  25. }
  26. void init_leds()
  27. {
  28. digitalWrite(LED1_RED, 0);
  29. pinMode(LED1_RED, OUTPUT);
  30. }
  31. void setup()
  32. {
  33. Serial.begin(115200);
  34. #if !defined(__MIPSEL__)
  35. while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  36. #endif
  37. Serial.println("\r\nADK demo start");
  38. if (Usb.Init() == -1) {
  39. Serial.println("OSCOKIRQ failed to assert");
  40. while(1); //halt
  41. }//if (Usb.Init() == -1...
  42. init_leds();
  43. init_buttons();
  44. b1 = digitalRead(BUTTON1);
  45. }
  46. void loop()
  47. {
  48. uint8_t rcode;
  49. uint8_t msg[3] = { 0x00 };
  50. Usb.Task();
  51. if( adk.isReady() == false ) {
  52. analogWrite(LED1_RED, 255);
  53. return;
  54. }
  55. uint16_t len = sizeof(msg);
  56. rcode = adk.RcvData(&len, msg);
  57. if( rcode ) {
  58. USBTRACE2("Data rcv. :", rcode );
  59. }
  60. if(len > 0) {
  61. USBTRACE("\r\nData Packet.");
  62. // assumes only one command per packet
  63. if (msg[0] == 0x2) {
  64. switch( msg[1] ) {
  65. case 0:
  66. analogWrite(LED1_RED, 255 - msg[2]);
  67. break;
  68. }//switch( msg[1]...
  69. }//if (msg[0] == 0x2...
  70. }//if( len > 0...
  71. msg[0] = 0x1;
  72. b = digitalRead(BUTTON1);
  73. if (b != b1) {
  74. USBTRACE("\r\nButton state changed");
  75. msg[1] = 0;
  76. msg[2] = b ? 0 : 1;
  77. rcode = adk.SndData( 3, msg );
  78. if( rcode ) {
  79. USBTRACE2("Button send: ", rcode );
  80. }
  81. b1 = b;
  82. }//if (b != b1...
  83. delay( 10 );
  84. }