Пример #1
0
def init(mapping, **kwargs):
    global axis_mapping, extreme_values, imperfection_offsets, joystick, DO_CALIBRATE
    choice = 0

    if "joystick" in kwargs.iteritems():
        choice = kwargs.iteritems()["joystick"]

    pygame.init()
    pygame.joystick.init()
    if pygame.joystick.get_count() == 0:
        return None

    print "Number of joysticks:" + str(pygame.joystick.get_count())
    #Copy axis mapping
    axis_mapping = mapping

    #init joystick
    joystick = pygame.joystick.Joystick(choice)
    joystick.init()

    #initialise final values
    for i in range(joystick.get_numaxes()):
        extreme_values.append([0, 0])
        imperfection_offsets.append(0.0)

    if "calibration" in kwargs.iteritems() and kwargs.iteritems(
    )["calibration"]:
        extreme_values = kwargs.iteritems()["calibration"]["boundaries"]
        imperfection_offsets = kwargs.iteritems()["calibration"]["offsets"]
        DO_CALIBRATE = False

    print "Axis count:" + str(joystick.get_numaxes())
    print "Joystick name:" + str(joystick.get_name())
    return joystick
Пример #2
0
    def __init__(self, joystick, mapping = None):
        self.no_stick = joystick is None
        if self.no_stick:
            self.joystick = NoStick()
        else:
            self.joystick = joystick

        self.name = joystick.get_name().strip()
        self.name_with_os = self.name+"__"+platform.system()
        self.mapping_name = None
        if mapping is not None:
            self.mapping = mapping
            self.mapping_name = "own"
        elif self.name_with_os in joystick_mappings.keys():
            self.mapping = joystick_mappings[self.name_with_os]
            self.mapping_name = self.name_with_os
        elif self.name in joystick_mappings.keys():
            self.mapping = joystick_mappings[self.name]
            self.mapping_name = self.name
        else:
            self.mapping = {}
        #print(self.mapping)

        self._A = self.mapping.get('A')
        self._B = self.mapping.get('B')
        self._X = self.mapping.get('X')
        self._Y = self.mapping.get('Y')
        self._select = self.mapping.get('SELECT')
        self._start  = self.mapping.get('START')
        self._shoulder_left  = self.mapping.get('SH_LEFT')
        self._shoulder_right = self.mapping.get('SH_RIGHT')
        self._axis_x = self.mapping.get('AXIS_X')
        self._axis_y = self.mapping.get('AXIS_Y')
Пример #3
0
 def __init__(self):
     pygame.joystick.init()
     pygame.joystick.get_init()
     self.count = pygame.joystick.get_count()
     if self.count > 0:
         self.joystick = pygame.joystick.Joystick(0)
         self.joystick.init()
         print '%d joystick(s) connected: %s' % (self.count, joystick.get_name())
         self.num_axes = joystick.get_numaxes()
         self.num_buttons = joystick.get_numbuttons()
         self.num_hats = joystick.get_numhats()
         print 'Joystick has %d axes, %d buttons and %d hats.' % (num_axes, num_buttons, num_hats)
         pygame.event.pump()
         self.old_axis = []
         for num in range(self.num_axes):
             self.old_axis.append(0.0)
Пример #4
0
def run():

    pygame.init()
    if not pygame.joystick.get_count():
        print 'ERROR: joystick count is 0'
        exit()

    # dasboard_app = subprocess.Popen('python app.py d')

    joystick = pygame.joystick.Joystick(0)
    joystick.init()

    joystick_num_buttons = joystick.get_numbuttons()
    joystick_num_axes = joystick.get_numaxes()

    print 'joystick found:', joystick.get_name()
    print 'count buttons:', joystick_num_buttons
    print 'count axes:', joystick_num_axes

    # кнопки
    JOY_STATE.extend([0 for jb in JoyButtons.BUTTONS])
    # ползунки
    JOY_STATE.extend([-1.0 if ja in (JoyButtons.JOY_LT, JoyButtons.JOY_RT) else 0.0 for ja in JoyButtons.JOYS])

    sender = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
    sender.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    sender.bind((settings.SOCKET_CLIENT_HOST, settings.SOCKET_CLIENT_PORT))

    # нам нужны только события джойстика
    pygame.event.set_allowed([JOYAXISMOTION, JOYBUTTONUP, JOYBUTTONDOWN, QUIT])

    get = pygame.event.get
    sleep = time.sleep

    send_to = sender.sendto
    host = settings.SERVER_HOST
    port = settings.SERVER_PORT

    while True:
        # бесконечный сбор событий с геймпада
        if process_events(get(), joystick_num_buttons):
            # print(JOY_STATE)
            send_to(','.join(str(i) for i in JOY_STATE), (host, port))
        else:
            break
        sleep(0.1)
Пример #5
0
    def __init__(self):
        print("")
        print("Joystick initializing...")
        if pygame.joystick.get_count() == 0:
            print("ERROR! Did not found a joystick!")
            time.sleep(2)
            print("")
            print("Keyboard initializing...")
            self.keyboardcontrole()
        else:
            joystick = pygame.joystick.Joystick(0)
            joystick.init()

            print("Joysticks gevonden:", pygame.joystick.get_count(), "(", joystick.get_name(), ")")
            print("Specs: assen:", joystick.get_numaxes(), ", knoppen:", joystick.get_numbuttons())
            self.knoppen = joystick.get_numbuttons()
            self.assen = joystick.get_numaxes()
            self.ps3Connected = True
