📜 ⬆️ ⬇️

We are playing with a thinkpad gyro on linux

In Linux, readings from this sensor can be read from the file / sys / devices / platform / hdaps / position
You can create a virtual joystick and, for example, roll balls into neverball, spin google earth, etc. :)
All this is described here.
I also decided to have fun and wrote a small program that causes cairo-clock to crawl around the screen depending on the tilt of the laptop.

I used wnck and pygtk to find and manage the window. To smooth movement I took the pyode physics engine (binding to the Open Dynamics Engine ).

 #! / usr / bin / python
 # coding: utf-8
 import gobject
 from os import system
 import gtk, wnck
 import ode

 STALE = (501,487) # values ​​in hdaps / position in normal position

 def hdaps ():
     pos = list (eval (open ('/ sys / devices / platform / hdaps / position'). read ()))
     pos [0] + = STALE [0]
     pos [1] + = STALE [1]
     if abs (pos [0]) <4: pos [0] = 0
     if abs (pos [1]) <4: pos [1] = 0
     if not any (pos): return 0
     return pos

 def find_clock (scr, body, world):
     "" "trying to find the cairo-clock window, if we don’t find it, run it
     and try to find again "" "
     clk = None
     for w in scr.get_windows ():
         if w.get_application (). get_name () == 'cairo-clock':
             print clk
             clk = w
             break
     if not clk:
         system ('cairo-clock &')
         gobject.timeout_add (500, find_clock, scr, body, world)
     else:
         print clk
         clk = gtk.gdk.window_foreign_new (clk.get_xid ())
         gscr = clk.get_screen ()
         body.setPosition ((list (clk.get_root_origin ()) + [1]))
         # we found a window and start a timer that will control it
         gobject.timeout_add (20, play, clk, body, world, gscr.get_width (), gscr.get_height ())

 def play (clk, body, world, sw, sh):
     gval = hdaps ()
     if gval:
         body.addForce ((gval + [0]))
     world.step (0.04)

     fx, fy, fz = body.getLinearVel ()
     body.addForce ((-fx / 2, -fy / 2.0)) # slow down movement

     # when you exit the edges of the screen, jump out from the opposite edge
     bx, by, bz = body.getPosition ()
     x, y = clk.get_position ()
     w, h = clk.get_size ()
     if x <- (w / 2): bx = sw- (w / 2)
     elif x> sw- (w / 2): bx = - (w / 2)
     if y <- (h / 2): by = sh- (h / 2)
     elif y> sh- (h / 2): by = - (h / 2)
     body.setPosition ((bx, by, bz))
     clk.move (int (bx), int (by))
     return true

 def main ():
     try: 
         scr = wnck.screen_get_default ()
         scr.get_windows ()
         world = ode.World ()
         world.setGravity ((0,0,0))
         body = ode.Body (world)
         M = ode.Mass ()
         M.setSphere (2500.0, 0.05)
         M.setBox (1000,250,250,1)
         M.mass = 0.5
         body.setMass (M)
         gobject.timeout_add (200, find_clock, scr, body, world)
         gtk.main ()
     except Exception, e: 
         print (e) 
         
 if __name__ == "__main__": 
     main ()


Here's what it looks like:
www.youtube.com/v/l_Ts2BESraI&hl=en - filmed with a fotikom for clarity
www.youtube.com/v/5oBpODqookY&hl=en_US&rel=0 - video from the screen
')
Another small script that turns the screen if you tilt it strongly. (The one on the ibm site is not working)
 #! / usr / bin / env python
 # coding: utf-8
 import os, sys
 from time import sleep

 ROTATE_ON = 100
 STALE_VALUE = 500 # first value in hdaps / position in normal position

 def rotate (where):
     return os.system ('xrandr - output LVDS - rotate% s'% where)

 def readx ():
     return int (open ('/ sys / devices / platform / hdaps / position'). read (). split (',') [0] .lstrip ('(')) + STALE_VALUE

 if __name__ == '__main__':
     while true:
         sleep (0.1)
         x = readx ()
         if x <-ROTATE_ON: rotate ('right')
         elif x> ROTATE_ON: rotate ('left')
         else: rotate ('normal')

Source: https://habr.com/ru/post/81027/


All Articles