/* * Simple real-time data plotter for byte-oriented RS232 data sources * * Copyright (C) 2012 Claire Xenia Wolf * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #include #include #include #include #include #include struct { char *name; int flag; } speeds[] = { {"50", B50}, {"75", B75}, {"110", B110}, {"134", B134}, {"150", B150}, {"200", B200}, {"300", B300}, {"600", B600}, {"1200", B1200}, {"2400", B2400}, {"4800", B4800}, {"9600", B9600}, {"19200", B19200}, {"38400", B38400}, {"57600", B57600}, {"115200", B115200}, {"230400", B230400}, {"460800", B460800}, {"500000", B500000}, {"576000", B576000}, {"921600", B921600}, {"1000000", B1000000}, {"1152000", B1152000}, {"1500000", B1500000}, {"2000000", B2000000}, {"2500000", B2500000}, {"3000000", B3000000}, {"3500000", B3500000}, {"4000000", B4000000}, {NULL, 0} }; SDL_Surface *screen; #define MAX_NUM_SAMPLES 1024 uint8_t buffer[MAX_NUM_SAMPLES]; int num_samples = MAX_NUM_SAMPLES; int idx = 0; void update_sdl() { struct SDL_Rect rect = { 0, 0, num_samples, 256 }; SDL_FillRect(screen, &rect, 0x00000000); rect.x = idx, rect.y = 0, rect.w = 1, rect.h = 256; SDL_FillRect(screen, &rect, 0x00888888); for (int i = 0; i < num_samples; i++) { rect.x = i, rect.y = 255-buffer[i], rect.w = 1, rect.h = buffer[i]+1; SDL_FillRect(screen, &rect, i == idx ? 0x00ff8888 : 0x00ff0088); } SDL_UpdateRect(screen, 0, 0, num_samples, 256); } int main(int argc, char **argv) { int opt; int zero_reset = 0; while ((opt = getopt(argc, argv, "n:z")) != -1) { switch (opt) { case 'n': num_samples = atoi(optarg); if (num_samples < 1) goto usage; if (num_samples > MAX_NUM_SAMPLES) num_samples = MAX_NUM_SAMPLES; break; case 'z': zero_reset = 1; break; default: goto usage; } } if (optind + 2 != argc) { usage: fprintf(stderr, "Usage: %s [-n N] [-z] tty-dev baud-rate\n", argv[0]); return 1; } SDL_Init(SDL_INIT_VIDEO); SDL_WM_SetCaption("RS232 Plot", "RS232 Plot"); screen = SDL_SetVideoMode(num_samples, 256, 32, SDL_SWSURFACE); if (screen == NULL) { fprintf(stderr, "Failed to init SDL!\n"); return 1; } int fd = open(argv[optind], O_RDONLY); if (fd < 0) { fprintf(stderr, "Failed to open device `%s'!\n", argv[1]); return 1; } struct termios oldtio; tcgetattr(fd, &oldtio); struct termios newtio = oldtio; newtio.c_cflag = CS8 | CREAD; newtio.c_iflag = IGNPAR; newtio.c_oflag = 0; newtio.c_lflag = 0; newtio.c_cc[VMIN] = 1; newtio.c_cc[VTIME] = 0; for (int i = 0; 1; i++) { if (speeds[i].name == NULL) { fprintf(stderr, "Unknown baud rate `%s'!\n", argv[optind+1]); return 1; } if (strcmp(argv[optind+1], speeds[i].name) == 0) { newtio.c_cflag |= speeds[i].flag; break; } } tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &newtio); uint8_t sample = 0; while (read(fd, &sample, 1) == 1) { SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) goto finish; if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_q) goto finish; } if (sample == 0 && zero_reset) { while (idx > 0) { buffer[idx] = 0; idx = (idx + 1) % num_samples; } } else { buffer[idx] = sample; idx = (idx + 1) % num_samples; } update_sdl(); } finish: tcsetattr(fd, TCSANOW, &oldtio); close(fd); return 0; }