1

I am receiving reliable gps data from my Adafruit gps sensor. I would like to to show the gpsd.utc data across the screen so that it appears like a regular clock. I have the following code which shows the system time, but I have no idea about how to modify it for use with gpsd.utc. Clock:

import wx
import time

class ClockWindow(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, None)

        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
        self.timer.Start(1000)

    def Draw(self, dc):
        t = time.localtime(time.time())
        st = time.strftime("%I:%M:%S", t)
        dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
        dc.Clear()
        dc.SetFont(wx.Font(30, wx.SWISS, wx.NORMAL, wx.NORMAL))
        tw, th = dc.GetTextExtent(st)
        dc.DrawText(st, 20, 20)

    def OnTimer(self, evt):
        dc = wx.BufferedDC(wx.ClientDC(self))
        self.Draw(dc)

    def OnPaint(self, evt):
        dc = wx.BufferedPaintDC(self)
        self.Draw(dc)

app = wx.App()
frm = ClockWindow(app)
frm.Show()
app.MainLoop()

GPS:

#! /usr/bin/python
# Written by Dan Mandle http://dan.mandle.me September 2012
# License: GPL 2.0

import os
from gps import *
from time import *
import time
import threading
import datetime
import dateutil.parser

gpsd = None

class GpsPoller(threading.Thread):
  def __init__(self):
    threading.Thread.__init__(self)
    global gpsd
    gpsd = gps(mode=WATCH_ENABLE)
    self.current_value = None
    self.running = True

  def run(self):
    global gpsd
    while gpsp.running:
      gpsd.next()

if __name__ == '__main__':
  gpsp = GpsPoller()
  try:
    gpsp.start()
    while True:
      gps_datetime = dateutil.parser.parse(gpsd.utc)
      print 'time        ' , '%s:%s:%s' % (gps_datetime.hour, gps_datetime.minute, gps_datetime.second)

      time.sleep(1) #set to whatever

  except (KeyboardInterrupt, SystemExit):
    print "\nKilling Thread..."
    gpsp.running = False
    gpsp.join()
  print "Done.\nExiting."
0

At a high level you have two applications here. One which displays a clock and the other which retrieves the time from GPS data and writes it to the console. Your goal is to successfully "merge the two". You are going to want to write a completely new application which might use these two apps as a degree of a template. You should study both apps in detail until you fully understand how they work. At that point, you can start to design a whole new app which, at a high level will begin by initializing the UI environment and the GPS environment. Then in a loop, you will want to retrieve the time from GPS and use that as the input to the UI function which, today, uses the PI system time as the clock value.

0

Ok. My programming skills are limited, but this method works... at least crudely. I welcome any suggestions for tweaking that would make it better.

#!/usr/bin/env python

import pygame
import gps
import sys
import datetime
import dateutil.parser

def main():
    # initialize Pygame modules
    pygame.init()

    # initialize gps connection
    session = gps.gps("localhost", "2947")
    session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)

    # set screen parameters
    screen = pygame.display.set_mode((0, 0),pygame.FULLSCREEN)
    #screen = pygame.display.set_mode((1280, 800))
    screenWIDTH, screenHEIGHT = screen.get_size()

    # set font parameters
    font = pygame.font.Font(None, 128)

    # set cursor visibility
    pygame.mouse.set_visible(False)

    # set initial value for gps_time
    gps_time = "updating..."

    # main loop flag
    done = False

    # start main loop
    while not done:
        # search gps data stream for time
        report = session.next()
        if report['class'] == 'TPV':
            if hasattr(report, 'time'):
                # read time from gps data stream
                gps_time = report.time
                # convert gps_time to datetime
                gps_datetime = dateutil.parser.parse(gps_time)
                # format gps_time as desired
                #gps_time = '%s/%s/%s' % (gps_datetime.month, gps_datetime.day, gps_datetime.year)
                gps_time = '%s:%s:%s' % (gps_datetime.hour, gps_datetime.minute, gps_datetime.second)
        # format gps_time
        text = font.render(gps_time, True, (255, 255, 255, 255))
        textWIDTH, textHEIGHT = text.get_size()

        # update screen
        screen.fill((0, 0, 0, 255))
        screen.blit(text, (screenWIDTH / 2 - textWIDTH / 2, screenHEIGHT / 2 - textHEIGHT / 2))
        pygame.display.flip()

        # process events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    done = True

    # reset cursor visibility
    pygame.mouse.set_visible(True)

    # proceed to exit
    session = None
    pygame.quit()
    sys.exit(0)

if __name__ == "__main__":
    main()
0

old question, but i believe this should do what you want:

import wx
import os
from gps import *
import threading
import datetime
import dateutil.parser

gpsd = None

class GpsPoller(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        global gpsd
        gpsd = gps(mode=WATCH_ENABLE)
        self.current_value = None
        self.running = True

    def run(self):
        global gpsd
        while gpsp.running:
            gpsd.next()

class ClockWindow(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, None)

        self.gpsp = GpsPoller()
        self.gpsp.start()

        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
        self.timer.Start(1000)

    def Draw(self, dc):
        gps_datetime = dateutil.parser.parse(gpsd.utc)
        st = '%s:%s:%s' % (gps_datetime.hour, gps_datetime.minute, gps_datetime.second)
        dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
        dc.Clear()
        dc.SetFont(wx.Font(30, wx.SWISS, wx.NORMAL, wx.NORMAL))
        tw, th = dc.GetTextExtent(st)
        dc.DrawText(st, 20, 20)

    def OnTimer(self, evt):
        dc = wx.BufferedDC(wx.ClientDC(self))
        self.Draw(dc)

    def OnPaint(self, evt):
        dc = wx.BufferedPaintDC(self)
        self.Draw(dc)

if __name__ == "__main__":
    app = wx.App()
    frm = ClockWindow(app)
    frm.Show()
    app.MainLoop()
    app.gpsp.running = False
    app.gpsp.join()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.