Commit caf04a111d5338990d50b179e657c6fe3bc29036
1 parent
6afdc6b9
Exists in
master
Demo code now draws graph of randomly chosen numbers - but is driven by redraw e…
…vents, rather than incoming serial data
Showing
1 changed file
with
26 additions
and
3 deletions
Show diff stats
telemetry/code/monitor/pyglet_test.py
| 1 | import pyglet | 1 | import pyglet |
| 2 | import math | 2 | import math |
| 3 | +import random | ||
| 4 | +import time | ||
| 3 | 5 | ||
| 4 | window = pyglet.window.Window() | 6 | window = pyglet.window.Window() |
| 5 | window.set_caption("Test graph") | 7 | window.set_caption("Test graph") |
| 8 | + | ||
| 9 | +starttime = time.time() | ||
| 6 | 10 | ||
| 7 | title = pyglet.text.Label('Test Graph', | 11 | title = pyglet.text.Label('Test Graph', |
| 8 | font_name='Arkhip', | 12 | font_name='Arkhip', |
| @@ -15,6 +19,7 @@ def round_sig(x, sig=2): | @@ -15,6 +19,7 @@ def round_sig(x, sig=2): | ||
| 15 | return round(x, sig-int(math.floor(math.log10(abs(x))))-1) | 19 | return round(x, sig-int(math.floor(math.log10(abs(x))))-1) |
| 16 | 20 | ||
| 17 | def drawgrid(xlines, ylines, xlimits, ylimits): | 21 | def drawgrid(xlines, ylines, xlimits, ylimits): |
| 22 | + pyglet.gl.glColor4f(0.5, 0.5, 0.5, 1.0) | ||
| 18 | for xpos in range(0, window.width, window.width/xlines): | 23 | for xpos in range(0, window.width, window.width/xlines): |
| 19 | pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (xpos, 0, xpos, window.height))) | 24 | pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (xpos, 0, xpos, window.height))) |
| 20 | tagtext = str(round((xlimits[1]-xlimits[0])*(float(xpos)/window.width), 1)) | 25 | tagtext = str(round((xlimits[1]-xlimits[0])*(float(xpos)/window.width), 1)) |
| @@ -28,15 +33,33 @@ def drawgrid(xlines, ylines, xlimits, ylimits): | @@ -28,15 +33,33 @@ def drawgrid(xlines, ylines, xlimits, ylimits): | ||
| 28 | 33 | ||
| 29 | 34 | ||
| 30 | def plotline(xdata, ydata): | 35 | def plotline(xdata, ydata): |
| 31 | - pass | 36 | + pyglet.gl.glColor4f(1, 0, 0, 1.0) |
| 37 | + points = [] | ||
| 38 | + for n in range(max(len(xdata), len(ydata))): | ||
| 39 | + xpos = ((xdata[n]-min(xdata))*window.width)/(max(xdata)-min(xdata)) | ||
| 40 | + xpos = int(xpos) | ||
| 41 | + ypos = ((ydata[n]-min(ydata))*window.height)/(max(ydata)-min(ydata)) | ||
| 42 | + ypos = int(ypos) | ||
| 43 | + points.append([xpos, ypos]) | ||
| 44 | + for n in range(len(points)-1): | ||
| 45 | + pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (points[n][0], points[n][1], points[n+1][0], points[n+1][1]))) | ||
| 46 | + | ||
| 32 | 47 | ||
| 48 | +xdata = [0] | ||
| 49 | +ydata = [0] | ||
| 33 | 50 | ||
| 34 | -xdata = [1, 2, 3, 4, 5, 6, 7, 7.4, 9, 10] | ||
| 35 | -ydata = [81, 86, 58, 20, 59, 3, -6, 4.8] | ||
| 36 | 51 | ||
| 37 | @window.event | 52 | @window.event |
| 38 | def on_draw(): | 53 | def on_draw(): |
| 39 | window.clear() | 54 | window.clear() |
| 55 | + ydata.append(random.uniform(0, 10)) | ||
| 56 | + if len(ydata) > 100: | ||
| 57 | + del ydata[0] | ||
| 58 | + xdata.append(round(time.time() - starttime, 3)) | ||
| 59 | + if len(xdata) > 100: | ||
| 60 | + del xdata[0] | ||
| 40 | drawgrid(10, 12, [min(xdata), max(xdata)], [min(ydata), max(ydata)]) | 61 | drawgrid(10, 12, [min(xdata), max(xdata)], [min(ydata), max(ydata)]) |
| 62 | + plotline(xdata, ydata) | ||
| 63 | + | ||
| 41 | 64 | ||
| 42 | pyglet.app.run() | 65 | pyglet.app.run() |
| 43 | \ No newline at end of file | 66 | \ No newline at end of file |