2012-10-17 00:43:34 +02:00
// The source for the Android application can be found at the following link: https://github.com/Lauszus/ArduinoBlinkLED
2012-03-25 16:28:50 +02:00
// 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
# include <adk.h>
2014-11-10 07:35:13 +01:00
//
// CAUTION! WARNING! ATTENTION! VORSICHT! ADVARSEL! ¡CUIDADO! ВНИМАНИЕ!
//
// Pin 13 is occupied by the SCK pin on various Arduino boards,
// including Uno, Duemilanove, etc., so use a different pin for those boards.
//
// CAUTION! WARNING! ATTENTION! VORSICHT! ADVARSEL! ¡CUIDADO! ВНИМАНИЕ!
//
# if defined(LED_BUILTIN)
# define LED LED_BUILTIN // Use built in LED
# else
# define LED 9 // Set to something here that makes sense for your board.
# endif
// Satisfy IDE, which only needs to see the include statment in the ino.
# ifdef dobogusinclude
# include <spi4teensy3.h>
# endif
2017-08-14 09:39:38 +02:00
# include <SPI.h>
2014-11-10 07:35:13 +01:00
2012-03-25 16:28:50 +02:00
USB Usb ;
2013-10-30 15:21:50 +01:00
ADK adk ( & Usb , " TKJElectronics " , // Manufacturer Name
" ArduinoBlinkLED " , // Model Name
" Example sketch for the USB Host Shield " , // Description (user-visible string)
" 1.0 " , // Version
" http://www.tkjelectronics.dk/uploads/ArduinoBlinkLED.apk " , // URL (web page to visit if no installed apps support the accessory)
" 123456789 " ) ; // Serial Number (optional)
2012-03-25 16:28:50 +02:00
2013-11-17 18:57:03 +01:00
uint32_t timer ;
2015-02-19 09:14:39 +01:00
bool connected ;
2013-11-17 18:57:03 +01:00
2013-10-30 15:21:50 +01:00
void setup ( ) {
2012-03-25 16:28:50 +02:00
Serial . begin ( 115200 ) ;
2014-11-10 07:35:13 +01:00
# if !defined(__MIPSEL__)
2013-10-21 19:58:03 +02:00
while ( ! Serial ) ; // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
2014-11-10 07:35:13 +01:00
# endif
2012-03-25 16:28:50 +02:00
if ( Usb . Init ( ) = = - 1 ) {
Serial . print ( " \r \n OSCOKIRQ failed to assert " ) ;
2013-10-30 15:21:50 +01:00
while ( 1 ) ; // halt
2012-03-25 16:28:50 +02:00
}
2012-04-11 01:48:46 +02:00
pinMode ( LED , OUTPUT ) ;
2013-10-21 19:58:03 +02:00
Serial . print ( " \r \n Arduino Blink LED Started " ) ;
2012-03-25 16:28:50 +02:00
}
2013-10-30 15:21:50 +01:00
void loop ( ) {
2012-03-25 16:28:50 +02:00
Usb . Task ( ) ;
2013-12-06 23:27:26 +01:00
2013-10-30 15:21:50 +01:00
if ( adk . isReady ( ) ) {
2013-12-06 23:27:26 +01:00
if ( ! connected ) {
connected = true ;
Serial . print ( F ( " \r \n Connected to accessory " ) ) ;
}
2012-03-25 16:28:50 +02:00
uint8_t msg [ 1 ] ;
uint16_t len = sizeof ( msg ) ;
uint8_t rcode = adk . RcvData ( & len , msg ) ;
2013-11-17 18:57:03 +01:00
if ( rcode & & rcode ! = hrNAK ) {
Serial . print ( F ( " \r \n Data rcv: " ) ) ;
Serial . print ( rcode , HEX ) ;
} else if ( len > 0 ) {
2012-03-25 16:28:50 +02:00
Serial . print ( F ( " \r \n Data Packet: " ) ) ;
Serial . print ( msg [ 0 ] ) ;
2013-10-30 15:21:50 +01:00
digitalWrite ( LED , msg [ 0 ] ? HIGH : LOW ) ;
2012-03-25 16:28:50 +02:00
}
2013-11-17 18:57:03 +01:00
2017-02-12 16:58:14 +01:00
if ( ( int32_t ) ( ( uint32_t ) millis ( ) - timer ) > = 1000 ) { // Send data every 1s
timer = ( uint32_t ) millis ( ) ;
2013-11-17 18:57:03 +01:00
rcode = adk . SndData ( sizeof ( timer ) , ( uint8_t * ) & timer ) ;
if ( rcode & & rcode ! = hrNAK ) {
Serial . print ( F ( " \r \n Data send: " ) ) ;
Serial . print ( rcode , HEX ) ;
} else if ( rcode ! = hrNAK ) {
Serial . print ( F ( " \r \n Timer: " ) ) ;
Serial . print ( timer ) ;
}
}
2013-12-06 23:27:26 +01:00
} else {
if ( connected ) {
connected = false ;
Serial . print ( F ( " \r \n Disconnected from accessory " ) ) ;
digitalWrite ( LED , LOW ) ;
}
2013-10-30 15:21:50 +01:00
}
2012-03-25 16:28:50 +02:00
}