Skip to content

Commit c9a4407

Browse files
committed
Initial commit
0 parents  commit c9a4407

22 files changed

+927
-0
lines changed

.github/workflows/test.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Python Tests
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
python: [3.5, 3.7, 3.9]
15+
16+
steps:
17+
- uses: actions/checkout@v2
18+
- name: Set up Python ${{ matrix.python }}
19+
uses: actions/setup-python@v2
20+
with:
21+
python-version: ${{ matrix.python }}
22+
- name: Install Dependencies
23+
run: |
24+
python -m pip install --upgrade setuptools tox
25+
- name: Run Tests
26+
working-directory: library
27+
run: |
28+
tox -e py
29+
- name: Coverage
30+
env:
31+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32+
working-directory: library
33+
run: |
34+
python -m pip install coveralls
35+
coveralls --service=github
36+
if: ${{ matrix.python == '3.9' }}
37+

.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
build/
2+
_build/
3+
*.o
4+
*.so
5+
*.a
6+
*.py[cod]
7+
*.egg-info
8+
dist/
9+
__pycache__
10+
.DS_Store
11+
*.deb
12+
*.dsc
13+
*.build
14+
*.changes
15+
*.orig.*
16+
packaging/*tar.xz
17+
library/debian/
18+
.coverage
19+
.pytest_cache
20+
.tox

.stickler.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
linters:
3+
flake8:
4+
python: 3
5+
max-line-length: 160

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Pimoroni Ltd.
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 all
13+
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 THE
21+
SOFTWARE.

Makefile

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
LIBRARY_VERSION=$(shell grep version library/setup.cfg | awk -F" = " '{print $$2}')
2+
LIBRARY_NAME=$(shell grep name library/setup.cfg | awk -F" = " '{print $$2}')
3+
4+
.PHONY: usage install uninstall
5+
usage:
6+
@echo "Library: ${LIBRARY_NAME}"
7+
@echo "Version: ${LIBRARY_VERSION}\n"
8+
@echo "Usage: make <target>, where target is one of:\n"
9+
@echo "install: install the library locally from source"
10+
@echo "uninstall: uninstall the local library"
11+
@echo "check: peform basic integrity checks on the codebase"
12+
@echo "python-readme: generate library/README.md from README.md + library/CHANGELOG.txt"
13+
@echo "python-wheels: build python .whl files for distribution"
14+
@echo "python-sdist: build python source distribution"
15+
@echo "python-clean: clean python build and dist directories"
16+
@echo "python-dist: build all python distribution files"
17+
@echo "python-testdeploy: build all and deploy to test PyPi"
18+
@echo "tag: tag the repository with the current version"
19+
20+
install:
21+
./install.sh
22+
23+
uninstall:
24+
./uninstall.sh
25+
26+
check:
27+
@echo "Checking for trailing whitespace"
28+
@! grep -IUrn --color "[[:blank:]]$$" --exclude-dir=sphinx --exclude-dir=.tox --exclude-dir=.git --exclude=PKG-INFO
29+
@echo "Checking for DOS line-endings"
30+
@! grep -lIUrn --color "" --exclude-dir=sphinx --exclude-dir=.tox --exclude-dir=.git --exclude=Makefile
31+
@echo "Checking library/CHANGELOG.txt"
32+
@cat library/CHANGELOG.txt | grep ^${LIBRARY_VERSION}
33+
@echo "Checking library/${LIBRARY_NAME}/__init__.py"
34+
@cat library/${LIBRARY_NAME}/__init__.py | grep "^__version__ = '${LIBRARY_VERSION}'"
35+
36+
tag:
37+
git tag -a "v${LIBRARY_VERSION}" -m "Version ${LIBRARY_VERSION}"
38+
39+
python-readme: library/README.md
40+
41+
python-license: library/LICENSE.txt
42+
43+
library/README.md: README.md library/CHANGELOG.txt
44+
cp README.md library/README.md
45+
printf "\n# Changelog\n" >> library/README.md
46+
cat library/CHANGELOG.txt >> library/README.md
47+
48+
library/LICENSE.txt: LICENSE
49+
cp LICENSE library/LICENSE.txt
50+
51+
python-wheels: python-readme python-license
52+
cd library; python3 setup.py bdist_wheel
53+
cd library; python setup.py bdist_wheel
54+
55+
python-sdist: python-readme python-license
56+
cd library; python setup.py sdist
57+
58+
python-clean:
59+
-rm -r library/dist
60+
-rm -r library/build
61+
-rm -r library/*.egg-info
62+
63+
python-dist: python-clean python-wheels python-sdist
64+
ls library/dist
65+
66+
python-testdeploy: python-dist
67+
twine upload --repository-url https://test.pypi.org/legacy/ library/dist/*
68+
69+
python-deploy: check python-dist
70+
twine upload library/dist/*

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SCD4X C02 Sensor
2+
3+
[![Build Status](https://shields.io/github/workflow/status/pimoroni/scd4x-python/Python%20Tests.svg)](https://github.com/pimoroni/scd4x-python/actions/workflows/test.yml)
4+
[![Coverage Status](https://coveralls.io/repos/github/pimoroni/scd4x-python/badge.svg?branch=master)](https://coveralls.io/github/pimoroni/scd4x-python?branch=master)
5+
[![PyPi Package](https://img.shields.io/pypi/v/scd4x.svg)](https://pypi.python.org/pypi/scd4x)
6+
[![Python Versions](https://img.shields.io/pypi/pyversions/scd4x.svg)](https://pypi.python.org/pypi/scd4x)
7+
8+
# Pre-requisites
9+
10+
You must enable:
11+
12+
* i2c: `sudo raspi-config nonint do_i2c 0`
13+
14+
You can optionally run `sudo raspi-config` or the graphical Raspberry Pi Configuration UI to enable interfaces.
15+
16+
# Installing
17+
18+
Stable library from PyPi:
19+
20+
* Just run `pip3 install scd4x`
21+
22+
In some cases you may need to use `sudo` or install pip with: `sudo apt install python3-pip`
23+
24+
Latest/development library from GitHub:
25+
26+
* `git clone https://github.com/pimoroni/scd4x-python`
27+
* `cd scd4x-python`
28+
* `sudo ./install.sh`
29+

examples/bargraph.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
from scd4x import SCD4X
4+
5+
6+
BAR_CHAR = u'\u2588'
7+
8+
ANSI_COLOR_RED = '\x1b[31m'
9+
ANSI_COLOR_GREEN = '\x1b[32m'
10+
ANSI_COLOR_YELLOW = '\x1b[33m'
11+
ANSI_COLOR_BLUE = '\x1b[34m'
12+
ANSI_COLOR_MAGENTA = '\x1b[35m'
13+
ANSI_COLOR_BLACK = '\x1b[30m'
14+
ANSI_COLOR_RESET = '\x1b[0m'
15+
16+
17+
colours = [ANSI_COLOR_BLUE, ANSI_COLOR_GREEN, ANSI_COLOR_YELLOW, ANSI_COLOR_RED, ANSI_COLOR_MAGENTA]
18+
19+
BAR_WIDTH = 80
20+
21+
min_co2 = 1 << 32
22+
max_co2 = 0
23+
24+
min_temperature = 1 << 32
25+
max_temperature = 0
26+
27+
device = SCD4X()
28+
device.start_periodic_measurement()
29+
30+
try:
31+
while True:
32+
co2, temperature, relative_humidity, timestamp = device.measure()
33+
34+
min_co2 = min(co2 - 100, min_co2)
35+
max_co2 = max(co2 + 100, max_co2)
36+
min_temperature = min(temperature - 5, min_temperature)
37+
max_temperature = max(temperature + 5, max_temperature)
38+
39+
if max_co2 - min_co2 == 0 or max_temperature - min_temperature == 0:
40+
continue
41+
42+
t_scale = min((temperature - min_temperature) / (max_temperature - min_temperature), 1.0)
43+
p_scale = min((co2 - min_co2) / (max_co2 - min_co2), 1.0)
44+
t_colour = colours[int((len(colours) - 1) * t_scale)]
45+
p_colour = colours[int((len(colours) - 1) * p_scale)]
46+
t_bar = BAR_CHAR * int(BAR_WIDTH * t_scale)
47+
c_bar = BAR_CHAR * int(BAR_WIDTH * p_scale)
48+
49+
t_bar += ANSI_COLOR_BLACK + (BAR_CHAR * (BAR_WIDTH - len(t_bar)))
50+
c_bar += ANSI_COLOR_BLACK + (BAR_CHAR * (BAR_WIDTH - len(c_bar)))
51+
52+
t_bar = t_colour + t_bar + ANSI_COLOR_RESET
53+
c_bar = p_colour + c_bar + ANSI_COLOR_RESET
54+
55+
t_reading = "{:.4f}c".format(temperature).ljust(BAR_WIDTH + 14)
56+
c_reading = "{:.0f}PPM".format(co2).ljust(BAR_WIDTH + 14)
57+
58+
sys.stdout.write('\x1b[0;1H')
59+
sys.stdout.write(u"""{title}
60+
{blank}
61+
Temperature: {t_bar}
62+
{t_reading}
63+
CO2: {c_bar}
64+
{c_reading}
65+
{blank}
66+
""".format(
67+
title="SCD4X Sensor".ljust(BAR_WIDTH + 14, " "),
68+
t_bar=t_bar,
69+
c_bar=c_bar,
70+
t_reading=t_reading,
71+
c_reading=c_reading,
72+
blank=" " * (BAR_WIDTH + 14)
73+
))
74+
sys.stdout.flush()
75+
76+
except KeyboardInterrupt:
77+
pass

examples/basic.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python3
2+
from datetime import datetime, timezone
3+
from scd4x import SCD4X
4+
5+
6+
device = SCD4X(quiet=False)
7+
device.start_periodic_measurement()
8+
9+
try:
10+
while True:
11+
co2, temperature, relative_humidity, timestamp = device.measure()
12+
date = datetime.fromtimestamp(timestamp, timezone.utc)
13+
print(f"""
14+
Time: {date.strftime("%Y/%m/%d %H:%M:%S:%f %Z %z")}
15+
CO2: {co2:.2f}PPM
16+
Temperature: {temperature:.4f}c
17+
Humidity: {relative_humidity:.2f}%RH""")
18+
except KeyboardInterrupt:
19+
pass

0 commit comments

Comments
 (0)