import smbus
import time
import serial
import pygame
import time

bus = smbus.SMBus(1)

address = 0x04

pygame.init()
j = pygame.joystick.Joystick(0)
j.init()
speedr = 0.0
speedl = 0.0
directionr = 0
directionl = 0
i=0
    
try:
    while True:
        
        pygame.event.pump()
        time.sleep(0.1)
        value = j.get_axis(1)
        values = j.get_axis(4)
        if  value < -0.02 : 
            directionl = 0  
            speedl = value*-255
            print " Achse 1" , + value
            print "speed front" , + int(speedl)
        elif value > 0.00:
            directionl = 1
            speedl = value*255
        else:
            speedl = 0.0
            print "speed back" , + int(speedl)
        if values < 0.00 :
            directionr = 0
            speedr = values*-255
        elif values > 0.00:
            directionr = 1
            speedr = values*255
        else:                 
            speedr = 0.0
              
        
        
        bus.write_i2c_block_data(address,directionl,[directionr,int(speedl),int(speedr)])
        #time.sleep(1)

except KeyboardInterrupt:
     j.quit()
