Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import pygame
- from subprocess import call
- import glob
- import serial
- from threading import Thread
- import time
- tasti_input = {3:'Quadrato',0:'Triangolo',1:'Cerchio',2:'X',5:'R1',4:'L1',9:'Start',8:'Select',7:'R2',6:'L2'}
- tasti_frecce = { (0,0):'Stop',(0,1):'Su' , (-1,0):'Sinistra',(0,-1):'Giu',(1,0):'Destra'}
- keyboard_pressed = 2
- keyboard_released = 3
- EXIT = 27
- done = False
- key=0
- #stop_frecce = (0,0)
- in_reading = False
- speed = 1
- max_speed = 4
- stop_speed = 0
- speed_vector = ['0','1','2','3']
- def serial_ports():
- """ Lists serial port names
- :raises EnvironmentError:
- On unsupported or unknown platforms
- :returns:
- A list of the serial ports available on the system
- """
- if sys.platform.startswith('win'):
- ports = ['COM%s' % (i + 1) for i in range(256)]
- elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
- # this excludes your current terminal "/dev/tty"
- ports = glob.glob('/dev/tty.usb[A-Za-z]*')
- # ports = glob.glob('/dev/tty[A-Za-z]*')
- elif sys.platform.startswith('darwin'):
- # ports = glob.glob('/dev/tty')
- ports = glob.glob('/dev/tty.usb*')
- else:
- raise EnvironmentError('Unsupported platform')
- result = []
- for port in ports:
- try:
- s = serial.Serial(port)
- s.close()
- result.append(port)
- except (OSError, serial.SerialException):
- pass
- return result
- def get_port(bR,check=False):
- name_port = serial_ports()
- if len(name_port) ==0:
- print("ATTENZIONE STM NON COLLEGATO")
- return None
- else:
- ser = serial.Serial(name_port.pop(), baudrate=bR) # open serial port
- # ser.open()
- if check:
- print(ser)
- return ser
- def read(ser,data_r):
- global in_reading
- while in_reading :
- tmp = ser.read(1)
- if (tmp != '\n'):
- data_r = data_r + tmp
- else:
- print(data_r)
- # time.sleep(1)
- data_r = ""
- def read_on(tr):
- tr.start()
- def read_off(tr):
- tr._stop()
- """Configurazione joystick"""
- pygame.init()
- pygame.joystick.init()
- joystick_count = pygame.joystick.get_count()
- joystick_init = pygame.joystick.get_init()
- print("Numero joystick = : "+str(joystick_count))
- if joystick_count>=1:
- print("Joystick inizializzato = : " + str(joystick_init))
- stick = pygame.joystick.Joystick(0)
- stick.init()
- clock = pygame.time.Clock()
- """Configurazione UART"""
- ser = get_port(115200, True)
- """data_r = ""
- tr = Thread(target=read, args=(ser, data_r))"""
- while not done:
- for event in pygame.event.get():
- # pulsanti joystick premuti
- if event.type == pygame.JOYBUTTONDOWN:
- button_key = event.button
- if(tasti_input.has_key(button_key)):
- print("Joystick button pressed : " + tasti_input[event.button])
- if tasti_input[button_key] == 'Start':
- print("Uscita in corso...")
- done = True
- elif tasti_input[button_key] == 'Select':
- if not in_reading:
- in_reading = True
- data_r = ""
- tr = Thread(target=read, args=(ser, data_r))
- read_on(tr)
- else:
- in_reading = False
- elif tasti_input[button_key] == 'R1' and speed < max_speed - 1 :
- speed = speed + 1
- # ser.write(speed_vector[speed])
- elif tasti_input[button_key] == 'L1' and speed > 1 :
- speed = speed - 1
- # ser.write(speed_vector[speed])
- else:
- print("Joystick button pressed : " + str(event.button))
- # freccette direzionali premute
- elif event.type == pygame.JOYHATMOTION:
- button_key = event.value
- if (tasti_frecce.has_key(event.value)):
- print("Joystick button pressed : " + tasti_frecce[button_key])
- if tasti_frecce[button_key] == 'Su':
- ser.write(speed_vector[speed])
- elif tasti_frecce[button_key] == 'Giu' or tasti_frecce[button_key] == 'Stop':
- ser.write(speed_vector[stop_speed])
- else:
- print("Joystick button pressed : " + str(event.value))
- # Comandi da tastiera
- elif event.type == keyboard_pressed:
- key_pressed = event.key
- if key_pressed == EXIT:
- print("Uscita in corso...")
- done = True
- elif not key_pressed < 0 and not key_pressed >= 127:
- char_key = chr(key_pressed)
- print("Pressed keyboard button " + str(char_key))
- if char_key == 'r' and not in_reading:
- in_reading = True
- data_r = ""
- tr = Thread(target=read, args=(ser, data_r))
- read_on(tr)
- elif char_key == 'r' and in_reading:
- in_reading = False
- else:
- print(event)
- # else:
- # print(event)
- clock.tick(600)
- pygame.joystick.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement