Skip to content

Commit

Permalink
Bump dependencies & removed global date-time variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis-van-Gils committed Jul 17, 2020
1 parent a414a57 commit d9a4457
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 98 deletions.
43 changes: 23 additions & 20 deletions Arduino_PyQt_demo_with_multithreading.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
__author__ = "Dennis van Gils"
__authoremail__ = "[email protected]"
__url__ = "https://github.com/Dennis-van-Gils/DvG_Arduino_PyQt_multithread_demo"
__date__ = "16-07-2020"
__date__ = "17-07-2020"
__version__ = "4.1"
# pylint: disable=bare-except, broad-except

Expand All @@ -22,15 +22,16 @@
from PyQt5.QtCore import QDateTime
import pyqtgraph as pg

from dvg_debug_functions import dprint, print_fancy_traceback as pft
from dvg_devices.Arduino_protocol_serial import Arduino
from dvg_qdeviceio import QDeviceIO

from DvG_pyqt_FileLogger import FileLogger
from DvG_pyqt_ChartHistory import ChartHistory
from DvG_pyqt_controls import create_Toggle_button, SS_GROUP
from dvg_debug_functions import dprint, print_fancy_traceback as pft

from dvg_devices.Arduino_protocol_serial import Arduino # I.e. the `device`
from dvg_qdeviceio import QDeviceIO

try:
# To enable OpenGL GPU acceleration:
# `conda install pyopengl` or `pip install pyopengl`
import OpenGL.GL as gl # pylint: disable=unused-import
except:
Expand All @@ -45,16 +46,21 @@
DAQ_INTERVAL_MS = 10 # 10 [ms]
CHART_INTERVAL_MS = 10 # 10 [ms]
CHART_HISTORY_TIME = 10 # 10 [s]

# Global variables for date-time keeping
cur_date_time = QDateTime.currentDateTime()
str_cur_date = cur_date_time.toString("dd-MM-yyyy")
str_cur_time = cur_date_time.toString("HH:mm:ss")
# fmt: on

# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
DEBUG = False


def get_current_date_time():
cur_date_time = QDateTime.currentDateTime()
return (
cur_date_time.toString("dd-MM-yyyy"), # Date
cur_date_time.toString("HH:mm:ss"), # Time
cur_date_time.toString("yyMMdd_HHmmss"), # Reverse notation date-time
)


# ------------------------------------------------------------------------------
# Arduino state
# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -148,11 +154,11 @@ def __init__(self, parent=None, **kwargs):
self.gw_chart.setBackground([20, 20, 20])
self.pi_chart = self.gw_chart.addPlot()

