Advertisement
Guest User

Untitled

a guest
Sep 19th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.33 KB | None | 0 0
  1. from Tkinter import *
  2. import serial
  3. import threading
  4. import ConfigParser
  5. import logging
  6. import sys
  7.  
  8. global comNum, bmnNum, com, v, COMPortNumber, BowlingMusicNetworkInputNumber
  9.  
  10. # Set up our logging files
  11. sys.stderr = open('ProjectorControl.log', 'w')
  12. sys.stdout = open('ProjectorControl.err', 'w')
  13.  
  14. # create logger
  15. logger = logging.getLogger("logging")
  16. logger.setLevel(logging.DEBUG)
  17. # create console handler and set level to debug
  18. ch = logging.StreamHandler()
  19. ch.setLevel(logging.DEBUG)
  20. # create formatter
  21. formatter = logging.Formatter("[%(asctime)s] - %(message)s")
  22. # add formatter to ch
  23. ch.setFormatter(formatter)
  24. # add ch to logger
  25. logger.addHandler(ch)
  26. logger.debug('====ProgramStart====')
  27.  
  28. # load our config and prepare to read the file
  29. configParser = ConfigParser.RawConfigParser()  
  30. configFilePath = r'config.conf'
  31. configParser.read(configFilePath)
  32.  
  33. # pull config values from the file to
  34. # fill our comNum and bmnNum globals
  35. comNum = configParser.get('general', 'COMPortNumber')
  36. bmnNum = configParser.get('general', 'BowlingMusicNetworkInputNumber')
  37.  
  38. # initialize our gui
  39. root = Tk()
  40.  
  41. root.iconbitmap('avicon.ico')
  42.  
  43. # init the variable to be used by our radio buttons    
  44. v = StringVar()
  45. v.set("1") # default it to 1, the first input
  46.  
  47. class Example(Frame):
  48.     def __init__(self, parent):
  49.         Frame.__init__(self, parent)
  50.         self.parent = parent        
  51.         self.initUI()
  52.    
  53.     # ########################
  54.     # Bowling Music to all
  55.     # ########################
  56.     # Puts bowling music across the entire house,
  57.     # standard setup for glow birthday parties
  58.     # and glow bowl on nights without sports games
  59.     # ########################
  60.    
  61.     def bmnToAll(self):
  62.         logger.debug('====================')
  63.         logger.debug('Set Bowling Music to all')
  64.         logger.debug(str(bmnNum) + 'All.')
  65.         logger.debug('====================')
  66.         logger.debug(' ')
  67.        
  68.         #lets see if we can open the port
  69.         try:
  70.             com = serial.Serial(int(comNum)-1)
  71.             #if it is open, then let's send our command
  72.             if com.isOpen():
  73.                 com.write(str(bmnNum) + 'All.')
  74.                 com.close()
  75.         #if we were unable to open it then let's log the exception
  76.         except serial.SerialException as ex:
  77.             logger.debug('Port ' + int(comNum)-1 + ' is unavailable: ' + ex)
  78.        
  79.    
  80.     # ########################
  81.     # Standard setup
  82.     # ########################
  83.     # Puts bowling music on each odd pair
  84.     # and DirectTV on each even pair, sequentially.
  85.     # You end up with:
  86.     #
  87.     # Lanes 1 & 2   Bowling Music
  88.     # Lanes 3 & 4   DTV1
  89.     # Lanes 5 & 6   Bowling Music
  90.     # Lanes 7 & 8   DTV2
  91.     #
  92.     # and so on
  93.     #
  94.     # need to automate into loop, but not today
  95.     # ########################
  96.     def standardInOut (self):
  97.         logger.debug('====================')
  98.         logger.debug('Set standard AV config')
  99.         logger.debug(str(bmnNum) + ' B1,3,5,7,9,11,13,15.')
  100.         logger.debug('1 B2.')
  101.         logger.debug('2 B4.')
  102.         logger.debug('3 B6.')
  103.         logger.debug('4 B8.')
  104.         logger.debug('5 B10.')
  105.         logger.debug('6 B12.')
  106.         logger.debug('7 B14.')
  107.         logger.debug('8 B16.')
  108.         logger.debug('====================')
  109.         logger.debug(' ')
  110.         #lets see if we can open the port
  111.         try:
  112.             com = serial.Serial(int(comNum)-1)
  113.             #if it is open, then let's send our command
  114.             if com.isOpen():
  115.                 com.write(str(bmnNum) + 'B1,3,5,7,9,11,13,15.') # BM to every other projector
  116.                 com.write('1 B2.') # DTV1 to Pair2
  117.                 com.write('2 B4.') # DTV2 to Pair4
  118.                 com.write('3 B6.') # and so on
  119.                 com.write('4 B8.') # and so forth
  120.                 com.write('5 B10.')
  121.                 com.write('6 B12.')
  122.                 com.write('7 B14.')
  123.                 com.write('8 B16.')
  124.                 com.close()
  125.         #if we were unable to open it then let's log the exception
  126.         except serial.SerialException as ex:
  127.             logger.debug('Port ' + int(comNum)-1 + ' is unavailable: ' + ex)
  128.    
  129.    
  130.     # ########################
  131.     # Standard In/Out function
  132.     # ########################
  133.     # takes three arguments, ignores the first
  134.     # as i don't know what it is or why it is
  135.     # passed, probably something related to
  136.     # the lambda function, maybe its identifier?
  137.     #
  138.     # The next two arguments are the input and
  139.     # output numbers we'd like to set. These are
  140.     # provided by radio button and button, respectfully.
  141.     # Only sets one output to one input at a time,
  142.     # most useful for changing a single pair to a
  143.     # certain DTV box or back to music videos,
  144.     # if the customer requests it specifically.
  145.     # ########################
  146.     def setCustomInOut(dummy, input, output):
  147.         logger.debug('====================')
  148.         logger.debug('Set a custom config')
  149.         logger.debug(str(input) + ' B' + str(output) + '.')
  150.         logger.debug('====================')
  151.         logger.debug(' ')
  152.         #lets see if we can open the port
  153.         try:
  154.             com = serial.Serial(int(comNum)-1)
  155.             #if it is open, then let's send our command
  156.             if com.isOpen():
  157.                 com.write(str(input) + ' B' + str(output) + '.')
  158.                 com.close()
  159.         #if we were unable to open it then let's log the exception
  160.         except serial.SerialException as ex:
  161.             logger.debug('Port ' + int(comNum)-1 + ' is unavailable: ' + ex)
  162.    
  163.    
  164.     def initUI(self):
  165.         # set window title
  166.         self.parent.title("Projector Control")
  167.        
  168.         # init menubar
  169.         menubar = Menu(self.parent)
  170.         self.parent.config(menu=menubar)
  171.        
  172.         # fill menubar with items
  173.         fileMenu = Menu(menubar)
  174.         fileMenu.add_command(label="Standard setup", command=self.standardInOut)
  175.         fileMenu.add_command(label="Bowling Music to all", command=self.bmnToAll)
  176.         menubar.add_cascade(label="File", menu=fileMenu)
  177.  
  178.         self.pack(fill=BOTH, expand=1)
  179.         self.var = IntVar()
  180.        
  181.         # pull total number of inputs as well
  182.         # as the Bowling Music input number
  183.         # from config file and assign var
  184.         logger.debug('==Read config file==')
  185.         numIn = configParser.get('general', 'numberOfInputs')
  186.         bmnIn = configParser.get('general', 'bowlingMusicNetworkInputNumber')
  187.        
  188.        
  189.        
  190.         #init our inputs dict
  191.         inputs= []
  192.        
  193.         # then fill it, naming it DTV# where # is the input number,
  194.         # unless it is # matches the bowling music input number (bmnIn)
  195.         # at which point we name it Bowling Music
  196.         logger.debug('=====Init input=====')
  197.         for i in range(int(numIn)):
  198.             if i != int(bmnIn) - 1:
  199.                 inputs.append(['DTV' + str(i + 1), str(i + 1)])
  200.             else:
  201.                 inputs.append(['Bowling Music', str(bmnIn)])
  202.        
  203.         # take our list of inputs and make radio buttons for them,
  204.         # storing the value in the variable "v" that we initialized earlier
  205.         for text, input in inputs:
  206.             b = Radiobutton(self, text=text,
  207.                             variable=v, value=input)
  208.             b.grid(column=0, padx = 10, sticky=W)
  209.        
  210.        
  211.         # get number of outputs from file and get current input
  212.         # status of each output, print to label
  213.         # Need better logging
  214.         numOut = configParser.get('general', 'numberOfOutputs')
  215.        
  216.         logger.debug('====Init Status====')
  217.         for output in range(int(numOut)):
  218.             print 'output ' + str(output)
  219.             #lets see if we can open the port
  220.             try:
  221.                 com = serial.Serial(int(comNum)-1)
  222.                 #if it is open, then let's send our command
  223.                 if com.isOpen():
  224.                     com.write('Status' + str(output) + '.')
  225.                     response = com.read(2)
  226.                     w = Label(self, text=response, relief=SUNKEN, width=3).grid(row=output, padx = 5,  column=1)
  227.                     print response
  228.                     com.close()
  229.             #if we were unable to open it then let's log the exception
  230.             except serial.SerialException as ex:
  231.                 logger.debug('Port ' + int(comNum)-1 + ' is unavailable: ' + ex)
  232.        
  233.         # get number of outputs from var and create buttons for each
  234.         # output, each corresponding to a specific projector. When clicked
  235.         # the buttons should send the value of "v" (which designates
  236.         # which input is currently selected) and the output number to our
  237.         # CustomInOut() function.
  238.         logger.debug('====Init outputs====')
  239.         for i in range(int(numOut)):
  240.             vars()['btnOut' + str(i)] = Button(self, width=15, text="Projector " + str(i + 1), command=lambda i=i: self.setCustomInOut(v.get(), i+1) )
  241.             vars()['btnOut' + str(i)].grid(row=i, column=2, sticky=E)
  242.            
  243.     def onExit(self):
  244.         logger.debug('=====ProgramEnd=====')
  245.         raise SystemExit
  246.  
  247. def main():
  248.  
  249.     # set window size
  250.     root.geometry("290x340+300+300")
  251.     app = Example(root)
  252.     root.mainloop()  
  253.  
  254.  
  255. if __name__ == '__main__':
  256.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement