# gps2qrcode.py
# Ben Ward, 2009, (http://badgertrack.com)

import time, gps, PyQrcodec

# Start a GPSd client session
session = gps.gps()

# Start a QrCode encoder
codec = PyQrcodec

# Experiments with a single-shot GPSd connection lead to bad results
# for best results, stay connected and query in a loop.  
while 1:
	# GPSd query flags:
	# a = altitude, d = date/time, m=mode,
	# o=postion/fix, s=status, y=satellites
	session.query('admosyp')
	
	# concatenate the data together
	# you could verify the GPS fix status at this point, which avoids the ambiguity of 0,0 coordinates 
	#  which are most likely false, but must be supported
	mytext = str(session.utc) + ' ' + str(session.fix.latitude) + ',' + str(session.fix.longitude)

	# minimum image width for decoding is ~256pixels, but we can do that with image enlargement during extraction
	size, image = codec.encode(mytext, image_width=128)
	image.save('qrcode.png')
	
	# bit of debugging
	print mytext 

	# wait for a bit, then do it again
	time.sleep(1)
