-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbg_temp-to-screen
executable file
·139 lines (113 loc) · 3.13 KB
/
bg_temp-to-screen
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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import os
import sys
import time
import atexit
import argparse
import subprocess
from mydaemon import Daemon
import Adafruit_DHT
import Adafruit_SSD1306
import Adafruit_GPIO.SPI as SPI
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
DAEMON_NAME = 'DMSPi Temp Output Process (id: #ID#)'
DAEMON_STOP_TIMEOUT = 10
PIDFILE = '/run/DSMPi_TOP_#ID#.pid'
RUNFILE = '/run/DSMPi_TOP_#ID#.run'
# Debug mode.
DEBUG = 0
RST = None
DC = 23
SPI_PORT = 0
SPI_DEVICE = 0
disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
disp.begin()
disp.clear()
disp.display()
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
draw = ImageDraw.Draw(image)
draw.rectangle((0,0,width,height), outline=0, fill=0)
padding = -2
top = padding
bottom = height-padding
x = 0
font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 36)
def kickit():
humidity, temperature = Adafruit_DHT.read_retry(22, 17)
if temperature is not None:
utemp = temperature * 9/5.0 + 32
ftemp=('{0:0.1f}\N{DEGREE SIGN}F'.format(utemp))
disp.clear()
draw.text((x, top), str(ftemp), font=font, fill=255)
else:
print('Failed to get reading. Try again!')
pass
def get_args():
'''
>>> get_args()
('start', 5)
>>> get_args()
('stop', 4)
'''
try:
parser = argparse.ArgumentParser()
parser.add_argument('action', help='action',
choices=['start', 'stop', 'restart'])
parser.add_argument('uniqid', help='Unique ID')
args = parser.parse_args()
result = (args.action, args.uniqid)
except Exception as err:
if DEBUG:
raise
else:
sys.stderr.write('%s\n' % (err))
result = (None, None)
return result
class MyProc(Daemon):
def run(self):
while True:
draw.rectangle((0,0,width,height), outline=0, fill=0)
kickit()
disp.image(image)
disp.display()
time.sleep(3)
atexit.register(self.delrun)
# Run while there is no stop request.
n = 0
while os.path.exists(RUNFILE):
print('.')
n += 1
if (n > 60):
break
time.sleep(1)
if __name__ == '__main__':
try:
# Get arguments.
(action, uniqid) = get_args()
# Create daemon object.
DAEMON_NAME = DAEMON_NAME.replace('#ID#', uniqid)
PIDFILE = PIDFILE.replace('#ID#', uniqid)
RUNFILE = RUNFILE.replace('#ID#', uniqid)
d = MyProc(name=DAEMON_NAME, pidfile=PIDFILE, runfile=RUNFILE,
stoptimeout=DAEMON_STOP_TIMEOUT, debug=DEBUG)
# Action requested.
if action == 'start':
d.start()
elif action == 'stop':
d.stop()
elif action == 'restart':
d.restart()
else:
raise NameError('Unknown action')
sys.exit(0)
except Exception as err:
if DEBUG:
raise
else:
sys.stderr.write('%s\n' % err)
sys.exit(1)