slitaz-arm view rpi/piborg/RemoteKeyBorgS.py @ rev 188

Add support for PiBorg (A PiCy will be used at Paris)
author Christophe Lincoln <pankso@slitaz.org>
date Thu May 15 18:57:11 2014 +0200 (2014-05-15)
parents
children
line source
1 #!/usr/bin/env python
2 # coding: Latin-1
4 # Load library functions we want
5 import SocketServer
6 import RPi.GPIO as GPIO
7 GPIO.setmode(GPIO.BCM)
8 GPIO.setwarnings(False)
10 # Set which GPIO pins the drive outputs are connected to
11 DRIVE_1 = 4
12 DRIVE_2 = 18
13 DRIVE_3 = 8
14 DRIVE_4 = 7
16 # Set all of the drive pins as output pins
17 GPIO.setup(DRIVE_1, GPIO.OUT)
18 GPIO.setup(DRIVE_2, GPIO.OUT)
19 GPIO.setup(DRIVE_3, GPIO.OUT)
20 GPIO.setup(DRIVE_4, GPIO.OUT)
22 # Map of drives to pins
23 lDrives = [DRIVE_1, DRIVE_2, DRIVE_3, DRIVE_4]
25 # Function to set all drives off
26 def MotorOff():
27 GPIO.output(DRIVE_1, GPIO.LOW)
28 GPIO.output(DRIVE_2, GPIO.LOW)
29 GPIO.output(DRIVE_3, GPIO.LOW)
30 GPIO.output(DRIVE_4, GPIO.LOW)
32 # Settings for the RemoteKeyBorg server
33 portListen = 9038 # What messages to listen for (LEDB on an LCD)
35 # Class used to handle UDP messages
36 class PicoBorgHandler(SocketServer.BaseRequestHandler):
37 # Function called when a new message has been received
38 def handle(self):
39 global isRunning
41 request, socket = self.request # Read who spoke to us and what they said
42 request = request.upper() # Convert command to upper case
43 driveCommands = request.split(',') # Separate the command into individual drives
44 if len(driveCommands) == 1:
45 # Special commands
46 if request == 'ALLOFF':
47 # Turn all drives off
48 MotorOff()
49 print 'All drives off'
50 elif request == 'EXIT':
51 # Exit the program
52 isRunning = False
53 else:
54 # Unknown command
55 print 'Special command "%s" not recognised' % (request)
56 elif len(driveCommands) == len(lDrives):
57 # For each drive we check the command
58 for driveNo in range(len(driveCommands)):
59 command = driveCommands[driveNo]
60 if command == 'ON':
61 # Set drive on
62 GPIO.output(lDrives[driveNo], GPIO.HIGH)
63 elif command == 'OFF':
64 # Set drive off
65 GPIO.output(lDrives[driveNo], GPIO.LOW)
66 elif command == 'X':
67 # No command for this drive
68 pass
69 else:
70 # Unknown command
71 print 'Drive %d command "%s" not recognised!' % (driveNo, command)
72 else:
73 # Did not get the right number of drive commands
74 print 'Command "%s" did not have %d parts!' % (request, len(lDrives))
76 try:
77 global isRunning
79 # Start by turning all drives off
80 MotorOff()
81 print '\nStarting remote keyborg...'
82 #raw_input('You can now turn on the power, press ENTER to continue')
83 # Setup the UDP listener
84 remoteKeyBorgServer = SocketServer.UDPServer(('', portListen), PicoBorgHandler)
85 # Loop until terminated remotely
86 isRunning = True
87 while isRunning:
88 remoteKeyBorgServer.handle_request()
89 # Turn off the drives and release the GPIO pins
90 print 'Finished'
91 MotorOff()
92 #raw_input('Turn the power off now, press ENTER to continue')
93 GPIO.cleanup()
94 except KeyboardInterrupt:
95 # CTRL+C exit, turn off the drives and release the GPIO pins
96 print 'Terminated'
97 MotorOff()
98 raw_input('Turn the power off now, press ENTER to continue')
99 GPIO.cleanup()