eric6/MicroPython/MicroPythonCommandsInterface.py

Tue, 13 Aug 2019 16:39:31 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 13 Aug 2019 16:39:31 +0200
branch
micropython
changeset 7136
bddcb634a9b8
parent 7126
376deb7fefe7
child 7137
4ed2573947ff
permissions
-rw-r--r--

MicroPythonCommandsInterface: finished testing.

7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2019 Detlev Offenbach <detlev@die-offenbachs.de>
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing some file system commands for MicroPython.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 from __future__ import unicode_literals
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import ast
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 import time
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 import os
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
7113
04ac3f9a87e6 MicroPythonCommandsInterface: fixed a code formatting issue and simplified the 'is_visible()' function.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7112
diff changeset
16 from PyQt5.QtCore import (
04ac3f9a87e6 MicroPythonCommandsInterface: fixed a code formatting issue and simplified the 'is_visible()' function.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7112
diff changeset
17 pyqtSlot, pyqtSignal, QObject, QThread, QTimer, QCoreApplication,
04ac3f9a87e6 MicroPythonCommandsInterface: fixed a code formatting issue and simplified the 'is_visible()' function.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7112
diff changeset
18 QEventLoop
04ac3f9a87e6 MicroPythonCommandsInterface: fixed a code formatting issue and simplified the 'is_visible()' function.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7112
diff changeset
19 )
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 from .MicroPythonSerialPort import MicroPythonSerialPort
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 import Preferences
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 class MicroPythonCommandsInterface(QObject):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 Class implementing some file system commands for MicroPython.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 Commands are provided to perform operations on the file system of a
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 connected MicroPython device. Supported commands are:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 <ul>
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 <li>ls: directory listing</li>
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 <li>lls: directory listing with meta data</li>
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 <li>cd: change directory</li>
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 <li>pwd: get the current directory</li>
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 <li>put: copy a file to the connected device</li>
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 <li>get: get a file from the connected device</li>
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 <li>rm: remove a file from the connected device</li>
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 <li>rmrf: remove a file/directory recursively (like 'rm -rf' in bash)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 <li>mkdir: create a new directory</li>
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 <li>rmdir: remove an empty directory</li>
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 </ul>
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 There are additional commands related to time and version.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 <ul>
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 <li>version: get version info about MicroPython</li>
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 <li>getImplementation: get some implementation information</li>
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 <li>syncTime: synchronize the time of the connected device</li>
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 <li>showTime: show the current time of the connected device</li>
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 </ul>
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 @signal executeAsyncFinished() emitted to indicate the end of an
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 asynchronously executed list of commands (e.g. a script)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 @signal dataReceived(data) emitted to send data received via the serial
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 connection for further processing
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 executeAsyncFinished = pyqtSignal()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 dataReceived = pyqtSignal(bytes)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 def __init__(self, parent=None):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 Constructor
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 @param parent reference to the parent object
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 @type QObject
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 super(MicroPythonCommandsInterface, self).__init__(parent)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69
7126
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
70 self.__repl = parent
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
71
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 self.__blockReadyRead = False
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 self.__serial = MicroPythonSerialPort(
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 timeout=Preferences.getMicroPython("SerialTimeout"),
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 parent=self)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 self.__serial.readyRead.connect(self.__readSerial)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 @pyqtSlot()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 def __readSerial(self):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 Private slot to read all available serial data and emit it with the
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 "dataReceived" signal for further processing.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 if not self.__blockReadyRead:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 data = bytes(self.__serial.readAll())
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 self.dataReceived.emit(data)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 @pyqtSlot()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 def connectToDevice(self, port):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 Public slot to start the manager.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 @param port name of the port to be used
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 @type str
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 @return flag indicating success
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 @rtype bool
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 return self.__serial.openSerialLink(port)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 @pyqtSlot()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 def disconnectFromDevice(self):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 Public slot to stop the thread.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 self.__serial.closeSerialLink()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 def isConnected(self):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 Public method to get the connection status.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 @return flag indicating the connection status
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 @rtype bool
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 return self.__serial.isConnected()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 @pyqtSlot()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 def handlePreferencesChanged(self):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 Public slot to handle a change of the preferences.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 self.__serial.setTimeout(Preferences.getMicroPython("SerialTimeout"))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 def write(self, data):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 Public method to write data to the connected device.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 @param data data to be written
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 @type bytes or bytearray
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 self.__serial.isConnected() and self.__serial.write(data)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 def __rawOn(self):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 Private method to switch the connected device to 'raw' mode.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 Note: switching to raw mode is done with synchronous writes.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 @return flag indicating success
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 @@rtype bool
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 if not self.__serial:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 return False
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144
7108
4f6133a01c6a Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7102
diff changeset
145 rawReplMessage = b"raw REPL; CTRL-B to exit\r\n>"
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 self.__serial.write(b"\x02") # end raw mode if required
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 self.__serial.waitForBytesWritten()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 for _i in range(3):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 # CTRL-C three times to break out of loops
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 self.__serial.write(b"\r\x03")
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 self.__serial.waitForBytesWritten()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 QThread.msleep(10)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 self.__serial.readAll() # read all data and discard it
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 self.__serial.write(b"\r\x01") # send CTRL-A to enter raw mode
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 self.__serial.readUntil(rawReplMessage)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 if self.__serial.hasTimedOut():
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
158 # it timed out; try it again and than fail
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 self.__serial.write(b"\r\x01") # send CTRL-A again
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 self.__serial.readUntil(rawReplMessage)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 if self.__serial.hasTimedOut():
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 return False
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
163
7108
4f6133a01c6a Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7102
diff changeset
164 QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 self.__serial.readAll() # read all data and discard it
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 return True
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 def __rawOff(self):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 Private method to switch 'raw' mode off.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 if self.__serial:
7102
5e77aa4671e6 MicroPythonCommandsInterface: fixed an issue causing the prompt being shown in the REPL pane for each file manager command sequence.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7095
diff changeset
173 self.__serial.write(b"\x02") # send CTRL-B to cancel raw mode
5e77aa4671e6 MicroPythonCommandsInterface: fixed an issue causing the prompt being shown in the REPL pane for each file manager command sequence.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7095
diff changeset
174 self.__serial.readUntil(b">>> ") # read until Python prompt
5e77aa4671e6 MicroPythonCommandsInterface: fixed an issue causing the prompt being shown in the REPL pane for each file manager command sequence.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7095
diff changeset
175 self.__serial.readAll() # read all data and discard it
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 def execute(self, commands):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 Public method to send commands to the connected device and return the
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 result.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 If no serial connection is available, empty results will be returned.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 @param commands list of commands to be executed
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 @type str
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 @return tuple containing stdout and stderr output of the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 @rtype tuple of (bytes, bytes)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 if not self.__serial:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 return b"", b""
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 if not self.__serial.isConnected():
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 return b"", b"Device not connected or not switched on."
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 result = bytearray()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 err = b""
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
198 # switch on raw mode
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 self.__blockReadyRead = True
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 ok = self.__rawOn()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 if not ok:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 self.__blockReadyRead = False
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 return (
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 b"",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 b"Could not switch to raw mode. Is the device switched on?"
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 )
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
208 # send commands
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 QThread.msleep(10)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 for command in commands:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 if command:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 commandBytes = command.encode("utf-8")
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 self.__serial.write(commandBytes + b"\x04")
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
214 QCoreApplication.processEvents(
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
215 QEventLoop.ExcludeUserInputEvents)
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
216 ok = self.__serial.readUntil(b"OK")
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
217 if ok != b"OK":
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
218 return (
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
219 b"",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
220 "Expected 'OK', got '{0}', followed by '{1}'".format(
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
221 ok, self.__serial.readAll()).encode("utf-8")
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
222 )
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
223
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 # read until prompt
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 response = self.__serial.readUntil(b"\x04>")
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 if self.__serial.hasTimedOut():
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 self.__blockReadyRead = False
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 return b"", b"Timeout while processing commands."
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
229 if b"\x04" in response[:-2]:
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 # split stdout, stderr
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
231 out, err = response[:-2].split(b"\x04")
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 result += out
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 else:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 err = b"invalid response received: " + response
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 if err:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 self.__blockReadyRead = False
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 return b"", err
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
238
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
239 # switch off raw mode
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 QThread.msleep(10)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 self.__rawOff()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242 self.__blockReadyRead = False
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 return bytes(result), err
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 def executeAsync(self, commandsList):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 Public method to execute a series of commands over a period of time
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 without returning any result (asynchronous execution).
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 @param commandsList list of commands to be execute on the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 @type list of bytes
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 def remainingTask(commands):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 self.executeAsync(commands)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 if commandsList:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 command = commandsList[0]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 self.__serial.write(command)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 remainder = commandsList[1:]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261 QTimer.singleShot(2, lambda: remainingTask(remainder))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 else:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 self.executeAsyncFinished.emit()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 def __shortError(self, error):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 Private method to create a shortened error message.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 @param error verbose error message
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 @type bytes
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 @return shortened error message
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 @rtype str
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 if error:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 decodedError = error.decode("utf-8")
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 try:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 return decodedError.split["\r\n"][-2]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 except Exception:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 return decodedError
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 return self.tr("Detected an error without indications.")
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 ##################################################################
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 ## Methods below implement the file system commands
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 ##################################################################
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
285
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
286 def ls(self, dirname=""):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
288 Public method to get a directory listing of the connected device.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 @param dirname name of the directory to be listed
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291 @type str
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 @return tuple containg the directory listing
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 @rtype tuple of str
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294 @exception IOError raised to indicate an issue with the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295 """
7126
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
296 if self.__repl.isMicrobit():
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
297 # BBC micro:bit does not support directories
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
298 commands = [
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
299 "import os as __os_",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
300 "print(__os_.listdir())",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
301 "del __os_",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
302 ]
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
303 else:
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
304 commands = [
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
305 "import os as __os_",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
306 "print(__os_.listdir('{0}'))".format(dirname),
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
307 "del __os_",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
308 ]
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309 out, err = self.execute(commands)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310 if err:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
311 raise IOError(self.__shortError(err))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312 return ast.literal_eval(out.decode("utf-8"))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
313
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
314 def lls(self, dirname="", fullstat=False):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
315 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
316 Public method to get a long directory listing of the connected device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
317 including meta data.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
318
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
319 @param dirname name of the directory to be listed
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
320 @type str
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
321 @param fullstat flag indicating to return the full stat() tuple
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
322 @type bool
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
323 @return list containing the directory listing with tuple entries of
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
324 the name and and a tuple of mode, size and time (if fullstat is
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325 false) or the complete stat() tuple. 'None' is returned in case the
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
326 directory doesn't exist.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
327 @rtype tuple of (str, tuple)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 @exception IOError raised to indicate an issue with the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329 """
7126
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
330 if self.__repl.isMicrobit():
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
331 # BBC micro:bit does not support directories
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
332 commands = [
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
333 "import os as __os_",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
334 "\n".join([
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
335 "def is_visible(filename):",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
336 " return filename[0] != '.' and filename[-1] != '~'",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
337 ]),
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
338 "\n".join([
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
339 "def stat(filename):",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
340 " size = __os_.size(filename)",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
341 " return (0, 0, 0, 0, 0, 0, size, 0, 0, 0)"
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
342 ]),
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
343 "\n".join([
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
344 "def listdir_stat():",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
345 " files = __os_.listdir()",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
346 " return list((f, stat(f)) for f in files if"
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
347 " is_visible(f))",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
348 ]),
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
349 "print(listdir_stat())",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
350 "del __os_, stat, listdir_stat, is_visible",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
351 ]
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
352 else:
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
353 commands = [
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
354 "import os as __os_",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
355 "\n".join([
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
356 "def is_visible(filename):",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
357 " return filename[0] != '.' and filename[-1] != '~'",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
358 ]),
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
359 "\n".join([
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
360 "def stat(filename):",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
361 " try:",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
362 " rstat = __os_.lstat(filename)",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
363 " except:",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
364 " rstat = __os_.stat(filename)",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
365 " return tuple(rstat)",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
366 ]),
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
367 "\n".join([
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
368 "def listdir_stat(dirname):",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
369 " try:",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
370 " files = __os_.listdir(dirname)",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
371 " except OSError:",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
372 " return None",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
373 " if dirname in ('', '/'):",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
374 " return list((f, stat(f)) for f in files if"
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
375 " is_visible(f))",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
376 " return list((f, stat(dirname + '/' + f))"
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
377 " for f in files if is_visible(f))",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
378 ]),
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
379 "print(listdir_stat('{0}'))".format(dirname),
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
380 "del __os_, stat, listdir_stat, is_visible",
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
381 ]
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
382 out, err = self.execute(commands)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
383 if err:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
384 raise IOError(self.__shortError(err))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
385 fileslist = ast.literal_eval(out.decode("utf-8"))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
386 if fileslist is None:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
387 return None
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
388 else:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
389 if fullstat:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
390 return fileslist
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
391 else:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
392 return [(f, (s[0], s[6], s[8])) for f, s in fileslist]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
394 def cd(self, dirname):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
396 Public method to change the current directory on the connected device.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
397
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
398 @param dirname directory to change to
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
399 @type str
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
400 @exception IOError raised to indicate an issue with the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
401 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
402 assert dirname
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
403
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
404 commands = [
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
405 "import os as __os_",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
406 "__os_.chdir('{0}')".format(dirname),
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
407 "del __os_",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
408 ]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
409 out, err = self.execute(commands)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
410 if err:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
411 raise IOError(self.__shortError(err))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
412
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413 def pwd(self):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
415 Public method to get the current directory of the connected device.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
416
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417 @return current directory
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 @rtype str
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419 @exception IOError raised to indicate an issue with the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
420 """
7126
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
421 if self.__repl.isMicrobit():
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
422 # BBC micro:bit does not support directories
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
423 return ""
376deb7fefe7 microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7124
diff changeset
424
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
425 commands = [
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
426 "import os as __os_",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
427 "print(__os_.getcwd())",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
428 "del __os_",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429 ]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
430 out, err = self.execute(commands)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431 if err:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432 raise IOError(self.__shortError(err))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 return out.decode("utf-8").strip()
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
434
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
435 def rm(self, filename):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
436 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
437 Public method to remove a file from the connected device.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
438
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
439 @param filename name of the file to be removed
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
440 @type str
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
441 @exception IOError raised to indicate an issue with the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
442 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
443 assert filename
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
444
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
445 commands = [
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
446 "import os as __os_",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
447 "__os_.remove('{0}')".format(filename),
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
448 "del __os_",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
449 ]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
450 out, err = self.execute(commands)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
451 if err:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
452 raise IOError(self.__shortError(err))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
453
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
454 def rmrf(self, name, recursive=False, force=False):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
455 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
456 Public method to remove a file or directory recursively.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
457
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
458 @param name of the file or directory to remove
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
459 @type str
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
460 @param recursive flag indicating a recursive deletion
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
461 @type bool
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
462 @param force flag indicating to ignore errors
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
463 @type bool
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
464 @return flag indicating success
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
465 @rtype bool
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
466 @exception IOError raised to indicate an issue with the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
467 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
468 assert name
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
469
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
470 commands = [
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
471 "import os as __os_",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
472 "\n".join([
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
473 "def remove_file(name, recursive=False, force=False):",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
474 " try:",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
475 " mode = __os_.stat(name)[0]",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
476 " if mode & 0x4000 != 0:",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
477 " if recursive:",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
478 " for file in __os_.listdir(name):",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
479 " success = remove_file(name + '/' + file,"
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
480 " recursive, force)",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
481 " if not success and not force:",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
482 " return False",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
483 " __os_.rmdir(name)",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
484 " else:",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
485 " if not force:",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
486 " return False",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
487 " else:",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
488 " __os_.remove(name)",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
489 " except:",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
490 " if not force:",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
491 " return False",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
492 " return True",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
493 ]),
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
494 "print(remove_file('{0}', {1}, {2}))".format(name, recursive,
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
495 force),
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
496 "del __os_, remove_file",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
497 ]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
498 out, err = self.execute(commands)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
499 if err:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
500 raise IOError(self.__shortError(err))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
501 return ast.literal_eval(out.decode("utf-8"))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
502
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
503 def mkdir(self, dirname):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
504 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
505 Public method to create a new directory.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
506
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
507 @param dirname name of the directory to create
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
508 @type str
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
509 @exception IOError raised to indicate an issue with the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
510 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
511 assert dirname
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
512
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
513 commands = [
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
514 "import os as __os_",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
515 "__os_.mkdir('{0}')".format(dirname),
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
516 "del __os_",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
517 ]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
518 out, err = self.execute(commands)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
519 if err:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
520 raise IOError(self.__shortError(err))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
521
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
522 def rmdir(self, dirname):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
523 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
524 Public method to remove a directory.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
525
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
526 @param dirname name of the directory to be removed
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
527 @type str
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
528 @exception IOError raised to indicate an issue with the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
529 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
530 assert dirname
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
531
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
532 commands = [
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
533 "import os as __os_",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
534 "__os_.rmdir('{0}')".format(dirname),
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
535 "del __os_",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
536 ]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
537 out, err = self.execute(commands)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
538 if err:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
539 raise IOError(self.__shortError(err))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
540
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
541 def put(self, hostFileName, deviceFileName=None):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
542 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
543 Public method to copy a local file to the connected device.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
544
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
545 @param hostFileName name of the file to be copied
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
546 @type str
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
547 @param deviceFileName name of the file to copy to
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
548 @type str
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
549 @return flag indicating success
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
550 @rtype bool
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
551 @exception IOError raised to indicate an issue with the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
552 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
553 if not os.path.isfile(hostFileName):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
554 raise IOError("No such file: {0}".format(hostFileName))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
555
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
556 with open(hostFileName, "rb") as hostFile:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
557 content = hostFile.read()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
558 # convert eol '\r'
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
559 content = content.replace(b"\r\n", b"\r")
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
560 content = content.replace(b"\n", b"\r")
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
561
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
562 if not deviceFileName:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
563 deviceFileName = os.path.basename(hostFileName)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
564
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
565 commands = [
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
566 "fd = open('{0}', 'wb')".format(deviceFileName),
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
567 "f = fd.write",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
568 ]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
569 while content:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
570 chunk = content[:64]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
571 commands.append("f(" + repr(chunk) + ")")
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
572 content = content[64:]
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
573 commands.extend([
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
574 "fd.close()",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
575 "del f, fd",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
576 ])
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
577
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
578 out, err = self.execute(commands)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
579 if err:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
580 raise IOError(self.__shortError(err))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
581 return True
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
582
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
583 def get(self, deviceFileName, hostFileName=None):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
584 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
585 Public method to copy a file from the connected device.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
586
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
587 @param deviceFileName name of the file to copy
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
588 @type str
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
589 @param hostFileName name of the file to copy to
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
590 @type str
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
591 @return flag indicating success
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
592 @rtype bool
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
593 @exception IOError raised to indicate an issue with the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
594 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
595 if not hostFileName:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
596 hostFileName = deviceFileName
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
597
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
598 commands = [
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
599 "\n".join([
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
600 "def send_data():",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
601 " try:",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
602 " from microbit import uart as u",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
603 " except ImportError:",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
604 " try:",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
605 " from machine import UART",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
606 " u = UART(0, {0})".format(115200),
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
607 " except Exception:",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
608 " try:",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
609 " from sys import stdout as u",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
610 " except Exception:",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
611 " raise Exception('Could not find UART module"
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
612 " in device.')",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
613 " f = open('{0}', 'rb')".format(deviceFileName),
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
614 " r = f.read",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
615 " result = True",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
616 " while result:",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
617 " result = r(32)",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
618 " if result:",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
619 " u.write(result)",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
620 " f.close()",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
621 ]),
7115
fe89c98430b6 Fixed some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7113
diff changeset
622 "send_data()",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
623 ]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
624 out, err = self.execute(commands)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
625 if err:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
626 raise IOError(self.__shortError(err))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
627
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
628 # write the received bytes to the local file
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
629 # convert eol to "\n"
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
630 out = out.replace(b"\r\n", b"\n")
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
631 out = out.replace(b"\r", b"\n")
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
632 with open(hostFileName, "wb") as hostFile:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
633 hostFile.write(out)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
634 return True
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
635
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
636 def fileSystemInfo(self):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
637 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
638 Public method to obtain information about the currently mounted file
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
639 systems.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
640
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
641 @return tuple of tuples containing the file system name, the total
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
642 size, the used size and the free size
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
643 @rtype tuple of tuples of (str, int, int, int)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
644 @exception IOError raised to indicate an issue with the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
645 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
646 commands = [
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
647 "import os as __os_",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
648 "\n".join([
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
649 "def fsinfo():",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
650 " infolist = []",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
651 " info = __os_.statvfs('/')",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
652 " if info[0] == 0:",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
653 # assume it is just mount points
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
654 " fsnames = __os_.listdir('/')",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
655 " for fs in fsnames:",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
656 " fs = '/' + fs",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
657 " infolist.append((fs, __os_.statvfs(fs)))",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
658 " else:",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
659 " infolist.append(('/', info))",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
660 " return infolist",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
661 ]),
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
662 "print(fsinfo())",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
663 "del __os_, fsinfo",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
664 ]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
665 out, err = self.execute(commands)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
666 if err:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
667 raise IOError(self.__shortError(err))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
668 infolist = ast.literal_eval(out.decode("utf-8"))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
669 if infolist is None:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
670 return None
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
671 else:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
672 filesystemInfos = []
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
673 for fs, info in infolist:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
674 totalSize = info[2] * info[1]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
675 freeSize = info[4] * info[1]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
676 usedSize = totalSize - freeSize
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
677 filesystemInfos.append((fs, totalSize, usedSize, freeSize))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
678
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
679 return tuple(filesystemInfos)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
680
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
681 ##################################################################
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
682 ## non-filesystem related methods below
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
683 ##################################################################
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
684
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
685 def version(self):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
686 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
687 Public method to get the MicroPython version information of the
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
688 connected device.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
689
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
690 @return dictionary containing the version information
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
691 @rtype dict
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
692 @exception IOError raised to indicate an issue with the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
693 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
694 commands = [
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
695 "import os as __os_",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
696 "print(__os_.uname())",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
697 "del __os_",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
698 ]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
699 out, err = self.execute(commands)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
700 if err:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
701 raise IOError(self.__shortError(err))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
702
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
703 rawOutput = out.decode("utf-8").strip()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
704 rawOutput = rawOutput[1:-1]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
705 items = rawOutput.split(",")
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
706 result = {}
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
707 for item in items:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
708 key, value = item.strip().split("=")
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
709 result[key.strip()] = value.strip()[1:-1]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
710 return result
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
711
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
712 def getImplementation(self):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
713 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
714 Public method to get some implementation information of the connected
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
715 device.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
716
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
717 @return dictionary containing the implementation information
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
718 @rtype dict
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
719 @exception IOError raised to indicate an issue with the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
720 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
721 commands = [
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
722 "import sys as __sys_",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
723 "res = {}", # __IGNORE_WARNING_M613__
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
724 "\n".join([
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
725 "try:",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
726 " res['name'] = __sys_.implementation.name",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
727 "except AttributeError:",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
728 " res['name'] = 'unknown'",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
729 ]),
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
730 "\n".join([
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
731 "try:",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
732 " res['version'] = '.'.join((str(i) for i in"
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
733 " __sys_.implementation.version))",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
734 "except AttributeError:",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
735 " res['version'] = 'unknown'",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
736 ]),
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
737 "print(res)",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
738 "del res, __sys_",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
739 ]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
740 out, err = self.execute(commands)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
741 if err:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
742 raise IOError(self.__shortError(err))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
743 return ast.literal_eval(out.decode("utf-8"))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
744
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
745 def syncTime(self):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
746 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
747 Public method to set the time of the connected device to the local
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
748 computer's time.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
749
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
750 @exception IOError raised to indicate an issue with the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
751 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
752 now = time.localtime(time.time())
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
753 commands = [
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
754 "\n".join([
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
755 "def set_time(rtc_time):",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
756 " rtc = None",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
757 " try:", # Pyboard (it doesn't have machine.RTC())
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
758 " import pyb as __pyb_",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
759 " rtc = __pyb_.RTC()",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
760 " clock_time = rtc_time[:6] + (rtc_time[6] + 1, 0)",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
761 " rtc.datetime(clock_time)",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
762 " del __pyb_",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
763 " except Exception:",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
764 " try:",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
765 " import machine as __machine_",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
766 " rtc = __machine_.RTC()",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
767 " try:", # ESP8266 may use rtc.datetime()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
768 " clock_time = rtc_time[:6] +"
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
769 " (rtc_time[6] + 1, 0)",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
770 " rtc.datetime(clock_time)",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
771 " except Exception:", # ESP32 uses rtc.init()
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
772 " rtc.init(rtc_time[:6])",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
773 " del __machine_",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
774 " except:",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
775 " try:",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
776 " import rtc as __rtc_",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
777 " import time as __time_",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
778 " clock=__rtc_.RTC()",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
779 " clock.datetime = __time_.struct_time("
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
780 "rtc_time + (-1, -1))",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
781 " del __rtc_, __time_",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
782 " except:",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
783 " pass",
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
784 ]),
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
785 "set_time({0})".format((now.tm_year, now.tm_mon, now.tm_mday,
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
786 now.tm_hour, now.tm_min, now.tm_sec,
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
787 now.tm_wday)),
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
788 "del set_time",
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
789 ]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
790 out, err = self.execute(commands)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
791 if err:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
792 raise IOError(self.__shortError(err))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
793
7108
4f6133a01c6a Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7102
diff changeset
794 def getTime(self):
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
795 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
796 Public method to get the current time of the device.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
797
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
798 @return time of the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
799 @rtype str
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
800 @exception IOError raised to indicate an issue with the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
801 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
802 commands = [
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
803 "import time as __time_",
7108
4f6133a01c6a Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7102
diff changeset
804 "\n".join([
4f6133a01c6a Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7102
diff changeset
805 "try:",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
806 " print(__time_.strftime('%Y-%m-%d %H:%M:%S',"
7115
fe89c98430b6 Fixed some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7113
diff changeset
807 # __IGNORE_WARNING_M601__
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
808 " __time_.localtime()))",
7108
4f6133a01c6a Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7102
diff changeset
809 "except AttributeError:",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
810 " tm = __time_.localtime()",
7108
4f6133a01c6a Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7102
diff changeset
811 " print('{0:04d}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d}'"
4f6133a01c6a Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7102
diff changeset
812 ".format(tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour,"
4f6133a01c6a Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7102
diff changeset
813 " tm.tm_min, tm.tm_sec))",
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
814 " del tm",
7108
4f6133a01c6a Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7102
diff changeset
815 ]),
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
816 "del __time_"
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
817 ]
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
818 out, err = self.execute(commands)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
819 if err:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
820 raise IOError(self.__shortError(err))
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
821 return out.decode("utf-8").strip()

eric ide

mercurial