# -*- coding: utf-8 -*-



import serial
#import sys

class Parameters( object ):
       """
       manages parameter values, should be globally available,
       need to look into getter setter value idea
       may allow some to come from a file similar to an ini file
       casts parameter values to other types if necessary, like to string
       for display.

       """

       def __init__(self,  aController  ):

          self.myController      = aController    # save a link to the controller

          self.gui_sends         = 4           # number of send frames in the gui

          self.myLogFile         = "terminal.log"  # name of log file

          self._logLevelDefault  = 10  # should be 0 to 10, 10 most logging perhaps - not implementd

          self.echoSend          = True          # locally echo the sent characters

          self.serialAppend      = "\r\n"        # "\r\n" is car. return line feed.
          self.serialAppend      = ""
          self.serialAppend      = "\r"
          
          self.loop_text         = "looping TEXT"   # do not make so long as to jam the transmitter sent every poll_delta_t
          
          self.poll_delta_t      = 2000             # in ms -- put in parms?
          
          self.logfilename       = "terminal.log"
          

          # set up to plug in the right driver only rs232 implemented so far

          #self.comm_mod     = "arduinodriveremu"
          #self.comm_class   = "ArduinoDriverEmu"

          self.comm_mod     = "rs232driver"
          self.comm_class   = "RS232Driver"

          #self.comm_mod     = = "i2c"
          #self.comm_class   = "I2C1"

          #self.comm_mod     = "spi"
          #self.comm_class   = "SPI1"

          # ----------  parameters that are relavant for rs232

          # comm parms for arduino
          # 9600 is ok as are many others try this for most reliable? comm
          #The default is 8 data bits, no parity, one stop bit.
          #http://arduino.cc/en/Serial/begin

          self.port              = "COM6"   # /dev/ttyUSB0 on GNU/Linux or COM3 on Windows. Device name or port number number or None.
          #self.port              = "/dev/ttyUSB0"
          #self.port              = "/dev/ttyUSB1"

          self.baudrate          = 9600                 # baudrate – Baud rate such as 9600 or 115200 etc.
          self.baudrate          = 19200
          self.bytesize          = serial.EIGHTBITS     # Possible values: FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS
          self.parity            = serial.PARITY_NONE
          self.stopbits          = serial.STOPBITS_ONE
          self.recTimeout        = .05           # serial.timeout in units of x seconds (float allowed)

          # parameters I am not managing 
          #timeout – Set a read timeout value.
          #xonxoff – Enable software flow control.
          #rtscts – Enable hardware (RTS/CTS) flow control.
          #dsrdtr – Enable hardware (DSR/DTR) flow control.
          #writeTimeout – Set a write timeout value.
          #interCharTimeout – Inter-character timeout, None to disable (default).

          # ------  utility values

          self.parityToStrs      = {  serial.PARITY_EVEN : "Parity Even",  \
                                      serial.PARITY_NONE : "Parity None",  \
                                      serial.PARITY_ODD  : "Parity Odd",   \
                                      serial.PARITY_MARK : "Parity Mark",  \
                                      serial.PARITY_SPACE: "Parity Space"  \
                                   }

          # ------

          self.stopbitsToStrs    = { serial.STOPBITS_ONE            : "1",    \
                                     serial.STOPBITS_ONE_POINT_FIVE : "1.5",  \
                                     serial.STOPBITS_TWO            : "2",    \
                                   }

          # ------

          #print "Parameters initialized"
          return


       # -------- utility functions, mostly get parameters as strings

       def getCommTypeAsStr( self, ):   # should get from driver and branch to right kind of parms

           return "RS232"

       def getParityAsStr( self, ):

           return self.parityToStrs[ self.parity ]

       def getBaudrateAsStr( self, ):

           return str( self.baudrate )

       def getPortAsStr( self, ):

           return self.port

       def getStopbitsAsStr( self, ):

           return self.stopbitsToStrs[ self.stopbits ]

       # -----------------------------
       # probably should just get the variable, more pythonic?

       def getBaud( self, ):
          return self.baudrate 

# =================== eof ==============================

