Initial commit
This commit is contained in:
commit
85b0d6b5ac
3 files changed
+133
No files matched your search
@@ -0,0 +1,72 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
|
||||
/*
|
||||
|
||||
baud - A tool to simulate a specified baud rate in a pipe.
|
||||
|
||||
I wrote this to be able to play some of the old VT100/ansi animations
|
||||
at the rate of your choice. Since most terminal emulator apps render
|
||||
the text as fast as the system can (which is effectively instantaneous),
|
||||
the animations do not actually animate. This allows you to add
|
||||
a throttle to view as if you were on an actual serial terminal.
|
||||
|
||||
It takes one parameter, which is the desired baud rate. If you do not
|
||||
specify one, it will use the default defined below. Extra parameters
|
||||
will be ignored.
|
||||
|
||||
-- Scott Garrett
|
||||
version 2022050401
|
||||
|
||||
*/
|
||||
|
||||
#define defaultbaudrate 9600
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int baudrate;
|
||||
int c;
|
||||
|
||||
if ( argc == 1 )
|
||||
baudrate = defaultbaudrate;
|
||||
else
|
||||
baudrate = atoi(argv[1]);
|
||||
|
||||
/* Cannot have a negative baud rate, and 0 makes no sense
|
||||
(and would cause a division by zero).
|
||||
*/
|
||||
if ( baudrate <= 0 )
|
||||
{
|
||||
printf("Invalid baud rate: %d\n", baudrate);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Convert the desired baud rate into nanoseconds */
|
||||
int baud = 10000000000 / baudrate;
|
||||
|
||||
/* Make sure the final value is in the valid range that nanosleep()
|
||||
supports.
|
||||
*/
|
||||
if ( baud < 0 || baud > 999999999 )
|
||||
{
|
||||
printf("Invalid baud rate: %d\n", baudrate);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Now that we know the desired baud rate delay in nanoseconds,
|
||||
load up the struct with the values. 0 seconds and the baud rate delay.
|
||||
*/
|
||||
struct timespec remaining, request = {0, baud};
|
||||
|
||||
while ((c = getchar()) != EOF)
|
||||
{
|
||||
/* Here we grab a character from stdin, write it, flush it out
|
||||
so it is seen immediately, then pause for "remaining" nanoseconds.
|
||||
*/
|
||||
putchar((char)c);
|
||||
fflush(stdout);
|
||||
int response = nanosleep(&request, &remaining);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user