Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Translating C code into words

Status
Not open for further replies.

o_0

Member level 3
Joined
Sep 30, 2009
Messages
58
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,873
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
(((movement[Y_AXIS]>>7)&0x1)<<5)
into words?
If someone just does one of them please, that would help me figure out the rest.

Code:
///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(btnPacket);
        queuePut(movement[X_AXIS] << resolution);   
        queuePut(movement[Y_AXIS] << resolution);   
        queuePut(0x00);
        //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:
Code:
enum{X_AXIS=0, Y_AXIS, Z_AXIS};

If interested, here's the function that sets the realMoveFlag:

Code:
//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
    for(i=0;i<3;i++)
    {
        movement[i] = curPosition[i];// - lastPosition[i] ;

        //only check x and y movements       
        if(i<2)
        {                             
            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

Code:
       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:
https://www.edaboard.com/threads/201730/
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...
 

>>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.

Keith
 
  • Like
Reactions: o_0

    o_0

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top