From 8602b82febf6044aed43a69526b15e99b6eabd3d Mon Sep 17 00:00:00 2001 From: Jonas Scharpf Date: Sat, 13 May 2023 14:24:00 +0200 Subject: [PATCH 1/2] fix logger usage error, see #31 --- changelog.md | 7 ++++++- nextion/nextion_rtc.py | 7 ++++--- nextion/nextion_upload.py | 34 +++++++++++++++++----------------- nextion/nextion_waveform.py | 6 +++--- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/changelog.md b/changelog.md index 39dcf1b..a3f733a 100644 --- a/changelog.md +++ b/changelog.md @@ -17,6 +17,10 @@ r"^\#\# \[\d{1,}[.]\d{1,}[.]\d{1,}\] \- \d{4}\-\d{2}-\d{2}$" --> ## Released +## [0.15.2] - 2023-05-14 +### Fixed +- Logger import fixed, see #31 + ## [0.15.1] - 2023-05-14 ### Fixed - Remove yet unavailable files from `package.json`, see #33 @@ -220,8 +224,9 @@ r"^\#\# \[\d{1,}[.]\d{1,}[.]\d{1,}\] \- \d{4}\-\d{2}-\d{2}$" - [Example HMI file](examples/everything.HMI) to be used for all examples -[Unreleased]: https://github.com/brainelectronics/micropython-nextion/compare/0.15.1...develop +[Unreleased]: https://github.com/brainelectronics/micropython-nextion/compare/0.15.2...develop +[0.15.2]: https://github.com/brainelectronics/micropython-nextion/tree/0.15.2 [0.15.1]: https://github.com/brainelectronics/micropython-nextion/tree/0.15.1 [0.15.0]: https://github.com/brainelectronics/micropython-nextion/tree/0.15.0 [0.14.0]: https://github.com/brainelectronics/micropython-nextion/tree/0.14.0 diff --git a/nextion/nextion_rtc.py b/nextion/nextion_rtc.py index 6e44e6a..f52075f 100644 --- a/nextion/nextion_rtc.py +++ b/nextion/nextion_rtc.py @@ -83,8 +83,9 @@ def write_rtc_time(self, *args, **kwargs) -> bool: format("2016,11,25,12,34,50", [2016, 11, 25, 12, 34, 50])) - self._logger.debug("Timestamp (ISO8601): {}-{}-{}T{}:{}:{}". - format(year, month, day, hour, minute, second)) + self._nh._logger.debug( + "Timestamp (ISO8601): {}-{}-{}T{}:{}:{}".format( + year, month, day, hour, minute, second)) cmd = "rtc0={}".format(year) self._nh.sendCommand(cmd) @@ -121,7 +122,7 @@ def write_rtc_time(self, *args, **kwargs) -> bool: else: raise NexRtcError("Either use keyword or positional args") - self._logger.debug("Set '{}' to '{}'".format(time_type, time)) + self._nh._logger.debug("Set '{}' to '{}'".format(time_type, time)) rtc_index = self.time_types.index(time_type.lower()) cmd = "rtc{}={}".format(rtc_index, time) diff --git a/nextion/nextion_upload.py b/nextion/nextion_upload.py index d6506b1..20628d9 100644 --- a/nextion/nextion_upload.py +++ b/nextion/nextion_upload.py @@ -112,7 +112,7 @@ def upload(self) -> bool: if not self._downloadTftFile(): raise NexUploadError("Download file error") - self._logger.debug("Download ok") + self._nh._logger.debug("Download ok") return True def _checkFile(self) -> bool: @@ -127,13 +127,13 @@ def _checkFile(self) -> bool: # https://docs.python.org/3/library/os.html#os.stat info = stat(self.file_name) self.file_size = info[6] - self._logger.debug("TFT file size is '{}' bytes". - format(self.file_size)) - self._logger.debug("File check ok") + self._nh._logger.debug("TFT file size is '{}' bytes". + format(self.file_size)) + self._nh._logger.debug("File check ok") result = True else: - self._logger.debug("File '{}' does not exist". - format(self.file_name)) + self._nh._logger.debug("File '{}' does not exist". + format(self.file_name)) return result def _getBaudrate(self) -> int: @@ -147,13 +147,13 @@ def _getBaudrate(self) -> int: _baudrate = 0 for baudrate in baudrate_array: - self._logger.debug("Checking connection with '{}' baud". - format(baudrate)) + self._nh._logger.debug("Checking connection with '{}' baud". + format(baudrate)) if self._searchBaudrate(baudrate): _baudrate = baudrate - self._logger.debug("Success, baudrate set to '{}' baud". - format(_baudrate)) + self._nh._logger.debug("Success, baudrate set to '{}' baud". + format(_baudrate)) return _baudrate return _baudrate @@ -174,8 +174,8 @@ def _searchBaudrate(self, baudrate: int) -> bool: self._nh.sendCommand("connect") sleep(0.1) # necessary, data might not be available otherwise response = self._recvRetString() - self._logger.debug("_searchBaudrate response for '{}' baud: {}". - format(baudrate, response)) + self._nh._logger.debug("_searchBaudrate response for '{}' baud: {}". + format(baudrate, response)) if "comok" in response: return True @@ -193,7 +193,7 @@ def _setDownloadBaudrate(self, baudrate: int) -> bool: :rtype: bool """ cmd = "whmi-wri {},{},0".format(self.file_size, baudrate) - self._logger.debug("Set download baudrate cmd: '{}'".format(cmd)) + self._nh._logger.debug("Set download baudrate cmd: '{}'".format(cmd)) self._nh.sendCommand("") self._nh.sendCommand(cmd) @@ -201,8 +201,8 @@ def _setDownloadBaudrate(self, baudrate: int) -> bool: self._nh._baudrate = baudrate self._nh._uart_init() response = self._recvRetString(500) - self._logger.debug("Set download baudrate response: '{}'". - format(response)) + self._nh._logger.debug( + "Set download baudrate response: '{}'".format(response)) if (0x05).to_bytes(1, 'little') in response: return True return False @@ -222,13 +222,13 @@ def _downloadTftFile(self) -> bool: data_size = update_file.readinto(file_content) if not data_size: - self._logger.debug("Reached EOF, update finished") + self._nh._logger.debug("Reached EOF, update finished") break self._nh._uart.write(file_content) response = self._recvRetString(500) - # self._logger.debug("File download response: '{}'". + # self._nh._logger.debug("File download response: '{}'". # format(response)) if (0x05).to_bytes(1, 'little') in response: diff --git a/nextion/nextion_waveform.py b/nextion/nextion_waveform.py index e90c4f4..b28913b 100644 --- a/nextion/nextion_waveform.py +++ b/nextion/nextion_waveform.py @@ -49,7 +49,7 @@ def addValue(self, ch: int, number: int) -> bool: :rtype: bool """ if ch > 3: - self._logger.debug("Only channels (0-3) supported by waveform") + self._nh._logger.debug("Only channels (0-3) supported by waveform") return False cmd = "add {},{},{}".format(self.cid, ch, number) @@ -67,8 +67,8 @@ def clearChannel(self, ch: int) -> bool: :rtype: bool """ if (ch > 3) and (ch != 255): - self._logger.debug("Only the channels (0-3) or all channels at " - "once (255) can be cleared") + self._nh._logger.debug("Only the channels (0-3) or all channels " + "at once (255) can be cleared") return False cmd = "cle {},{}".format(self.cid, ch) From 0d9c2bbbe9d9416595343cce753ae8bb2a864b0c Mon Sep 17 00:00:00 2001 From: Jonas Scharpf Date: Sun, 14 May 2023 20:57:00 +0200 Subject: [PATCH 2/2] add port specific UART pin dummy code in examples README, see #30 --- changelog.md | 1 + examples/README.md | 74 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/changelog.md b/changelog.md index a3f733a..751954e 100644 --- a/changelog.md +++ b/changelog.md @@ -20,6 +20,7 @@ r"^\#\# \[\d{1,}[.]\d{1,}[.]\d{1,}\] \- \d{4}\-\d{2}-\d{2}$" ## [0.15.2] - 2023-05-14 ### Fixed - Logger import fixed, see #31 +- Examples README improved with MicroPython port specific UART pin usage example, see #30 ## [0.15.1] - 2023-05-14 ### Fixed diff --git a/examples/README.md b/examples/README.md index c4d2fc2..84e183f 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,29 +1,29 @@ ## Examples Overview -| Example | Nextion name | Supported | +| Example | Nextion name | Supported | | ------------------------- | ------------- | ------------------ | | [Button](button) | NexButton | :heavy_check_mark: | | [Checkbox](checkbox) | NexCheckbox | :heavy_check_mark: | -| [Crop](crop) | NexCrop | :x: | +| [Crop](crop) | NexCrop | :x: | | [DualButton](dual_button) | NexDual | :heavy_check_mark: | | [Gauge](gauge) | NexGauge | :heavy_check_mark: | | [Gpio](gpio) | NexGpio | :heavy_check_mark: | | [Hardware](hardware) | NexHardware | :heavy_check_mark: | -| [Hotspot](hotspot) | NexHotspot | :x: | +| [Hotspot](hotspot) | NexHotspot | :x: | | [Number](number) | NexNumber | :heavy_check_mark: | | [Page](page) | NexPage | :heavy_check_mark: | -| [Picture](picture) | NexPicture | :x: | +| [Picture](picture) | NexPicture | :x: | | [Progress](progress) | NexProgress | :heavy_check_mark: | | [Radio](radio) | NexRadio | :heavy_check_mark: | | [Rtc](rtc) | NexRtc | :heavy_check_mark: | -| [Scrolltext](scrolltext) | NexScrolltext | :x: | +| [Scrolltext](scrolltext) | NexScrolltext | :x: | | [Slider](slider) | NexSlider | :heavy_check_mark: | | [Text](text) | NexText | :heavy_check_mark: | -| [Timer](timer) | NexTimer | :x: | -| [Touch](touch) | NexTouch | :x: | +| [Timer](timer) | NexTimer | :x: | +| [Touch](touch) | NexTouch | :x: | | [Upload](upload) | NexUpload | :heavy_check_mark: | -| [Variable](variable) | NexVariable | :heavy_check_mark: | -| [waveform](waveform) | NexWaveform | :heavy_check_mark: | +| [Variable](variable) | NexVariable | :heavy_check_mark: | +| [waveform](waveform) | NexWaveform | :heavy_check_mark: | @@ -32,6 +32,62 @@ The [basic example](basic/main.py) shows the usage of all supported Nextion elements by simple UART command calls. +The provided examples are designed for an **ESP32**. + +Check [MicroPython UART documentation](https://docs.micropython.org/en/latest/library/machine.UART.html) +and the device/port specific setup for further details. + +On a RP2 the RX and TX pin needs to be defined as e.g. `tx_pin = Pin(4)`, +`rx_pin = Pin(5)` and `uart_id = 1` whereas an ESP32 can use any pin with e.g. +`tx_pin = 21` and `rx_pin = 22` and `uart_id = 1`. + +The following example code provides a universal RX/TX pin and UART ID setup. + +```python +tx_pin = 21 +rx_pin = 22 +uart_id = 1 + +try: + from machine import Pin + import os + from umodbus import version + + os_info = os.uname() + print('MicroPython infos: {}'.format(os_info)) + print('Used micropthon-modbus version: {}'.format(version.__version__)) + + if 'pyboard' in os_info: + # NOT YET TESTED ! + # https://docs.micropython.org/en/latest/library/pyb.UART.html#pyb-uart + # (TX, RX) = (X9, X10) = (PB6, PB7) + tx_pin = Pin(PB6) + rx_pin = Pin(PB7) + uart_id = 1 + elif 'esp8266' in os_info: + # NOT YET TESTED ! + # https://docs.micropython.org/en/latest/esp8266/quickref.html#uart-serial-bus + raise Exception( + 'UART0 of ESP8266 is used by REPL, UART1 can only be used for TX' + ) + elif 'esp32' in os_info: + # https://docs.micropython.org/en/latest/esp32/quickref.html#uart-serial-bus + tx_pin = 21 + rx_pin = 22 + uart_id = 1 + elif 'rp2' in os_info: + # https://docs.micropython.org/en/latest/rp2/quickref.html#uart-serial-bus + uart_id = 1 + tx_pin = Pin(4) + rx_pin = Pin(5) +except AttributeError: + pass +except Exception as e: + raise e + +nh = NexHardware(rx_pin=rx_pin, tx_pin=tx_pin, uart_id=uart_id) +``` + ## Hardware example The [hardware example](hardware/main.py) shows the usage of the hardware