#! /usr/bin/env python
"""
gpsduel - Records output of 2 gps modules simultaneously

Can optionally filter for certain NMEA sentence types.
Just add the option(s) of the type(s) you want to see.

:Copyright: Copyright 2007 Dean Hall.  All rights reserved.
:Author: Dean Hall
:Revision: 0.1
:Date: 2007/07/19

usage: gpsduel [-cghlstv]

-c          Filter in $GPRMC sentence type.
-g          Filter in $GPGGA sentence type.
-h          Prints this help message
-l          Filter in $GPGLL sentence type
-s          Filter in $GPGSA sentence type.
-t          Filter in $GPVTG sentence type.
-v          Filter in $GPGSV sentence type.
--rmc       Filter in $GPRMC sentence type.
--gga       Filter in $GPGGA sentence type.
--gll       Filter in $GPGLL sentence type
--gsa       Filter in $GPGSA sentence type.
--vtg       Filter in $GPVTG sentence type.
--gsv       Filter in $GPGSV sentence type.
"""


# Device pathnames for various GPS modules
DEVICES = {
    "Holux": "/dev/cu.usbserial",
    "EverMore": "/dev/cu.SLAB_USBtoUART",
    }
DEFAULT_BAUD = 19200


import getopt, os, sys
import serial


def main():
    # Parse command line options
    try:
        opts, args = getopt.getopt(sys.argv[1:], "cghlstv",
                                   ["rmc", "gga", "help", "gll", "gsa", "vtg",
                                    "gsv"])
    except getopt.GetoptError:
        print __doc__
        sys.exit(2)

    sentencefilter = []
    for o, a in opts:
        if o in ("-h", "--help"):
            print __doc__
            sys.exit()

        elif o in ("-c", "--rmc"):
            sentencefilter.append("RMC")
        elif o in ("-g", "--gga"):
            sentencefilter.append("GGA")
        elif o in ("-l", "--gll"):
            sentencefilter.append("GLL")
        elif o in ("-s", "--gsa"):
            sentencefilter.append("GSA")
        elif o in ("-t", "--vtg"):
            sentencefilter.append("VTG")
        elif o in ("-v", "--gsv"):
            sentencefilter.append("GSV")

    # Write output to 2 specific files
    cwd = os.path.curdir
    foE = open(os.path.join(cwd, "gpsduelEverMoreData.nmea"), "w")
    foH = open(os.path.join(cwd, "gpsduelHoluxData.nmea"), "w")

    # Use input from both GPS modules
    fiE = serial.Serial(DEVICES["EverMore"], DEFAULT_BAUD)
    fiH = serial.Serial(DEVICES["Holux"], DEFAULT_BAUD)

    # Begin infinite loop (until users presses Ctl+C)
    try:
        # Read the first lines to clear out any junk
        lineE = fiE.readline()
        lineH = fiH.readline()

        # Read lines and write them to the output file
        while lineE or lineH:
            lineE = fiE.readline()
            lineH = fiH.readline()

            # If there is no filter, write every sentence
            if not sentencefilter:
                foE.write(lineE)
                foH.write(lineH)

            # Else, write only the sentence types that are in the filter
            else:
                if lineE[3:6] in sentencefilter:
                    foE.write(lineE)
                if lineH[3:6] in sentencefilter:
                    foH.write(lineH)

    # Exit cleanly when user types Ctl+C
    except KeyboardInterrupt:
        pass

    # Close files in any event
    finally:
        fiE.close()
        fiH.close()
        foE.close()
        foH.close()


if __name__ == "__main__":
    main()