Created
January 6, 2015 12:16
-
-
Save YuuichiAkagawa/ae5d28ce6fc108ff9bfe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Max3421e.h> | |
#include <Usb.h> | |
#include <AndroidAccessory2.h> | |
#define JOYSTICK_VERT A0 | |
#define JOYSTICK_HORZ A1 | |
#define JOYSTICK_SEL A2 | |
#define DIVIDER 32 | |
#define LED1 3 | |
#define BUTTON1 4 | |
AndroidAccessory acc("ammlab.org", | |
"HelloADK", | |
"HelloADK for Arduino", | |
"1.0", | |
"https://play.google.com/store/apps/details?id=org.ammlab.android.helloadk", | |
"0000000012345678"); | |
const byte _hidReportDescriptor[] = { | |
// Mouse | |
0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x09, 0x01, | |
0xA1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x08, | |
0x15, 0x00, 0x25, 0x01, 0x95, 0x08, 0x75, 0x01, | |
0x81, 0x02, 0x95, 0x00, 0x81, 0x03, 0x05, 0x01, | |
0x09, 0x30, 0x09, 0x31, 0x09, 0x38, 0x15, 0x81, | |
0x25, 0x7F, 0x75, 0x08, 0x95, 0x03, 0x81, 0x06, | |
0x05, 0x0C, 0x0A, 0x38, 0x02, 0x95, 0x01, 0x81, | |
0x06, 0xC0, 0xC0, | |
}; | |
int vCenterH, vCenterL; | |
int hCenterH, hCenterL; | |
byte oldval = 0; | |
void setup(); | |
void loop(); | |
// Joystick center calibration | |
void calibration(){ | |
//Vertical | |
int min = 1024; | |
int max = 0; | |
int val; | |
for(int i=0; i<10; i++) | |
{ | |
val = analogRead(A0); | |
if(val < min){ | |
min = val; | |
} | |
if( val > max ){ | |
max = val; | |
} | |
delay(100); | |
} | |
vCenterH = max; | |
vCenterL = min; | |
//Horizontal | |
min = 1024; | |
max = 0; | |
for(int i=0; i<10; i++) | |
{ | |
val = analogRead(A1); | |
if(val < min){ | |
min = val; | |
} | |
if( val > max ){ | |
max = val; | |
} | |
delay(100); | |
} | |
hCenterH = max; | |
hCenterL = min; | |
} | |
char m[5]; | |
bool isHidInit = false; | |
void hidfunc() | |
{ | |
//HID init ? | |
if( isHidInit == false ){ | |
int len = sizeof(_hidReportDescriptor); | |
acc.hidRegist(0, len); | |
acc.hidSetReportDesc(0, (char*)_hidReportDescriptor, len); | |
isHidInit = true; | |
} | |
//Read Joystick value | |
int vVal = analogRead(JOYSTICK_VERT); | |
int hVal = analogRead(JOYSTICK_HORZ); | |
//Vertical | |
if( vVal < vCenterH+20 && vVal > vCenterL-20 ){ //neutral | |
m[2] = 0; | |
}else{ | |
if(vVal > vCenterL){ //+ | |
m[2] = (byte)(((vVal - vCenterL)/DIVIDER) *-1); | |
}else{ | |
m[2] = (byte)((vCenterL - vVal)/DIVIDER); | |
} | |
} | |
//Horizontal | |
if( hVal < hCenterH+20 && hVal > hCenterL-20 ){ //neutral | |
m[1]= 0; | |
}else{ | |
if(hVal > hCenterL){ //+ | |
m[1] = (byte)(((hVal - hCenterL)/DIVIDER) * -1); | |
}else{ | |
m[1] = (byte)((hCenterL - hVal)/DIVIDER); | |
} | |
} | |
//Push | |
if( digitalRead(JOYSTICK_SEL) == 0 ){ | |
m[0] = 1; | |
}else{ | |
m[0] = 0; | |
} | |
//Send HID event to Android | |
acc.hidSendEvent(0, m, sizeof(m)); | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
pinMode(LED1, OUTPUT); | |
pinMode(BUTTON1, INPUT_PULLUP); | |
pinMode(JOYSTICK_SEL, INPUT_PULLUP); | |
m[0] = m[1] = m[2] = m[3] = m[4] = 0; | |
calibration(); | |
acc.powerOn(); | |
} | |
void loop() | |
{ | |
byte msg[64]; | |
byte val = 0; | |
if (acc.isConnected()) { | |
//HID | |
hidfunc(); | |
//accessory | |
int len = acc.read(msg, sizeof(msg), 1); | |
if (len > 0) { | |
// assumes only one command per packet | |
if (msg[0] == 0x1) { | |
if( msg[1] == 0x01){ | |
digitalWrite(LED1, HIGH); | |
} else { | |
digitalWrite(LED1, LOW); | |
} | |
} | |
} | |
val = digitalRead(BUTTON1); | |
if( val != oldval ){ | |
msg[0] = 0x1; | |
if( val == 0 ){ | |
msg[1] = 0x1; | |
}else{ | |
msg[1] = 0x0; | |
} | |
acc.write(msg, 2); | |
oldval = val; | |
} | |
} | |
delay(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment