Beispiel #1
0
class Joypad:
    """ Joypad """
    def __init__(self):
        """ init """
        self.x = 120
        self.y = 120
        self.z = 240 - 64
        self.b = 0
        self.lastUpdateTime = time.time()
        self.running = True
        self.display = Display()
        self.screen = self.display.screen

        self.loadImages()
        self.connectBot()
        self.clock = pygame.time.Clock()

    def loadImages(self):
        self.logo = pygame.image.load(
            basedir + "/images/cannybots_logo_small.png").convert_alpha()
        self.joystick = pygame.image.load(
            basedir + "/images/joystick.png").convert_alpha()
        self.knob = pygame.image.load(basedir +
                                      "/images/knob.png").convert_alpha()
        self.button = pygame.image.load(basedir +
                                        "/images/button.png").convert_alpha()

    def connectBot(self):
        self.ble = BLE()
        if botName != "":
            self.myBot = self.ble.findNearest()
        else:
            self.myBot = self.ble.findByName(botName)
        self.joypadClient = SimpleJoypadClient(self.myBot)

    def updateJoypad(self, (newX, newY), force):
        if (newX > 320 - 64) and newY < 240 - 32 and newY > 32:
            self.z = newY - 32

        if newX < 240:
            self.x = newX
            self.y = newY

        if force:
            self.z = 240 - 64

        if force or (time.time() - self.lastUpdateTime) > 0.05:
            x = arduino_map(self.x - 120, -120, 120, -255, 255)
            y = arduino_map(self.y - 120, -120, 120, -255, 255)
            z = arduino_map(240 - 64 - self.z, 0, 240 - 32, 0, -255)
            self.joypadClient.updateJoypadWithZ(x, y, z * zMultiplier, self.b)
            self.lastUpdateTime = time.time()
Beispiel #2
0
class Joypad:
    """ Joypad """
    
    def __init__ (self):
        """ init """
        self.x  = 120
        self.y  = 120
        self.z  = 240-64
        self.b  = 0
        self.lastUpdateTime = time.time()
        self.running = True  
        self.display = Display()
        self.screen  = self.display.screen
        
        self.loadImages()
        self.connectBot()
        self.clock = pygame.time.Clock()
    
    
    def loadImages(self):
        self.logo        = pygame.image.load(basedir+"/images/cannybots_logo_small.png").convert_alpha()
        self.joystick    = pygame.image.load(basedir+"/images/joystick.png").convert_alpha()
        self.knob        = pygame.image.load(basedir+"/images/knob.png").convert_alpha()
        self.button      = pygame.image.load(basedir+"/images/button.png").convert_alpha()
    
    
    def connectBot(self):
        self.ble   = BLE() 
        if botName != "":
            self.myBot = self.ble.findNearest()   
        else:
            self.myBot = self.ble.findByName(botName)   
        self.joypadClient = SimpleJoypadClient(self.myBot)

    def updateJoypad(self, (newX,newY), force):   
        if (newX>320-64) and newY<240-32 and newY>32:
            self.z=newY-32 
   
        if newX<240:
            self.x = newX
            self.y = newY 			
            
        if force:
			self.z=240-64;
	

        if force or (time.time() - self.lastUpdateTime) > 0.05:
			x = arduino_map(self.x-120, -120, 120, -255,255)
			y = arduino_map(self.y-120, -120, 120, -255,255)    
			z = arduino_map(240-64-self.z, 0, 240-32, 0, -255)    
			self.joypadClient.updateJoypadWithZ(x, y, z*zMultiplier,self.b) 
			self.lastUpdateTime = time.time()
