Skip to content

Commit 21732d8

Browse files
authored
Merge pull request #8 from dglaude/library-to-package
Library to package
2 parents 0940f71 + 4c71550 commit 21732d8

File tree

6 files changed

+178
-77
lines changed

6 files changed

+178
-77
lines changed

adafruit_pm25.py renamed to adafruit_pm25/__init__.py

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@
4343
4444
"""
4545

46-
# imports
47-
import time
4846
import struct
49-
from adafruit_bus_device.i2c_device import I2CDevice
50-
from digitalio import Direction
5147

5248
__version__ = "0.0.0-auto.0"
5349
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PM25.git"
@@ -117,72 +113,3 @@ def read(self):
117113
) = frame
118114

119115
return self.aqi_reading
120-
121-
122-
class PM25_I2C(PM25):
123-
"""
124-
A driver for the PM2.5 Air quality sensor over I2C
125-
"""
126-
127-
def __init__(self, i2c_bus, reset_pin=None, address=0x12):
128-
if reset_pin:
129-
# Reset device
130-
reset_pin.direction = Direction.OUTPUT
131-
reset_pin.value = False
132-
time.sleep(0.01)
133-
reset_pin.value = True
134-
# it takes at least a second to start up
135-
time.sleep(1)
136-
137-
for _ in range(5): # try a few times, it can be sluggish
138-
try:
139-
self.i2c_device = I2CDevice(i2c_bus, address)
140-
break
141-
except ValueError:
142-
time.sleep(1)
143-
continue
144-
else:
145-
raise RuntimeError("Unable to find PM2.5 device")
146-
super().__init__()
147-
148-
def _read_into_buffer(self):
149-
with self.i2c_device as i2c:
150-
try:
151-
i2c.readinto(self._buffer)
152-
except OSError:
153-
raise RuntimeError("Unable to read from PM2.5 over I2C")
154-
155-
156-
class PM25_UART(PM25):
157-
"""
158-
A driver for the PM2.5 Air quality sensor over UART
159-
"""
160-
161-
def __init__(self, uart, reset_pin=None):
162-
if reset_pin:
163-
# Reset device
164-
reset_pin.direction = Direction.OUTPUT
165-
reset_pin.value = False
166-
time.sleep(0.01)
167-
reset_pin.value = True
168-
# it takes at least a second to start up
169-
time.sleep(1)
170-
171-
self._uart = uart
172-
super().__init__()
173-
174-
def _read_into_buffer(self):
175-
while True:
176-
b = self._uart.read(1)
177-
if not b:
178-
raise RuntimeError("Unable to read from PM2.5 (no start of frame)")
179-
if b[0] == 0x42:
180-
break
181-
self._buffer[0] = b[0] # first byte and start of frame
182-
183-
remain = self._uart.read(31)
184-
if not remain or len(remain) != 31:
185-
raise RuntimeError("Unable to read from PM2.5 (incomplete frame)")
186-
for i in range(31):
187-
self._buffer[i + 1] = remain[i]
188-
# print([hex(i) for i in self._buffer])

adafruit_pm25/i2c.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2020 ladyada for Adafruit Industries
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
"""
23+
`adafruit_pm25.i2c`
24+
================================================================================
25+
26+
I2C module for CircuitPython library for PM2.5 Air Quality Sensors
27+
28+
29+
* Author(s): ladyada
30+
31+
Implementation Notes
32+
--------------------
33+
34+
**Hardware:**
35+
36+
Works with most (any?) Plantower I2C interfaced PM2.5 sensor.
37+
38+
**Software and Dependencies:**
39+
40+
* Adafruit CircuitPython firmware for the supported boards:
41+
https://github.com/adafruit/circuitpython/releases
42+
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
43+
44+
"""
45+
46+
# imports
47+
import time
48+
from digitalio import Direction
49+
from adafruit_bus_device.i2c_device import I2CDevice
50+
from . import PM25
51+
52+
53+
class PM25_I2C(PM25):
54+
"""
55+
A module for using the PM2.5 Air quality sensor over I2C
56+
"""
57+
58+
def __init__(self, i2c_bus, reset_pin=None, address=0x12):
59+
if reset_pin:
60+
# Reset device
61+
reset_pin.direction = Direction.OUTPUT
62+
reset_pin.value = False
63+
time.sleep(0.01)
64+
reset_pin.value = True
65+
# it takes at least a second to start up
66+
time.sleep(1)
67+
68+
for _ in range(5): # try a few times, it can be sluggish
69+
try:
70+
self.i2c_device = I2CDevice(i2c_bus, address)
71+
break
72+
except ValueError:
73+
time.sleep(1)
74+
continue
75+
else:
76+
raise RuntimeError("Unable to find PM2.5 device")
77+
super().__init__()
78+
79+
def _read_into_buffer(self):
80+
with self.i2c_device as i2c:
81+
try:
82+
i2c.readinto(self._buffer)
83+
except OSError as err:
84+
raise RuntimeError("Unable to read from PM2.5 over I2C") from err

adafruit_pm25/uart.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2020 ladyada for Adafruit Industries
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
"""
23+
`adafruit_pm25.uart`
24+
================================================================================
25+
26+
UART module for CircuitPython library for PM2.5 Air Quality Sensors
27+
28+
29+
* Author(s): ladyada
30+
31+
Implementation Notes
32+
--------------------
33+
34+
**Hardware:**
35+
36+
Works with most (any?) Plantower UART or I2C interfaced PM2.5 sensor.
37+
38+
**Software and Dependencies:**
39+
40+
* Adafruit CircuitPython firmware for the supported boards:
41+
https://github.com/adafruit/circuitpython/releases
42+
43+
"""
44+
45+
import time
46+
from digitalio import Direction
47+
from . import PM25
48+
49+
50+
class PM25_UART(PM25):
51+
"""
52+
A driver for the PM2.5 Air quality sensor over UART
53+
"""
54+
55+
def __init__(self, uart, reset_pin=None):
56+
if reset_pin:
57+
# Reset device
58+
reset_pin.direction = Direction.OUTPUT
59+
reset_pin.value = False
60+
time.sleep(0.01)
61+
reset_pin.value = True
62+
# it takes at least a second to start up
63+
time.sleep(1)
64+
65+
self._uart = uart
66+
super().__init__()
67+
68+
def _read_into_buffer(self):
69+
while True:
70+
b = self._uart.read(1)
71+
if not b:
72+
raise RuntimeError("Unable to read from PM2.5 (no start of frame)")
73+
if b[0] == 0x42:
74+
break
75+
self._buffer[0] = b[0] # first byte and start of frame
76+
77+
remain = self._uart.read(31)
78+
if not remain or len(remain) != 31:
79+
raise RuntimeError("Unable to read from PM2.5 (incomplete frame)")
80+
for i in range(31):
81+
self._buffer[i + 1] = remain[i]
82+
# print([hex(i) for i in self._buffer])

docs/api.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@
66
77
.. automodule:: adafruit_pm25
88
:members:
9+
10+
.. automodule:: adafruit_pm25.i2c
11+
:members:
12+
13+
.. automodule:: adafruit_pm25.uart
14+
:members:

examples/pm25_simpletest.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import board
88
import busio
99
from digitalio import DigitalInOut, Direction, Pull
10-
import adafruit_pm25
10+
from adafruit_pm25.i2c import PM25_I2C
11+
1112

1213
reset_pin = None
1314
# If you have a GPIO, its not a bad idea to connect it to the RESET pin
@@ -33,12 +34,13 @@
3334
# uart = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=0.25)
3435

3536
# Connect to a PM2.5 sensor over UART
36-
# pm25 = adafruit_pm25.PM25_UART(uart, reset_pin)
37+
# from adafruit_pm25.uart import PM25_UART
38+
# pm25 = PM25_UART(uart, reset_pin)
3739

3840
# Create library object, use 'slow' 100KHz frequency!
3941
i2c = busio.I2C(board.SCL, board.SDA, frequency=100000)
4042
# Connect to a PM2.5 sensor over I2C
41-
pm25 = adafruit_pm25.PM25_I2C(i2c, reset_pin)
43+
pm25 = PM25_I2C(i2c, reset_pin)
4244

4345
print("Found PM2.5 sensor, reading data...")
4446

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@
4949
# simple. Or you can use find_packages().
5050
# TODO: IF LIBRARY FILES ARE A PACKAGE FOLDER,
5151
# CHANGE `py_modules=['...']` TO `packages=['...']`
52-
py_modules=["adafruit_pm25"],
52+
packages=["adafruit_pm25"],
5353
)

0 commit comments

Comments
 (0)