Advertisement
Guest User

Untitled

a guest
Jan 11th, 2019
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.44 KB | None | 0 0
  1. import serial, math
  2. from evdev import UInput, AbsInfo, ecodes as e
  3. from time import sleep
  4. from gpiozero import Button
  5. from alsaaudio import Mixer
  6.  
  7. #Variables
  8. serialInputDevice = '/dev/ttyUSB0'
  9. timeToSleep = .03333 # Time between loops
  10. joystickDividend = 12.0 # tanh(x/joystickDividend) to convert pulses to axis
  11.  
  12. ##########################################################################
  13. # Definitions that shouldnt be changed
  14. ##########################################################################
  15. def convertAxisData(data):
  16.     if data == '\x01' :
  17.         return 1
  18.     elif data == '\xfe' :
  19.         return -1
  20.     return 0
  21.  
  22. mouse = {
  23.     e.EV_KEY : [e.BTN_MOUSE],
  24.     e.EV_REL : [e.REL_X, e.REL_Y, e.REL_Z],
  25.     e.EV_MSC : [e.MSC_SCAN]
  26. }
  27.  
  28. joystick = {
  29.     e.EV_KEY : [e.BTN_START, e.BTN_SELECT, e.BTN_A, e.BTN_B, e.BTN_C ],
  30.     e.EV_ABS : [(e.ABS_X, AbsInfo(0, -32767, 32767, 0, 0, 0)),
  31.                 (e.ABS_Y, AbsInfo(0, -32767, 32767, 0, 0, 0)),
  32.                 (e.ABS_Z, AbsInfo(0, -32767, 32767, 0, 0, 0))]
  33. }
  34.  
  35. downA = False
  36. downB = False
  37. downC = False
  38. downStart = False
  39. downSelect = False
  40. downModeSelect = False
  41. joystickMode = True
  42. volumeLevel = -1
  43. ###########################################################################
  44. mix = Mixer('PCM')
  45. ser = serial.Serial(serialInputDevice,100000)
  46. uiMouse = UInput(mouse, name='A1UP-Trackball-Spinner', version = 0x3)
  47. uiJoystick = UInput(joystick, name='A1UP-Centipede-Panel', version = 0x3)
  48.  
  49. buttonA = Button(5)
  50. buttonB = Button(6)
  51. buttonC = Button(13)
  52. buttonStart = Button(19)
  53. buttonSelect = Button(26)
  54. switchVolA = Button(20)
  55. switchVolB = Button(21)
  56.  
  57. while 1:
  58.     #K, let's check buttons. Start with mode change
  59.     if buttonSelect.is_pressed and buttonA.is_pressed:
  60.         if not downModeSelect:
  61.             downModeSelect = True
  62.             joystickMode = not joystickMode
  63.             if joystickMode:
  64.                 print("Joystick Mode ON")
  65.             else:
  66.                 print("Mouse Mode ON")
  67.     else:
  68.         downModeSelect = False
  69.  
  70.     if buttonA.is_pressed and not downModeSelect:
  71.         if not downA:
  72.             downA = True
  73.             if joystickMode:
  74.                 uiJoystick.write(e.EV_KEY, e.BTN_A,1)
  75.                 uiMouse.write(e.EV_KEY, e.BTN_MOUSE,0)
  76.             else:
  77.                 uiMouse.write(e.EV_KEY, e.BTN_MOUSE,1)
  78.                 uiJoystick.write(e.EV_KEY, e.BTN_A,0)
  79.     elif downA:
  80.         downA = False
  81.         uiMouse.write(e.EV_KEY, e.BTN_MOUSE,0)
  82.         uiJoystick.write(e.EV_KEY,e.BTN_A,0)
  83.  
  84.     if buttonB.is_pressed:
  85.         if not downB:
  86.             downB = True
  87.             uiJoystick.write(e.EV_KEY, e.BTN_B,1)
  88.     elif downB:
  89.         downB = False
  90.         uiJoystick.write(e.EV_KEY,e.BTN_B,0)
  91.  
  92.     if buttonC.is_pressed:
  93.         if not downC:
  94.             downC = True
  95.             uiJoystick.write(e.EV_KEY, e.BTN_C, 1)
  96.     elif downC:
  97.         downC = False
  98.         uiJoystick.write(e.EV_KEY,e.BTN_C,0)
  99.  
  100.     if buttonStart.is_pressed:
  101.         if not downStart:
  102.             downStart = True
  103.             uiJoystick.write(e.EV_KEY, e.BTN_START, 1)
  104.     elif downStart:
  105.         downStart = False
  106.         uiJoystick.write(e.EV_KEY, e.BTN_START, 0)
  107.  
  108.     if buttonSelect.is_pressed:
  109.         if not downSelect:
  110.             downSelect = True
  111.             uiJoystick.write(e.EV_KEY, e.BTN_SELECT, 1)
  112.     elif downSelect:
  113.         downSelect = False
  114.         uiJoystick.write(e.EV_KEY, e.BTN_SELECT, 0)
  115.  
  116.     if switchVolA.is_pressed:
  117.         if volumeLevel != 0:
  118.             volumeLevel = 0
  119.             mix.setvolume(0)
  120.     elif switchVolB.is_pressed:
  121.         if volumeLevel != 2:
  122.             volumeLevel = 2
  123.             mix.setvolume(90)
  124.     else:
  125.         if volumeLevel != 1:
  126.             volumeLevel = 1
  127.             mix.setvolume(60)
  128.  
  129.     # Now do the trackball stuff
  130.     lengthOfBuffer = ser.in_waiting
  131.     bytesRead = ser.read(lengthOfBuffer)
  132.     deltaX = 0
  133.     deltaY = 0
  134.     deltaZ = 0
  135.  
  136.     iterator = 0
  137.     loopTimes = math.floor(lengthOfBuffer/4)
  138.     #buffer should always be a multiple of 4, but sometimes we drop bytes.
  139.     #if that happens, we don't want a fraction on the loop.
  140.     while iterator < loopTimes :
  141.         if bytesRead[iterator*4] != '\xff':
  142.             #The 4 byte packet always starts with 255, if we dropped some bytes
  143.             #and it's no longer in sync it's just easier to end the loop.
  144.             break
  145.         deltaX += convertAxisData(bytesRead[iterator*4+1])
  146.         deltaY += convertAxisData(bytesRead[iterator*4+2])
  147.         deltaZ += convertAxisData(bytesRead[iterator*4+3])
  148.        
  149.         iterator+=1
  150.  
  151.     #We use the deltas to simulate the mouse/joystick!
  152.     if joystickMode:
  153.         uiJoystick.write(e.EV_ABS, e.ABS_X, int(math.tanh(deltaX/joystickDividend)*32767))
  154.         uiJoystick.write(e.EV_ABS, e.ABS_Y, int(math.tanh(deltaY/joystickDividend)*32767))
  155.         uiJoystick.write(e.EV_ABS, e.ABS_Z, int(math.tanh(deltaZ/joystickDividend)*32767))
  156.         uiMouse.write(e.EV_REL, e.REL_X, 0)
  157.         uiMouse.write(e.EV_REL, e.REL_Y, 0)
  158.         uiMouse.write(e.EV_REL, e.REL_Z, 0)
  159.     else:
  160.         uiMouse.write(e.EV_REL,e.REL_X, deltaX)
  161.         uiMouse.write(e.EV_REL,e.REL_Y, deltaY)
  162.         uiMouse.write(e.EV_REL,e.REL_Z, deltaZ)
  163.         uiJoystick.write(e.EV_ABS, e.ABS_X, 0)
  164.         uiJoystick.write(e.EV_ABS, e.ABS_Y, 0)
  165.         uiJoystick.write(e.EV_ABS, e.ABS_Z, 0)
  166.     #Sync our fake inputs
  167.     uiMouse.syn()
  168.     uiJoystick.syn()
  169.  
  170.     sleep(timeToSleep)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement