1
0
mirror of git://projects.qi-hardware.com/ben-blinkenlights.git synced 2024-11-23 20:33:45 +02:00

swuart-chat/chat.c: options -r and -t to select pins; bit rate argument

This commit is contained in:
Werner Almesberger 2013-01-16 12:07:27 -03:00
parent b8f2a8b764
commit 5be0b56aa1

View File

@ -16,6 +16,7 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <strings.h>
#include <termios.h> #include <termios.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
@ -24,8 +25,10 @@
#include <ubb/swuart.h> #include <ubb/swuart.h>
#define RX UBB_DAT0 #define DEFAULT_RX UBB_DAT0
#define TX UBB_DAT1 #define DEFAULT_TX UBB_DAT1
#define DEFAULT_BPS 38400
/* ----- TTY raw mode ------------------------------------------------------ */ /* ----- TTY raw mode ------------------------------------------------------ */
@ -69,10 +72,66 @@ static void at_exit(void)
} }
static uint32_t translate(const char *s)
{
if (!strcasecmp(s, "DAT0"))
return UBB_DAT0;
if (!strcasecmp(s, "DAT1"))
return UBB_DAT1;
if (!strcasecmp(s, "DAT2"))
return UBB_DAT2;
if (!strcasecmp(s, "DAT3"))
return UBB_DAT3;
if (!strcasecmp(s, "CMD"))
return UBB_CMD;
if (!strcasecmp(s, "CLK"))
return UBB_CLK;
if (!strcasecmp(s, "NONE"))
return 0;
fprintf(stderr, "unknown pin \"%s\"\n", s);
exit(1);
}
static void usage(const char *name)
{
fprintf(stderr, "usage: %s [-r rx_pin] [-t tx_pin] [bps]\n", name);
exit(1);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
uint32_t rx = DEFAULT_RX;
uint32_t tx = DEFAULT_TX;
int bps = DEFAULT_BPS;
uint8_t local[10], remote[100]; uint8_t local[10], remote[100];
int got, i; int got, i;
int c;
while ((c = getopt(argc, argv, "r:t:")) != EOF)
switch (c) {
case 'r':
rx = translate(optarg);
break;
case 't':
tx = translate(optarg);
break;
default:
usage(*argv);
}
switch (argc-optind) {
case 0:
break;
case 1:
bps = atoi(argv[optind]);
if (!bps)
usage(*argv);
break;
default:
usage(*argv);
}
if (ubb_open(0) < 0) { if (ubb_open(0) < 0) {
perror("ubb_open"); perror("ubb_open");
@ -80,7 +139,7 @@ int main(int argc, char **argv)
} }
atexit(at_exit); atexit(at_exit);
raw(); raw();
if (swuart_open(TX, RX, 38400) < 0) { if (swuart_open(tx, rx, bps) < 0) {
perror("swuart_open"); perror("swuart_open");
exit(1); exit(1);
} }