Skip to content

Commit 54058dc

Browse files
committed
raw read working
1 parent 54e3ac9 commit 54058dc

File tree

4 files changed

+185
-57
lines changed

4 files changed

+185
-57
lines changed

.github/workflows/build.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
name: Build CI
6+
7+
on: [pull_request, push]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Dump GitHub context
14+
env:
15+
GITHUB_CONTEXT: ${{ toJson(github) }}
16+
run: echo "$GITHUB_CONTEXT"
17+
- name: Translate Repo Name For Build Tools filename_prefix
18+
id: repo-name
19+
run: |
20+
echo ::set-output name=repo-name::$(
21+
echo ${{ github.repository }} |
22+
awk -F '\/' '{ print tolower($2) }' |
23+
tr '_' '-'
24+
)
25+
- name: Set up Python 3.6
26+
uses: actions/setup-python@v1
27+
with:
28+
python-version: 3.6
29+
- name: Versions
30+
run: |
31+
python3 --version
32+
- name: Checkout Current Repo
33+
uses: actions/checkout@v1
34+
with:
35+
submodules: true
36+
- name: Checkout tools repo
37+
uses: actions/checkout@v2
38+
with:
39+
repository: adafruit/actions-ci-circuitpython-libs
40+
path: actions-ci
41+
- name: Install dependencies
42+
# (e.g. - apt-get: gettext, etc; pip: circuitpython-build-tools, requirements.txt; etc.)
43+
run: |
44+
source actions-ci/install.sh
45+
- name: Pip install pylint, Sphinx, pre-commit
46+
run: |
47+
pip install --force-reinstall pylint Sphinx sphinx-rtd-theme pre-commit
48+
- name: Library version
49+
run: git describe --dirty --always --tags
50+
- name: Pre-commit hooks
51+
run: |
52+
pre-commit run --all-files
53+
- name: PyLint
54+
run: |
55+
pylint $( find . -path './adafruit*.py' )
56+
([[ ! -d "examples" ]] || pylint --disable=missing-docstring,invalid-name,bad-whitespace $( find . -path "./examples/*.py" ))
57+
- name: Build assets
58+
run: circuitpython-build-bundles --filename_prefix ${{ steps.repo-name.outputs.repo-name }} --library_location .
59+
- name: Archive bundles
60+
uses: actions/upload-artifact@v2
61+
with:
62+
name: bundles
63+
path: ${{ github.workspace }}/bundles/
64+
- name: Build docs
65+
working-directory: docs
66+
run: sphinx-build -E -W -b html . _build/html
67+
- name: Check For setup.py
68+
id: need-pypi
69+
run: |
70+
echo ::set-output name=setup-py::$( find . -wholename './setup.py' )
71+
- name: Build Python package
72+
if: contains(steps.need-pypi.outputs.setup-py, 'setup.py')
73+
run: |
74+
pip install --upgrade setuptools wheel twine readme_renderer testresources
75+
python setup.py sdist
76+
python setup.py bdist_wheel --universal
77+
twine check dist/*

.github/workflows/release.yml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
name: Release Actions
6+
7+
on:
8+
release:
9+
types: [published]
10+
11+
jobs:
12+
upload-release-assets:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Dump GitHub context
16+
env:
17+
GITHUB_CONTEXT: ${{ toJson(github) }}
18+
run: echo "$GITHUB_CONTEXT"
19+
- name: Translate Repo Name For Build Tools filename_prefix
20+
id: repo-name
21+
run: |
22+
echo ::set-output name=repo-name::$(
23+
echo ${{ github.repository }} |
24+
awk -F '\/' '{ print tolower($2) }' |
25+
tr '_' '-'
26+
)
27+
- name: Set up Python 3.6
28+
uses: actions/setup-python@v1
29+
with:
30+
python-version: 3.6
31+
- name: Versions
32+
run: |
33+
python3 --version
34+
- name: Checkout Current Repo
35+
uses: actions/checkout@v1
36+
with:
37+
submodules: true
38+
- name: Checkout tools repo
39+
uses: actions/checkout@v2
40+
with:
41+
repository: adafruit/actions-ci-circuitpython-libs
42+
path: actions-ci
43+
- name: Install deps
44+
run: |
45+
source actions-ci/install.sh
46+
- name: Build assets
47+
run: circuitpython-build-bundles --filename_prefix ${{ steps.repo-name.outputs.repo-name }} --library_location .
48+
- name: Upload Release Assets
49+
# the 'official' actions version does not yet support dynamically
50+
# supplying asset names to upload. @csexton's version chosen based on
51+
# discussion in the issue below, as its the simplest to implement and
52+
# allows for selecting files with a pattern.
53+
# https://github.com/actions/upload-release-asset/issues/4
54+
#uses: actions/[email protected]
55+
uses: csexton/release-asset-action@master
56+
with:
57+
pattern: "bundles/*"
58+
github-token: ${{ secrets.GITHUB_TOKEN }}
59+
60+
upload-pypi:
61+
runs-on: ubuntu-latest
62+
steps:
63+
- uses: actions/checkout@v1
64+
- name: Check For setup.py
65+
id: need-pypi
66+
run: |
67+
echo ::set-output name=setup-py::$( find . -wholename './setup.py' )
68+
- name: Set up Python
69+
if: contains(steps.need-pypi.outputs.setup-py, 'setup.py')
70+
uses: actions/setup-python@v1
71+
with:
72+
python-version: '3.x'
73+
- name: Install dependencies
74+
if: contains(steps.need-pypi.outputs.setup-py, 'setup.py')
75+
run: |
76+
python -m pip install --upgrade pip
77+
pip install setuptools wheel twine
78+
- name: Build and publish
79+
if: contains(steps.need-pypi.outputs.setup-py, 'setup.py')
80+
env:
81+
TWINE_USERNAME: ${{ secrets.pypi_username }}
82+
TWINE_PASSWORD: ${{ secrets.pypi_password }}
83+
run: |
84+
python setup.py sdist
85+
twine upload dist/*

adafruit_sgp40.py

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,18 @@
3232
from struct import unpack_from, pack_into
3333
from micropython import const
3434
import adafruit_bus_device.i2c_device as i2c_device
35-
from adafruit_register.i2c_struct import ROUnaryStruct, Struct
36-
from adafruit_register.i2c_bits import RWBits
37-
from adafruit_register.i2c_bit import RWBit, ROBit
3835

3936
__version__ = "0.0.0-auto.0"
4037
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SGP40.git"
4138

4239
_WORD_LEN = 2
40+
# no point in generating this each time
41+
_READ_CMD = [0x26, 0x0F, 0x7F, 0xFF, 0x8F, 0x66, 0x66, 0x93]
4342

4443

4544
class SGP40:
4645
"""Class to use the SGP40 Ambient Light and UV sensor"""
4746

48-
# _reset_bit = RWBit(_CTRL, 4)
49-
# _gain_bits = RWBits(3, _GAIN, 0)
50-
# _id_reg = ROUnaryStruct(0xFF, "<B")
51-
# _uvs_data_reg = UnalignedStruct(_UVSDATA, "<I", 24, 3) # bits, bytes to read/write
52-
"""Ask the sensor if new data is available"""
53-
5447
def __init__(self, i2c, address=0x59):
5548
self.i2c_device = i2c_device.I2CDevice(i2c, address)
5649
self._command_buffer = bytearray(2)
@@ -60,33 +53,30 @@ def __init__(self, i2c, address=0x59):
6053
def initialize(self):
6154
"""Reset the sensor to it's initial unconfigured state and configure it with sensible
6255
defaults so it can be used"""
63-
56+
# check serial number
6457
self._command_buffer[0] = 0x36
6558
self._command_buffer[1] = 0x82
6659
serialnumber = self.readWordFromCommand(3)
67-
print("Serial Number:")
68-
for i in serialnumber:
69-
print("0x{:04X}".format(i), end=" ")
70-
print("")
60+
61+
if serialnumber[0] != 0x0000:
62+
raise RuntimeError("Serial number does not match")
63+
64+
# Check feature set
7165
self._command_buffer[0] = 0x20
7266
self._command_buffer[1] = 0x2F
7367
featureset = self.readWordFromCommand()
74-
print("Feature Set:")
75-
for i in featureset:
76-
print("0x{:04X}".format(i), end=" ")
77-
print("")
68+
if featureset[0] != 0x3220:
69+
70+
raise RuntimeError("Feature set does not match: %s" % hex(featureset[0]))
7871

7972
# VocAlgorithm_init(&voc_algorithm_params)
8073

8174
# Self Test
82-
print("Self test:")
83-
8475
self._command_buffer[0] = 0x28
8576
self._command_buffer[1] = 0x0E
86-
reply = self.readWordFromCommand(delay_ms=250)
87-
if 0xD400 != reply[0]:
77+
self_test = self.readWordFromCommand(delay_ms=250)
78+
if self_test[0] != 0xD400:
8879
raise RuntimeError("Self test failed")
89-
print("\tOK\n")
9080
self._reset()
9181

9282
def _reset(self):
@@ -95,37 +85,20 @@ def _reset(self):
9585
self._command_buffer[0] = 0x00
9686
self._command_buffer[1] = 0x06
9787
try:
98-
print("Reset:")
99-
self.readWordFromCommand()
88+
self.readWordFromCommand(delay_ms=50)
10089
except OSError:
101-
print("\tGot expected OSError from reset")
90+
# print("\tGot expected OSError from reset")
91+
pass
92+
sleep(1)
10293

10394
@property
10495
def raw(self):
10596
"""The raw gas value"""
106-
# uint8_t command[8]
107-
# uint16_t reply
108-
109-
# command[0] = 0x26
110-
# command[1] = 0x0F
111-
112-
# uint16_t rhticks = (uint16_t)((humidity * 65535) / 100 + 0.5)
113-
# command[2] = rhticks >> 8
114-
# command[3] = rhticks & 0xFF
115-
# command[4] = generateCRC(command + 2, 2)
116-
# uint16_t tempticks = (uint16_t)(((temperature + 45) * 65535) / 175)
117-
# command[5] = tempticks >> 8
118-
# command[6] = tempticks & 0xFF
119-
# command[7] = generateCRC(command + 5, 2)
120-
#
121-
122-
# if (!readWordFromCommand(command, 8, 250, &reply, 1))
123-
# return 0x0
124-
125-
# return reply
126-
# }
127-
128-
return 101
97+
# recycle a single buffer
98+
self._command_buffer = bytearray(_READ_CMD)
99+
read_value = self.readWordFromCommand(delay_ms=250)
100+
self._command_buffer = bytearray(2)
101+
return read_value[0]
129102

130103
def readWordFromCommand(
131104
self,
@@ -139,8 +112,6 @@ def readWordFromCommand(
139112
readlen (int, optional): The number of bytes to read. Defaults to 1.
140113
"""
141114

142-
# print("Command Buffer:")
143-
# print(["0x{:02X}".format(i) for i in self._command_buffer])
144115
with self.i2c_device as i2c:
145116
i2c.write(self._command_buffer)
146117

examples/sgp40_simpletest.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@
88

99
i2c = busio.I2C(board.SCL, board.SDA)
1010
sgp = adafruit_sgp40.SGP40(i2c)
11-
print("Found SGP40 serial #")
12-
# print(hex(sgp.serialnumber[0]), end=" ")
13-
# print(hex(sgp.serialnumber[1]), end=" ")
14-
# print(hex(sgp.serialnumber[2]))
15-
# print("")
1611

1712
while True:
18-
print("Measurement: ", sgp.raw)
13+
print("Raw Gas: ", sgp.raw)
1914
print("")
2015
time.sleep(1)

0 commit comments

Comments
 (0)