slitaz-arm view rpi/piborg/RemoteKeyBorgC.py @ rev 210

tazbian: rpi2+ support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Mar 27 12:20:30 2016 +0200 (2016-03-27)
parents
children
line source
1 #!/usr/bin/env python
2 # coding: Latin-1
4 # Load library functions we want
5 import socket
6 import time
7 import pygame
9 # Settings for the RemoteKeyBorg client
10 broadcastIP = '192.168.0.255' # IP address to send to, 255 in one or more positions is a broadcast / wild-card
11 broadcastPort = 9038 # What message number to send with (LEDB on an LCD)
12 leftDrive = 1 # Drive number for left motor
13 rightDrive = 4 # Drive number for right motor
14 interval = 0.1 # Time between keyboard updates in seconds, smaller responds faster but uses more processor time
15 regularUpdate = True # If True we send a command at a regular interval, if False we only send commands when keys are pressed or released
17 # Setup the connection for sending on
18 sender = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # Create the socket
19 sender.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) # Enable broadcasting (sending to many IPs based on wild-cards)
20 sender.bind(('0.0.0.0', 0)) # Set the IP and port number to use locally, IP 0.0.0.0 means all connections and port 0 means assign a number for us (do not care)
22 # Setup pygame and key states
23 global hadEvent
24 global moveUp
25 global moveDown
26 global moveLeft
27 global moveRighte
28 global moveQuit
29 hadEvent = True
30 moveUp = False
31 moveDown = False
32 moveLeft = False
33 moveRight = False
34 moveQuit = False
35 pygame.init()
36 screen = pygame.display.set_mode([300,300])
37 pygame.display.set_caption("RemoteKeyBorg - Press [ESC] to quit")
39 # Function to handle pygame events
40 def PygameHandler(events):
41 # Variables accessible outside this function
42 global hadEvent
43 global moveUp
44 global moveDown
45 global moveLeft
46 global moveRight
47 global moveQuit
48 # Handle each event individually
49 for event in events:
50 if event.type == pygame.QUIT:
51 # User exit
52 hadEvent = True
53 moveQuit = True
54 elif event.type == pygame.KEYDOWN:
55 # A key has been pressed, see if it is one we want
56 hadEvent = True
57 if event.key == pygame.K_UP:
58 moveUp = True
59 elif event.key == pygame.K_DOWN:
60 moveDown = True
61 elif event.key == pygame.K_LEFT:
62 moveLeft = True
63 elif event.key == pygame.K_RIGHT:
64 moveRight = True
65 elif event.key == pygame.K_ESCAPE:
66 moveQuit = True
67 elif event.type == pygame.KEYUP:
68 # A key has been released, see if it is one we want
69 hadEvent = True
70 if event.key == pygame.K_UP:
71 moveUp = False
72 elif event.key == pygame.K_DOWN:
73 moveDown = False
74 elif event.key == pygame.K_LEFT:
75 moveLeft = False
76 elif event.key == pygame.K_RIGHT:
77 moveRight = False
78 elif event.key == pygame.K_ESCAPE:
79 moveQuit = False
81 try:
82 print 'Press [ESC] to quit'
83 # Loop indefinitely
84 while True:
85 # Get the currently pressed keys on the keyboard
86 PygameHandler(pygame.event.get())
87 if hadEvent or regularUpdate:
88 # Keys have changed, generate the command list based on keys
89 hadEvent = False
90 driveCommands = ['X', 'X', 'X', 'X'] # Default to do not change
91 if moveQuit:
92 break
93 elif moveLeft:
94 driveCommands[leftDrive - 1] = 'OFF'
95 driveCommands[rightDrive - 1] = 'ON'
96 elif moveRight:
97 driveCommands[leftDrive - 1] = 'ON'
98 driveCommands[rightDrive - 1] = 'OFF'
99 elif moveUp:
100 driveCommands[leftDrive - 1] = 'ON'
101 driveCommands[rightDrive - 1] = 'ON'
102 else:
103 # None of our expected keys, stop
104 driveCommands[leftDrive - 1] = 'OFF'
105 driveCommands[rightDrive - 1] = 'OFF'
106 # Send the drive commands
107 command = ''
108 for driveCommand in driveCommands:
109 command += driveCommand + ','
110 command = command[:-1] # Strip the trailing comma
111 sender.sendto(command, (broadcastIP, broadcastPort))
112 # Wait for the interval period
113 time.sleep(interval)
114 # Inform the server to stop
115 sender.sendto('ALLOFF', (broadcastIP, broadcastPort))
116 except KeyboardInterrupt:
117 # CTRL+C exit, inform the server to stop
118 sender.sendto('ALLOFF', (broadcastIP, broadcastPort))