Votes Internally!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.9 KiB

import socket
import pygame
import sys
import time
from packet import Packet, CMD
pygame.init()
disp = pygame.display.set_mode((640, 480))
font = pygame.font.SysFont(pygame.font.get_default_font(), 24)
joys = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())]
for joy in joys:
joy.init()
LIN_AX = 1
ANG_AX = 0
LIN_V = 0.2
ANG_V = 1.0
PARMS = {
0: 'LIN_V',
1: 'ANG_V',
}
DOWN = 4
UP = 5
TIMER_KA = pygame.USEREVENT
KA_PKT = Packet()
KA_PKT.write_ubyte(CMD.KEEPALIVE)
pygame.time.set_timer(TIMER_KA, 100)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
addr = (sys.argv[1], int(sys.argv[2]))
lasthat = None
vmap = [
[7, 0, 1],
[6, -1, 2],
[5, 4, 3],
]
clock = pygame.time.Clock()
while True:
for ev in pygame.event.get():
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_ESCAPE:
exit()
if ev.type == pygame.JOYHATMOTION and ev.joy == joy.get_id():
lasthat = time.time()
if ev.type == TIMER_KA:
sock.sendto(bytes(KA_PKT), addr)
if any(joy.get_button(i) for i in (UP, DOWN)):
cnt = 0
for but, var in PARMS.items():
if joy.get_button(but):
globals()[var] += (0.1 if joy.get_button(UP) else -0.1)
print(var, ':', globals()[var])
cnt += 1
if not cnt:
print('WARN: no parms modified, try pressing another button')
if lasthat is not None and time.time() - lasthat > 0.05:
hx, hy = joy.get_hat(0)
val = vmap[1 - hy][1 + hx]
print('snd', val)
if val >= 0 and val <= 6:
pkt = Packet()
pkt.write_ubyte(CMD.SOUND)
pkt.write_ubyte(val)
sock.sendto(bytes(pkt), addr)
lasthat = None
lv, av = joy.get_axis(LIN_AX), joy.get_axis(ANG_AX)
pkt = Packet()
pkt.write_ubyte(CMD.MOTION)
pkt.write_double(-LIN_V * lv)
pkt.write_double(-ANG_V * av)
sock.sendto(bytes(pkt), addr)
w, h = disp.get_size()
r = int(min(w / 2, h / 2))
disp.fill((0, 0, 0))
pygame.draw.circle(disp, (0, 0, 255), (int(w / 2), int(h / 2)), r, 1)
pygame.draw.rect(disp, (127, 0, 255), (int(w / 2 - r), int(h / 2 - r), 2*r, 2*r), 1)
pygame.draw.circle(disp, (255, 255, 255), (int(w / 2 + r * av), int(h / 2 + r * lv)), 5)
bottom = h
for but, var in PARMS.items():
if joy.get_button(but):
val = globals()[var]
surf = font.render('%s: %0.1f'%(var, val), True, (255, 255, 255))
disp.fill((0, 0, 255), (0, bottom - surf.get_height(), int(w * val / 5), surf.get_height()))
disp.blit(surf, (int(w / 2 - surf.get_width() / 2), bottom - surf.get_height()))
bottom -= surf.get_height()
pygame.display.flip()
clock.tick(30)