sparkfun_accel.c from EmStar at Krugle
Show sparkfun_accel.c syntax highlighted
#include <libmisc/misc.h>
#include <sys/poll.h>
#include <sndfile.h>
struct sparkfun {
char start[2];
char number[2];
char x[2];
char y[2];
char z[2];
char end;
} __attribute__((packed));
int swap(char *x)
{
uint8_t *p = (uint8_t *)x;
int retval = p[0];
retval <<= 8;
retval |= p[1];
return retval;
}
int quit = 0;
void quitnow(int x) { quit = 1; }
int main(int argc, char **argv)
{
int fd;
char *outfile = misc_parse_out_option(&argc, argv, "outfile", 0);
char *port = misc_parse_out_option(&argc, argv, "port", 0);
if (port == NULL)
port = "/dev/ttyUSB0";
SNDFILE* sf = NULL;
if (outfile) {
SF_INFO sfinfo = {
samplerate: 500,
format: SF_FORMAT_WAV | SF_FORMAT_PCM_16,
channels: 3
};
sf = sf_open(outfile, SFM_WRITE, &sfinfo);
if (sf == NULL) {
elog(LOG_WARNING, "arg: %m");
exit(1);
}
}
signal(SIGINT, quitnow);
fd = open(port, O_RDWR);
if (misc_config_serial(fd, 57600, 0) < 0) {
elog(LOG_WARNING, "Unable to configure serial port: %m");
}
sleep(1);
write(fd, "S\n", 2);
char buf[128];
int buflen=0;
while (!quit) {
struct pollfd pfd = {
fd: fd,
events: POLLIN
};
poll(&pfd, 1, -1);
int status = read(fd, buf+buflen, sizeof(buf)-buflen);
if (status < 0) {
elog(LOG_WARNING, "error reading: %m");
}
else if (status == 0) {
elog(LOG_WARNING, "unexpected eof: %m");
}
else {
buflen += status;
while (buflen >= sizeof(struct sparkfun)) {
if (buf[0] != '#' && buf[1] != '@' && buf[10] != '$') {
elog(LOG_WARNING, "dropped char %d", buf[0]);
buflen--;
memmove(buf, buf+1, buflen);
}
else {
int x,y,z,count;
struct sparkfun *s = (struct sparkfun *)buf;
count = swap(s->number);
x = swap(s->x);
y = swap(s->y);
z = swap(s->z);
printf("%u %d %d %d\n", count, x-512, y-512, z-512);
fflush(stdout);
if (sf) {
short frame[3];
frame[0] = x; frame[1] = y; frame[2] = z;
sf_writef_short(sf, frame, 1);
}
buflen -= sizeof(struct sparkfun);
memmove(buf, buf + sizeof(struct sparkfun), buflen);
}
}
}
}
if (sf)
sf_close(sf);
return 0;
}
See more files for this project here
EmStar is a software system for developing and deploying wireless sensor networks involving Linux-based platforms. As the wireless sensor network community has attempted to deploy more complex designs---large-scale, long-lived systems that need self-organization and adaptivity---a number of difficult software design issues have arisen. Advances in software design have not kept pace with the capabilities of hardware. This is because designing for an adaptive, efficient, and useful sensor network has turned out to be surprisingly complex and difficult. EmStar is a Linux-based software framework, whose goal is to dramatically reduce this complexity, enabling work to be shared and reused, and simplifying and speeding the design of new sensor network applications.
Project homepage:
http://cvs.cens.ucla.edu/emstar/
Programming language(s): C,Shell Script
License: other
BUILD
logvehspd.c
map
motelog.c
readfreebob.c
snippets
sparkfun_accel.c
start.run
syncfiles.c