Android open accessory

From LVL1
Jump to navigation Jump to search

This stub is to document the process of trying to get AOA working with older hardware. ie freeduino + spark fun host shield and phones not currently supported.


What I am using

Hardware

  • Freeduino 1.22 with a ATmega328p-pu chip
  • Sparkfun USB Host shield
  • HTC G2

Software


* indicate that I had to change the code to get it to work.

Changes

There are a few changes that have to be made to the stoke software to get this to work.

demokit.pde

Out of the box the Arduino sketch for the demokit will not compile for the ATmega328. There are a lot of pins that are used that do not have the same name or do not exist.

#include <Wire.h>
#include <Servo.h>
#include <Max3421e.h> #include <Usb.h> #include <AndroidAccessory.h>
#include <CapSense.h>
#define LED3_RED 2 #define LED3_GREEN 4 #define LED3_BLUE 3
#define LED2_RED 5 #define LED2_GREEN 7 #define LED2_BLUE 6
#define LED1_RED 8 #define LED1_GREEN 10 #define LED1_BLUE 9
#define SERVO1 11 #define SERVO2 12 #define SERVO3 13
#define TOUCH_RECV 14 #define TOUCH_SEND 15
#define RELAY1 A0 #define RELAY2 A1
#define LIGHT_SENSOR A2 #define TEMP_SENSOR A3
#define BUTTON1 A6 #define BUTTON2 A7 #define BUTTON3 A8
#define JOY_SWITCH A9 // pulls line down when pressed #define JOY_nINT A10 // active low interrupt input #define JOY_nRESET A11 // active low reset output

After removing the definitions that are not used. removed all of the functions that control things that will not be used and changed everything to LED/buttons you this is whats left.

#include <Wire.h>

#include <Max3421e.h>      //must be edited before use
#include <Usb.h>      
#include <AndroidAccessory.h>

#define  LED1        2  //digital
#define  LED2        4  //digital
#define  LED3        3  //pwm
#define  LED4        5  //pwm

#define  Button1     A0
#define  Button2     A1

AndroidAccessory acc("Google, Inc.",             //manufacturer
		     "DemoKit",                  //model
		     "DemoKit Arduino Board",    //discription
		     "1.0",                      //version
		     "http://www.android.com",   //uri
		     "0000000012345678");        //serial
void setup();
void loop();

void init_buttons()
{
	pinMode(Button1, INPUT);
	pinMode(Button2, INPUT);

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


void init_leds()
{
	digitalWrite(LED1, 1);
	digitalWrite(LED2, 1);
	digitalWrite(LED3, 1);
	digitalWrite(LED4, 1);

	pinMode(LED1, OUTPUT);
	pinMode(LED2, OUTPUT);
	pinMode(LED3, OUTPUT);
	pinMode(LED4, OUTPUT);

}

byte b1, b2;
void setup()
{
	Serial.begin(115200);
	Serial.print("\r\nStart");

	init_leds();
	init_buttons();

	b1 = digitalRead(Button1);
	b2 = digitalRead(Button2);

	acc.powerOn();
}

void loop()
{
//	byte err;
//	byte idle;
//	static byte count = 0;
	byte msg[3];

	if (acc.isConnected()) {       //If the arduino is talking to the phoen
		int len = acc.read(msg, sizeof(msg), 1); //set len to the size of the last msg from the phone
//		int i;
		byte b;
//		uint16_t val;
//		int x, y;
//		char c0;

		if (len > 0) {
			// assumes only one command per packet
			if (msg[0] == 0x2) {
				if (msg[1] == 0x0)
					analogWrite(LED1, 255 - msg[2]);
				else if (msg[1] == 0x1)
					analogWrite(LED2, 255 - msg[2]);
				else if (msg[1] == 0x2)
					analogWrite(LED3, 255 - msg[2]);
				else if (msg[1] == 0x3)
					analogWrite(LED4, 255 - msg[2]);
			} else if (msg[0] == 0x3) {
		}

		msg[0] = 0x1;

		b = digitalRead(Button1);
		if (b != b1) {
			msg[1] = 0;
			msg[2] = b ? 0 : 1;
			acc.write(msg, 3);
			b1 = b;
		}

		b = digitalRead(Button2);
		if (b != b2) {
			msg[1] = 1;
			msg[2] = b ? 0 : 1;
			acc.write(msg, 3);
			b2 = b;
		}
		}
	} else {
		// reset outputs to default values on disconnect
		analogWrite(LED1, 255);
		analogWrite(LED2, 255);
		analogWrite(LED3, 255);
		analogWrite(LED4, 255);
	}

	delay(10);
}