Introduction
The PEX header exposes three of the four pins of USART0. Those can be used to communicate with other serial devices, most notably including another Pokitto for building 2-player games. Very little setup is required for this, either in hardware or software.
Components
The simplest test is a loopback test with a single Pokitto, and all that takes is a single breadboard jumper cable. Beyond that, you’ll need another device with a serial interface. The simplest setup is with a second Pokitto (note: I only have one Pokitto, so haven’t tested this, but document it here anyway).
A more complex alternative is to use a USB/serial adapter to connect to a USB port on a PC or mobile device. These are readily available from Amazon, or more cheaply (though probably more slowly) from ebay. This should be for a TTL-level output, not an RS-232 level output.
Finally, you’ll need three breadboard jumper cables. One end should be male to plug into the Pokitto, and the other end of the appropriate gender to plug into the other device, so male to male to connect two Pokitto. The TTL/RS-232 converter will need a fourth jumper cable.
Hardware connections
First, some warnings:
- These setups are for testing. A connection you are using regularly should use a hat of some kind, and Pokitto-to-Pokitto connections need some buffering compnents.
- Make sure any other devices are 3.3V, not 5V. Using a 5V device could damage your Pokitto.
- Power down everything while you are connecting things.
- Power up the other device first. The Pokitto can handle this, and the other device may not.
For a loopback test, connect EXT6 to EXT7 on your Pokitto.
If you’re connecting two Pokitto (lucky you!), then connect EXT6 on one to EXT7 on the other, and vice versa. Use the last cable to connect Ground to Ground.
To connect to a USB/Serial converter, connect EXT7 to the Rx pin, EXT6 to the Tx pin and Ground to Ground.
This image shows a Pokitto connected to a USB. The black cable is Ground, green is Pokitto Tx to converter Rx, and the orange is Pokitto Rx to converter Tx.
Software
The demo program is a simple “terminal” emulator. It prints incoming characters to the Pokitto display, and reports button presses/releases to the other end. If you are using mbed online, you can copy it from my program page. Otherwise, you can start with the source here:
/*
* Serial PEX header demo.
*/
#include "mbed.h"
#include "Pokitto.h"
Pokitto::Core demo;
Serial ser(EXT7, EXT6);
struct {
uint8_t button;
char *name;
} buttons[] = {
{BTN_A, "Button A"},
{BTN_B, "Button B"},
{BTN_C, "Button C"},
{BTN_UP, "Up button"},
{BTN_DOWN, "Down button"},
{BTN_LEFT, "Left button"},
{BTN_RIGHT, "Right button"}
};
int main () {
demo.begin();
demo.display.persistence = 1;
demo.display.clear();
ser.baud(115200);
while (demo.isRunning()) {
if (demo.update()) {
while (ser.readable()) {
demo.display.write(ser.getc());
}
for (uint8_t i = 0; i < NUM_BTN; i += 1) {
if (demo.buttons.pressed(buttons[i].button)) {
ser.printf("%s pressed\r\n", buttons[i].name);
} else if (demo.buttons.released(buttons[i].button)) {
ser.printf("%s released\r\n", buttons[i].name);
}
}
}
}
}
The serial interface is ser
. The interface used is baud
to set the baud rate, readable
to see if there are any characters available, getc
to read those characters, and finally printf
to report button up and down events.
Build the project, or downloadmy build (35.9 KB) and install it on your Pokitto.
Loopback test with one Pokitto
This looks a lot like a simple button test. You press buttons on the Pokitto, and strings announcing the press and release events are displayed on the screen. The Pokitto display code and button handling code don’t quite agree with each other, so you don’t get proper newlines.
Testing with second Pokitto
Just start the two connected Pokitto once the demo is installed on both. If everything is connected properly, buttons pressed on one Pokitto will be reported on the other. Yes, it’s just that easy!
Testing
We’re going to use the Arduino serial monitor for testing. Not only is it cross-platform, working the same on Windows, the Mac and Linux, but it may be familiar to many of you already.
If you don’t already have it, you can download the Arduino application from arduino.cc. Download the IDE, don’t use the web app.
Now start Arduino application. Under the “Tools” menu, check the “Port” menu for a list of serial ports. On Linux, you’re looking for something of the form /dev/ttyUSB#, where the # is normally 0, but may be higher than that if you’ve been playing with USB converters. On a Mac, it’s of the frorm /dev/tty.subserial-########, where the last bit depends on the chip you are using. On Windows, it will be COM#. If you’re using a TTL serial converter, it’ll be something different on the Mac and Linux. If there’s more than one name that matches the pattern you’re looking for, you’ll have to figure out which one is the Pokitto. Unplugging it and seeing which one vanishes works very well.
Here you can see the Arduino IDE with the two menus you need to deal with highlighted in red.
Once you’ve identified the proper port, select it. Then select “Serial Monitor” under the “Tools” menu to open the serial monitor. At the bottom right are a pair of drop-down menus. Set the “Line ending” one to “Both NL & CR”, and set the other to “115200 baud”. Check “Autoscroll” on the bottom left.
And here is the serial monitor window showing the correct settings, again highlighted in red.
Try pressing buttons on the Pokitto. You should see reports about button’s being pressed and released show up in the monitor. Now type some text into the line above that, and hit “Send”. It should show up on the Pokitto. If neither work, try swapping the cables going to EXT6 and EXT7 - you may have gotten them backwards.
Testing with Android
If you have an Android device with OTG usb support and a USB convertern, you can use it to test as well. Install the serial usb terminal from the play store. Connect the Pokitto and the USB converter, connect the converter to your Phone, then start the Pokitto and the app. Select “USB Device” from the menu, and choose your device - there should only be one. Under the “Setting” menu, set the baud rate to 115200, then make sure it’s set to 8 bits, 1 stop bit, no parity and Send is “CR+LF”.
Now select “Terminal” from the menu, and if the plug icon on the right - left of the trash can - isn’t connected, tap it to connect. Once it’s connected, try pushing buttons on the Pokitto. You should get reports in the terminal window. If that works, try typing some text into the input line at the bottom, and hitting the airplane to send it to the Pokitto. That text should show up on the Pokitto display. If neither works, try swapping the cables going to EXT6 and EXT7 - you may have them backwards.
Some Alternatives for testing
If your PC has an RS-232 output, you can also get a TTL to RS-232 converter to use instead. The setup on your PC will be nearly identical to the USB converter but with a different device name. You will need to provide power to the converter
from the Pokitto.
There are also Bluetooth/Serial (SPP) adapters. Setting those up is much more difficult, as you need to get a pair of wireless devices talking to each other. But it does provide a wireless connection, so cool. Like the RS-232 converters, you’ll need to provide the adapter with power from the Pokitto.
If you’re interested on more information about either of these, please ask in the forums.