-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaster_lc.py
120 lines (91 loc) · 2.42 KB
/
master_lc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import socket
import sys
import RPi.GPIO as GPIO
import time
import pygame, time
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Pygame Keyboard Test')
pygame.mouse.set_visible(0)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(2, GPIO.OUT)
GPIO.setup(3, GPIO.OUT)
GPIO.setup(7, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)
def right():
GPIO.output(2, GPIO.HIGH)
GPIO.output(3, GPIO.LOW)
GPIO.output(7, GPIO.HIGH)
GPIO.output(17, GPIO.LOW)
time.sleep(0.5)
def left():
GPIO.output(3, GPIO.HIGH)
GPIO.output(2, GPIO.LOW)
GPIO.output(17, GPIO.HIGH)
GPIO.output(7, GPIO.LOW)
time.sleep(0.5)
def back():
GPIO.output(2, GPIO.HIGH)
GPIO.output(3, GPIO.LOW)
GPIO.output(17, GPIO.HIGH)
GPIO.output(7, GPIO.LOW)
time.sleep(0.5)
def forward():
GPIO.output(3, GPIO.HIGH)
GPIO.output(2, GPIO.LOW)
GPIO.output(7, GPIO.HIGH)
GPIO.output(17, GPIO.LOW)
time.sleep(0.5)
def create_socket():
try:
global host
global s
global port
host=""
port=9999
s=socket.socket()
except socket.error as mag:
print("socket creation error",str(mag))
def bind_socket():
try:
global host
global s
global port
print("binding the socket")
s.bind((host,port))
s.listen(5)
except socket.error as mag:
print("socket binding error ",str(mag)," retrying")
bind_socket()
def accept():
conn,adress=s.accept()
print("ip : ",adress[0]," port : ",adress[1])
send_command(conn)
conn.close()
def send_command(conn):
while True:
for event in pygame.event.get():
if (event.type == pygame.QUIT):
pygame.quit()
conn.close()
s.close()
sys.exit()
if event.type == pygame.KEYDOWN:
if(event.key== K_RIGHT):
conn.send(str.encode('right'))
right()
elif(event.key== K_LEFT):
conn.send(str.encode('left'))
left()
elif(event.key== K_DOWN):
conn.send(str.encode('back'))
back()
elif(event.key== K_UP):
conn.send(str.encode('forward'))
forward()
def main():
create_socket()
bind_socket()
accept()
main()