slitaz-arm diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rpi/piborg/RemoteKeyBorgS.py	Thu May 15 18:57:11 2014 +0200
     1.3 @@ -0,0 +1,99 @@
     1.4 +#!/usr/bin/env python
     1.5 +# coding: Latin-1
     1.6 +
     1.7 +# Load library functions we want
     1.8 +import SocketServer
     1.9 +import RPi.GPIO as GPIO
    1.10 +GPIO.setmode(GPIO.BCM)
    1.11 +GPIO.setwarnings(False)
    1.12 +
    1.13 +# Set which GPIO pins the drive outputs are connected to
    1.14 +DRIVE_1 = 4
    1.15 +DRIVE_2 = 18
    1.16 +DRIVE_3 = 8
    1.17 +DRIVE_4 = 7
    1.18 +
    1.19 +# Set all of the drive pins as output pins
    1.20 +GPIO.setup(DRIVE_1, GPIO.OUT)
    1.21 +GPIO.setup(DRIVE_2, GPIO.OUT)
    1.22 +GPIO.setup(DRIVE_3, GPIO.OUT)
    1.23 +GPIO.setup(DRIVE_4, GPIO.OUT)
    1.24 +
    1.25 +# Map of drives to pins
    1.26 +lDrives = [DRIVE_1, DRIVE_2, DRIVE_3, DRIVE_4]
    1.27 +
    1.28 +# Function to set all drives off
    1.29 +def MotorOff():
    1.30 +    GPIO.output(DRIVE_1, GPIO.LOW)
    1.31 +    GPIO.output(DRIVE_2, GPIO.LOW)
    1.32 +    GPIO.output(DRIVE_3, GPIO.LOW)
    1.33 +    GPIO.output(DRIVE_4, GPIO.LOW)
    1.34 +
    1.35 +# Settings for the RemoteKeyBorg server
    1.36 +portListen = 9038                       # What messages to listen for (LEDB on an LCD)
    1.37 +
    1.38 +# Class used to handle UDP messages
    1.39 +class PicoBorgHandler(SocketServer.BaseRequestHandler):
    1.40 +    # Function called when a new message has been received
    1.41 +    def handle(self):
    1.42 +        global isRunning
    1.43 +
    1.44 +        request, socket = self.request          # Read who spoke to us and what they said
    1.45 +        request = request.upper()               # Convert command to upper case
    1.46 +        driveCommands = request.split(',')      # Separate the command into individual drives
    1.47 +        if len(driveCommands) == 1:
    1.48 +            # Special commands
    1.49 +            if request == 'ALLOFF':
    1.50 +                # Turn all drives off
    1.51 +                MotorOff()
    1.52 +                print 'All drives off'
    1.53 +            elif request == 'EXIT':
    1.54 +                # Exit the program
    1.55 +                isRunning = False
    1.56 +            else:
    1.57 +                # Unknown command
    1.58 +                print 'Special command "%s" not recognised' % (request)
    1.59 +        elif len(driveCommands) == len(lDrives):
    1.60 +            # For each drive we check the command
    1.61 +            for driveNo in range(len(driveCommands)):
    1.62 +                command = driveCommands[driveNo]
    1.63 +                if command == 'ON':
    1.64 +                    # Set drive on
    1.65 +                    GPIO.output(lDrives[driveNo], GPIO.HIGH)
    1.66 +                elif command == 'OFF':
    1.67 +                    # Set drive off
    1.68 +                    GPIO.output(lDrives[driveNo], GPIO.LOW)
    1.69 +                elif command == 'X':
    1.70 +                    # No command for this drive
    1.71 +                    pass
    1.72 +                else:
    1.73 +                    # Unknown command
    1.74 +                    print 'Drive %d command "%s" not recognised!' % (driveNo, command)
    1.75 +        else:
    1.76 +            # Did not get the right number of drive commands
    1.77 +            print 'Command "%s" did not have %d parts!' % (request, len(lDrives))
    1.78 +
    1.79 +try:
    1.80 +    global isRunning
    1.81 +
    1.82 +    # Start by turning all drives off
    1.83 +    MotorOff()
    1.84 +    print '\nStarting remote keyborg...'
    1.85 +    #raw_input('You can now turn on the power, press ENTER to continue')
    1.86 +    # Setup the UDP listener
    1.87 +    remoteKeyBorgServer = SocketServer.UDPServer(('', portListen), PicoBorgHandler)
    1.88 +    # Loop until terminated remotely
    1.89 +    isRunning = True
    1.90 +    while isRunning:
    1.91 +        remoteKeyBorgServer.handle_request()
    1.92 +    # Turn off the drives and release the GPIO pins
    1.93 +    print 'Finished'
    1.94 +    MotorOff()
    1.95 +    #raw_input('Turn the power off now, press ENTER to continue')
    1.96 +    GPIO.cleanup()
    1.97 +except KeyboardInterrupt:
    1.98 +    # CTRL+C exit, turn off the drives and release the GPIO pins
    1.99 +    print 'Terminated'
   1.100 +    MotorOff()
   1.101 +    raw_input('Turn the power off now, press ENTER to continue')
   1.102 +    GPIO.cleanup()