From: Sandy Macdonald Date: Sat, 8 Jun 2019 09:07:18 +0000 (+0100) Subject: Adding a couple of new examples X-Git-Url: https://zdv2.bktei.com/gitweb/EVA-2020-02-2.git/commitdiff_plain/a155e335d1f16716fe7c8a80491773a450c10fed?ds=inline;hp=29dd29750d748ca35799843b0f273fbefcfaaefd Adding a couple of new examples --- diff --git a/examples/compensated-temperature.py b/examples/compensated-temperature.py new file mode 100755 index 0000000..87daf97 --- /dev/null +++ b/examples/compensated-temperature.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python + +import time +from bme280 import BME280 +from subprocess import PIPE, Popen + +try: + from smbus2 import SMBus +except ImportError: + from smbus import SMBus + +print("""compensated-temperature.py - Use the CPU temperature to compensate temperature +readings from the BME280 sensor. Method adapted from Initial State's Enviro pHAT +review: https://medium.com/@InitialState/tutorial-review-enviro-phat-for-raspberry-pi-4cd6d8c63441 + +Press Ctrl+C to exit! + +""") + +bus = SMBus(1) +bme280 = BME280(i2c_dev=bus) + +def get_cpu_temperature(): + process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE) + output, _error = process.communicate() + return float(output[output.index('=') + 1:output.rindex("'")]) + +factor = 0.6 + +while True: + cpu_temp = get_cpu_temperature() + raw_temp = bme280.get_temperature() + comp_temp = raw_temp - ((cpu_temp - raw_temp) / factor) + print("Compensated temperature: {:05.2f} *C".format(comp_temp)) + time.sleep(1.0) diff --git a/examples/fonts/Asap/Asap-Bold.ttf b/examples/fonts/Asap/Asap-Bold.ttf new file mode 100644 index 0000000..52a14e5 Binary files /dev/null and b/examples/fonts/Asap/Asap-Bold.ttf differ diff --git a/examples/fonts/Asap/Asap-BoldItalic.ttf b/examples/fonts/Asap/Asap-BoldItalic.ttf new file mode 100644 index 0000000..df29023 Binary files /dev/null and b/examples/fonts/Asap/Asap-BoldItalic.ttf differ diff --git a/examples/fonts/Asap/Asap-Italic.ttf b/examples/fonts/Asap/Asap-Italic.ttf new file mode 100644 index 0000000..b07a0bc Binary files /dev/null and b/examples/fonts/Asap/Asap-Italic.ttf differ diff --git a/examples/fonts/Asap/Asap-Medium.ttf b/examples/fonts/Asap/Asap-Medium.ttf new file mode 100644 index 0000000..81ef310 Binary files /dev/null and b/examples/fonts/Asap/Asap-Medium.ttf differ diff --git a/examples/fonts/Asap/Asap-MediumItalic.ttf b/examples/fonts/Asap/Asap-MediumItalic.ttf new file mode 100644 index 0000000..6f8d906 Binary files /dev/null and b/examples/fonts/Asap/Asap-MediumItalic.ttf differ diff --git a/examples/fonts/Asap/Asap-Regular.ttf b/examples/fonts/Asap/Asap-Regular.ttf new file mode 100644 index 0000000..af00196 Binary files /dev/null and b/examples/fonts/Asap/Asap-Regular.ttf differ diff --git a/examples/fonts/Asap/Asap-SemiBold.ttf b/examples/fonts/Asap/Asap-SemiBold.ttf new file mode 100644 index 0000000..5328f3f Binary files /dev/null and b/examples/fonts/Asap/Asap-SemiBold.ttf differ diff --git a/examples/fonts/Asap/Asap-SemiBoldItalic.ttf b/examples/fonts/Asap/Asap-SemiBoldItalic.ttf new file mode 100644 index 0000000..6415ef2 Binary files /dev/null and b/examples/fonts/Asap/Asap-SemiBoldItalic.ttf differ diff --git a/examples/fonts/Asap/OFL.txt b/examples/fonts/Asap/OFL.txt new file mode 100644 index 0000000..ad56d30 --- /dev/null +++ b/examples/fonts/Asap/OFL.txt @@ -0,0 +1,93 @@ +Copyright 2016 The Asap Project Authors (omnibus.type@gmail.com) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/examples/gas.py b/examples/gas.py index 8717922..0ffe917 100755 --- a/examples/gas.py +++ b/examples/gas.py @@ -3,7 +3,6 @@ import time from enviroplus import gas - print("""gas.py - Print readings from the MICS6812 Gas sensor. Press Ctrl+C to exit! diff --git a/examples/lcd.py b/examples/lcd.py new file mode 100755 index 0000000..aa0e193 --- /dev/null +++ b/examples/lcd.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python + +import ST7735 +from PIL import Image, ImageDraw, ImageFont + +print("""lcd.py - Hello, World! example on the 0.96" LCD. + +Press Ctrl+C to exit! + +""") + +# Create LCD class instance. +disp = ST7735.ST7735( + port=0, + cs=1, + dc=9, + backlight=12, + rotation=270, + spi_speed_hz=10000000 +) + +# Initialize display. +disp.begin() + +# Width and height to calculate text position. +WIDTH = disp.width +HEIGHT = disp.height + +# New canvas to draw on. +img = Image.new('RGB', (WIDTH, HEIGHT), color=(0, 0, 0)) +draw = ImageDraw.Draw(img) + +# Text settings. +font_size = 25 +font = ImageFont.truetype("fonts/Asap/Asap-Bold.ttf", font_size) +text_colour = (255, 255, 255) +back_colour = (0, 170, 170) + +message = "Hello, World!" +size_x, size_y = draw.textsize(message, font) + +# Calculate text position +x = (WIDTH - size_x) / 2 +y = (HEIGHT / 2) - (size_y / 2) + +# Draw background rectangle and write text. +draw.rectangle((0, 0, 160, 80), back_colour) +draw.text((x, y), message, font=font, fill=text_colour) +disp.display(img) + +# Keep running. +try: + while True: + pass + +# Turn off backlight on control-c +except KeyboardInterrupt: + disp.set_backlight(0) diff --git a/examples/light.py b/examples/light.py index 5700eb1..216477a 100755 --- a/examples/light.py +++ b/examples/light.py @@ -3,7 +3,6 @@ import time import ltr559 - print("""light.py - Print readings from the LTR559 Light & Proximity sensor. Press Ctrl+C to exit! diff --git a/examples/particles.py b/examples/particulates.py similarity index 77% rename from examples/particles.py rename to examples/particulates.py index 6123ad2..99803ec 100755 --- a/examples/particles.py +++ b/examples/particulates.py @@ -3,8 +3,7 @@ import time from pms5003 import PMS5003 - -print("""particles.py - Print readings from the PM5003 Particle sensor. +print("""particulates.py - Print readings from the PMS5003 particulate sensor. Press Ctrl+C to exit! @@ -13,7 +12,6 @@ Press Ctrl+C to exit! pms5003 = PMS5003() time.sleep(1.0) - try: while True: readings = pms5003.read() diff --git a/examples/weather.py b/examples/weather.py new file mode 100755 index 0000000..17aba20 --- /dev/null +++ b/examples/weather.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python + +import time +from bme280 import BME280 + +try: + from smbus2 import SMBus +except ImportError: + from smbus import SMBus + +print("""weather.py - Print readings from the BME280 weather sensor. + +Press Ctrl+C to exit! + +""") + +bus = SMBus(1) +bme280 = BME280(i2c_dev=bus) + +while True: + temperature = bme280.get_temperature() + pressure = bme280.get_pressure() + humidity = bme280.get_humidity() + print("""Temperature: {:05.2f} *C +Pressure: {:05.2f} hPa +Relative humidity: {:05.2f} % +""".format(temperature, pressure, humidity)) + time.sleep(1) diff --git a/library/enviroplus/gas.py b/library/enviroplus/gas.py index 4f61940..e5fbdd9 100644 --- a/library/enviroplus/gas.py +++ b/library/enviroplus/gas.py @@ -20,9 +20,9 @@ class Mics6812Reading(object): self.nh3 = nh3 def __repr__(self): - return """Oxidising: {:05.02f} -Reducing: {:05.02f} -NH3: {:05.02f} + return """Oxidising: {:05.02f} Ohms +Reducing: {:05.02f} Ohms +NH3: {:05.02f} Ohms """.format(self.oxidising, self.reducing, self.nh3) __str__ = __repr__