Advertisement
Tlams

Voiture_master

Aug 10th, 2013
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.24 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: iso-8859-15 -*-
  3.  
  4. import threading
  5. import socket
  6. import hashlib
  7. import time
  8. from Tkinter import *
  9.  
  10. ############################
  11. ###Variables à configurer###
  12. ############################
  13. UDP_IP = "x.x.x.x" #DEST
  14. UDP_IP_loc = "x.x.x.x" #Local IP
  15. UDP_PORT = 26010
  16. PW = "Password"
  17. compteur = 0
  18.  
  19. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  20. sock_recu = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  21. #sock_recu.bind((socket.gethostbyname(socket.gethostname()), UDP_PORT))
  22. sock_recu.bind((UDP_IP_loc, UDP_PORT))
  23. date_heure = time.strftime('%d/%m/%y %H:%M:%S',time.localtime())  
  24. ############################
  25. #####Def. des fonctions#####
  26. ############################
  27.  
  28. def dat_heure():
  29.     return time.strftime('%d/%m/%y %H:%M:%S',time.localtime())
  30.  
  31. def recep_k8055_info():  
  32.     while True:
  33.         data, addr = sock_recu.recvfrom(1024) # buffer size is 1024 bytes
  34.         if(hashlib.sha256(PW).hexdigest() == data.split('|-|')[0]):
  35.             if(data.split('|-|')[1] == "L"):
  36.                 liste_ana = eval(data.split('|-|')[2])
  37.                 liste_digi = eval(data.split('|-|')[3])
  38.                 print dat_heure(), "(" + addr[0] + ")" + " Données reçu: " + str(liste_ana) + str(liste_digi)
  39.             else:
  40.                 print dat_heure(), "(" + addr[0] + ")" + " Données reçu: " + data.split('|-|')[1]
  41.  
  42. def send_k8055_cmd(event,value1,value2):
  43.     if value1 and value2:
  44.         print dat_heure(), "(" + UDP_IP + ")" + " Commande envoyé:"+event+"("+value1+","+value2+")"
  45.         sock.sendto(hashlib.sha256(PW).hexdigest()+"|-|"+event+"|-|"+value1+"|-|"+value2, (UDP_IP, UDP_PORT))
  46.     elif value1:
  47.         print dat_heure(),"(" + UDP_IP + ")" + " Commande envoyé:"+event+"("+value1+")"
  48.         sock.sendto(hashlib.sha256(PW).hexdigest()+"|-|"+event+"|-|"+value1, (UDP_IP, UDP_PORT))
  49.     else:
  50.         print dat_heure(), "(" + UDP_IP + ")" + " Commande envoyé:"+event+"()"
  51.         sock.sendto(hashlib.sha256(PW).hexdigest()+"|-|"+event, (UDP_IP, UDP_PORT))
  52.          
  53.     if(event == "end"):
  54.         sock_recu.close()
  55.         print dat_heure(),"Déconnexion du socket"
  56.        
  57.  
  58.  
  59.  
  60. a = threading.Thread(None,recep_k8055_info)
  61. a.start()
  62.  
  63. root = Tk( )
  64. prompt='Appuyez sur une touche du clavier'
  65. lbl = Label(root, text=prompt, width=len(prompt))
  66. lbl.pack( )
  67.  
  68.  
  69. class Evenement(Frame):
  70.     def __init__(self, parent=None):
  71.         Frame.__init__( self )
  72.         self.master.title("Touche de direction")
  73.         self.master.geometry("600x150")         #La Taille du fenêtre
  74.         self.pack(expand=YES, fill=BOTH)
  75.         lbl=Label(self, text="Appuyez sur une touche de direction")
  76.         lbl["bg"]="blue"
  77.         lbl["font"]=('courier', 20, 'bold')
  78.         lbl.pack(expand=YES, fill=BOTH)
  79.         lbl.focus()
  80.         # Lier un événement aux quatre touches de direction du clavier
  81.        
  82.         # Avant
  83.         lbl.bind_all("<KeyPress-Up><KeyPress-Right>", self.onFW_Right_Down)    # direction droite '-->'
  84.         lbl.bind_all("<KeyPress-Up><KeyPress-Left>", self.onFW_Left_Down)     # direction gauche '<--'
  85.  
  86.         lbl.bind_all("<KeyRelease-Up><KeyRelease-Right>", self.onFW_Right_Up)    # direction droite '-->'
  87.         lbl.bind_all("<KeyRelease-Up><KeyRelease-Left>", self.onFW_Left_Up)     # direction gauche '<--'
  88.  
  89.         lbl.bind("<KeyPress-Up>", self.onForward_Down)       # direction haut
  90.         lbl.bind("<KeyRelease-Up>", self.onForward_Up)       # direction haut
  91.  
  92.  
  93.        
  94.         # Arrière
  95.         lbl.bind("<Down> + <Right>", self.onBA_Right)    # direction droite '-->'
  96.         lbl.bind("<Down> + <Left>", self.onBA_Left)     # direction gauche '<--'
  97.         lbl.bind("<KeyPress-Down>", self.onBack_Up)     # direction bas
  98.         lbl.bind("<KeyRelease-Down>", self.onBack_Down)     # direction bas
  99.        
  100.         # Autre
  101.         lbl.bind("q", self.onEsc)  #Quitter
  102.         lbl.bind("<Left> + <Right>", self.on360)    # 360 '-->'
  103.        
  104.        
  105.     def onFW_Right_Up(self,event):
  106.         send_k8055_cmd("digital_all_off","","")
  107.  
  108.     def onFW_Right_Down(self,event):
  109.         send_k8055_cmd("digital_write","4","")
  110.  
  111.     def onFW_Left_Down(self,event):
  112.         send_k8055_cmd("digital_write","1","")
  113.        
  114.     def onFW_Left_Up(self,event):
  115.         send_k8055_cmd("digital_all_off","","")
  116.  
  117.              
  118.     def onForward_Up(self,event):
  119.         send_k8055_cmd("digital_all_off","","")
  120.        
  121.     def onForward_Down(self,event):
  122.         send_k8055_cmd("digital_write","5","")
  123.  
  124.  
  125.  
  126.     def onBA_Right(self,event):
  127.         send_k8055_cmd("digital_write","8","")
  128.        
  129.     def onBA_Left(self,event):
  130.         send_k8055_cmd("digital_write","2","")
  131.        
  132.     def onBack_Down(self,event):
  133.         send_k8055_cmd("digital_all_off","","")
  134.        
  135.     def onBack_Up(self,event):
  136.         send_k8055_cmd("digital_write","10","")
  137.  
  138.        
  139.  
  140.     def on360(self,event):
  141.         send_k8055_cmd("digital_write","9","")
  142.        
  143.     def onEsc(self,event):
  144.         send_k8055_cmd("end","","")
  145.         a._Thread__stop()
  146.         sock_recu.close()
  147.        
  148.        
  149. def main():             #Fonction principale
  150.    Evenement().mainloop()
  151.  
  152. if __name__=="__main__":
  153.     main()
  154.  
  155.    
  156. a._Thread__stop()
  157. sock_recu.close()
  158. print "FIN"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement