mirror of
git://projects.qi-hardware.com/ben-blinkenlights.git
synced 2024-11-05 08:25:19 +02:00
114 lines
2.0 KiB
C
114 lines
2.0 KiB
C
|
|
/*
|
|
* swuart-chat/chat.c - Simple two-way chat using swuart
|
|
*
|
|
* Written 2012 by Werner Almesberger
|
|
* Copyright 2012 Werner Almesberger
|
|
*
|
|
* 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.
|
|
*/
|
|
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <termios.h>
|
|
#include <fcntl.h>
|
|
#include <errno.h>
|
|
|
|
#include <ubb/ubb.h>
|
|
#include <ubb/swuart.h>
|
|
|
|
|
|
#define RX UBB_DAT0
|
|
#define TX UBB_DAT1
|
|
|
|
|
|
/* ----- TTY raw mode ------------------------------------------------------ */
|
|
|
|
|
|
static struct termios old_term;
|
|
|
|
static void raw(void)
|
|
{
|
|
struct termios term;
|
|
|
|
if (tcgetattr(0, &old_term) < 0) {
|
|
perror("tcgetattr");
|
|
exit(1);
|
|
}
|
|
term = old_term;
|
|
cfmakeraw(&term);
|
|
if (tcsetattr(0, TCSAFLUSH, &term) < 0) {
|
|
perror("tcsetattr");
|
|
exit(1);
|
|
}
|
|
if (fcntl(0, F_SETFL, O_NONBLOCK) < 0) {
|
|
perror("fcntl");
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
|
|
static void restore_term(void)
|
|
{
|
|
if (tcsetattr(0, TCSAFLUSH, &old_term) < 0)
|
|
perror("tcsetattr");
|
|
}
|
|
|
|
|
|
static void at_exit(void)
|
|
{
|
|
restore_term();
|
|
swuart_close();
|
|
ubb_close(0);
|
|
}
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
uint8_t local[10], remote[100];
|
|
int got, i;
|
|
|
|
if (ubb_open(0) < 0) {
|
|
perror("ubb_open");
|
|
exit(1);
|
|
}
|
|
atexit(at_exit);
|
|
raw();
|
|
if (swuart_open(TX, RX, 38400) < 0) {
|
|
perror("swuart_open");
|
|
exit(1);
|
|
}
|
|
|
|
while (1) {
|
|
got = read(0, local, sizeof(local));
|
|
if (got < 0 && errno == EAGAIN)
|
|
got = 0;
|
|
if (got < 0) {
|
|
perror("read");
|
|
exit(1);
|
|
}
|
|
if (memchr(local, 3, got))
|
|
break;
|
|
got = swuart_trx(local, got, remote, sizeof(remote),
|
|
1000, 100);
|
|
for (i = 0; i != got; i++)
|
|
if (remote[i] >= ' ' && remote[i] <= '~')
|
|
printf("%c", remote[i]);
|
|
else if (remote[i] == 13)
|
|
printf("\r\n");
|
|
else if (remote[i] == 8 || remote[i] == 127)
|
|
printf("\b \b");
|
|
else
|
|
printf("\\%02o", remote[i]);
|
|
fflush(stdout);
|
|
}
|
|
printf("\r\n");
|
|
return 0;
|
|
}
|