14Nov

New work: Autorotate for the MS Surface Pro 3 running Linux & Krita

Posted by Elf Sternberg as programming, python

New work: autorotate. As I mentioned earlier, I managed to score a brand new Microsoft Surface Pro 3 for $230 (not including the additional cost of a new keyboard, stylus, and power supply… sigh) from a woman who much preferred her MacBook and was moving and had to get rid of everything she didn’t want to carry. I even checked with Microsoft to see if it was registered as stolen, and bought it on the spot.

If you see my earlier post about getting it working, you can see that I liked Ayko Poel’s autorotate script. And it’s true, I did… until I bought a stylus and tried to draw on the thing in portrait mode. Poel’s scripts broke. There’s a bit of deep, almost occult esoterica about mapping screen touches to drawing coordinates, especially when rotating the screen, and Poel’s scripts failed at the intersection of advanced touch devices (the pen and the eraser) and coordinate transformation.

So I fixed it.

Along the way, I removed the huge horkin’ if statement and replaced it with a transformation matrix which encapsulates all the rules quite nicely. It actually doesn’t change the line count (sadly): Detecting if you’re in landscape or portrait is two expressions, and which landscape or portrait is two more expressions each, followed by a four-line table that maps these rules to transformations and labels. But I think it’s a hell of a lot more maintainable. I also fixed the palm rejection code (which is now a heck of a lot shorter) to understand that the pen and eraser both require palm rejection.

I changed device recognition to be a heck of a lot more robust, and I used Python’s namedtuple to make this all much more readable. I used a lot of regular expressions— but then, I’ve been good at regular expressions since 1993— and that lets me recognize the devices, the drivers, and everything else without any fiddling or meddling.

1 Response to New work: Autorotate for the MS Surface Pro 3 running Linux & Krita

Greg Bell

November 18th, 2017 at 2:00 pm

Hi Elf,

Holey moley that’s cleaner. I’m giving it a good go-over to learn from it.

Named tuples – good tip.

You’re using enough os.system calls that you might really like https://amoffat.github.io/sh/

My only confusions/criticism (if I may be so bold) is that I have no idea what lsn, lsx, lsi, ptx, ptl, and ptr stand for. ls = landscape, pt = portrait, sure, but n, x, i, and r? Maybe if I had more domain knowledge I’d know right way.

Also, the magic numbers – 65000, 64700, 64500, 64000, 1100, 1000, 800, 650…

Thanks for sharing that! Good stuff. I always learn something new when reading others’ Python code.

Comment Form

Subscribe to Feed

Categories

Calendar

November 2017
M T W T F S S
« Oct    
 12345
6789101112
13141516171819
20212223242526
27282930