Initial commit of Pollcat library
This commit is contained in:
98
example/main.c
Normal file
98
example/main.c
Normal file
@@ -0,0 +1,98 @@
|
||||
/** Minimal terminal using pollcat - main loop
|
||||
*
|
||||
* This file is part of the Pollcat Library.
|
||||
* Copyright (C) 2022 Expatria Technologies Inc.
|
||||
* Contact: Morgan Hughes <morgan@expatria.ca>
|
||||
*
|
||||
* The Pollcat Library is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the the GNU Lesser General Public License as published by the Free
|
||||
* Software Foundation; either version 3 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 copies of the GNU General Public License and the GNU Lesser
|
||||
* General Public License along with the Pollcat Library. If not, see
|
||||
* https://www.gnu.org/licenses/
|
||||
*
|
||||
* vim:ts=4:noexpandtab
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include <pollcat.h>
|
||||
|
||||
#include "terminal.h"
|
||||
|
||||
|
||||
char *opt_node = "/dev/ttyUSB0";
|
||||
char *opt_speed = "115200N81";
|
||||
|
||||
|
||||
static void signal_fatal (int signum)
|
||||
{
|
||||
fprintf(stderr, "Fatal signal %d, exit loop\n", signum);
|
||||
pollcat_loop_exit();
|
||||
}
|
||||
|
||||
extern char *__progname;
|
||||
static void usage (void)
|
||||
{
|
||||
printf("Usage: %s [-t timeout] [-b speed] [node]\n"
|
||||
"-t timeout Timeout for poll() calls (ms)\n"
|
||||
"-b speed Speed/parity/data/stop bits (default 115200N81)\n"
|
||||
"node should be a serial device, such as /dev/ttyS0 or /dev/ttyUSB0\n",
|
||||
__progname);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
int opt;
|
||||
while ( (opt = getopt(argc, argv, "t:b:")) != -1 )
|
||||
switch ( opt )
|
||||
{
|
||||
case 't':
|
||||
pollcat_time_base = atoi(optarg);
|
||||
break;
|
||||
|
||||
case 'b':
|
||||
opt_speed = optarg;
|
||||
break;
|
||||
|
||||
default:
|
||||
usage();
|
||||
}
|
||||
|
||||
if ( optind < argc )
|
||||
opt_node = argv[optind];
|
||||
|
||||
if ( serial_init(opt_node, opt_speed) )
|
||||
{
|
||||
perror(opt_node);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ( tty_init() )
|
||||
{
|
||||
perror("tty_init");
|
||||
return 1;
|
||||
}
|
||||
|
||||
signal(SIGTERM, signal_fatal);
|
||||
signal(SIGINT, signal_fatal);
|
||||
|
||||
while ( pollcat_loop(NULL) )
|
||||
;
|
||||
|
||||
tty_done();
|
||||
serial_done();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user