Berkeley Hackathon 2009
My buddy Chase and I just won the UC Berkeley Hackathon 2009, and got some new laptops out of the deal. This isn’t really related to code, but I thought I’d earned some bragging rights. Our project was a widgeting API that allowed for rendering to both wxWidgets and XHTML controls, allowing a sort of remote desktop in your browser. Check out the video of our demonstration here. We plan to further develop the project elsewhere.
Sneaky MacBook and MacBook Pro Battery Replacement Program
This isn’t really a technical post, but my ~2 year old MacBook Pro (which has no warranty coverage whatsoever) decided to have the battery go kaput. Specifically, at 153 charge cycles, the battery capacity was only 40% of the original. I figured I was shit out of luck, and that I’d have to shell out $129 for a new battery, but after some digging, I found this article tucked away on Apple’s site:
http://www.apple.com/support/macbook_macbookpro/batteryupdate/
Specifically, if you have a MacBook or MacBook Pro built between February 2006 and April 2007, and your battery has gone to shit, you can take it to the Apple retail store and get a new one, free of charge, even if you’re out of warranty. Naturally the people at the Genius Bar didn’t believe me and told me to buy a new battery, but after I showed them that article, they changed their tune and brought me a new battery right away. Anyway, I just thought I’d mention this in case anyone else out there was considering buying a new battery to replace their failing Apple battery.
Reading and Parsing USB GPS Data in C (with NmeaLib)
I’m currently working on a project that requires GPS data, and need a way to retrieve data from a USB-based GPS receiver I purchased (a USGlobSat BU-353 USB GPS receiver). Fortunately, most GPS receivers, particularly USB ones, support the NMEA 0183 protocol. NMEA 0183 is a serial-based protocol was developed by the National Marine Electronics Association and is used to collect data from a variety of aquatic electronic devices (such as sonar, GPS, and others). Most USB-based GPS receivers have a serial-to-USB converter built in, which allows data to be read from a device as if it were a serial device. For instance, the BU-353 shows up in Mac OS X as /dev/cu.usbserial. If you plug in your GPS device and boot up minicom, you’ll see output that looks a lot like this
Obviously the strings are somewhat cryptic, but you can discern some data among them that looks like GPS data. Unfortunately the protocol is not open, and costs at least $270 to buy from the NMEA. Fortunately, most of it has been reverse engineered, and there is a great library which already exists to parse these strings into useable data. It is called NmeaLib and it is released under the LGPL. The way it works is that you first initialize it, then just feed it every string that the GPS device feeds you over the serial device, and it will interpret the data and keep a cumulative collection of all available data from the GPS receiver. In addition to the basic latitude and longitude data, it will also report the number of satellites in view, the number satellites being used, the signal strengths of both, as well as the current GPS time and many other useful pieces of information.
The big remaining task once you have the library is to read from the GPS serial device. Reading from a serial device might seem like it would be as simple as reading from a file (and in some ways it is), but because serial port data can come at various rates and in various formats (for instance it might come at 9600 Baud with no parity bit and with 8 bits of data), there are some other calls you need to make to set the various serial settings.
To begin with, we need to get a file descriptor for the serial port. This can be done simply with
You will also need some of the structures and functions included in termios.h, the POSIX terminal definitions, in order to set the appropriate terminal settings.
struct termios origPortOptions;
//Get the existing options so they can be restored
tcgetattr(gpsFd, &origPortOptions);
bzero(&options, sizeof(options));
//Set the BAUD rate to NMEA specifications
cfsetospeed(&options, B4800);
options.c_cflag |= (CLOCAL | CREAD);
//Settings equivalent to 8N1
options.c_cflag &= ~PARENB; //Disabled parity
options.c_cflag &= ~CSTOPB; //Set 1 stop bit (| would give us 2 stop bits)
options.c_cflag |= CS8; //Set character size to 8 bits
options.c_cflag |= CRTSCTS; //Enable hardware flow control
options.c_oflag |= (OPOST | ONLCR); //Processed output and mapping of newlines to CR-LF pairs
tcsetattr(gpsFd, TCSAFLUSH, &options); //Flush buffers and apply the new settings
After that, you can read from gpsFd like any other file descriptor. If you want to get the number of bytes waiting in the buffer to be read, you can use ioctl() as follows:
//Get the number of bytes waiting in the serial buffer
ioctl(gpsFd, FIONREAD, &bytesInBuffer);
I have attached a simple library (with NmeaLib included) that you can use in your own projects (assuming your serial or USB GPS device uses the same data settings). To use it, you simply call initializeGps(), then get the most recent GPS info by calling getGpsInfo() (which returns an NmeaLib structure). Once you’re finished retrieving GPS data, call closeGps() to free up the serial port and restore its previous settings.
The library uses setitimer() to install an alarm that will call a read handler that will pull all data waiting in the serial buffer and then parse it using NmeaLib. You can change the update frequency if you like, but it is already much faster than it really needs to be, and I’d recommend just leaving it alone as it is very low overhead.
I’ve also included a test program that shows how to use the code in gps.h/.c. Its output looks something like the following:
Yeah so now you know where I live, but I suppose a determined individual could get that from a domain registrar anyway.
Feel free to reuse this code however you see fit, although if you do, some credit is always appreciated. Have fun building those autonomous spy blimps.
How to Monitor Parallel Port Interrupts in User Space in Linux
DISCLAIMER: A computer is not a cheap device and home-made electronics are not always reliable. I take no responsibility for damage caused by you sticking shit into your parallel port or anywhere else on your computer to see what happens. I also take no responsibility for any side-effects of the following code. That said, if you’re careful, the parallel port can be a very useful device for interfacing your computer with external devices, and the following code is working fine for me.
Anyway, I’m guessing you want to install an interrupt handler for the parallel port, but aren’t a kernel hacker and don’t want to read a ton of documentation? Well, fortunately I had the same issue and was able to get something similar working. Unfortunately dealing with the parallel port in user space isn’t exactly the same as writing a kernel module, and there are a few important things to remember:
- Code which accesses the parallel port must be run as root
- Only one piece of code which uses this method may listen for interrupts at a time
- The parallel port cannot be sharing an IRQ (although this shouldn’t be an issue since the parallel port usually has a dedicated IRQ anyway, number 5 or 7 most of the time)
So without further ado, here is what I’ve got:
#include <linux/parport.h>
#include <stropts.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#define PP_DEV_NAME "/dev/parport0"
int main()
{
//Get a file descriptor for the parallel port
int ppfd;
ppfd = open(PP_DEV_NAME, O_RDWR);
if(ppfd == -1)
{
printf(“Unable to open parallel port!\n“);
return 1;
}
//Have to claim the device
if(ioctl(ppfd, PPCLAIM))
{
printf(“Couldn’t claim parallel port!\n“);
close(ppfd);
return 1;
}
while(1)
{
//Set up the control lines for when an interrupt happens
int int_count;
int busy = PARPORT_STATUS_ACK | PARPORT_STATUS_ERROR;
int acking = PARPORT_STATUS_ERROR;
int ready = PARPORT_STATUS_ACK | PARPORT_STATUS_ERROR;
char int_value;
ioctl(ppfd, PPWCTLONIRQ, &busy);
//Let ppdev know we’re ready for interrupts
ioctl(ppfd, PPWCONTROL, &ready);
//Wait for an interrupt
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(ppfd, &rfds);
if(select(ppfd + 1, &rfds, NULL, NULL, NULL))
{
printf(“Received interrupt\n“);
}
else
continue; //Caught a signal
//Fetch the associated data
ioctl(ppfd, PPRDATA, &int_value);
//Clear the interrupt
ioctl(ppfd, PPCLRIRQ, &int_count);
if(int_count > 1)
printf(“Uh oh, missed %i interrupts!\n“, int_count - 1);
//Acknowledge the interrupt
ioctl(ppfd, PPWCONTROL, &acking);
usleep(2);
ioctl(ppfd, PPWCONTROL, &busy);
}
return 0;
}
As you can see, what you’re essentially doing is just opening the parallel port device like any old file and then calling select() to wait until an interrupt (or some other signal) happens. Here all I’m doing is writing a line to the console whenever an interrupt is received, but of course you can do anything (so long as the interrupt handler is fast enough to deal with your interrupt frequency). There are also a few ioctl() calls for claiming the port and clearing interrupts, but for the most part, the comments are self-explanatory and the code can just be copied into your project.
There is of course one fundamental issue with this code, and that is that it blocks until an interrupt is received. My suggestion, and the way I use this code, is to launch off another thread that runs this code and just sits dormant until an interrupt occurs. This seems to be relatively low overhead and pretty reliable. I’ve avoided giving the sample code in a threaded example so that it’s easy to extract and use in your own projects. Feel free to email me if you have a question or suggestion.
Also, in case you’re wondering how to trigger a parallel port interrupt, it’s triggered on the rising edge (or on some boards the falling edge) of a signal on pin 10, so just short pin 10 to ground (pins 18-15) and you should see an interrupt triggered (it may be triggered when you unshort it).
Here is a zipped copy of the code for your viewing/programming pleasure:
Sample Code for Parallel Port Interrupt Handling in Linux
keep looking »
