Android Open Accessory Library化してみました。 バルク転送の使い方が分かっていないため、相変わらず動作は不安定。 I do not understand the usage of the bulk transfer. Please teach someone.

Dependencies:   mbed

Committer:
abe00makoto
Date:
Fri Jun 03 17:43:15 2011 +0000
Revision:
0:4fcfc05943ff
Android Open Accessory Library+ demokit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
abe00makoto 0:4fcfc05943ff 1
abe00makoto 0:4fcfc05943ff 2 #include "mbed.h"
abe00makoto 0:4fcfc05943ff 3 #include "USBHost.h"
abe00makoto 0:4fcfc05943ff 4 #include "AndroidAccessory.h"
abe00makoto 0:4fcfc05943ff 5
abe00makoto 0:4fcfc05943ff 6
abe00makoto 0:4fcfc05943ff 7 //device setup
abe00makoto 0:4fcfc05943ff 8
abe00makoto 0:4fcfc05943ff 9 PwmOut led1(LED1);
abe00makoto 0:4fcfc05943ff 10 PwmOut led2(LED2);
abe00makoto 0:4fcfc05943ff 11 PwmOut led3(LED3);
abe00makoto 0:4fcfc05943ff 12 PwmOut led4(LED4);
abe00makoto 0:4fcfc05943ff 13
abe00makoto 0:4fcfc05943ff 14 InterruptIn sw1(p21);
abe00makoto 0:4fcfc05943ff 15 InterruptIn sw2(p22);
abe00makoto 0:4fcfc05943ff 16 InterruptIn sw3(p23);
abe00makoto 0:4fcfc05943ff 17
abe00makoto 0:4fcfc05943ff 18
abe00makoto 0:4fcfc05943ff 19 /*
abe00makoto 0:4fcfc05943ff 20 DigitalIn sw1(p21);
abe00makoto 0:4fcfc05943ff 21 DigitalIn sw2(p22);
abe00makoto 0:4fcfc05943ff 22 DigitalIn sw3(p23);
abe00makoto 0:4fcfc05943ff 23 */
abe00makoto 0:4fcfc05943ff 24
abe00makoto 0:4fcfc05943ff 25
abe00makoto 0:4fcfc05943ff 26
abe00makoto 0:4fcfc05943ff 27 class Demokit :public AndroidAccessory {
abe00makoto 0:4fcfc05943ff 28 public:
abe00makoto 0:4fcfc05943ff 29 Demokit():AndroidAccessory(3,3,
abe00makoto 0:4fcfc05943ff 30 "Google, Inc.",
abe00makoto 0:4fcfc05943ff 31 "DemoKit",
abe00makoto 0:4fcfc05943ff 32 "DemoKit Arduino Board",
abe00makoto 0:4fcfc05943ff 33 "1.0",
abe00makoto 0:4fcfc05943ff 34 "http://www.android.com",
abe00makoto 0:4fcfc05943ff 35 "0000000012345678") {};
abe00makoto 0:4fcfc05943ff 36 virtual int callbackRead(u8 *buff, int len);
abe00makoto 0:4fcfc05943ff 37 virtual void setupDevice();
abe00makoto 0:4fcfc05943ff 38 virtual void resetDevice();
abe00makoto 0:4fcfc05943ff 39 void switchchk();
abe00makoto 0:4fcfc05943ff 40 private:
abe00makoto 0:4fcfc05943ff 41
abe00makoto 0:4fcfc05943ff 42 u8 sw1b,sw2b,sw3b; //swich data
abe00makoto 0:4fcfc05943ff 43 };
abe00makoto 0:4fcfc05943ff 44
abe00makoto 0:4fcfc05943ff 45
abe00makoto 0:4fcfc05943ff 46
abe00makoto 0:4fcfc05943ff 47 void Demokit::setupDevice() {
abe00makoto 0:4fcfc05943ff 48 sw1.mode(PullUp);
abe00makoto 0:4fcfc05943ff 49 sw2.mode(PullUp);
abe00makoto 0:4fcfc05943ff 50 sw3.mode(PullUp);
abe00makoto 0:4fcfc05943ff 51 sw1.rise(this,&Demokit::switchchk);
abe00makoto 0:4fcfc05943ff 52 sw1.fall(this,&Demokit::switchchk);
abe00makoto 0:4fcfc05943ff 53 sw2.rise(this,&Demokit::switchchk);
abe00makoto 0:4fcfc05943ff 54 sw2.fall(this,&Demokit::switchchk);
abe00makoto 0:4fcfc05943ff 55 sw3.rise(this,&Demokit::switchchk);
abe00makoto 0:4fcfc05943ff 56 sw3.fall(this,&Demokit::switchchk);
abe00makoto 0:4fcfc05943ff 57 sw1b=sw2b=sw3b=sw1;
abe00makoto 0:4fcfc05943ff 58
abe00makoto 0:4fcfc05943ff 59 }
abe00makoto 0:4fcfc05943ff 60
abe00makoto 0:4fcfc05943ff 61
abe00makoto 0:4fcfc05943ff 62 void Demokit::resetDevice() {
abe00makoto 0:4fcfc05943ff 63 led1=0;
abe00makoto 0:4fcfc05943ff 64 led2=0;
abe00makoto 0:4fcfc05943ff 65 led3=0;
abe00makoto 0:4fcfc05943ff 66 led4=0;
abe00makoto 0:4fcfc05943ff 67 }
abe00makoto 0:4fcfc05943ff 68
abe00makoto 0:4fcfc05943ff 69 int Demokit::callbackRead(u8 *buf, int len) {
abe00makoto 0:4fcfc05943ff 70
abe00makoto 0:4fcfc05943ff 71
abe00makoto 0:4fcfc05943ff 72 if (buf[0] == 0x2) {
abe00makoto 0:4fcfc05943ff 73 if (buf[1] == 0x0) {
abe00makoto 0:4fcfc05943ff 74 log("led1=%d\r\n",buf[2]);
abe00makoto 0:4fcfc05943ff 75 led1=((float)buf[2])/255.0;
abe00makoto 0:4fcfc05943ff 76 } else if (buf[1]==0x3) {
abe00makoto 0:4fcfc05943ff 77 led2=((float)buf[2])/255.0;
abe00makoto 0:4fcfc05943ff 78 } else if (buf[1]==0x6) {
abe00makoto 0:4fcfc05943ff 79 led3=((float)buf[2])/255.0;
abe00makoto 0:4fcfc05943ff 80 }
abe00makoto 0:4fcfc05943ff 81 } else if (buf[0] ==0x3) {
abe00makoto 0:4fcfc05943ff 82 if (buf[1] == 0x0) {
abe00makoto 0:4fcfc05943ff 83 led4=buf[2]? 1.0:0.0;
abe00makoto 0:4fcfc05943ff 84 }
abe00makoto 0:4fcfc05943ff 85 }
abe00makoto 0:4fcfc05943ff 86
abe00makoto 0:4fcfc05943ff 87 return 0;
abe00makoto 0:4fcfc05943ff 88 }
abe00makoto 0:4fcfc05943ff 89
abe00makoto 0:4fcfc05943ff 90 void Demokit::switchchk() {
abe00makoto 0:4fcfc05943ff 91 log("Demokit::switchchk()\r\n");
abe00makoto 0:4fcfc05943ff 92 //int ret;
abe00makoto 0:4fcfc05943ff 93 __disable_irq();
abe00makoto 0:4fcfc05943ff 94
abe00makoto 0:4fcfc05943ff 95
abe00makoto 0:4fcfc05943ff 96 u8* wbuf=_writebuff;
abe00makoto 0:4fcfc05943ff 97 wbuf[0]=0x01;
abe00makoto 0:4fcfc05943ff 98 //switch1
abe00makoto 0:4fcfc05943ff 99 if (sw1!=sw1b) {
abe00makoto 0:4fcfc05943ff 100 log("sw1=%d\r\n",sw1.read());
abe00makoto 0:4fcfc05943ff 101 wbuf[1]=0;
abe00makoto 0:4fcfc05943ff 102 wbuf[2]=!sw1;
abe00makoto 0:4fcfc05943ff 103 this->write();
abe00makoto 0:4fcfc05943ff 104 wait_ms(4);
abe00makoto 0:4fcfc05943ff 105 sw1b=sw1;
abe00makoto 0:4fcfc05943ff 106 //w_flag=true;
abe00makoto 0:4fcfc05943ff 107 }
abe00makoto 0:4fcfc05943ff 108 //switch2
abe00makoto 0:4fcfc05943ff 109 else if (sw2!=sw2b) {
abe00makoto 0:4fcfc05943ff 110 log("sw2=%d\r\n",sw2.read());
abe00makoto 0:4fcfc05943ff 111 wbuf[1]=1;
abe00makoto 0:4fcfc05943ff 112 wbuf[2]=!sw2;
abe00makoto 0:4fcfc05943ff 113 this->write();
abe00makoto 0:4fcfc05943ff 114 wait_ms(4);
abe00makoto 0:4fcfc05943ff 115 sw2b=sw2;
abe00makoto 0:4fcfc05943ff 116 //w_flag=true;
abe00makoto 0:4fcfc05943ff 117 }
abe00makoto 0:4fcfc05943ff 118 //switch3
abe00makoto 0:4fcfc05943ff 119 else if (sw3!=sw3b) {
abe00makoto 0:4fcfc05943ff 120 log("sw3=%d\r\n",sw3.read());
abe00makoto 0:4fcfc05943ff 121 wbuf[1]=2;
abe00makoto 0:4fcfc05943ff 122 wbuf[2]=!sw3;
abe00makoto 0:4fcfc05943ff 123 this->write();
abe00makoto 0:4fcfc05943ff 124 wait_ms(4);
abe00makoto 0:4fcfc05943ff 125 sw3b=sw3;
abe00makoto 0:4fcfc05943ff 126 // w_flag=true;
abe00makoto 0:4fcfc05943ff 127 }
abe00makoto 0:4fcfc05943ff 128
abe00makoto 0:4fcfc05943ff 129 else {
abe00makoto 0:4fcfc05943ff 130 wbuf[0]=wbuf[1]=wbuf[2]=0;
abe00makoto 0:4fcfc05943ff 131 this->write();
abe00makoto 0:4fcfc05943ff 132 wait_ms(4);
abe00makoto 0:4fcfc05943ff 133 }
abe00makoto 0:4fcfc05943ff 134
abe00makoto 0:4fcfc05943ff 135 __enable_irq();
abe00makoto 0:4fcfc05943ff 136 }
abe00makoto 0:4fcfc05943ff 137
abe00makoto 0:4fcfc05943ff 138
abe00makoto 0:4fcfc05943ff 139
abe00makoto 0:4fcfc05943ff 140 Demokit demokit;
abe00makoto 0:4fcfc05943ff 141
abe00makoto 0:4fcfc05943ff 142 int main() {
abe00makoto 0:4fcfc05943ff 143 printf("Android Development Kit: start\r\n");
abe00makoto 0:4fcfc05943ff 144 USBInit();
abe00makoto 0:4fcfc05943ff 145 while (1) {
abe00makoto 0:4fcfc05943ff 146 USBLoop();
abe00makoto 0:4fcfc05943ff 147 //if(demokit.read(demokit._readbuff,demokit._readbuffsize)<0)demokit.adkEnd();
abe00makoto 0:4fcfc05943ff 148 }
abe00makoto 0:4fcfc05943ff 149
abe00makoto 0:4fcfc05943ff 150 }