<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://hades.mech.northwestern.edu//api.php?action=feedcontributions&amp;feedformat=atom&amp;user=ShengWu</id>
	<title>Mech - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://hades.mech.northwestern.edu//api.php?action=feedcontributions&amp;feedformat=atom&amp;user=ShengWu"/>
	<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php/Special:Contributions/ShengWu"/>
	<updated>2026-04-04T17:19:59Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.9</generator>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=22136</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=22136"/>
		<updated>2013-04-22T16:28:50Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version).&lt;br /&gt;
&lt;br /&gt;
Get the starter code: [[Media:Android_PIC_starter_code.zip]] or on [https://github.com/shengwu/android-pic32 Github]&lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. In Eclipse, this requires going to the Help menu at the very left and selecting &amp;quot;Install New Software&amp;quot;. &amp;#039;&amp;#039;&amp;#039;You must install the ADT Plugin to develop Android applications with Eclipse.&amp;#039;&amp;#039;&amp;#039; After doing that, you&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation (linked above), [[Media:Android_PIC_starter_code.zip]]. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming. Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a random set of coordinates that will end up somewhere in Africa, Europe, or Asia. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information.&lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends this maximum amplitude to the PIC as an integer. The PIC comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Overview of an Android application ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when certain member functions are called in an Activity&amp;#039;s lifecycle. Activities are exited when a user presses &amp;quot;back&amp;quot; but are only paused when a user presses &amp;quot;home.&amp;quot; Source: [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credit ==&lt;br /&gt;
&lt;br /&gt;
This project adapts code from two projects for the PIC32. The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=22038</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=22038"/>
		<updated>2013-02-11T10:25:59Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Credits */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version).&lt;br /&gt;
&lt;br /&gt;
Get the starter code: [[Media:Android_PIC_starter_code.zip]]&lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. In Eclipse, this requires going to the Help menu at the very left and selecting &amp;quot;Install New Software&amp;quot;. &amp;#039;&amp;#039;&amp;#039;You must install the ADT Plugin to develop Android applications with Eclipse.&amp;#039;&amp;#039;&amp;#039; After doing that, you&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation (linked above), [[Media:Android_PIC_starter_code.zip]]. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming. Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a random set of coordinates that will end up somewhere in Africa, Europe, or Asia. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information.&lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends this maximum amplitude to the PIC as an integer. The PIC comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Overview of an Android application ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when certain member functions are called in an Activity&amp;#039;s lifecycle. Activities are exited when a user presses &amp;quot;back&amp;quot; but are only paused when a user presses &amp;quot;home.&amp;quot; Source: [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credit ==&lt;br /&gt;
&lt;br /&gt;
This project adapts code from two projects for the PIC32. The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21683</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21683"/>
		<updated>2012-11-14T20:18:39Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Things you need */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version).&lt;br /&gt;
&lt;br /&gt;
Get the starter code: [[Media:Android_PIC_starter_code.zip]]&lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. In Eclipse, this requires going to the Help menu at the very left and selecting &amp;quot;Install New Software&amp;quot;. &amp;#039;&amp;#039;&amp;#039;You must install the ADT Plugin to develop Android applications with Eclipse.&amp;#039;&amp;#039;&amp;#039; After doing that, you&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation (linked above), [[Media:Android_PIC_starter_code.zip]]. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming. Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a random set of coordinates that will end up somewhere in Africa, Europe, or Asia. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information.&lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends this maximum amplitude to the PIC as an integer. The PIC comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Overview of an Android application ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when certain member functions are called in an Activity&amp;#039;s lifecycle. Activities are exited when a user presses &amp;quot;back&amp;quot; but are only paused when a user presses &amp;quot;home.&amp;quot; Source: [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21670</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21670"/>
		<updated>2012-10-24T18:43:30Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Things you need */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version).&lt;br /&gt;
&lt;br /&gt;
Get the starter code: [[Media:Android_PIC_starter_code.zip]]&lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. In Eclipse, this requires going to the Help menu at the very left and selecting &amp;quot;Install New Software&amp;quot;. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation (linked above), [[Media:Android_PIC_starter_code.zip]]. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming. Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a random set of coordinates that will end up somewhere in Africa, Europe, or Asia. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information.&lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends this maximum amplitude to the PIC as an integer. The PIC comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Overview of an Android application ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when certain member functions are called in an Activity&amp;#039;s lifecycle. Activities are exited when a user presses &amp;quot;back&amp;quot; but are only paused when a user presses &amp;quot;home.&amp;quot; Source: [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21669</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21669"/>
		<updated>2012-10-08T01:30:18Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Overview of an Android application */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version).&lt;br /&gt;
