0

I need to poll USB mouse movement and have the code run only when the mouse is moving.

Firstly I've taught myself Python in about 5 days so apologies if the coding is bad. Secondly I'm using PyUSB on a Raspberry Pi running Stretch.

I've written some Python code that allows me to interface a USB mouse with an old serial type connector like for an old commodore (except its a different type of protocol). The code works and outputs to the GPIO how I need it too, however the data array holds its last value, this causes my code see that as movement so the mouse is never still on the screen. I've tried zeroing the array (gets overwritten next iteration), tried storing the data at the end of the loop and comparing the two arrays but can't get that to work.

How do I make the code only update when the mouse is moving? Can I poll the Mouse movement somehow?

#Start USB Data Collection
while 1:
    try:
        data = dev.read(endpoint.bEndpointAddress,endpoint.wMaxPacketSize)
        #Extract data we need from array

        B = data[0]                 # Button Data
        X = data[1]                 # X Data
        Y = data[2]                 # Y Data
        S = data[3]                 # Scroll Wheel

        IO.output(dataPinBL, 1)     #Set Mouse Buttons to HIGH
        IO.output(dataPinBR, 1)     #Set Mouse Buttons to HIGH
        IO.output(dataPinST, 1)     #Turn on Status LED
##        print('START')

        #Debugging only
        print (data)               #Testing Only- Uncomment to see outputs

        if Y == 0:
            uFreq=0
            dFreq=0
            YD.ChangeDutyCycle(dFreq)
            YU.ChangeDutyCycle(uFreq)
            print("Y Zero") 

       if X == 0:
           rFreq=0
           lFreq=0
           XL.ChangeDutyCycle(lFreq)
           XR.ChangeDutyCycle(rFreq)
           print("X Zero")

    #X Movement
    if X >> 0:
        if X <= 127:
            rFreq=((X/127.0)*100.0)
            XR.ChangeDutyCycle(rFreq)
            print(X, "RIGHT",data) #Testing Only



        elif X >= 128:
            lFreq= 200-0.78*X
            XL.ChangeDutyCycle(lFreq)
            print("LEFT",data)     #Testing Only



    #Y Movement   
        if Y >> 0:
            if Y <= 127:
                dFreq=((Y/127.0)*100.0)
                YD.ChangeDutyCycle(dFreq)
              ##print("DOWN",dFreq)     #Testing Only



        elif Y >=128:
            uFreq= 200-0.78*Y
            YU.ChangeDutyCycle(uFreq)
            print("UP",uFreq)       #Testing Only


    Y=0
    X=0

    #Button Presses   
    if B == 1:
        IO.output(dataPinBL , 0)
        print("LEFT CLICK")         #Testing Only
        sleep(0.0)

    elif B == 2:
        IO.output(dataPinBR, 0)
        print("RIGHT CLICK")        #Testing Only
        sleep(0.0)

except usb.core.USBError as e:
    data = None
    if e.args == ('Operation timed out',):
        print ('Time Out')
    continue
    break

#End USB Data Collection      
#release Mouse back to OS

I expect the array (or at least the outputs) to stay at 0 until the mouse is moved but cannot figure out how to achieve this.

0 Answers0