Monday, November 29, 2010

Schrodenger's Computer

I was working on writing a Modbus Communication Driver for the 7395 and ran into some interesting behavior.

The device I needed to communicate with used the binary style protocol with CRC. This of course means that your data payload can and will have Null chars 0x00. Unfortunately, C/C++ functions that deal with strings or arrays of char will treat the first 0x00 as the end of the data and terminate at that point regardless of the actual size of the string.

At first, I wrote my own functions to ignore null chars by using a length parameter. This seemed to work well enough but I noticed that the data going over the port wasn't always right. Specifically, if I printed the chars with printf (using %x for each character), the data would be as expected. If I did not use printf I would get data that was consistently not what I was loading into the array.

It appeared that by observing the data I could affect it's content!

It actually turns out that this is true. The reason is that the %x argument in my printf (used to print a variable in hex format) was doing an in place cast from char to unsigned integer, thus if I did printf then broadcast the data it would be transmitted as expected.


I wound up reimplementing most of my code to use an array of integer in the first place to avoid this problem but still who would have thought?

Thursday, November 4, 2010

Yet another patch to TSLIB

Once you get everything running on the TS-TPC-73xx you'll probably notice some strange behavior from your touchscreen when you're trying to interact with applications.

When you first open a QT app and tap a widget, the mouse pointer doesn't seem to have a pre-defined location so everything works like it should that first tap. If you watch what is going on  in ts_test you can see that you'll get a few readings at the place you tap with pressure=255 then when you let up you will see a final reading at those X,Y coordinates and pressure=0.

Now when you tap on again what you actually get is this:
$Timestamp:     OLD Xpos     OLD Ypos     255
$Timestamp:     OLD Xpos     OLD Ypos     0
$Timestamp:     NEW Xpos     NEW Ypos      255
$Timestamp:     NEW Xpos     NEW Ypos      255
$Timestamp:     NEW Xpos     NEW Ypos      255
$Timestamp:     NEW Xpos     NEW Ypos      0

To fix it I modified the dejitter filter. In dejitter.c do the following

     Change this:
    if (djt->nr == 1)
        samp [count] = *s;

    To this:
       if (djt->nr  ==  1){
                        samp [count].tv = s->tv;
                        samp [count].x = s->x;
                        samp [count].y = s->y;
                        samp [count].pressure = 0;
                    }

Of course for this to work you need to make sure that you call the dejitter filter in your ts.conf file (if you use the settings I did this will already be taken care of for you).