eric6/MicroPython/MicroPythonGraphWidget.py

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 8067
a467ab075be0
child 8143
2c730d5fd177
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
7923
91e843545d9a Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
3 # Copyright (c) 2019 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing the MicroPython graph widget.
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 from collections import deque
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import bisect
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
12 import os
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
13 import time
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
14 import csv
7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
16 from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt
7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 from PyQt5.QtGui import QPainter
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 from PyQt5.QtWidgets import (
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
19 QWidget, QHBoxLayout, QVBoxLayout, QToolButton, QSizePolicy, QSpacerItem,
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
20 QLabel, QSpinBox
7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 )
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 from PyQt5.QtChart import QChartView, QChart, QLineSeries, QValueAxis
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
24 from E5Gui import E5MessageBox
7535
dac9bc72a0f3 MicroPython: made the chart widget color scheme aware and added a config option to configure a specific chart color theme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
25 from E5Gui.E5Application import e5App
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
26
7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 import UI.PixmapCache
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
28 import Preferences
7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 class MicroPythonGraphWidget(QWidget):
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 """
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 Class implementing the MicroPython graph widget.
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 @signal dataFlood emitted to indicate, that too much data is received
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 """
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 dataFlood = pyqtSignal()
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 def __init__(self, parent=None):
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 """
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 Constructor
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 @param parent reference to the parent widget
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 @type QWidget
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 """
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 super(MicroPythonGraphWidget, self).__init__(parent)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 self.__layout = QHBoxLayout()
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
49 self.__layout.setContentsMargins(2, 2, 2, 2)
7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 self.setLayout(self.__layout)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 self.__chartView = QChartView(self)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 self.__chartView.setSizePolicy(
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 QSizePolicy.Expanding, QSizePolicy.Expanding)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 self.__layout.addWidget(self.__chartView)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
57 self.__verticalLayout = QVBoxLayout()
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
58 self.__verticalLayout.setContentsMargins(0, 0, 0, 0)
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
59 self.__layout.addLayout(self.__verticalLayout)
7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 self.__saveButton = QToolButton(self)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 self.__saveButton.setIcon(UI.PixmapCache.getIcon("fileSave"))
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 self.__saveButton.setToolTip(self.tr("Press to save the raw data"))
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 self.__saveButton.clicked.connect(self.on_saveButton_clicked)
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
65 self.__verticalLayout.addWidget(self.__saveButton)
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
66 self.__verticalLayout.setAlignment(self.__saveButton, Qt.AlignHCenter)
7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 spacerItem = QSpacerItem(20, 20, QSizePolicy.Minimum,
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 QSizePolicy.Expanding)
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
70 self.__verticalLayout.addItem(spacerItem)
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
71
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
72 label = QLabel(self.tr("max. X:"))
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
73 self.__verticalLayout.addWidget(label)
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
74 self.__verticalLayout.setAlignment(label, Qt.AlignHCenter)
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
75
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
76 self.__maxX = 100
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
77 self.__maxXSpinBox = QSpinBox()
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
78 self.__maxXSpinBox.setMinimum(100)
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
79 self.__maxXSpinBox.setMaximum(1000)
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
80 self.__maxXSpinBox.setSingleStep(100)
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
81 self.__maxXSpinBox.setToolTip(self.tr(
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
82 "Enter the maximum number of data points to be plotted."))
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
83 self.__maxXSpinBox.setValue(self.__maxX)
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
84 self.__maxXSpinBox.setAlignment(Qt.AlignRight)
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
85 self.__verticalLayout.addWidget(self.__maxXSpinBox)
7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 # holds the data to be checked for plotable data
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 self.__inputBuffer = []
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 # holds the raw data
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 self.__rawData = []
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
91 self.__dirty = False
7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 self.__maxY = 1000
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 self.__flooded = False # flag indicating a data flood
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 self.__data = [deque([0] * self.__maxX)]
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 self.__series = [QLineSeries()]
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 # Y-axis ranges
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 self.__yRanges = [1, 5, 10, 25, 50, 100, 250, 500, 1000]
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 # setup the chart
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 self.__chart = QChart()
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 self.__chart.legend().hide()
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 self.__chart.addSeries(self.__series[0])
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 self.__axisX = QValueAxis()
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 self.__axisX.setRange(0, self.__maxX)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 self.__axisX.setLabelFormat("time")
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 self.__axisY = QValueAxis()
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 self.__axisY.setRange(-self.__maxY, self.__maxY)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 self.__axisY.setLabelFormat("%d")
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 self.__chart.setAxisX(self.__axisX, self.__series[0])
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 self.__chart.setAxisY(self.__axisY, self.__series[0])
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 self.__chartView.setChart(self.__chart)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 self.__chartView.setRenderHint(QPainter.Antialiasing)
7535
dac9bc72a0f3 MicroPython: made the chart widget color scheme aware and added a config option to configure a specific chart color theme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
116 self.preferencesChanged()
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
117
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
118 self.__maxXSpinBox.valueChanged.connect(self.__handleMaxXChanged)
7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119
7535
dac9bc72a0f3 MicroPython: made the chart widget color scheme aware and added a config option to configure a specific chart color theme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
120 @pyqtSlot()
dac9bc72a0f3 MicroPython: made the chart widget color scheme aware and added a config option to configure a specific chart color theme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
121 def preferencesChanged(self):
dac9bc72a0f3 MicroPython: made the chart widget color scheme aware and added a config option to configure a specific chart color theme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
122 """
dac9bc72a0f3 MicroPython: made the chart widget color scheme aware and added a config option to configure a specific chart color theme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
123 Public slot to apply changed preferences.
dac9bc72a0f3 MicroPython: made the chart widget color scheme aware and added a config option to configure a specific chart color theme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
124 """
dac9bc72a0f3 MicroPython: made the chart widget color scheme aware and added a config option to configure a specific chart color theme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
125 chartColorTheme = Preferences.getMicroPython("ChartColorTheme")
dac9bc72a0f3 MicroPython: made the chart widget color scheme aware and added a config option to configure a specific chart color theme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
126 if chartColorTheme == -1:
dac9bc72a0f3 MicroPython: made the chart widget color scheme aware and added a config option to configure a specific chart color theme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
127 # automatic selection of light or dark depending on desktop
dac9bc72a0f3 MicroPython: made the chart widget color scheme aware and added a config option to configure a specific chart color theme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
128 # color scheme
dac9bc72a0f3 MicroPython: made the chart widget color scheme aware and added a config option to configure a specific chart color theme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
129 if e5App().usesDarkPalette():
dac9bc72a0f3 MicroPython: made the chart widget color scheme aware and added a config option to configure a specific chart color theme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
130 self.__chart.setTheme(QChart.ChartThemeDark)
dac9bc72a0f3 MicroPython: made the chart widget color scheme aware and added a config option to configure a specific chart color theme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
131 else:
dac9bc72a0f3 MicroPython: made the chart widget color scheme aware and added a config option to configure a specific chart color theme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
132 self.__chart.setTheme(QChart.ChartThemeLight)
dac9bc72a0f3 MicroPython: made the chart widget color scheme aware and added a config option to configure a specific chart color theme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
133 else:
dac9bc72a0f3 MicroPython: made the chart widget color scheme aware and added a config option to configure a specific chart color theme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
134 self.__chart.setTheme(chartColorTheme)
dac9bc72a0f3 MicroPython: made the chart widget color scheme aware and added a config option to configure a specific chart color theme.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
135
7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 @pyqtSlot(bytes)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 def processData(self, data):
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 """
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 Public slot to process the raw data.
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 It takes raw bytes, checks the data for a valid tuple of ints or
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 floats and adds the data to the graph. If the the length of the bytes
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 data is greater than 1024 then a dataFlood signal is emitted to ensure
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 eric can take action to remain responsive.
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 @param data raw data received from the connected device via the main
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 device widget
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 @type bytes
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 """
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 # flooding guard
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 if self.__flooded:
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 return
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 if len(data) > 1024:
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 self.__flooded = True
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 self.dataFlood.emit()
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 return
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
159 # disable the inputs while processing data
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
160 self.__saveButton.setEnabled(False)
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
161 self.__maxXSpinBox.setEnabled(False)
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
162
7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 data = data.replace(b"\r\n", b"\n").replace(b"\r", b"\n")
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 self.__inputBuffer.append(data)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 # check if the data contains a Python tuple containing numbers (int
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 # or float) on a single line
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 inputBytes = b"".join(self.__inputBuffer)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 lines = inputBytes.splitlines(True)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 for line in lines:
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 if not line.endswith(b"\n"):
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 # incomplete line (last line); skip it
7067
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7066
diff changeset
173 break
7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 line = line.strip()
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 if line.startswith(b"(") and line.endswith(b")"):
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 # it may be a tuple we are interested in
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 rawValues = [val.strip() for val in line[1:-1].split(b",")]
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 values = []
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 for raw in rawValues:
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 try:
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 values.append(int(raw))
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 # ok, it is an integer
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 continue
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 except ValueError:
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 # test for a float
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 pass
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 try:
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 values.append(float(raw))
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 except ValueError:
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 # it is not an int or float, ignore it
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 continue
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 if values:
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 self.__addData(tuple(values))
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 self.__inputBuffer = []
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 if lines[-1] and not lines[-1].endswith(b"\n"):
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 # Append any left over bytes for processing next time data is
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 # received.
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 self.__inputBuffer.append(lines[-1])
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
201
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
202 # re-enable the inputs
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
203 self.__saveButton.setEnabled(True)
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
204 self.__maxXSpinBox.setEnabled(True)
7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 def __addData(self, values):
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 """
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 Private method to add a tuple of values to the graph.
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 It ensures there are the required number of line series, adds the data
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 to the line series and updates the range of the chart so the chart
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 displays nicely.
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 @param values tuple containing the data to be added
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 @type tuple of int or float
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 """
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 # store incoming data to be able to dump it as CSV upon request
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 self.__rawData.append(values)
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
219 self.__dirty = True
7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 # check number of incoming values and adjust line series accordingly
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 if len(values) != len(self.__series):
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 valuesLen = len(values)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 seriesLen = len(self.__series)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 if valuesLen > seriesLen:
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 # add a nwe line series
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 for _index in range(valuesLen - seriesLen):
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 newSeries = QLineSeries()
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 self.__chart.addSeries(newSeries)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 self.__chart.setAxisX(self.__axisX, newSeries)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 self.__chart.setAxisY(self.__axisY, newSeries)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 self.__series.append(newSeries)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 self.__data.append(deque([0] * self.__maxX))
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 else:
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 # remove obsolete line series
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 for oldSeries in self.__series[valuesLen:]:
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 self.__chart.removeSeries(oldSeries)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 self.__series = self.__series[:valuesLen]
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 self.__data = self.__data[:valuesLen]
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 # add the new values to the display and compute the maximum range
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242 maxRanges = []
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 for index, value in enumerate(values):
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 self.__data[index].appendleft(value)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 maxRanges.append(max([max(self.__data[index]),
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 abs(min(self.__data[index]))]))
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 if len(self.__data[index]) > self.__maxX:
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 self.__data[index].pop()
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 # re-scale the y-axis
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 maxYRange = max(maxRanges)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 yRange = bisect.bisect_left(self.__yRanges, maxYRange)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 if yRange < len(self.__yRanges):
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 self.__maxY = self.__yRanges[yRange]
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 elif maxYRange > self.__maxY:
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 self.__maxY += self.__maxY
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 elif maxYRange < self.__maxY / 2:
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 self.__maxY /= 2
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 self.__axisY.setRange(-self.__maxY, self.__maxY)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261 # ensure that floats are used to label the y-axis if the range is small
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 if self.__maxY <= 5:
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 self.__axisY.setLabelFormat("%2.2f")
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 else:
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 self.__axisY.setLabelFormat("%d")
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 # update the line series
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 for index, series in enumerate(self.__series):
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 series.clear()
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 xyValues = []
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 for x in range(self.__maxX):
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 value = self.__data[index][self.__maxX - 1 - x]
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 xyValues.append((x, value))
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 for xy in xyValues:
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 series.append(*xy)
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 @pyqtSlot()
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 def on_saveButton_clicked(self):
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 """
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 Private slot to save the raw data to a CSV file.
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 """
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 self.saveData()
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 def hasData(self):
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285 """
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286 Public method to check, if the chart contains some valid data.
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
288 @return flag indicating valid data
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289 @rtype bool
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 """
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
291 return len(self.__rawData) > 0
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
292
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
293 def isDirty(self):
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
294 """
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
295 Public method to check, if the chart contains unsaved data.
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
296
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
297 @return flag indicating unsaved data
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
298 @rtype bool
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
299 """
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
300 return self.hasData() and self.__dirty
7065
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302 def saveData(self):
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
303 """
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304 Public method to save the dialog's raw data.
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306 @return flag indicating success
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307 @rtype bool
e3d04faced34 Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308 """
8067
a467ab075be0 MicroPython: added buttons to go to the 'home' directory (local and on device) to the MicroPython file manager and improved the workspace handling.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
309 baseDir = (
a467ab075be0 MicroPython: added buttons to go to the 'home' directory (local and on device) to the MicroPython file manager and improved the workspace handling.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
310 Preferences.getMicroPython("MpyWorkspace") or
a467ab075be0 MicroPython: added buttons to go to the 'home' directory (local and on device) to the MicroPython file manager and improved the workspace handling.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
311 Preferences.getMultiProject("Workspace") or
a467ab075be0 MicroPython: added buttons to go to the 'home' directory (local and on device) to the MicroPython file manager and improved the workspace handling.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
312 os.path.expanduser("~")
a467ab075be0 MicroPython: added buttons to go to the 'home' directory (local and on device) to the MicroPython file manager and improved the workspace handling.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
313 )
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
314 dataDir = os.path.join(baseDir, "data_capture")
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
315
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
316 if not os.path.exists(dataDir):
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
317 os.makedirs(dataDir)
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
318
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
319 # save the raw data as a CSV file
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
320 fileName = "{0}.csv".format(time.strftime("%Y%m%d-%H%M%S"))
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
321 fullPath = os.path.join(dataDir, fileName)
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
322 try:
7785
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7780
diff changeset
323 with open(fullPath, "w") as csvFile:
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7780
diff changeset
324 csvWriter = csv.writer(csvFile)
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7780
diff changeset
325 csvWriter.writerows(self.__rawData)
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
326
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
327 self.__dirty = False
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
328 return True
7836
2f0d208b8137 Changed code to not use the OSError aliases (IOError, EnvironmentError, socket.error and select.error) anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7785
diff changeset
329 except OSError as err:
7066
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
330 E5MessageBox.critical(
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
331 self,
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
332 self.tr("Save Chart Data"),
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
333 self.tr(
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
334 """<p>The chart data could not be saved into file"""
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
335 """ <b>{0}</b>.</p><p>Reason: {1}</p>""").format(
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
336 fullPath, str(err)))
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
337 return False
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
338
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
339 @pyqtSlot(int)
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
340 def __handleMaxXChanged(self, value):
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
341 """
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
342 Private slot handling a change of the max. X spin box.
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
343
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
344 @param value value of the spin box
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
345 @type int
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
346 """
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
347 delta = value - self.__maxX
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
348 if delta == 0:
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
349 # nothing to change
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
350 return
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
351 elif delta > 0:
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
352 # range must be increased
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
353 for deq in self.__data:
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
354 deq.extend([0] * delta)
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
355 else:
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
356 # range must be decreased
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
357 data = []
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
358 for deq in self.__data:
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
359 data.append(deque(list(deq)[:value]))
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
360 self.__data = data
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
361
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
362 self.__maxX = value
e3d034e65afc Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7065
diff changeset
363 self.__axisX.setRange(0, self.__maxX)

eric ide

mercurial