Beispiel #3
0
def main():
    xAxis = 0
    yAxis = 0
    lastUpdateTime = time.time()
    joysticks = []
    clock = pygame.time.Clock()
    keepPlaying = True
    ble = BLE()
    myBot = ble.findNearest()
    joypadClient = SimpleJoypadClient(myBot)

    # for al the connected joysticks
    for i in range(0, pygame.joystick.get_count()):
        # create an Joystick object in our list
        joysticks.append(pygame.joystick.Joystick(i))
        # initialize them all (-1 means loop forever)
        joysticks[-1].init()
        # print a statement telling what the name of the controller is
        print "Detected joystick '", joysticks[-1].get_name(), "'"
    while keepPlaying:
        if (time.time() - lastUpdateTime) > 0.05:
            joypadClient.updateJoypadWithZ(int(xAxis * 255), int(yAxis * 255),
                                           0, 0)
            lastUpdateTime = time.time()
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print "Received event 'Quit', exiting."
                keepPlaying = False
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                print "Escape key pressed, exiting."
                keepPlaying = False
            elif event.type == pygame.JOYAXISMOTION:
                #print "Joystick '",joysticks[event.joy].get_name(),"' axis",event.axis,"motion."
                if event.axis == 0:
                    xAxis = joysticks[-1].get_axis(0)
                if event.axis == 1:
                    yAxis = joysticks[-1].get_axis(1)
            elif event.type == pygame.JOYBUTTONDOWN:
                print "Joystick '", joysticks[
                    event.joy].get_name(), "' button", event.button, "down."
            elif event.type == pygame.JOYBUTTONUP:
                print "Joystick '", joysticks[
                    event.joy].get_name(), "' button", event.button, "up."
            elif event.type == pygame.JOYHATMOTION:
                print "Joystick '", joysticks[
                    event.joy].get_name(), "' hat", event.hat, " moved."
Beispiel #4
0
def main(): 
    xAxis=0
    yAxis=0
    lastUpdateTime = time.time()
    joysticks = []
    clock = pygame.time.Clock()
    keepPlaying = True
    ble   = BLE()
    myBot = ble.findNearest()
    joypadClient = SimpleJoypadClient(myBot)
 
    # for al the connected joysticks
    for i in range(0, pygame.joystick.get_count()):
        # create an Joystick object in our list
        joysticks.append(pygame.joystick.Joystick(i))
        # initialize them all (-1 means loop forever)
        joysticks[-1].init()
        # print a statement telling what the name of the controller is
        print "Detected joystick '",joysticks[-1].get_name(),"'"
    while keepPlaying:
        if (time.time() - lastUpdateTime) > 0.05:
                joypadClient.updateJoypadWithZ(int(xAxis*255), int(yAxis*255), 0,0) 
		lastUpdateTime=time.time() 
        clock.tick(60)
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    print "Received event 'Quit', exiting."
                    keepPlaying = False
                elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    print "Escape key pressed, exiting."
                    keepPlaying = False
                elif event.type == pygame.JOYAXISMOTION:
                    #print "Joystick '",joysticks[event.joy].get_name(),"' axis",event.axis,"motion."
                    if event.axis==0:
                        xAxis=joysticks[-1].get_axis(0)
                    if event.axis==1:
                        yAxis=joysticks[-1].get_axis(1)
                elif event.type == pygame.JOYBUTTONDOWN:
                    print "Joystick '",joysticks[event.joy].get_name(),"' button",event.button,"down."
                elif event.type == pygame.JOYBUTTONUP:
                    print "Joystick '",joysticks[event.joy].get_name(),"' button",event.button,"up."
                elif event.type == pygame.JOYHATMOTION:
                    print "Joystick '",joysticks[event.joy].get_name(),"' hat",event.hat," moved."
Beispiel #5
0
#!/usr/bin/python
#
# Cannybots Scratch integration
# By Wayne Keenan 02/12/2014
# www.cannybots.com 

from scratra import *

from time import sleep
from cannybots.radio import BLE
from cannybots.clients.joypad  import SimpleJoypadClient

ble   = BLE() 
myBot = ble.findNearest()   
joypadClient = SimpleJoypadClient(myBot)


motorASpeed = 0
motorBSpeed = 0



@start
def whenstart(scratch):
  print 'Scratch connection started.'

@end
def whenend(scratch):
  print 'Scratch connection ended'

Beispiel #6
0
#!/usr/bin/python

from time import sleep
from cannybots.radio import BLE
from cannybots.clients.joypad import SimpleJoypadClient

ble = BLE()
myBot = ble.findNearest()
joypadClient = SimpleJoypadClient(myBot)

joypadClient.updateJoypad(255, 255, 0)
sleep(1)

joypadClient.updateJoypad(-255, -255, 0)
sleep(1)

joypadClient.updateJoypad(0, 0, 0)
sleep(2)