-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.py
executable file
·168 lines (144 loc) · 3.74 KB
/
report.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
"""
Display weather metrics on ePaper.
"""
import argparse
import logging
import sys
import time
try:
import board
import busio
import digitalio
except NotImplementedError as exc:
print(f"Will only support running with -o: {exc}")
from adafruit_epd.ssd1680 import Adafruit_SSD1680
from display import Display
from logutil import LogLevelAction
def get_e_ink_display(height, width):
"""
:return: eInk display instance
"""
# Create the SPI device and pins we will need.
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
ecs = digitalio.DigitalInOut(board.CE0)
# pylint: disable=invalid-name
dc = digitalio.DigitalInOut(board.D22)
rst = digitalio.DigitalInOut(board.D27)
busy = digitalio.DigitalInOut(board.D17)
display = Adafruit_SSD1680(
height,
width,
spi,
cs_pin=ecs,
dc_pin=dc,
sramcs_pin=None,
rst_pin=rst,
busy_pin=busy,
)
display.rotation = 1
return display
def update_e_ink_display(display, image):
"""
Display image.
:param display:
:param image:
:return:
"""
logger = logging.getLogger(__name__)
logger.debug("display in progress")
display.image(image)
display.display()
logger.debug("display done")
def parse_args():
"""
Parse command line arguments
"""
parser = argparse.ArgumentParser(
description="Update eInk paper display with weather metrics",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"-l",
"--loglevel",
action=LogLevelAction,
help='Set log level (e.g. "ERROR")',
default=logging.INFO,
)
parser.add_argument(
"-t",
"--timeout",
help="Timeout in seconds to sleep between updating the display",
default=120,
)
parser.add_argument(
"-U",
"--url",
help="URL to query for metrics via Prometheus API",
default="http://weather:9090",
)
parser.add_argument(
"-o",
"--output",
help="Instead of updating the display, print the image to a JPG file",
)
parser.add_argument(
"-m",
"--medium_font",
help="Medium font path",
default="/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
)
parser.add_argument(
"-L",
"--large_font",
help="Large font path",
default="/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
)
parser.add_argument(
"-T",
"--temp_sensor_name",
help="Temperature sensor name",
required=True,
)
return parser.parse_args()
def main():
"""
The main function. Parses args and runs infinite loop to update the display.
"""
args = parse_args()
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(args.loglevel)
# 2.13" HD Tri-color or mono display
display_height = 122
display_width = 250
display = Display(
args.url,
display_width,
display_height,
args.medium_font,
args.large_font,
args.temp_sensor_name,
)
if args.output:
image = display.draw_image()
image.save(args.output)
return
e_display = get_e_ink_display(display_height, display_width)
display = Display(
args.url,
e_display.width,
e_display.height,
args.medium_font,
args.large_font,
args.temp_sensor_name,
)
while True:
image = display.draw_image()
update_e_ink_display(e_display, image)
time.sleep(args.timeout)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.exit(0)