+ Post New Thread
Results 1 to 2 of 2
  1. #1
    Member level 3
    Points: 1,036, Level: 7

    Join Date
    Sep 2009
    2 / 2

    Translating C code into words

    This is code for an Atmega32-based PS/2 mouse controlled with accelerometers. I'm looking at the function for generating the packet. Right now I'm only considering pointer movement, not scroll or buttons, so those have been omitted from the code sample below. I'm trying to figure out what this code is saying to do. Can anyone please help me interpret it? Just to check it to see if it's doing what it's supposed to. To me, what's confusing is the >> and <<.

    In particular, does anyone know how to translate statements like
    into words?
    If someone just does one of them please, that would help me figure out the rest.

    ///Generate a standard 4 byte Microsoft IntelliMouse: left, middle, right, x, y, z packet.
    void packetGen(void)
        unsigned char btnPacket=0x08;
        //the first byte is at least 0x08, because the third bit is always 1
    //scroll and button parts taken out
    //for now only looking at movement
     else if(realMoveFlag == TRUE)
            btnPacket = btnPacket | (((movement[Y_AXIS]>>7)&0x1)<<5) | (((movement[X_AXIS]>>7)&0x1)<<4) | ((middleBtn&0x1) <<2) | ((rightBtn&0x1)<<1) | (leftBtn&0x1);                             
            queuePut(movement[X_AXIS] << resolution);   
            queuePut(movement[Y_AXIS] << resolution);   
            //queuePut((movement[Z_AXIS]<2 && movement[Z_AXIS]>-2)?0:movement[Z_AXIS]&0x0f);
    X_AXIS, Y_AXIS, and Z_AXIS are the indices, which were declared as follows:
    enum{X_AXIS=0, Y_AXIS, Z_AXIS};
    If interested, here's the function that sets the realMoveFlag:

    //Obtain x, y, z movements.
    void getMovement(void)
        unsigned char i=0;   
        //position conversion formula.
        // xxxxxxxOLD (int)(voltage - first voltage(when not moving) * 100)
        // ADC reading - init adc reading(calibrating data)
        curPosition[X_AXIS] = (signed char)(xADC - xCal);
        curPosition[Y_AXIS] = (signed char)(yADC - yCal);
        curPosition[Z_AXIS] = (signed char)(zADC - zCal);
        //only when the mouse has significent movement we generate a packet
        //get the difference between the previous position and current position
            movement[i] = curPosition[i];// - lastPosition[i] ;
            //only check x and y movements       
                if(!(movement[i] < 3 || movement[i] > -3))
                    realMoveFlag = TRUE;               
                if(movement[i] > 3 || movement[i] < -3)
                realMoveFlag = TRUE;

    Here's the packet format:
    Microsoft "3-Button" IntelliMouse Z-Mode Report Format
    A Microsoft IntelliMouse 3-Button Z-mode motion report consists of a 4-byte packet defined as shown in Table 2.
    Table 2. PS/2 4-Byte Mouse Packets

           D7  D6   D5  D4  D3  D2  D1 D0
    Byte 1 Yov Xov Y8   X8   1  MB  RB  LB
    Byte 2 X7   X6   X5   X4 X3  X2  X1  X0
    Byte 3 Y7   Y6   Y5   Y4 Y3  Y2  Y1  Y0
    Byte 4 Zse Zse  Zse Zse  Zs  Z2  Z1  Z0
    Symbol definition:
    LB: State of the left button. Pressed = 1.
    RB: State of the right button. Pressed = 1.
    MB: State of the middle button. Pressed = 1.
    Xov, Yov: Overflow. Indicates more than 9 bits of movement detected. X8-X0 or Y8-Y0 should be set to maximum magnitude in the appropriate direction.
    X8-X0: 9-bit signed twos complement integer that presents the relative displacement of the device in the X direction since the last data transmission. A positive value indicates motion to the right; a negative value indicates motion to the left.
    Y8-Y0: Same as X8-X0. A positive value indicates device motion upward; a negative value indicates motion downward.
    Z2-Z0: Z-wheel motion. Twos complement with Z3, the sign bit (+7 and -8 counts maximum).
    Zs: Z-wheel twos complement sign bit.
    Zse: Z-wheel sign extension bits. Always the same as Z3. Used for compatibility with old drivers.
    MS Extension: Many Microsoft mice use 8-bit internal counters and sign extend the values into X8 and Y8. Microsoft mice do not use the Yov or Xov bits, and they set them to 0.

    P.S. For further info about this code, please refer to the following thread:
    I made a separate thread for this because it's a different topic, though it's the same code. Probably DineshSL and prmurthy are getting tired of this code, (particularly the latter), sorry I'm stuck with this...

    •   AltAdvertisement


  2. #2
    Super Moderator
    Points: 50,045, Level: 54
    7 years registered
    keith1200rs's Avatar
    Join Date
    Oct 2009
    Yorkshire, UK
    2075 / 2075

    Re: Translating C code into words

    >>x means shift right x times. So your statement earlier shifts right 7 times, ANDs it with 0x1 and then shifts left 5 times. Personally I would have just shifted right twice and ANDed with a different number.

    I started life with nothing and I've still got most of it left. (Seasick Steve)

    1 members found this post helpful.

--[[ ]]--