p = {"color": "#BBB", "font-size": "10pt"}
p = {"color": "#CCC", "font-size": "10pt"}
self.pi_chart.showGrid(x=1, y=1)
self.pi_chart.setTitle("Arduino timeseries", **p)
self.pi_chart.setLabel("bottom", text="history (sec)", **p)
self.pi_chart.setLabel("left", text="readings", **p)
self.pi_chart.setLabel("left", text="amplitude", **p)
self.pi_chart.setRange(
xRange=[-1.04 * CHART_HISTORY_TIME, CHART_HISTORY_TIME * 0.04],
yRange=[-1.1, 1.1],
Expand Down Expand Up @@ -283,6 +289,7 @@ def set_text_qpbt_record(self, text_str):

@QtCore.pyqtSlot()
def update_GUI():
str_cur_date, str_cur_time, _ = get_current_date_time()
window.qlbl_cur_date_time.setText("%s %s" % (str_cur_date, str_cur_time))
window.qlbl_update_counter.setText("%i" % qdev_ard.update_counter_DAQ)
window.qlbl_DAQ_rate.setText("DAQ: %.1f Hz" % qdev_ard.obtained_DAQ_rate_Hz)
Expand Down Expand Up @@ -328,9 +335,10 @@ def stop_running():
def notify_connection_lost():
stop_running()

excl = " ! ! ! ! ! ! ! "
excl = " ! ! ! ! ! ! "
window.qlbl_title.setText("%sLOST CONNECTION%s" % (excl, excl))

str_cur_date, str_cur_time, _ = get_current_date_time()
str_msg = ("%s %s\n" "Lost connection to Arduino.\n" " '%s': %salive") % (
str_cur_date,
str_cur_time,
Expand Down Expand Up @@ -360,10 +368,7 @@ def about_to_quit():

def DAQ_function():
# Date-time keeping
global cur_date_time, str_cur_date, str_cur_time
cur_date_time = QDateTime.currentDateTime()
str_cur_date = cur_date_time.toString("dd-MM-yyyy")
str_cur_time = cur_date_time.toString("HH:mm:ss")
str_cur_date, str_cur_time, str_cur_datetime = get_current_date_time()

# Query the Arduino for its state
success, tmp_state = ard.query_ascii_values("?", delimiter="\t")
Expand Down Expand Up @@ -396,7 +401,7 @@ def DAQ_function():

# Logging to file
if file_logger.starting:
fn_log = cur_date_time.toString("yyMMdd_HHmmss") + ".txt"
fn_log = str_cur_datetime + ".txt"
if file_logger.create_log(state.time, fn_log, mode="w"):
file_logger.signal_set_recording_text.emit(
"Recording to file: " + fn_log
Expand Down Expand Up @@ -480,9 +485,7 @@ def DAQ_function():
debug = DEBUG,
)
# fmt: on

qdev_ard.create_worker_jobs(debug=DEBUG)
# """

# Connect signals to slots
qdev_ard.signal_DAQ_updated.connect(update_GUI)
Expand Down
53 changes: 28 additions & 25 deletions Arduino_PyQt_demo_with_multithreading__LARGE_TEXT.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
__author__ = "Dennis van Gils"
__authoremail__ = "[email protected]"
__url__ = "https://github.com/Dennis-van-Gils/DvG_Arduino_PyQt_multithread_demo"
__date__ = "16-07-2020"
__version__ = "4.0"
__date__ = "17-07-2020"
__version__ = "4.1"
# pylint: disable=bare-except, broad-except

import os
import sys
Expand All @@ -21,29 +22,34 @@
from PyQt5.QtCore import QDateTime
import pyqtgraph as pg

from DvG_pyqt_FileLogger import FileLogger
from DvG_pyqt_ChartHistory import ChartHistory
from DvG_pyqt_controls import create_Toggle_button
from dvg_debug_functions import dprint, print_fancy_traceback as pft

from dvg_devices.Arduino_protocol_serial import Arduino
from dvg_qdeviceio import QDeviceIO

from DvG_pyqt_FileLogger import FileLogger
from DvG_pyqt_ChartHistory import ChartHistory
from DvG_pyqt_controls import create_Toggle_button

# Constants
# fmt: off
DAQ_INTERVAL_MS = 10 # 10 [ms]
DRAW_INTERVAL_CHART = 10 # 10 [ms]
CHART_HISTORY_TIME = 10 # 10 [s]

# Global variables for date-time keeping
cur_date_time = QDateTime.currentDateTime()
str_cur_date = cur_date_time.toString("dd-MM-yyyy")
str_cur_time = cur_date_time.toString("HH:mm:ss")
DAQ_INTERVAL_MS = 10 # 10 [ms]
CHART_INTERVAL_MS = 10 # 10 [ms]
CHART_HISTORY_TIME = 10 # 10 [s]
# fmt: on

# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
DEBUG = False


def get_current_date_time():
cur_date_time = QDateTime.currentDateTime()
return (
cur_date_time.toString("dd-MM-yyyy"), # Date
cur_date_time.toString("HH:mm:ss"), # Time
cur_date_time.toString("yyMMdd_HHmmss"), # Reverse notation date-time
)


# ------------------------------------------------------------------------------
# Arduino state
# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -304,6 +310,7 @@ def set_text_qpbt_record(self, text_str):

@QtCore.pyqtSlot()
def update_GUI():
str_cur_date, str_cur_time, _ = get_current_date_time()
window.qlbl_cur_date_time.setText("%s %s" % (str_cur_date, str_cur_time))
window.qlbl_update_counter.setText("%i" % qdev_ard.update_counter_DAQ)
window.qlbl_DAQ_rate.setText("DAQ: %.1f Hz" % qdev_ard.obtained_DAQ_rate_Hz)
Expand All @@ -326,8 +333,7 @@ def update_chart():
if DEBUG:
dprint(
" update_curve done in %.2f ms"
% (time.perf_counter() - tick)
* 1000
% ((time.perf_counter() - tick) * 1000)
)


Expand All @@ -350,9 +356,10 @@ def stop_running():
def notify_connection_lost():
stop_running()

excl = " ! ! ! ! ! ! ! ! "
excl = " ! ! ! ! ! ! "
window.qlbl_title.setText("%sLOST CONNECTION%s" % (excl, excl))

str_cur_date, str_cur_time, _ = get_current_date_time()
str_msg = ("%s %s\n" "Lost connection to Arduino.\n" " '%s': %salive") % (
str_cur_date,
str_cur_time,
Expand Down Expand Up @@ -382,10 +389,7 @@ def about_to_quit():

def DAQ_function():
# Date-time keeping
global cur_date_time, str_cur_date, str_cur_time
cur_date_time = QDateTime.currentDateTime()
str_cur_date = cur_date_time.toString("dd-MM-yyyy")
str_cur_time = cur_date_time.toString("HH:mm:ss")
str_cur_date, str_cur_time, str_cur_datetime = get_current_date_time()

# Query the Arduino for its state
success, tmp_state = ard.query_ascii_values("?", delimiter="\t")
Expand Down Expand Up @@ -418,7 +422,7 @@ def DAQ_function():

# Logging to file
if file_logger.starting:
fn_log = cur_date_time.toString("yyMMdd_HHmmss") + ".txt"
fn_log = str_cur_datetime + ".txt"
if file_logger.create_log(state.time, fn_log, mode="w"):
file_logger.signal_set_recording_text.emit(
"Recording to file: " + fn_log
Expand Down Expand Up @@ -500,9 +504,8 @@ def DAQ_function():
DAQ_interval_ms = DAQ_INTERVAL_MS,
critical_not_alive_count = 3,
debug = DEBUG,
)
)
# fmt: on

qdev_ard.create_worker_jobs(debug=DEBUG)

# Connect signals to slots
Expand All @@ -518,7 +521,7 @@ def DAQ_function():

timer_chart = QtCore.QTimer()
timer_chart.timeout.connect(update_chart)
timer_chart.start(DRAW_INTERVAL_CHART)
timer_chart.start(CHART_INTERVAL_MS)

# --------------------------------------------------------------------------
# Start the main GUI event loop
Expand Down
45 changes: 24 additions & 21 deletions Arduino_PyQt_demo_with_multithreading__minimalistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
__author__ = "Dennis van Gils"
__authoremail__ = "[email protected]"
__url__ = "https://github.com/Dennis-van-Gils/DvG_Arduino_PyQt_multithread_demo"
__date__ = "16-07-2020"
__version__ = "4.0"
__date__ = "17-07-2020"
__version__ = "4.1"
# pylint: disable=bare-except, broad-except

import os
import sys
Expand All @@ -21,27 +22,32 @@
from PyQt5.QtCore import QDateTime
import pyqtgraph as pg

from DvG_pyqt_ChartHistory import ChartHistory
from dvg_debug_functions import dprint, print_fancy_traceback as pft

from dvg_devices.Arduino_protocol_serial import Arduino
from dvg_qdeviceio import QDeviceIO

from DvG_pyqt_ChartHistory import ChartHistory

# Constants
# fmt: off
DAQ_INTERVAL_MS = 10 # 10 [ms]
DRAW_INTERVAL_CHART = 10 # 10 [ms]
CHART_HISTORY_TIME = 10 # 10 [s]

# Global variables for date-time keeping
cur_date_time = QDateTime.currentDateTime()
str_cur_date = cur_date_time.toString("dd-MM-yyyy")
str_cur_time = cur_date_time.toString("HH:mm:ss")
DAQ_INTERVAL_MS = 10 # 10 [ms]
CHART_INTERVAL_MS = 10 # 10 [ms]
CHART_HISTORY_TIME = 10 # 10 [s]
# fmt: on

# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally.
DEBUG = False


def get_current_date_time():
cur_date_time = QDateTime.currentDateTime()
return (
cur_date_time.toString("dd-MM-yyyy"), # Date
cur_date_time.toString("HH:mm:ss"), # Time
cur_date_time.toString("yyMMdd_HHmmss"), # Reverse notation date-time
)


# ------------------------------------------------------------------------------
# Arduino state
# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -82,11 +88,11 @@ def __init__(self, parent=None, **kwargs):
self.gw_chart.setBackground([20, 20, 20])
self.pi_chart = self.gw_chart.addPlot()

p = {"color": "#BBB", "font-size": "10pt"}
p = {"color": "#CCC", "font-size": "10pt"}
self.pi_chart.showGrid(x=1, y=1)
self.pi_chart.setTitle("Arduino timeseries", **p)
self.pi_chart.setLabel("bottom", text="history (sec)", **p)
self.pi_chart.setLabel("left", text="readings", **p)
self.pi_chart.setLabel("left", text="amplitude", **p)
self.pi_chart.setRange(
xRange=[-1.04 * CHART_HISTORY_TIME, CHART_HISTORY_TIME * 0.04],
yRange=[-1.1, 1.1],
Expand Down Expand Up @@ -127,10 +133,7 @@ def about_to_quit():

def DAQ_function():
# Date-time keeping
global cur_date_time, str_cur_date, str_cur_time
cur_date_time = QDateTime.currentDateTime()
str_cur_date = cur_date_time.toString("dd-MM-yyyy")
str_cur_time = cur_date_time.toString("HH:mm:ss")
str_cur_date, str_cur_time, str_cur_datetime = get_current_date_time()

# Query the Arduino for its state
success, tmp_state = ard.query_ascii_values("?", delimiter="\t")
Expand All @@ -154,7 +157,7 @@ def DAQ_function():
return False

# Use Arduino time or PC time?
use_PC_time = False
use_PC_time = True
if use_PC_time:
state.time = time.perf_counter()

Expand Down Expand Up @@ -230,8 +233,8 @@ def DAQ_function():
# --------------------------------------------------------------------------

timer_chart = QtCore.QTimer()
timer_chart.timeout.connect(lambda: window.CH_1.update_curve())
timer_chart.start(DRAW_INTERVAL_CHART)
timer_chart.timeout.connect(window.CH_1.update_curve)
timer_chart.start(CHART_INTERVAL_MS)

# --------------------------------------------------------------------------
# Start the main GUI event loop
Expand Down
Loading

0 comments on commit d9a4457

Please sign in to comment.