Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
demokit.cpp
00001 00002 #include "mbed.h" 00003 #include "AndroidAccessory.h" 00004 00005 00006 //device setup 00007 00008 PwmOut led1(LED1); 00009 PwmOut led2(LED2); 00010 PwmOut led3(LED3); 00011 PwmOut led4(LED4); 00012 00013 00014 00015 DigitalOut ind(p21); 00016 00017 00018 00019 #define OUTL 100 00020 #define INBL 100 00021 00022 00023 00024 00025 class AdkTerm :public AndroidAccessory { 00026 public: 00027 AdkTerm():AndroidAccessory(INBL,OUTL, 00028 "ARM", 00029 "mbed", 00030 "mbed Terminal", 00031 "0.1", 00032 "http://www.mbed.org", 00033 "0000000012345678"),pc(USBTX,USBRX),Right(p19),Left(p20) {}; 00034 virtual int callbackRead(u8 *buff, int len); 00035 virtual void setupDevice(); 00036 virtual void resetDevice(); 00037 virtual int callbackWrite(); 00038 00039 private: 00040 void serialIRQ(); 00041 void onTick(); 00042 void AttachTick(); 00043 char buffer[OUTL]; 00044 int bcount; 00045 Serial pc; 00046 AnalogIn Right; 00047 AnalogIn Left; 00048 Ticker tick; 00049 float right,left,rl,ll; 00050 int tl,tr; 00051 Timeout n; 00052 bool settick; 00053 }; 00054 00055 00056 00057 void AdkTerm::setupDevice() { 00058 pc.baud(112500); 00059 pc.printf("Welcome to adkTerm\n\n\n\n\n\n\r"); 00060 settick = false; 00061 pc.attach(this, &AdkTerm::serialIRQ, Serial::RxIrq); 00062 for (int i = 0; i<OUTL; i++) { 00063 buffer[i] = 0; 00064 } 00065 bcount = 0; 00066 //n.attach(this,&AdkTerm::AttachTick,5); 00067 //tick.attach(this,&AdkTerm::onTick,0.1); 00068 00069 00070 } 00071 00072 void AdkTerm::AttachTick() { 00073 if(!settick)tick.attach(this,&AdkTerm::onTick,0.04); 00074 settick = true; 00075 } 00076 00077 void AdkTerm::onTick() { 00078 right = 1-Right; 00079 left = 1-Left; 00080 bool update = false; 00081 int templ, tempr; 00082 00083 00084 00085 templ = int(left * 10000); 00086 tempr = int(right * 10000); 00087 00088 00089 00090 if (abs(templ-tl)>170) { 00091 update = true; 00092 } 00093 if (abs(tempr-tr)>170) { 00094 update = true; 00095 } 00096 if (update) { 00097 u8* wbuf = _writebuff; 00098 00099 wbuf[0] = 'P'; 00100 wbuf[1] = templ&0xFF; 00101 wbuf[2] = (templ>>8) & 0xFF; 00102 wbuf[3] = tempr&0xFF; 00103 wbuf[4] = (tempr>>8) & 0xFF; 00104 wbuf[5] = 0; 00105 00106 this->write(wbuf,5); 00107 00108 00109 } 00110 } 00111 00112 void AdkTerm::resetDevice() { 00113 pc.printf("adkTerm reset\n\r"); 00114 for (int i = 0; i<OUTL; i++) { 00115 buffer[i] = 0; 00116 } 00117 bcount = 0; 00118 } 00119 00120 int AdkTerm::callbackRead(u8 *buf, int len) { 00121 pc.printf("%i %s\n\r\n\n\n",len,buf); 00122 for (int i = 0; i<INBL; i++) { 00123 buf[i] = 0; 00124 } 00125 00126 AttachTick(); 00127 00128 return 0; 00129 } 00130 00131 int AdkTerm::callbackWrite() { 00132 00133 ind = false; 00134 return 0; 00135 } 00136 00137 00138 void AdkTerm::serialIRQ() { 00139 buffer[bcount] = pc.getc(); 00140 00141 pc.putc(buffer[bcount]); 00142 00143 if (buffer[bcount] == '\n' || buffer[bcount] == '\r') { 00144 u8* wbuf = _writebuff; 00145 for (int i = 0; i<OUTL; i++) { 00146 wbuf[i] = buffer[i]; 00147 buffer[i] = 0; 00148 } 00149 pc.printf("Sending: %s\n\r",wbuf); 00150 ind = true; 00151 this->write(wbuf,bcount); 00152 bcount = 0; 00153 } else { 00154 if (buffer[bcount] != 0x08 && buffer[bcount] != 0x7F ) { 00155 bcount++; 00156 if (bcount == OUTL) { 00157 bcount = 0; 00158 } 00159 } else { 00160 bcount--; 00161 } 00162 } 00163 00164 } 00165 00166 00167 00168 AdkTerm AdkTerm; 00169 00170 00171 00172 int main() { 00173 00174 00175 00176 AdkTerm.setupDevice(); 00177 printf("Android Development Kit: start\r\n"); 00178 USBInit(); 00179 while (1) { 00180 USBLoop(); 00181 } 00182 00183 }
Generated on Sun Jul 17 2022 23:50:41 by
1.7.2