generated from promethee/pimoroni.breakout-garden.oled
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
84 lines (73 loc) · 2.83 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Code shamelessly taken from the pimoroni repository, thank you Sandy Macdonald:
https://github.com/pimoroni/bmp280-python/blob/master/examples/temperature-and-pressure.py
and
Richard Hull (and contributors)
https://github.com/rm-hull/luma.examples/blob/master/examples/clock.py
"""
import datetime
import time
import sys
from luma.core import cmdline
from luma.core.render import canvas
from bmp280 import BMP280
from PIL import ImageFont
try:
from smbus2 import SMBus
except ImportError:
from smbus import SMBus
emoji = "¯\_(ツ)_/¯"
platform = "@github"
author = "promethee"
FontTemp = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf', 92)
FontTemp2 = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf', 32)
FontDate = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 22)
FontDebug = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 20)
FontEmoji = ImageFont.truetype('./CODE2000.TTF', 22)
offset = sys.argv[1] if len(sys.argv) > 1 else 0
last_temperature = 0
def init_bmp280():
bus = SMBus(1)
bmp280 = BMP280(i2c_dev=bus)
return bmp280
def init_oled():
parser = cmdline.create_parser(description='luma.examples arguments')
config = cmdline.load_config('./ssh1107.pimoroni.conf')
args = parser.parse_args(config + [])
device = cmdline.create_device(args)
return device
def main(bmp280, device):
global last_temperature
while True:
time.sleep(1)
now = datetime.datetime.now()
datetimestamp = now.strftime("%Y/%m/%d @ %H:%M:%S")
temperature = '{:d}'.format(int(bmp280.get_temperature() - float(offset)))
try:
if temperature != last_temperature:
last_temperature = temperature
with canvas(device) as draw:
print(datetimestamp, temperature, '°C')
draw.text((0, 0), temperature, fill="white", font=FontTemp)
draw.text((48, 92), '°C', fill="white", font=FontTemp2)
except Error as e:
print(datetimestamp, 'bmp280 has been removed or is missing', e)
bmp280 = init_bmp280()
device = init_oled()
with canvas(device) as draw:
error_message = 'bmp280 \nhas been \nremoved \nor is missing'
draw.text((0, 0), error_message, fill="white", font=FontDebug)
if __name__ == "__main__":
try:
bmp280 = init_bmp280()
device = init_oled()
with canvas(device) as draw:
draw.text((16, 12), emoji, fill="white", font=FontEmoji)
draw.text((16, 54), platform, fill="white", font=FontDate)
draw.text((8, 96), author, fill="white", font=FontDate)
time.sleep(3)
main(bmp280, device)
except KeyboardInterrupt:
pass