From caf04a111d5338990d50b179e657c6fe3bc29036 Mon Sep 17 00:00:00 2001 From: Christopher Stone Date: Thu, 8 Feb 2018 19:47:59 +0000 Subject: [PATCH] Demo code now draws graph of randomly chosen numbers - but is driven by redraw events, rather than incoming serial data --- telemetry/code/monitor/pyglet_test.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/telemetry/code/monitor/pyglet_test.py b/telemetry/code/monitor/pyglet_test.py index 1c6eb5e..e03abb5 100644 --- a/telemetry/code/monitor/pyglet_test.py +++ b/telemetry/code/monitor/pyglet_test.py @@ -1,8 +1,12 @@ import pyglet import math +import random +import time window = pyglet.window.Window() window.set_caption("Test graph") + +starttime = time.time() title = pyglet.text.Label('Test Graph', font_name='Arkhip', @@ -15,6 +19,7 @@ def round_sig(x, sig=2): return round(x, sig-int(math.floor(math.log10(abs(x))))-1) def drawgrid(xlines, ylines, xlimits, ylimits): + pyglet.gl.glColor4f(0.5, 0.5, 0.5, 1.0) for xpos in range(0, window.width, window.width/xlines): pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (xpos, 0, xpos, window.height))) tagtext = str(round((xlimits[1]-xlimits[0])*(float(xpos)/window.width), 1)) @@ -28,15 +33,33 @@ def drawgrid(xlines, ylines, xlimits, ylimits): def plotline(xdata, ydata): - pass + pyglet.gl.glColor4f(1, 0, 0, 1.0) + points = [] + for n in range(max(len(xdata), len(ydata))): + xpos = ((xdata[n]-min(xdata))*window.width)/(max(xdata)-min(xdata)) + xpos = int(xpos) + ypos = ((ydata[n]-min(ydata))*window.height)/(max(ydata)-min(ydata)) + ypos = int(ypos) + points.append([xpos, ypos]) + for n in range(len(points)-1): + pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (points[n][0], points[n][1], points[n+1][0], points[n+1][1]))) + +xdata = [0] +ydata = [0] -xdata = [1, 2, 3, 4, 5, 6, 7, 7.4, 9, 10] -ydata = [81, 86, 58, 20, 59, 3, -6, 4.8] @window.event def on_draw(): window.clear() + ydata.append(random.uniform(0, 10)) + if len(ydata) > 100: + del ydata[0] + xdata.append(round(time.time() - starttime, 3)) + if len(xdata) > 100: + del xdata[0] drawgrid(10, 12, [min(xdata), max(xdata)], [min(ydata), max(ydata)]) + plotline(xdata, ydata) + pyglet.app.run() \ No newline at end of file -- libgit2 0.21.2