Demo entry 6783227

pygame

   

Submitted by anonymous on Feb 04, 2019 at 03:35
Language: Python. Code size: 1.9 kB.

import pygame, sys
import time


discon = False
def check_pad(joystick,depth):
    global discon
    if depth > 20:
        return "null"
    depth=depth+1
    pygame.joystick.quit()
    pygame.joystick.init()
    joystick_count = pygame.joystick.get_count()
    for i in range(joystick_count):
        joystick = pygame.joystick.Joystick(i)
        joystick.init()
    if not joystick_count: 
        if not discon:
           print "reconnect you meat bag"
           discon = True
        time.sleep(5)
        check_pad(joystick,depth)
    else:
        discon = False
        #print 'Initialized Joystick : %s' % joystick.get_name()
        return joystick.get_name()


# Initialise the pygame library
pygame.init()

joystick=None

name=check_pad(joystick,0)

print name

if name == "null":
    quit()


# Only start the motors when the inputs go above the following threshold
threshold = 0.60


LeftTrack = 0
RightTrack = 0


Checkperiod=3000000
currenttick=1


# Try and run the main code, and in case of failure we can stop the motors
try:

    # This is the main loop
    while True:
        if currenttick >= Checkperiod:
            check_pad(joystick,0)
            currenttick=1
            print "checking pad....."
            
        elif currenttick < Checkperiod:
            currenttick=currenttick+1
            #print "current tick is... %d"%currenttick
        
            
        # Check for any queued events and then process each one
        events = pygame.event.get()
        for event in events:
          if event.type == pygame.JOYBUTTONDOWN:
              print event.button
              
          # Check if one of the joysticks has moved
          if event.type == pygame.JOYAXISMOTION:
              print "joy: %f,axis: %f, value :%f"%(event.joy,event.axis,event.value)

           


except KeyboardInterrupt:
    joystick.quit()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).