It is worth noting that this USB Host Shield is not entirely successful, especially in combination with the Arduino Mega 2560. The first problem was that this expansion card was made for the Arduino UNO, and it differs from the Mega in the SPI pin positions, so I had to throw jumpers (see a photo). The second problem, although quite expected, was the need for an external power source for the operation of this expansion card. USB Host Shield 2.0 from Circuits @ Home is considered more successful, but it is also more expensive.
app
, firmware
, and hardware
folders should appear.firmware/arduino_libs/AndroidAccessory
and firmware/arduino_libs/USB_Host_Shield
to <arduino_installation_root>/libraries/
.<arduino_installation_root>/libraries/
and copy CapSense.cpp
and CapSense.h
into it from the CapSense archive.firmware/demokit/demokit.pde
, compile it and firmware/demokit/demokit.pde
to the board.app
folder, in the Build Target we specify Google APIs (Platform 2.3.3 , API Level 10). We assemble the application and install it on the phone. Who does not want to mess with the assembly - can download the finished APK . ... /* */ AndroidAccessory acc("Google, Inc.", "DemoKit", "DemoKit Arduino Board", "1.0", "http://www.android.com", "0000000012345678"); void setup() { .... acc.powerOn(); } void loop() { byte msg[3]; /* */ if (acc.isConnected()) { /* Android */ int len = acc.read(msg, sizeof(msg), 1); if (len > 0) { /* */ if (msg[0] == 0x2) { if (msg[1] == 0x0) analogWrite(LED3_RED, msg[2]); else if (msg[1] == 0x1) analogWrite(LED3_GREEN, msg[2]); else if (msg[1] == 0x2) analogWrite(LED3_BLUE, msg[2]); } } msg[0] = 0x1; b = digitalRead(BUTTON1); if (b != b1) { msg[1] = 0; msg[2] = b ? 1 : 0; /* */ acc.write(msg, 3); b1 = b; } } }
import com.android.future.usb.UsbAccessory; import com.android.future.usb.UsbManager; ... public class DemoKitActivity extends Activity implements Runnable { private UsbManager mUsbManager; UsbAccessory mAccessory; FileInputStream mInputStream; FileOutputStream mOutputStream; ... private void openAccessory(UsbAccessory accessory) { mFileDescriptor = mUsbManager.openAccessory(accessory); if (mFileDescriptor != null) { mAccessory = accessory; FileDescriptor fd = mFileDescriptor.getFileDescriptor(); mInputStream = new FileInputStream(fd); mOutputStream = new FileOutputStream(fd); Thread thread = new Thread(null, this, "AccessoryThread"); thread.start(); } } public void run() { int ret = 0; byte[] buffer = new byte[16384]; int i; while (ret >= 0) { // ret = mInputStream.read(buffer); i = 0; while (i < ret) { int len = ret - i; switch (buffer[i]) { case 0x1: // if (len >= 3) { Message m = Message.obtain(mHandler, MESSAGE_SWITCH); m.obj = new SwitchMsg(buffer[i + 1], buffer[i + 2]); mHandler.sendMessage(m); } i += 3; break; } } } } // - : // mActivity.sendCommand((byte)2, (byte)0, (byte)255) public void sendCommand(byte command, byte target, int value) { byte[] buffer = new byte[3]; if (value > 255) value = 255; buffer[0] = command; buffer[1] = target; buffer[2] = (byte) value; if (mOutputStream != null && buffer[1] != -1) { try { mOutputStream.write(buffer); } catch (IOException e) { ... } } } }
Source: https://habr.com/ru/post/123361/
All Articles