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