1515
1616frame = bytearray (7 )
1717
18- def envoi_commande (room , action ): #Sending a frame
18+ #Function to send a frame
19+ def send (room , action ):
20+
1921 checksum = 0
20- #Printing Room name
21- print "Room : " + room
2222
2323 #Defining button action
2424 if action == "open" :
@@ -30,11 +30,17 @@ def envoi_commande(room, action): #Sending a frame
3030 elif action == "register" :
3131 bouton = btnProg
3232 else :
33- print "Unknown button"
34- print "Action : " + action
33+ print "Unknown action."
34+ print "Please use open, close, stop or register."
35+ sys .exit ()
36+ print "Action : " + action
37+
38+ #Defining room
39+ print "Room : " + room
3540
3641 #Reading remote
37- with open ("remotes/" + room + ".txt" , 'r' ) as file :# the files are un a subfolder "somfy"
42+ #The files are stored in a subfolder called "remotes"
43+ with open ("remotes/" + room + ".txt" , 'r' ) as file :
3844 data = file .readlines ()
3945
4046 remote = int (data [0 ], 16 )
@@ -44,28 +50,27 @@ def envoi_commande(room, action): #Sending a frame
4450 with open ("remotes/" + room + ".txt" , 'w' ) as file :
4551 file .writelines (data )
4652
47- pi = pigpio . pi () # connect to Pi
48-
53+ #Connecting to Pi
54+ pi = pigpio . pi ()
4955 if not pi .connected :
5056 exit ()
5157
5258 pi .wave_add_new ()
5359 pi .set_mode (TXGPIO , pigpio .OUTPUT )
5460
55- print "Remote : " + "0x%0.2X" % remote
56- print "Button : " + "0x%0.2X" % bouton
61+ print "Remote : " + "0x%0.2X" % remote
62+ print "Button : " + "0x%0.2X" % bouton
5763 print "Rolling code : " + str (code )
58- print ""
59-
60- frame [0 ] = 0xA7 ; # Encryption key. Doesn't matter much
61- frame [1 ] = bouton << 4 # Which button did you press? The 4 LSB will be the checksum
64+
65+ frame [0 ] = 0xA7 ; # Encryption key. Doesn't matter much
66+ frame [1 ] = bouton << 4 # Which action did you chose? The 4 LSB will be the checksum
6267 frame [2 ] = code >> 8 # Rolling code (big endian)
6368 frame [3 ] = (code & 0xFF ) # Rolling code
6469 frame [4 ] = remote >> 16 # Remote address
6570 frame [5 ] = ((remote >> 8 ) & 0xFF ) # Remote address
6671 frame [6 ] = (remote & 0xFF ) # Remote address
6772
68- print "Frame : " ,
73+ print "Frame : " ,
6974 for octet in frame :
7075 print "0x%0.2X" % octet ,
7176 print ""
@@ -77,15 +82,15 @@ def envoi_commande(room, action): #Sending a frame
7782
7883 frame [1 ] |= checksum ;
7984
80- print "With cks : " ,
85+ print "With cks : " ,
8186 for octet in frame :
8287 print "0x%0.2X" % octet ,
8388 print ""
8489
8590 for i in range (1 , 7 ):
8691 frame [i ] ^= frame [i - 1 ];
8792
88- print "Obfuscated :" ,
93+ print "Obfuscated :" ,
8994 for octet in frame :
9095 print "0x%0.2X" % octet ,
9196 print ""
@@ -153,5 +158,12 @@ def envoi_commande(room, action): #Sending a frame
153158 pi .wave_delete (wid )
154159
155160 pi .stop ()
156-
157- envoi_commande (sys .argv [1 ], sys .argv [2 ])
161+
162+ #Calling send function with args
163+ if len (sys .argv ) > 2 :
164+ send (sys .argv [1 ], sys .argv [2 ])
165+ else :
166+ #Exiting if not enough args
167+ print "Action is missing."
168+ print "Please use open, close, stop or register as second argument."
169+ sys .exit ()
0 commit comments