&lt;br /&gt;
Get the starter code: [[Media:Android_PIC_starter_code.zip]]&lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation (linked above), [[Media:Android_PIC_starter_code.zip]]. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming. Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a random set of coordinates that will end up somewhere in Africa, Europe, or Asia. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information.&lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends this maximum amplitude to the PIC as an integer. The PIC comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Overview of an Android application ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when certain member functions are called in an Activity&amp;#039;s lifecycle. Activities are exited when a user presses &amp;quot;back&amp;quot; but are only paused when a user presses &amp;quot;home.&amp;quot; Source: [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21668</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21668"/>
		<updated>2012-10-08T01:30:01Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Overview of an Android application */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version).&lt;br /&gt;
&lt;br /&gt;
Get the starter code: [[Media:Android_PIC_starter_code.zip]]&lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation (linked above), [[Media:Android_PIC_starter_code.zip]]. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming. Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a random set of coordinates that will end up somewhere in Africa, Europe, or Asia. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information.&lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends this maximum amplitude to the PIC as an integer. The PIC comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Overview of an Android application ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when certain member functions are called in an Activity&amp;#039;s lifecycle. Activities are exited when a user presses &amp;quot;back&amp;quot; but are only paused when a user presses &amp;quot;home.&amp;quot; From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21667</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21667"/>
		<updated>2012-10-08T01:28:57Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Overview of an Android application */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version).&lt;br /&gt;
&lt;br /&gt;
Get the starter code: [[Media:Android_PIC_starter_code.zip]]&lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation (linked above), [[Media:Android_PIC_starter_code.zip]]. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming. Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a random set of coordinates that will end up somewhere in Africa, Europe, or Asia. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information.&lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends this maximum amplitude to the PIC as an integer. The PIC comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Overview of an Android application ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when certain member functions are called in an Activity&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21666</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21666"/>
		<updated>2012-10-08T01:28:35Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Basics of Android programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version).&lt;br /&gt;
&lt;br /&gt;
Get the starter code: [[Media:Android_PIC_starter_code.zip]]&lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation (linked above), [[Media:Android_PIC_starter_code.zip]]. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming. Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a random set of coordinates that will end up somewhere in Africa, Europe, or Asia. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information.&lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends this maximum amplitude to the PIC as an integer. The PIC comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Overview of an Android application ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when the member functions are called in an Activity&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21665</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21665"/>
		<updated>2012-10-08T01:27:38Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Microphone demo */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version).&lt;br /&gt;
&lt;br /&gt;
Get the starter code: [[Media:Android_PIC_starter_code.zip]]&lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation (linked above), [[Media:Android_PIC_starter_code.zip]]. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming. Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a random set of coordinates that will end up somewhere in Africa, Europe, or Asia. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information.&lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends this maximum amplitude to the PIC as an integer. The PIC comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when the member functions are called in an Activity&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21661</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21661"/>
		<updated>2012-10-05T13:40:49Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Things you need */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version).&lt;br /&gt;
&lt;br /&gt;
Get the starter code: [[Media:Android_PIC_starter_code.zip]]&lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation (linked above), [[Media:Android_PIC_starter_code.zip]]. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming. Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a random set of coordinates that will end up somewhere in Africa, Europe, or Asia. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information.&lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when the member functions are called in an Activity&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21660</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21660"/>
		<updated>2012-10-05T13:40:28Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version).&lt;br /&gt;
&lt;br /&gt;
Get the starter code: [[Media:Android_PIC_starter_code.zip]]&lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation, [[Media:Android_PIC_starter_code.zip]]. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming. Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a random set of coordinates that will end up somewhere in Africa, Europe, or Asia. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information.&lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when the member functions are called in an Activity&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21659</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21659"/>
		<updated>2012-10-05T13:40:06Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Things you need */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation, [[Media:Android_PIC_starter_code.zip]]. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming. Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a random set of coordinates that will end up somewhere in Africa, Europe, or Asia. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information.&lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when the member functions are called in an Activity&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21658</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21658"/>
		<updated>2012-10-05T13:39:49Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Things you need */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation, [Media:Android_PIC_starter_code.zip]. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming. Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a random set of coordinates that will end up somewhere in Africa, Europe, or Asia. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information.&lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when the member functions are called in an Activity&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21657</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21657"/>
		<updated>2012-10-05T13:39:32Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Accessing USB on the PIC */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming. Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a random set of coordinates that will end up somewhere in Africa, Europe, or Asia. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information.&lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when the member functions are called in an Activity&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21656</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21656"/>
		<updated>2012-10-05T13:39:23Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Accessing USB on the PIC */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming. Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a random set of coordinates that will end up somewhere in Africa, Europe, or Asia. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information.&lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when the member functions are called in an Activity&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:Android_PIC_starter_code.zip&amp;diff=21655</id>
		<title>File:Android PIC starter code.zip</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:Android_PIC_starter_code.zip&amp;diff=21655"/>
		<updated>2012-10-05T13:38:48Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: Starter code v1 for interfacing an Android device with a PIC32 development board&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Starter code v1 for interfacing an Android device with a PIC32 development board&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21654</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21654"/>
		<updated>2012-10-05T06:44:30Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* GPS demo */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming. Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a random set of coordinates that will end up somewhere in Africa, Europe, or Asia. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information.&lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when the member functions are called in an Activity&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21653</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21653"/>
		<updated>2012-10-05T06:44:17Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* GPS demo */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming. Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a random set of coordinates from somewhere in Africa, Europe, or Asia. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information.&lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when the member functions are called in an Activity&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21652</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21652"/>
		<updated>2012-10-05T06:12:08Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Things you need */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming. Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a somewhat random set of coordinates. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when the member functions are called in an Activity&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21651</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21651"/>
		<updated>2012-10-05T06:11:56Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Basics of Android programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a somewhat random set of coordinates. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when the member functions are called in an Activity&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21650</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21650"/>
		<updated>2012-10-05T06:11:31Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Basics of Android programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a somewhat random set of coordinates. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when the member functions are called in an Activity&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
Tip: Ctrl-Shift-O in Eclipse updates your Java imports based on the code in the file. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21649</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21649"/>
		<updated>2012-10-05T05:39:43Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Things you need */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages. For my Nexus S 4G, I downloaded the Android 2.3.3 and the Google USB Driver packages. Turn on USB debugging on your Android device in Settings &amp;gt; Applications &amp;gt; Development &amp;gt; USB debugging. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a somewhat random set of coordinates. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when the member functions are called in an Activity&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21648</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21648"/>
		<updated>2012-10-05T05:34:05Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Microphone demo */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a somewhat random set of coordinates. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when the member functions are called in an Activity&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21647</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21647"/>
		<updated>2012-10-05T05:33:56Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Android Sensor Demo */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC. When the PIC receives a set of coordinates, it will send back a somewhat random set of coordinates. &lt;br /&gt;
&lt;br /&gt;
See the [http://developer.android.com/training/basics/location/index.html Location documentation] for more information. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
See Android&amp;#039;s [http://developer.android.com/training/camera/index.html Camera documentation]. Usually, it&amp;#039;s best to use the approach described in &amp;quot;Taking Photos Simply&amp;quot;, where you use the device&amp;#039;s built-in camera activity to capture an image. &lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
This activity uses &amp;lt;tt&amp;gt;AudioRecord&amp;lt;/tt&amp;gt; to access raw data from the microphone, rather than &amp;lt;tt&amp;gt;MediaRecorder&amp;lt;/tt&amp;gt;, which is designed to record audio and/or video for saving to files.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when the member functions are called in an Activity&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21646</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21646"/>
		<updated>2012-10-05T04:44:32Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Basics of Android programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when the member functions are called in an Activity&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21645</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21645"/>
		<updated>2012-10-05T04:43:36Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Basics of Android programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|This describes when &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; functions are called in an application&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Android documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21644</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21644"/>
		<updated>2012-10-05T04:42:34Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Basics of Android programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|When &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; functions are called in an application&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Android documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (see right). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21643</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21643"/>
		<updated>2012-10-05T04:42:00Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Basics of Android programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|513 px|When &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; functions are called in an application&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Android documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21642</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21642"/>
		<updated>2012-10-05T04:41:37Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Basics of Android programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|513 px|When &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; functions are called in an application&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Android documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21641</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21641"/>
		<updated>2012-10-05T04:41:25Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Basics of Android programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Activity_lifecycle.png|thumb|400 px|When &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; functions are called in an application&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Android documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21640</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21640"/>
		<updated>2012-10-05T04:41:11Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Basics of Android programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
[[Image:Android_lifecycle.png|thumb|400 px|When &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; functions are called in an application&amp;#039;s lifecycle. From the [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Android documentation].|right]]&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:Activity_lifecycle.png&amp;diff=21639</id>
		<title>File:Activity lifecycle.png</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:Activity_lifecycle.png&amp;diff=21639"/>
		<updated>2012-10-05T04:39:51Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: The Android activity lifecycle. 

Licensed under Apache 2.0 http://www.apache.org/licenses/LICENSE-2.0&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Android activity lifecycle. &lt;br /&gt;
&lt;br /&gt;
Licensed under Apache 2.0 http://www.apache.org/licenses/LICENSE-2.0&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21638</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21638"/>
		<updated>2012-10-05T04:38:04Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Troubleshooting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs, with the correct Android SDK platforms. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21637</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21637"/>
		<updated>2012-10-05T04:37:14Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Troubleshooting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Everything should work as downloaded.&amp;#039;&amp;#039;&amp;#039; If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21636</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21636"/>
		<updated>2012-10-05T04:27:12Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* String Passing Demo */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. (You must connect or reset the pic AFTER starting the application.) If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
Everything should work as downloaded. If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21634</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21634"/>
		<updated>2012-10-03T07:16:36Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Accessing USB on the PIC */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. (Avoid using large amounts of solder.)|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
Everything should work as downloaded. If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21633</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21633"/>
		<updated>2012-10-03T06:58:31Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Accessing USB on the PIC */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. Avoid using large amounts of solder.|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
Everything should work as downloaded. If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21632</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21632"/>
		<updated>2012-10-03T06:58:20Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Accessing USB on the PIC */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Wires soldered to the VBUS and VUSB pads on the NU32. Avoid large amounts of solder.|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
Everything should work as downloaded. If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21631</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21631"/>
		<updated>2012-10-03T06:57:14Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[Image:Vbus_vusb_soldered.jpg|thumb|300 px|Avoid using large amounts of solder.|right]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
Everything should work as downloaded. If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21630</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21630"/>
		<updated>2012-10-03T06:55:38Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Accessing USB on the PIC */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
[[File:Vbus_vusb_soldered.jpg‎]]&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
Everything should work as downloaded. If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:Vbus_vusb_soldered.jpg&amp;diff=21629</id>
		<title>File:Vbus vusb soldered.jpg</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:Vbus_vusb_soldered.jpg&amp;diff=21629"/>
		<updated>2012-10-03T06:55:21Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: Soldered wires to the VBUS and VUSB pads on the NU32 development board.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Soldered wires to the VBUS and VUSB pads on the NU32 development board.&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21628</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21628"/>
		<updated>2012-10-03T06:54:16Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|500 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
Everything should work as downloaded. If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21627</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21627"/>
		<updated>2012-10-03T06:54:06Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|400 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
Everything should work as downloaded. If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21626</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21626"/>
		<updated>2012-10-03T06:53:56Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:PICAndroid.jpeg|thumb|300 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect the USB cable to the Android device. If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received.&lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
Everything should work as downloaded. If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=File:PICAndroid.jpeg&amp;diff=21625</id>
		<title>File:PICAndroid.jpeg</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=File:PICAndroid.jpeg&amp;diff=21625"/>
		<updated>2012-10-03T06:53:27Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: A photo of the touchscreen demo from the PIC Android project in action.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A photo of the touchscreen demo from the PIC Android project in action.&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21588</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21588"/>
		<updated>2012-10-01T16:02:21Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Accessing USB on the PIC */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:test.jpg|thumb|300 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need to create a USB cable to connect your Android device to the PIC and some other parts. Here&amp;#039;s a list: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. Ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on the development board and connect your Android device, the device should begin charging.&lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect it. If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received. &lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
Everything should work as downloaded. If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21587</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21587"/>
		<updated>2012-10-01T15:59:07Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Things you need */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:test.jpg|thumb|300 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclipse and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
&lt;br /&gt;
First, you&amp;#039;ll need a USB cable to connect your Android device to the PIC. You&amp;#039;ll probably need the following: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. We can ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on your development board and connect your Android device, the device should begin charging. &lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect it. If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received. &lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
Everything should work as downloaded. If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21586</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21586"/>
		<updated>2012-10-01T15:58:56Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Things you need */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:test.jpg|thumb|300 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website])&lt;br /&gt;
&lt;br /&gt;
Follow the instructions to get the ADT Plugin for Eclips and to link the SDK Manager to Eclipse. You&amp;#039;ll need to open the SDK Manager (probably as administrator) and download the appropriate packages - for my Nexus S 4G, the Android 2.3.3 and the Google USB Driver packages. It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
&lt;br /&gt;
First, you&amp;#039;ll need a USB cable to connect your Android device to the PIC. You&amp;#039;ll probably need the following: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. We can ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on your development board and connect your Android device, the device should begin charging. &lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect it. If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received. &lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
Everything should work as downloaded. If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21585</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21585"/>
		<updated>2012-10-01T15:56:42Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Troubleshooting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:test.jpg|thumb|300 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website] and follow the instructions to link it to Eclipse)&lt;br /&gt;
