#!/bin/sh
#
#    ptt, press to talk radio emulator for olpc mesh
#    Copyright (C) 2007 James Cameron <quozl@us.netrek.org>
#
#    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
#

# olpc node setup:
#    iwconfig msh0 mode ad-hoc essid "xo"
#    ifconfig msh0 11.0.0.5
#    ip route add unicast 224.0.0.1 dev msh0

# known problems:
#    holding down t causes keyboard repeat oscillation
#    process termination messages appear
#    two people talking at once would cause data stream timing confusion
#    significant latency is observed
#    no audio feedback to indicate operating mode

# start receiver
ptt-rx.py &
RX=$!
echo ptt: receiver start

while true; do
    echo "ptt: read (q to quit, or t to transmit)"
    read -s -n 1 KEY
    if test "$KEY" = 'q'; then
	echo ptt: quit
	# stop receiver
	kill $RX
	echo ptt: receiver stop
	break;
    fi
    if test "$KEY" = 't'; then
	# stop receiver
	kill $RX
	echo ptt: receiver stop

        # start transmitter
	echo ptt: transmitter start
	ptt-tx.py &
	TX=$!

	# wait for user
	echo "ptt: read (q to quit, any other key to stop transmit)"
	read -s -n 1 KEY

	# stop transmitter
	kill $TX
	echo ptt: transmitter stop

	if test "$KEY" = 'q'; then
	    echo ptt: quit
	    break;
	fi

	# start receiver
	ptt-rx.py &
	RX=$!
	echo ptt: receiver start
    fi
done
