From f8ce5793847217ca5f5f3cbf4d72bec7d41da6aa Mon Sep 17 00:00:00 2001 From: David Glaude Date: Sun, 15 Nov 2020 23:19:41 +0100 Subject: [PATCH 1/5] Drop unnecessary temporary frame variable From @Gadgetoid PR https://github.com/pimoroni/Adafruit_CircuitPython_PM25/commit/eecc6b222ca838af2176b15c08140c03353c0d56 --- adafruit_pm25/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/adafruit_pm25/__init__.py b/adafruit_pm25/__init__.py index 1a9ca45..946c9ec 100644 --- a/adafruit_pm25/__init__.py +++ b/adafruit_pm25/__init__.py @@ -96,7 +96,6 @@ def read(self): raise RuntimeError("Invalid PM2.5 checksum") # unpack data - frame = struct.unpack(">HHHHHHHHHHHH", self._buffer[4:28]) ( self.aqi_reading["pm10 standard"], self.aqi_reading["pm25 standard"], @@ -110,6 +109,6 @@ def read(self): self.aqi_reading["particles 25um"], self.aqi_reading["particles 50um"], self.aqi_reading["particles 100um"], - ) = frame + ) = struct.unpack(">HHHHHHHHHHHH", self._buffer[4:28]) return self.aqi_reading From 36d6e118754f8ebcfd57749ba6c71f4180b25ae3 Mon Sep 17 00:00:00 2001 From: David Glaude Date: Sun, 15 Nov 2020 23:22:49 +0100 Subject: [PATCH 2/5] Favour readability over performance for SOF check From @Gadgetoid https://github.com/pimoroni/Adafruit_CircuitPython_PM25/commit/78a5e145b248fd59cbcab4017872fba6fc550cab --- adafruit_pm25/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_pm25/__init__.py b/adafruit_pm25/__init__.py index 946c9ec..469884a 100644 --- a/adafruit_pm25/__init__.py +++ b/adafruit_pm25/__init__.py @@ -82,7 +82,7 @@ def read(self): # print([hex(i) for i in self._buffer]) # check packet header - if (self._buffer[0] != 0x42) or (self._buffer[1] != 0x4D): + if not self._buffer[0:2] == b'BM': raise RuntimeError("Invalid PM2.5 header") # check frame length From 811e7d000184927b40297d12394517ad5dda02a8 Mon Sep 17 00:00:00 2001 From: David Glaude Date: Sun, 15 Nov 2020 23:25:47 +0100 Subject: [PATCH 3/5] use slice to assign directly to buffer Part of @Gadgetoid change from https://github.com/pimoroni/Adafruit_CircuitPython_PM25/commit/3e59013b0e80f307d9874db01fbf1dcd8b6001dd --- adafruit_pm25/uart.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/adafruit_pm25/uart.py b/adafruit_pm25/uart.py index 1b91e05..2cd17ed 100644 --- a/adafruit_pm25/uart.py +++ b/adafruit_pm25/uart.py @@ -77,6 +77,5 @@ def _read_into_buffer(self): remain = self._uart.read(31) if not remain or len(remain) != 31: raise RuntimeError("Unable to read from PM2.5 (incomplete frame)") - for i in range(31): - self._buffer[i + 1] = remain[i] - # print([hex(i) for i in self._buffer]) + self._buffer[1:] = remain +# print([hex(i) for i in self._buffer]) From 0a956534cfed9b1e53de0ab4986c1e04323c6b3e Mon Sep 17 00:00:00 2001 From: David Glaude Date: Sun, 15 Nov 2020 23:37:34 +0100 Subject: [PATCH 4/5] Make black happy --- adafruit_pm25/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_pm25/__init__.py b/adafruit_pm25/__init__.py index 469884a..82af02d 100644 --- a/adafruit_pm25/__init__.py +++ b/adafruit_pm25/__init__.py @@ -82,7 +82,7 @@ def read(self): # print([hex(i) for i in self._buffer]) # check packet header - if not self._buffer[0:2] == b'BM': + if not self._buffer[0:2] == b"BM": raise RuntimeError("Invalid PM2.5 header") # check frame length From d20436001f553605b7a0cbc28bba24889076ae61 Mon Sep 17 00:00:00 2001 From: David Glaude Date: Sun, 15 Nov 2020 23:38:35 +0100 Subject: [PATCH 5/5] Make Black happy --- adafruit_pm25/uart.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/adafruit_pm25/uart.py b/adafruit_pm25/uart.py index 2cd17ed..f8569ae 100644 --- a/adafruit_pm25/uart.py +++ b/adafruit_pm25/uart.py @@ -78,4 +78,6 @@ def _read_into_buffer(self): if not remain or len(remain) != 31: raise RuntimeError("Unable to read from PM2.5 (incomplete frame)") self._buffer[1:] = remain + + # print([hex(i) for i in self._buffer])