User:Ojw/Notes

From Openmoko

< User:Ojw
Revision as of 21:33, 15 July 2010 by Ojw (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

accel

Technical:Accelerometer Fundamentals Accelerometer data retrieval

#!/usr/bin/python
import struct
from math import sqrt

x = 0
y = 0
z = 0

in_file = open("/dev/input/event3","rb")
event = in_file.read(16)

while event:
    (time1,time2, type, code, value) = struct.unpack('iihhi',event)
    time = time2 / 1000.0

    if type == 2 or type == 3:
        if code == 0:
            x = value
        if code == 1:
            y = value
        if code == 2:
            z = value
    if type == 0 and code == 0:

        sum = sqrt(x*x + y*y + z*z)
        px = x / sum
        py = y / sum
        pz = z / sum
        
        orientation = "unknown"
        if(px > 0.5):
            orientation =  "landscape"
        elif(px < -0.5):
            orientation =  "landscape, inverted"
        elif(py > 0.5):
            orientation =  "portrait, inverted"
        elif(py < -0.5):
            orientation =  "portrait"
        elif(pz > 0):
            orientation =  "upright"
        elif(pz < 0):
            orientation =  "on back"

        print "%s (%+4.1f %+4.1f %+4.1f)" % (orientation, px,py,pz)

    event = in_file.read(16)
in_file.close()


Personal tools

accel

Technical:Accelerometer Fundamentals Accelerometer data retrieval

#!/usr/bin/python
import struct
from math import sqrt

x = 0
y = 0
z = 0

in_file = open("/dev/input/event3","rb")
event = in_file.read(16)

while event:
    (time1,time2, type, code, value) = struct.unpack('iihhi',event)
    time = time2 / 1000.0

    if type == 2 or type == 3:
        if code == 0:
            x = value
        if code == 1:
            y = value
        if code == 2:
            z = value
    if type == 0 and code == 0:

        sum = sqrt(x*x + y*y + z*z)
        px = x / sum
        py = y / sum
        pz = z / sum
        
        orientation = "unknown"
        if(px > 0.5):
            orientation =  "landscape"
        elif(px < -0.5):
            orientation =  "landscape, inverted"
        elif(py > 0.5):
            orientation =  "portrait, inverted"
        elif(py < -0.5):
            orientation =  "portrait"
        elif(pz > 0):
            orientation =  "upright"
        elif(pz < 0):
            orientation =  "on back"

        print "%s (%+4.1f %+4.1f %+4.1f)" % (orientation, px,py,pz)

    event = in_file.read(16)
in_file.close()