<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/sodnpoo.xsl"?>
<xml>
<post>
  <title>volume control</title>
  <date>19 Dec 2008</date>
  <p>
  As I use my n95 as my mp3 player in the car, thru the stereo I thought it'd be good if the volume would 
  adjust automatically to stay above the engine noise.
  </p>
  <p>
  Not wanting to have to resort to a virtual machine so I could install Carbide++ I did some googling and found 
  pys60. Pys60 is python for the s60!
  </p>
  <p>
  I've never coded in python before so the code below may well suck - but I was impressed at the amount of time 
  it took to produce working code. It took 4 hours to go from zero to an almost working app.
  </p>
  <p>
  Anyhow the code just polls the GPS to get your speed and if you cross the 'step' threshold (seems to work 
  best around 17 mph in my car.. You might have to tweak this if your volume rises and falls too quickly 
  and/or slowly) it simulates a volume up/down keypress.
  </p>
  <p>
  I imagine this would work on any s60 with attached GPS - even bluetooth - you'd need to find what the name of 
  the device was tho..
  </p>
  <pre>
import positioning, keypress, math 
from key_codes import *

updateSecs = 3
gpsName = "Integrated GPS"
speedStep = 17

for module in positioning.modules():
  if module['name'] == gpsName:
    positioning.select_module(module['id'])
    
positioning.set_requestors([{"type":"service","format":"application","data":"test_app"}])
lastSpeedMph = 0

def cb(event):
  speedMph = event['course']['speed'] * 2.237
  print "mph: ",speedMph
  global lastSpeedMph
  top = math.ceil(lastSpeedMph/speedStep)*speedStep
  bottom = math.floor(lastSpeedMph/speedStep)*speedStep
  print "top :", top
  print "bottom :", bottom
  
  lastSpeedMph = speedMph
  
  if speedMph &lt; bottom:
    print "volume down"
    keypress.send_raw_event(keypress.EKeyDown, EStdKeyDecVolume)
    keypress.send_raw_event(keypress.EKeyUp, EStdKeyDecVolume)
    
  if speedMph &gt; top:
    print "volume up"
    keypress.send_raw_event(keypress.EKeyDown, EStdKeyIncVolume)
    keypress.send_raw_event(keypress.EKeyUp, EStdKeyIncVolume)
    
positioning.position(course=1,satellites=1,callback=cb, interval=updateSecs*1000000,partial=0)
#execfile("E:\Python\Gps.py")
#positioning.stop_position()  
  </pre>
</post>
</xml>


