Audio wpt.py

From OpenStreetMap Wiki
Jump to navigation Jump to search
# 2009 Diogo W. Nunes - Made for Open Street Maps - osm.org
# Made for PyS60 1.4.5 from http://sourceforge.net/projects/pys60/
# I tried using the positioning module, but kept getting errors
# Uses portions of code from http://ruk.ca/w/index.php/Set_a_Nokia_N70_Clock_from_a_Bluetooth_GPS
# and http://mobilenin.com/pys60/menu.htm
#
# By default, saves data to E:\OSM, make sure you have a memory card with this folder
# Also change the target address a couple lines down to your GPS address
#
# Sample GPX data to include in the resulting GPX file (remove # from text)
#<?xml version="1.0" encoding="UTF-8"?>
#<gpx version="1.0" creator="Audio_wpt.py" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
#... your waypoints go here ...
#</gpx>


import appuifw, e32, socket, audio
from audio import *

appuifw.app.screen='normal'

running = True
def quit():
    global running
    running = False
    appuifw.app.set_exit()

appuifw.app.exit_key_handler = quit
appuifw.app.title = u"Waypoint recorder"

target = ('00:0b:0d:00:02:b6', 1) #change this to your GPS address
sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM)
sock.connect(target)

recording = False

def cb_record():
    global recording
    recording = True
    global s
    (t, lat, lon, hdop, alt) = read_pos()
    gpx_file = open('e:\\osm\\wpt.gpx','a')
    gpx_file.write('<wpt lat="' + str(lat) + '" lon="' + str(lon)  + '"><ele>' + str(alt) + '</ele><hdop>' + str(hdop) + '</hdop><desc>T' + t[0:2] + ':' + t[2:4] + ':' + t[4:6] + 'Z</desc><link href="VOX' + t + '.wav"><text>VOX' + t + '.wav</text></link></wpt>')
    gpx_file.close()
    audio.say(u'Speak now')
    s = Sound.open(u'e:\\osm\\VOX' + t + '.wav')
    s.record()

def cb_stop():
    global recording
    recording = False
    global s
    s.stop()
    s.close()
    s = None

def cb_draw():
    if not recording:
        l = [u"Record"]
        index = appuifw.selection_list(l)
        if index == 0:
            cb_record()
        else:
            quit()
    else:
        l = [u"Stop"]
        index = appuifw.selection_list(l)
        if index == 0:
            cb_stop()
        else:
            quit()

def read_pos():
    print "waiting for fix..."
    while True:
        ch = sock.recv(1)
        # Loop until packet received
        buffer = ""
        while ch !='\n':
            buffer+=ch
            ch = sock.recv(1)
        
        if buffer.startswith("$GPGGA"):
            gpsData = buffer.split(",")
            t = gpsData[1][0:6]
            lat = round(float(gpsData[2][0:2]) + (float(gpsData[2][2:9])/60),6)
            if gpsData[3] == 'S':
                lat = -lat
            lon = round(float(gpsData[4][0:3]) + (float(gpsData[4][3:10])/60),6)
            if gpsData[5] == 'W':
                lon = -lon
            quality = int(gpsData[6])
            try:
                hdop = float(gpsData[8])
            except:
                hdop = 0
            alt = float(gpsData[9])
            if quality != 0:
                break
    return (t, lat, lon, hdop, alt)

while running:
    cb_draw()
    e32.ao_yield()