-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgraph-view.py
154 lines (134 loc) · 5.16 KB
/
graph-view.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ListProperty
from kivy.lang import Builder
from libs.graph import Graph, MeshLinePlot
from kivy.utils import get_color_from_hex as rgb
import math
import time
Builder.load_string('''
#:import rgb kivy.utils.get_color_from_hex
<GraphView>:
Graph:
id: graph
label_options: { 'color': rgb('444444'), 'bold': True } # color of tick labels and titles
background_color: (0, 0, 0, 1) # back ground color of canvas
tick_color: (0,0,1, 1) # ticks and grid
border_color: (0,1,1, 1) # border drawn around each graph
font_size: '8sp'
precision: '%.0f' # axis label formatting
# xlabel: 'Time (Mins)'
# ylabel: 'Temp °C'
x_ticks_minor: 1
x_ticks_major: 1
#y_ticks_minor: 10
y_ticks_major: 50
y_grid_label: True
x_grid_label: True
y_ticks_angle: 90
padding: 5
xlog: False
ylog: False
x_grid: False
y_grid: False
xmin: 0
xmax: 5
ymin: 10
ymax: 300
''')
class GraphView(FloatLayout):
def __init__(self, **kwargs):
super(GraphView, self).__init__(**kwargs)
self.he1_plot = None
self.he1sp_plot = None
self.bed_plot = None
self.bedsp_plot = None
self.secs = 0
self.maxsecs = None
self.last_time = None
def reset(self):
for w in [self.he1_plot, self.he1sp_plot, self.bed_plot, self.bedsp_plot]:
if w is not None:
self.ids.graph.remove_plot(w)
w.points = []
self.he1_plot = None
self.he1sp_plot = None
self.bed_plot = None
self.bedsp_plot = None
self.secs = 0
self.last_time = None
def update_temperature(self, heater, temp, setpoint):
if heater == 'disconnected':
if self.secs == 0:
self.reset()
return
if self.maxsecs is None:
self.maxsecs = self.ids.graph.xmax * 60.
if heater == 'hotend0':
# if temp < 0 or math.isinf(temp):
# if self.he1_plot is not None:
# self.ids.graph.remove_plot(self.he1_plot)
# self.he1_plot.points = []
# self.he1_plot = None
# return
if self.he1_plot is None:
self.he1_plot = MeshLinePlot(color=(1, 0, 0))
self.ids.graph.add_plot(self.he1_plot)
if temp < 10 or math.isinf(temp):
temp = 10
self.he1_plot.points.append((self.secs / 60., temp))
# truncate points
if len(self.he1_plot.points) > self.maxsecs:
del self.he1_plot.points[0]
# now draw in setpoint if set
if not math.isnan(setpoint) and setpoint > 0:
if self.he1sp_plot is None:
self.he1sp_plot = MeshLinePlot(color=(1, 1, 0))
self.ids.graph.add_plot(self.he1sp_plot)
self.he1sp_plot.points.append((self.secs / 60., setpoint))
# truncate points
if len(self.he1sp_plot.points) > self.maxsecs:
del self.he1sp_plot.points[0]
else:
if self.he1sp_plot is not None:
self.ids.graph.remove_plot(self.he1sp_plot)
self.he1sp_plot.points = []
self.he1sp_plot = None
elif heater == 'bed':
# if temp < 0 or math.isinf(temp):
# if self.bed_plot is not None:
# self.ids.graph.remove_plot(self.bed_plot)
# self.bed_plot.points = []
# self.bed_plot = None
# return
if self.bed_plot is None:
self.bed_plot = MeshLinePlot(color=(0, 1, 0))
self.ids.graph.add_plot(self.bed_plot)
if temp < 10 or math.isinf(temp):
temp = 10
self.bed_plot.points.append((self.secs / 60., temp))
if len(self.bed_plot.points) > self.maxsecs:
del self.bed_plot.points[0]
if not math.isnan(setpoint) and setpoint > 0:
if self.bedsp_plot is None:
self.bedsp_plot = MeshLinePlot(color=(0, 1, 1))
self.ids.graph.add_plot(self.bedsp_plot)
self.bedsp_plot.points.append((self.secs / 60., setpoint))
# truncate points
if len(self.bedsp_plot.points) > self.maxsecs:
del self.bedsp_plot.points[0]
else:
if self.bedsp_plot is not None:
self.ids.graph.remove_plot(self.bedsp_plot)
self.bedsp_plot.points = []
self.bedsp_plot = None
# get elapsed time since last one
ts = time.time()
if self.last_time is None:
self.last_time = ts
else:
self.secs += ts - self.last_time
self.last_time = ts
if self.secs / 60. > self.ids.graph.xmax:
self.ids.graph.xmin += 1.
self.ids.graph.xmax += 1.
# TODO add he2