Пример #6
0
def load_translations():
    global translations
    global translations_default
    global translations_sixaxis
    global joystick

    translations = {}
    #just sticking to the standard controls for now
    #try:
    #    filename = game.make_dataname('input')
    #    translations = pickle.load(open(filename, 'rb'))
    #except (IOError, OSError, KeyError):
    #print 'ERROR OPENING CONTROL FILE, loading defaults'
    translations = translations_default

    if joystick:
        if joystick.get_name(
        ) == "Sony Computer Entertainment Wireless Controller":
            translations.update(translations_sixaxis)
Пример #7
0
def goon():
    global q, q_gui
    if not dryrun:
        t = threading.Thread(target=gcodespitter)
        t.daemon = True
        t.start()

    st = 0
    b0 = b1 = b2 = b3 = 0
    correction = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

    def print_(x):
        print >> sys.stderr, x

    calibd = skip_calib  # False
    if calibaction == CALIB_LOAD:
        calibd = True
        with open(calibfile, "r") as f:
            correction = map(
                lambda j: map(float, j), map(lambda x: map(lambda z: z.strip(), x.split(",")), f.readlines())
            )

    pygame.joystick.init()  # initialize joystick module
    pygame.joystick.get_init()  # verify initialization (boolean)

    joystick_count = pygame.joystick.get_count()  # get number of joysticks
    # it's for some case, important to get the joystick number ...

    if joystick_count == 1:
        joystick = pygame.joystick.Joystick(0)
        joystick.get_name()
        joystick.init()
        joystick.get_init()

    while True:
        #      time.sleep(0.00001)

        if joystick_count == 1:

            # verify initialization (maybe cool to do some error trapping with this so    game doesn't crash
            pygame.event.pump()
            # So Now i can get my joystick Axis events

            xax_ = joystick.get_axis(0)
            yax_ = joystick.get_axis(1)
            zax_ = joystick.get_axis(3)

            buttons = joystick.get_numbuttons()
            b0 = joystick.get_button(0)
            b1 = joystick.get_button(1)
            b2 = joystick.get_button(2)
            b3 = joystick.get_button(3)

            if st == 1:
                print >> sys.stderr, "put joystick to center and press B0",
            elif st == 3:
                print >> sys.stderr, "put joystick to left and press B0",
            elif st == 5:
                print >> sys.stderr, "put joystick to right and press B0",
            elif st == 7:
                print >> sys.stderr, "put joystick to down and press B0",
            elif st == 9:
                print >> sys.stderr, "put joystick to up and press B0",
            elif st == 11:
                print >> sys.stderr, "put joystick to bottom and press B0",
            elif st == 13:
                print >> sys.stderr, "put joystick to top and press B0",

            if not calibd:
                st = calib(st, b0, (xax_, yax_, zax_), correction)
                print >> sys.stderr, correction
                if st == 14:
                    print >> sys.stderr, "CALIBRATED.",
                    calibd = True
                    if calibaction == CALIB_SAVE:
                        with open(calibfile, "w+") as f:
                            f.write("\n".join(map(lambda x: ",".join(map(str, x)), correction)))
                continue

            (xax, yax, zax) = correct(correction, (xax_, yax_, zax_))
        #          print >> sys.stderr, correction,
        #          print >> sys.stderr, (xax, yax, zax)
        if not dryrun:
            if b1 != b2:
                if b1:
                    # q.put('G1 Z10', block=False)
                    put_nonblock(q, "G1 Z%f" % (zax))
                if b2:
                    # q.put('G1 Z-10', block=False)
                    put_nonblock(q, "G1 Z%f" % (-zax))
                    # q.put('G1 Z-%f' % (zax), block=False)
            if b3:
                put_nonblock(q, "G1 X%f Y%f" % (xax, yax))
                # q.put('G1 X%f Y%f' % (xax, yax), block=False)

            print "q.join"
            # q.join()
        if b0:
            put_nonblock(q_gui, "save")
    sleep(1)
    print 'PROGRAMA REINICIADO COM SUCESSO!!!'
    sleep(1)
    print 'FASE DE COLETA INICIALIZADA! FORNECA OS COMANDOS PELO JOYSTICK!'
    #print grafo.g.es['weight']


#print grafo.g.es.get_attribute_values('weight')
# Initialize all imported Pygame modules (a.k.a., get things started)
pygame.init()

# Enable joystick support
pygame.joystick.init()

# Detect if joystick is available
joysticks = pygame.joystick.get_count()
if joysticks:
    sleep(1)
    print str(joysticks) + " joystick(s) detected!"

# Initialize the joystick
joystick = pygame.joystick.Joystick(0)
joystick.init()
name = joystick.get_name()
#print "Joystick " + str(0) + " name: " + name
sleep(1)
print 'FASE DE COLETA INICIALIZADA! FORNECA OS COMANDOS PELO JOYSTICK!'

# The game loop
while True:
    input(pygame.event.get())
Пример #9
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pygame.joystick

pygame.init()
pygame.joystick.init()

joystick_count = pygame.joystick.get_count()
print("Number of joysticks: ", joystick_count)

joystick = pygame.joystick.Joystick(0)
joystick.init()

name = joystick.get_name()
print(name)

axes = joystick.get_numaxes()
print("Nombre d'axes : ", axes)

buttons = joystick.get_numbuttons()
print("Nombre de boutons : ", buttons)

hats = joystick.get_numhats()
print("Nombres de HAT : ", hats)

running = True
while running:
    pygame.event.get()
    axis_0 = joystick.get_axis(0)
    axis_1 = joystick.get_axis(1)