分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
前面Android ADK 编程简介 介绍了ADK编程的一般步骤,这里给出一个简单的实例说明Android手机如果通过USB端口来控制LED。
所使用的Arduino 板子为Freetronics 的 EtherTen ,就一红一绿LED连接到7,8口上。如下图所示:
参考DemoKit的 Arudino 代码,为LED 驱动编写如下代码:
#include <avrpins.h>#include <max3421e.h>#include <usbhost.h>#include <usb_ch9.h>#include <Usb.h>#include <usbhub.h>#include <avr/pgmspace.h>#include <address.h> #include <adk.h> #include <printhex.h>#include <message.h>#include <hexdump.h>#include <parsetools.h> USB Usb;USBHub hub0(&Usb);USBHub hub1(&Usb);ADK adk(&Usb,"Guidebee Pty Ltd.","LedDemoKit","DemoKit Arduino Board","1.0","http://www.imobilebbs.com","0000000012345678");uint8_t b, b1; #define START_MOTOR 8#define STOP_MOTOR 7 #define ERROR_INDICATOR 13 void setup();void loop(); void init_leds(){pinMode(START_MOTOR, OUTPUT);pinMode(STOP_MOTOR, OUTPUT);digitalWrite(START_MOTOR, LOW);digitalWrite(STOP_MOTOR, LOW);} void setup(){Serial.begin(115200);Serial.println("\r\nADK demo start"); if (Usb.Init() == -1) {Serial.println("OSCOKIRQ failed to assert");while(1); //halt}//if (Usb.Init() == -1... init_leds(); } void loop(){uint8_t rcode;uint8_t msg[3] = { 0x00 };Usb.Task(); if( adk.isReady() == false ) {digitalWrite(ERROR_INDICATOR, HIGH);return;}else{digitalWrite(ERROR_INDICATOR, LOW);}uint16_t len = sizeof(msg);rcode = adk.RcvData(&len, msg); if(len > 0) {USBTRACE("\r\nData Packet.");// assumes only one command per packetif (msg[0] == 0x2) {switch( msg[1] ) {case 0:USBTRACE("LED 1\r\n.");if(msg[2]>128){digitalWrite(START_MOTOR, HIGH);} else{digitalWrite(START_MOTOR, LOW);}break;case 1:USBTRACE("LED 2\r\n.");if(msg[2]>128){digitalWrite(STOP_MOTOR, HIGH);} else{digitalWrite(STOP_MOTOR, LOW);}break;}//switch( msg[1]...}}//if( len > 0... msg[0] = 0x1;delay( 10 );}
编写一个简单的Android应用,包含四个按钮,如下:
这里设计一个帮助类ArduinoHelper,用于和Arduino板子USB通信,这个类可以和任意的Activity绑定,因此很容易应用到你自己的代码中。参见代码下载。
有了ArduinoHelper, Android应用和Arduino板子通信就非常简单,在OnCreate 中创建ArudinoHelp 的实例, 在onResume和onDestroy 方法中调用ArduinoHelper 的对于的方法:
public class ArduinoLedDemoActivity extends Activity {/** Called when the activity is first created. */ protected ArduinoHelper mArduinoHelper; @Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mArduinoHelper = new ArduinoHelper(this);Button button1 = (Button) findViewById(R.id.button1);button1.setOnClickListener(new OnClickListener() { @Overridepublic void onClick(View v) {mArduinoHelper.sendCommand(ArduinoHelper.LED_SERVO_COMMAND,(byte) 0x1, 250); }}); Button button2 = (Button) findViewById(R.id.button2);button2.setOnClickListener(new OnClickListener() { @Overridepublic void onClick(View v) {mArduinoHelper.sendCommand(ArduinoHelper.LED_SERVO_COMMAND,(byte) 0x1, 1); }}); Button button3 = (Button) findViewById(R.id.button3);button3.setOnClickListener(new OnClickListener() { @Overridepublic void onClick(View v) {mArduinoHelper.sendCommand(ArduinoHelper.LED_SERVO_COMMAND,(byte) 0x0, 250); }}); Button button4 = (Button) findViewById(R.id.button4);button4.setOnClickListener(new OnClickListener() { @Overridepublic void onClick(View v) {mArduinoHelper.sendCommand(ArduinoHelper.LED_SERVO_COMMAND,(byte) 0x0, 1); }});} @Overridepublic void onDestroy() {super.onDestroy();mArduinoHelper.onDestroy();} @Overridepublic void onResume() {super.onResume();mArduinoHelper.onResume();} }
代码下载