Skip to content

Instantly share code, notes, and snippets.

@teknoman117
Created March 17, 2015 08:26
Show Gist options
  • Save teknoman117/be4a03faf511d46b01d8 to your computer and use it in GitHub Desktop.
Save teknoman117/be4a03faf511d46b01d8 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstring>
#include <cerrno>
#include <vector>
#include <unistd.h>
#include <termios.h>
#include <linux/input.h>
#include <sys/ioctl.h>
#include <dispatch/dispatch.h>
using namespace std;
int main (int argc, char** argv)
{
// Open the joystick
int fd = open("/dev/kybernetes/gps", O_RDONLY);
if(fd < 0)
{
cerr << "Unable to open GPS" << endl;
return 1;
}
// Configure the serial port for GPS settings (9600 8N1)
struct termios settings;
tcgetattr(fd, &settings);
// Set the baudrate
cfsetispeed(&settings, B9600);
cfsetospeed(&settings, B9600);
// Set the port flow options
settings.c_cflag &= ~(PARENB | CSTOPB | CSIZE);
settings.c_cflag |= (CLOCAL | CREAD | CS8);
settings.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
settings.c_iflag &= ~(IXON | IXOFF | IXANY | INLCR | ICRNL);
settings.c_oflag &= ~(OPOST | ONLCR | OCRNL);
// Set the port settings
tcsetattr(fd, TCSANOW, &settings);
// Locate the high priority queue
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
// Create a libdispatch io channel for the joystick
dispatch_io_t serialport = dispatch_io_create(DISPATCH_IO_STREAM, fd, queue, ^(int error)
{
if(error)
{
cerr << "Got an error from GPS: " << error << " (" << strerror(error) << ")" << endl;
}
});
// The GPS sentence is always 57 bytes long
dispatch_io_set_low_water(serialport, 57);
dispatch_io_set_high_water(serialport, 57);
// Establish a read handler
/*dispatch_io_read(serialport, 0, -1, queue, ^(bool done, dispatch_data_t data, int error)
{
// Did we successfully get data?
if(data)
{
dispatch_release(data);
}
});*/
dispatch_async(queue, ^{
cout << "Hello, from a block in dispatch" << endl;
});
dispatch_main();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment