User:Ojw/Notes

From Openmoko

< User:Ojw
Revision as of 23:29, 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()


vibe

Vibrator

import sys
import time

outfile = open("/sys/class/leds/neo1973:vibrator/brightness", "w", 1)

for i in range(0,4):
    for f in range(10,255):
        outfile.write(str(f) + "\n")
        time.sleep(0.01)

outfile.write("0\n")
outfile.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()


vibe

Vibrator

import sys
import time

outfile = open("/sys/class/leds/neo1973:vibrator/brightness", "w", 1)

for i in range(0,4):
    for f in range(10,255):
        outfile.write(str(f) + "\n")
        time.sleep(0.01)

outfile.write("0\n")
outfile.close()