View Single Post
Posts: 137 | Thanked: 150 times | Joined on Jan 2010
#1
I've written a short python script to turn vibration on when I put my phone in my pocket (using the proximity sensor and accelerometer to check it is not just face down or covered). It is working great but I am struggling to get it to run properly on startup. I tried an upstart script in /etc/event.d/ and guessed at what event I wanted to run at. That seems to run things as root, but even if I su to user I don't seem to have access to the dbus session bus to talk to profiled. Is there somewhere else I should be launching this script from? Script is below

Code:
#! /usr/bin/python
from time import sleep
from commands import getoutput

pocket = False

while True:
  f = open("/sys/devices/platform/gpio-switch/proximity/state")
  closed = f.readline().strip() == "closed"
  f.close()
  
  f = open("/sys/class/i2c-adapter/i2c-3/3-001d/coord")
  coords = [int(w) for w in f.readline().split()]
  f.close()
  
  # check phone is not flat on face or back
  newstate = closed and ((coords[0] < -300 or coords[0] > 300) or (coords[1] < -300 or coords[1] > 300))
  if pocket and not newstate:
    print "took out of pocket"
    print getoutput("dbus-send --print-reply --dest='com.nokia.profiled' /com/nokia/profiled com.nokia.profiled.set_value string:general string:vibrating.alert.enabled string:Off")
  elif newstate and not pocket:
    print "put into pocket"
    print getoutput("dbus-send --print-reply --dest='com.nokia.profiled' /com/nokia/profiled com.nokia.profiled.set_value string:general string:vibrating.alert.enabled string:On")
  pocket = newstate
  
  sleep(5)