Dobz
Dobz

Reputation: 1213

Comparion of values won't work without delay

I'm writing an application that will time mouse latency for our product.

The way it works is that it sends a mouse movement, and measures the time between then and when we get a pixel change on screen.

Why does this delay affect the program.

int check_for_pixel_change() {

    // Gets the pixels R value
    unsigned char value = *((unsigned char *) (0x100));

    // If this delay is not here then the loop will always return 1
    usleep(5);

    if(value == (0x80)) return 0;
    else return 1;
}

int main() {
    // Send move / start timer

    while(check_for_pixel_change());

    // stop timer
    return 0;
}

Upvotes: 2

Views: 72

Answers (1)

Dobz
Dobz

Reputation: 1213

Here is the code that worked.

Thanks to @barak-manos && @santosh-a.

I needed to change the char to volatile because it would be constantly changing by a different program.

int check_for_pixel_change() {

    // Gets the pixels R value
    volatile unsigned char value = *(volatile unsigned char *) (0x100);

    if(value == (0x80)) return 0;
    else return 1;
}

int main() {
    // Send move / start timer

    while(check_for_pixel_change());

    // stop timer
    return 0;
}

Upvotes: 1

Related Questions