&lt;br /&gt;
It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
&lt;br /&gt;
First, you&amp;#039;ll need a USB cable to connect your Android device to the PIC. You&amp;#039;ll probably need the following: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. We can ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on your development board and connect your Android device, the device should begin charging. &lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect it. If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received. &lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
Everything should work as downloaded. If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for each firmware project. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. Because of these specific settings, it&amp;#039;s recommended to right click the String Passing Demo and select &amp;quot;Copy...&amp;quot; to start a new firmware project. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
	<entry>
		<id>https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21584</id>
		<title>Interfacing the PIC32 with an Android device</title>
		<link rel="alternate" type="text/html" href="https://hades.mech.northwestern.edu//index.php?title=Interfacing_the_PIC32_with_an_Android_device&amp;diff=21584"/>
		<updated>2012-10-01T15:55:29Z</updated>

		<summary type="html">&lt;p&gt;ShengWu: /* Troubleshooting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Image:test.jpg|thumb|300 px|The touchscreen demo in action|right]]&lt;br /&gt;
__TOC__&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Learn to connect an Android device and a PIC32 microprocessor. This guide was developed with the NU32 (2012 version). &lt;br /&gt;
&lt;br /&gt;
== Getting Started ==&lt;br /&gt;
&lt;br /&gt;
=== Things you need ===&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll need the following hardware:&lt;br /&gt;
&lt;br /&gt;
* A PIC32 microprocessor (i.e. the NU32 development board)&lt;br /&gt;
* An Android device running at least version 2.2&lt;br /&gt;
* USB cables:&lt;br /&gt;
** Type-A to Mini-B cable (computer to PIC32)&lt;br /&gt;
** Type-A to Micro-B cable (computer to Android device)&lt;br /&gt;
** A spare USB cable with a Micro-B end (Android device to PIC32)&lt;br /&gt;
&lt;br /&gt;
and the following software:&lt;br /&gt;
&lt;br /&gt;
* Microchip&amp;#039;s MPLAB X with the C32 compiler (download from [http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads Microchip&amp;#039;s website])&lt;br /&gt;
* Eclipse (download from the Eclipse project&amp;#039;s [http://www.eclipse.org/downloads/ website] -- Eclipse Classic recommended)&lt;br /&gt;
* Android SDK (download from the [http://developer.android.com/sdk/index.html Android website] and follow the instructions to link it to Eclipse)&lt;br /&gt;
&lt;br /&gt;
It&amp;#039;s optional, but you&amp;#039;ll want the HD44780, a 2 by 16 character LCD with libraries available for the PIC32. &lt;br /&gt;
&lt;br /&gt;
In addition, you&amp;#039;ll need the starter code associated with this documentation, FILE_LINK_HERE. The code includes the &amp;#039;&amp;#039;&amp;#039;String Passing Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that passes raw sequences of bytes back and forth, interpreted as strings. There&amp;#039;s also the &amp;#039;&amp;#039;&amp;#039;Android Sensor Demo&amp;#039;&amp;#039;&amp;#039;, a firmware/application pair that shows you how to access the GPS, camera, touchscreen, and microphone of the Android device to collect data that may be useful for the PIC.&lt;br /&gt;
&lt;br /&gt;
The PIC32 firmware is written in C and compiled with MPLAB X and C32; the Android applications are written in Java and are built with Eclipse. Be sure to take advantage of the features in MPLAB X and Eclipse. These IDEs can quickly rename variables (Ctrl-R in MPLAB X, Alt-Shift-R in Eclipse), jump to where a function is defined (hold Ctrl and click), and perform other refactoring that will save you time when programming.&lt;br /&gt;
&lt;br /&gt;
=== Accessing USB on the PIC ===&lt;br /&gt;
&lt;br /&gt;
First, you&amp;#039;ll need a USB cable to connect your Android device to the PIC. You&amp;#039;ll probably need the following: &lt;br /&gt;
&lt;br /&gt;
* Spare USB cable with a Micro-B end&lt;br /&gt;
* (2) capacitors in the 1 uF range&lt;br /&gt;
* Soldering iron&lt;br /&gt;
* Red, black, green, and white wire&lt;br /&gt;
* Electrical tape&lt;br /&gt;
&lt;br /&gt;
Cut the non-Micro-B end off your USB cable. Inside the outer insulation, you should see four wires: a red wire (VBUS), a black wire (GND), a green wire (D+), and a white wire (D-). Solder each stranded wire to a solid wire of the same color, and wrap the joints in electrical tape so they don&amp;#039;t accidentally bump into each other. &lt;br /&gt;
&lt;br /&gt;
On the NU32 board, you&amp;#039;ll need to solder wires to the VBUS and VUSB pads right below the microprocessor. &lt;br /&gt;
&lt;br /&gt;
Now, you&amp;#039;ll need to wire everything up. Here&amp;#039;s the diagram from the PIC32MX reference manual: [[Media:Pic32_usb_wiring.pdf]]. We can ignore VBUSON and wire VBUS directly to 5V. D+ is G2 and D- is G3 on the NU32. When you power on your development board and connect your Android device, the device should begin charging. &lt;br /&gt;
&lt;br /&gt;
== String Passing Demo ==&lt;br /&gt;
&lt;br /&gt;
Unzip the folder you downloaded earlier. From the applications folder, import String Passing Demo into Eclipse. From the firmware folder, import String Passing Demo into MPLAB X. Connect your Android device to your computer and write the program onto the device by pressing the green play button. In MPLAB X, build the firmware and write the .hex file onto the PIC. &lt;br /&gt;
&lt;br /&gt;
Start the String Passing Demo on your Android device. Then turn on the PIC and connect it. If you see &amp;quot;Hello from PIC32!&amp;quot; on your Android device, congratulations!&lt;br /&gt;
&lt;br /&gt;
=== Firmware ===&lt;br /&gt;
&lt;br /&gt;
The firmware consists of initialization code and a while loop with a state machine. When the phone is connected to the PIC, the state is STATE_CONNECTED. The code under the STATE_CONNECTED case transmits a string to the Android device if the user button has been pressed or released. When data is received, the function ADBCallback is called. The ADBCallback for the String Passing Demo displays any bytes received as a string on the LCD. &lt;br /&gt;
&lt;br /&gt;
=== Application ===&lt;br /&gt;
&lt;br /&gt;
The main parts of the application code are StringPassing.java inside &amp;lt;tt&amp;gt;src/lims.stringpassing.app&amp;lt;/tt&amp;gt; and main.xml inside &amp;lt;tt&amp;gt;res/layout&amp;lt;/tt&amp;gt;. There&amp;#039;s also code that manages the connection inside &amp;lt;tt&amp;gt;src/lims.stringpassing.server&amp;lt;/tt&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
StringPassing.java starts with the declaration of several variables: Buttons, TextViews, etc. Inside &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt;, these variables are linked to parts of the layout in main.xml. Most of the code describes listeners - basically, code that is executed in response to an event like a button press or when data is received. &lt;br /&gt;
&lt;br /&gt;
== Android Sensor Demo ==&lt;br /&gt;
&lt;br /&gt;
This code contains demonstrations of four Android sensors: the GPS/location service, the camera, the touchscreen, and the microphone. &lt;br /&gt;
&lt;br /&gt;
=== GPS demo ===&lt;br /&gt;
&lt;br /&gt;
The location demonstration will try to determine the device&amp;#039;s current location. It will transmit the coordinates to the PIC and the PIC will return the location directly across the world (the antipode). For Chicago, this is southwest of Australia in the ocean. &lt;br /&gt;
&lt;br /&gt;
=== Camera demo ===&lt;br /&gt;
&lt;br /&gt;
The camera demo captures a 640 by 480 image. It saves the image as a .jpeg, then creates a 40 by 30 pixel version and sends that to the PIC in a 3600-byte array of RGB values. The PIC inverts the RGB data and sends the sample back.&lt;br /&gt;
&lt;br /&gt;
=== Touchscreen demo ===&lt;br /&gt;
&lt;br /&gt;
The touchscreen demonstration detects where you&amp;#039;ve touched the screen and indicates this with the purple cursor. It scales each of the touch coordinates to a float between 0 and 1 and sends this pair of floats to the PIC. (That&amp;#039;s why the integer coordinates displayed on the PIC&amp;#039;s LCD aren&amp;#039;t exact.) Every time the PIC receives data, it responds with coordinates for the orange cursor, which is moved around randomly.&lt;br /&gt;
&lt;br /&gt;
=== Microphone demo ===&lt;br /&gt;
&lt;br /&gt;
The microphone demo captures data from the Android device&amp;#039;s microphone but doesn&amp;#039;t do much with it. The level bar in the application shows the maximum amplitude found in the previous sample. It sends up to 100 bytes of each sample to the PIC, which comments on the loudness.&lt;br /&gt;
&lt;br /&gt;
== Basics of Android programming ==&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Activity&amp;#039;&amp;#039;&amp;#039; is the basic building block of an Android application. There&amp;#039;s an Activity for each screen in an app. The String Passing Demo is an application that consists of a single Activity because there&amp;#039;s only one screen. The Android Sensor Demo has one Activity for the menu and four Activities for the four sensor demos. To create your own activity, you create ClassName.java file containing a subclass of Android&amp;#039;s &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class. In the String Passing Demo, the file is called &amp;lt;tt&amp;gt;StringPassing.java&amp;lt;/tt&amp;gt; and the class declaration is &amp;lt;tt&amp;gt;public class StringPassing extends Activity&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Inside the &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class, there are functions that are called at different parts of the Activity&amp;#039;s life (for a diagram, see [http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle Activity Lifecycle] in the documentation). When the Activity is started by the user, a method called &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is called. When the user closes the Activity by pressing the back button, the &amp;lt;tt&amp;gt;onDestroy&amp;lt;/tt&amp;gt; method is called. For example, here&amp;#039;s some code from the String Passing Demo:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public void onCreate(Bundle savedInstanceState) {&lt;br /&gt;
    super.onCreate(savedInstanceState);&lt;br /&gt;
    setContentView(R.layout.main);&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;tt&amp;gt;@Override&amp;lt;/tt&amp;gt; designation means we&amp;#039;re overriding the base &amp;lt;tt&amp;gt;Activity&amp;lt;/tt&amp;gt; class&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function. For things to work properly, the superclass&amp;#039;s &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function still needs to be called; that&amp;#039;s what &amp;lt;tt&amp;gt;super.onCreate(savedInstanceState)&amp;lt;/tt&amp;gt; does. The &amp;lt;tt&amp;gt;savedInstanceState&amp;lt;/tt&amp;gt; is a structure in which you can save variables. We&amp;#039;re not going to worry about it. The final line, &amp;lt;tt&amp;gt;setContentView(R.layout.main)&amp;lt;/tt&amp;gt;, tells Android that the application should display the &amp;lt;tt&amp;gt;main.xml&amp;lt;/tt&amp;gt; layout. The rest of &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; is code that we want to execute at the start of the Activity.&lt;br /&gt;
&lt;br /&gt;
An application&amp;#039;s layout files are found in &amp;lt;tt&amp;gt;res/layouts&amp;lt;/tt&amp;gt; in the Eclipse Package Explorer. A layout file is an xml file describing how text fields, buttons, checkboxes, and other widgets will be laid out on the screen. Eclipse gives you a friendly drag-and-drop interface with which to create these.&lt;br /&gt;
&lt;br /&gt;
Of course, we need to connect the layout widgets to our application code so we can control their functionality. In the String Passing Demo, you&amp;#039;ll see&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class StringPassing extends Activity {&lt;br /&gt;
&lt;br /&gt;
    Button sendButton;&lt;br /&gt;
    Button clearButton;&lt;br /&gt;
    ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and later, in the &amp;lt;tt&amp;gt;onCreate&amp;lt;/tt&amp;gt; function,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sendButton = (Button) findViewById(R.id.button1);&lt;br /&gt;
clearButton = (Button) findViewById(R.id.button2);&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This associates the buttons in the layout with variables (here, &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt;). Now, we can call &amp;lt;tt&amp;gt;sendButton.setOnClickListener&amp;lt;/tt&amp;gt; to execute certain code when &amp;lt;tt&amp;gt;sendButton&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;clearButton&amp;lt;/tt&amp;gt; is pressed by the user.&lt;br /&gt;
&lt;br /&gt;
To learn more, see the official [http://developer.android.com/guide/components/index.html documentation].&lt;br /&gt;
&lt;br /&gt;
== Communication overview ==&lt;br /&gt;
[[Image:Communication_overview.png|thumb|400 px|A basic look at Android-PIC32 communication using ADB. The dotted arrows represent the functions used in the firmware and application code to transfer data. The solid arrows represent the actual data path.|right]]&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s a little bit about how we&amp;#039;re transferring data. &lt;br /&gt;
&lt;br /&gt;
We&amp;#039;re using a protocol based on the [http://developer.android.com/tools/help/adb.html Android Debug Bridge] (ADB). ADB is a tool that&amp;#039;s usually used for development of Android applications. Google has released the Open Accessory protocol for communication with Android USB accessories, but it&amp;#039;s only guaranteed to be supported on devices running Android 3.1 or above, where ADB is compatible with devices running Android 1.6 or above. &lt;br /&gt;
&lt;br /&gt;
In this code, the Android device acts as a TCP server. On the PIC, we&amp;#039;re able to abuse ADB to send arbitrary sequences of bytes to an Android device. The Android side uses ADB libraries to accept incoming connections from the PIC and to send data to the PIC. All of this runs on top of Microchip USB libraries for the PIC and Linux USB drivers used by the Android operating system. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
Everything should work as downloaded. If you&amp;#039;re getting errors, the first thing to do is to make sure you&amp;#039;ve installed all of the correct programs. &lt;br /&gt;
&lt;br /&gt;
In MPLAB X, there&amp;#039;s a very specific compiler configuration for the firmware projects. Right click the project name in the right toolbar and choose Properties. The compiler toolchain should be C32 (v2.02 when this was written). Under the tab at left called C32 (Global Options), you&amp;#039;ll see &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt;. Under &amp;lt;tt&amp;gt;pic32-as&amp;lt;/tt&amp;gt;, only &amp;quot;Have symbols in production build&amp;quot; should be checked. Under &amp;lt;tt&amp;gt;pic32-gcc&amp;lt;/tt&amp;gt;, &amp;quot;Have symbols in production build&amp;quot; should be checked too. The &amp;quot;Preprocessor macros&amp;quot; should appear as &amp;lt;tt&amp;gt;PLATFORM=400;NDEBUG&amp;lt;/tt&amp;gt;. The include directories should reference all of the libraries and should be the following list:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
..\_Libraries\libadb&lt;br /&gt;
..\_Libraries\libconn&lt;br /&gt;
..\_Libraries\libusb&lt;br /&gt;
..\_Libraries\libbtstack\src&lt;br /&gt;
..\_Libraries\libbtstack\include\btstack&lt;br /&gt;
..\_Libraries\libbtstack&lt;br /&gt;
..\_Libraries\microchip\common&lt;br /&gt;
..\_Libraries\microchip\usb&lt;br /&gt;
..\_Libraries\microchip\include&lt;br /&gt;
..\_Libraries\microchip\include\USB&lt;br /&gt;
..\_Libraries\common&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of this list, you shouldn&amp;#039;t need to import the library projects into MPLAB X. Doing so may introduce problems. Under the &amp;lt;tt&amp;gt;pic32-ld&amp;lt;/tt&amp;gt; tab, the heap size should be 3000 bytes and &amp;quot;Remove unused sections&amp;quot; should be checked. &lt;br /&gt;
&lt;br /&gt;
Each of the firmware projects includes &amp;lt;tt&amp;gt;procdefs.ld&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NU32.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;NU32.c&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;LCD.h&amp;lt;/tt&amp;gt;/&amp;lt;tt&amp;gt;LCD.c&amp;lt;/tt&amp;gt; libraries meant for the NU32. If you aren&amp;#039;t using the NU32, remove these files and their references in the code. &lt;br /&gt;
&lt;br /&gt;
If you want to use XC32 compiler (recently released when this was written), you&amp;#039;ll need to right click the project name in the left toolbar, select Properties, and switch to the XC32 compiler toolchain. You&amp;#039;ll have to do this for the firmware (Android Sensor Demo or String Passing Demo) as well as for each of the four libraries and patch up the compiler errors. It&amp;#039;s best to just download and use the C32 compiler.&lt;br /&gt;
&lt;br /&gt;
In Eclipse: if you&amp;#039;re getting compiler errors with the projects, right click the project name at left &amp;gt; Android Tools &amp;gt; Fix Project Properties. If there are still errors, go to right click &amp;gt; Properties and check the following. In the Android tab, make sure you&amp;#039;re building for Android 2.3.3 or for a version that&amp;#039;s at least 2.2. In the Java compiler tab, make sure the compiler compliance level is 1.6.&lt;br /&gt;
&lt;br /&gt;
== Credits ==&lt;br /&gt;
&lt;br /&gt;
The firmware uses Ytai Ben-Tsvi&amp;#039;s libadb, libbtstack, libconn, and libusb libraries from the [https://github.com/ytai/ioio/wiki ioio project]. The application code uses server libraries from [http://code.google.com/p/microbridge/ microbridge].&lt;/div&gt;</summary>
		<author><name>ShengWu</name></author>
	</entry>
</feed>