MicroPython: added an option to synchronize the device time to the host time after connecting the serial port. micropython

Tue, 13 Aug 2019 16:28:43 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 13 Aug 2019 16:28:43 +0200
branch
micropython
changeset 7135
44fcfc99b864
parent 7134
21d23ca51680
child 7136
bddcb634a9b8

MicroPython: added an option to synchronize the device time to the host time after connecting the serial port.

eric6/MicroPython/MicroPythonWidget.py file | annotate | diff | comparison | revisions
eric6/Preferences/ConfigurationPages/MicroPythonPage.py file | annotate | diff | comparison | revisions
eric6/Preferences/ConfigurationPages/MicroPythonPage.ui file | annotate | diff | comparison | revisions
eric6/Preferences/__init__.py file | annotate | diff | comparison | revisions
--- a/eric6/MicroPython/MicroPythonWidget.py	Tue Aug 13 15:51:35 2019 +0200
+++ b/eric6/MicroPython/MicroPythonWidget.py	Tue Aug 13 16:28:43 2019 +0200
@@ -137,8 +137,6 @@
 }
 
 
-# TODO: add config option to synchronize the device time upon connect
-
 class MicroPythonWidget(QWidget, Ui_MicroPythonWidget):
     """
     Class implementing the MicroPython REPL widget.
@@ -217,7 +215,7 @@
         self.__interface = MicroPythonCommandsInterface(self)
         self.__device = None
         self.__connected = False
-        self.setConnected(False)
+        self.__setConnected(False)
         
         if not HAS_QTSERIALPORT:
             self.replEdit.setHtml(self.tr(
@@ -391,9 +389,9 @@
         menu.addSeparator()
         menu.exec_(self.replEdit.mapToGlobal(pos))
     
-    def setConnected(self, connected):
+    def __setConnected(self, connected):
         """
-        Public method to set the connection status LED.
+        Private method to set the connection status LED.
         
         @param connected connection state
         @type bool
@@ -841,7 +839,10 @@
         """
         port = self.getCurrentPort()
         if self.__interface.connectToDevice(port):
-            self.setConnected(True)
+            self.__setConnected(True)
+            
+            if Preferences.getMicroPython("SyncTimeAfterConnect"):
+                self.__synchronizeTime(quiet=True)
         else:
             E5MessageBox.warning(
                 self,
@@ -854,7 +855,7 @@
         Private method to disconnect from the device.
         """
         self.__interface.disconnectFromDevice()
-        self.setConnected(False)
+        self.__setConnected(False)
     
     @pyqtSlot()
     def on_runButton_clicked(self):
@@ -1164,28 +1165,36 @@
         except Exception as exc:
             self.__showError("getImplementation()", str(exc))
     
-    # TODO: show device time after sync
     @pyqtSlot()
-    def __synchronizeTime(self):
+    def __synchronizeTime(self, quiet=False):
         """
         Private slot to set the time of the connected device to the local
         computer's time.
+        
+        @param quiet flag indicating to not show a message
+        @type bool
         """
         try:
             self.__interface.syncTime()
             
-            E5MessageBox.information(
-                self,
-                self.tr("Synchronize Time"),
-                self.tr("The time of the connected device was synchronized"
-                        " with the local time."))
+            if not quiet:
+                E5MessageBox.information(
+                    self,
+                    self.tr("Synchronize Time"),
+                    self.tr("<p>The time of the connected device was"
+                            " synchronized with the local time.</p>")
+                    + self.__getDeviceTime()
+                )
         except Exception as exc:
             self.__showError("syncTime()", str(exc))
     
-    @pyqtSlot()
-    def __showDeviceTime(self):
+    def __getDeviceTime(self):
         """
-        Private slot to show the date and time of the connected device.
+        Private method to get a string containing the date and time of the
+        connected device.
+        
+        @return date and time of the connected device
+        @rtype str
         """
         try:
             dateTimeString = self.__interface.getTime()
@@ -1203,13 +1212,21 @@
                     "<h3>Device Date and Time</h3>"
                     "<p>{0}</p>"
                 ).format(dateTimeString.strip())
-            
-            E5MessageBox.information(
-                self,
-                self.tr("Device Date and Time"),
-                msg)
+            return msg
         except Exception as exc:
             self.__showError("getTime()", str(exc))
+            return ""
+    
+    @pyqtSlot()
+    def __showDeviceTime(self):
+        """
+        Private slot to show the date and time of the connected device.
+        """
+        msg = self.__getDeviceTime()
+        E5MessageBox.information(
+            self,
+            self.tr("Device Date and Time"),
+            msg)
     
     @pyqtSlot()
     def __showLocalTime(self):
--- a/eric6/Preferences/ConfigurationPages/MicroPythonPage.py	Tue Aug 13 15:51:35 2019 +0200
+++ b/eric6/Preferences/ConfigurationPages/MicroPythonPage.py	Tue Aug 13 16:28:43 2019 +0200
@@ -38,6 +38,8 @@
         self.timeoutSpinBox.setValue(
             Preferences.getMicroPython("SerialTimeout") / 1000)
         # converted to seconds
+        self.syncTimeCheckBox.setChecked(
+            Preferences.getMicroPython("SyncTimeAfterConnect"))
         self.colorSchemeComboBox.setCurrentIndex(
             self.colorSchemeComboBox.findText(
                 Preferences.getMicroPython("ColorScheme")))
@@ -52,6 +54,8 @@
             "SerialTimeout", self.timeoutSpinBox.value() * 1000)
         # converted to milliseconds
         Preferences.setMicroPython(
+            "SyncTimeAfterConnect", self.syncTimeCheckBox.isChecked())
+        Preferences.setMicroPython(
             "ColorScheme", self.colorSchemeComboBox.currentText())
         Preferences.setMicroPython(
             "ReplLineWrap", self.replWrapCheckBox.isChecked())
--- a/eric6/Preferences/ConfigurationPages/MicroPythonPage.ui	Tue Aug 13 15:51:35 2019 +0200
+++ b/eric6/Preferences/ConfigurationPages/MicroPythonPage.ui	Tue Aug 13 16:28:43 2019 +0200
@@ -36,15 +36,15 @@
      <property name="title">
       <string>Serial Link</string>
      </property>
-     <layout class="QHBoxLayout" name="horizontalLayout">
-      <item>
+     <layout class="QGridLayout" name="gridLayout_2">
+      <item row="0" column="0">
        <widget class="QLabel" name="label_2">
         <property name="text">
          <string>Timeout for Serial Link Communication:</string>
         </property>
        </widget>
       </item>
-      <item>
+      <item row="0" column="1">
        <widget class="QSpinBox" name="timeoutSpinBox">
         <property name="toolTip">
          <string>Enter the timout value</string>
@@ -63,7 +63,7 @@
         </property>
        </widget>
       </item>
-      <item>
+      <item row="0" column="2">
        <spacer name="horizontalSpacer">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
@@ -76,6 +76,22 @@
         </property>
        </spacer>
       </item>
+      <item row="1" column="0" colspan="3">
+       <widget class="QCheckBox" name="syncTimeCheckBox">
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
+        </property>
+        <property name="toolTip">
+         <string>Select to synchronize the time after connection is established</string>
+        </property>
+        <property name="text">
+         <string>Synchronize Time at Connect</string>
+        </property>
+       </widget>
+      </item>
      </layout>
     </widget>
    </item>
@@ -135,6 +151,7 @@
  </widget>
  <tabstops>
   <tabstop>timeoutSpinBox</tabstop>
+  <tabstop>syncTimeCheckBox</tabstop>
   <tabstop>colorSchemeComboBox</tabstop>
   <tabstop>replWrapCheckBox</tabstop>
  </tabstops>
--- a/eric6/Preferences/__init__.py	Tue Aug 13 15:51:35 2019 +0200
+++ b/eric6/Preferences/__init__.py	Tue Aug 13 16:28:43 2019 +0200
@@ -1592,8 +1592,9 @@
     
     # defaults for MicroPython
     microPythonDefaults = {
-        "SerialTimeout": 2000,         # timeout in milliseconds
+        "SerialTimeout": 2000,          # timeout in milliseconds
         "ReplLineWrap": True,           # wrap the REPL lines
+        "SyncTimeAfterConnect": True,
     }
     if Globals.isWindowsPlatform():
         microPythonDefaults["ColorScheme"] = "Windows 10"

eric ide

mercurial