-
Notifications
You must be signed in to change notification settings - Fork 0
/
Executor.py
69 lines (52 loc) · 1.86 KB
/
Executor.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
import Adafruit_DHT
import RPi.GPIO as GPIO
import Adafruit_Nokia_LCD as LCD
import Adafruit_GPIO.SPI as SPI
class LocalExecutor():
def __init__(self):
GPIO.setup(17, GPIO.OUT)
# Raspberry Pi hardware SPI config:
DC = 23
RST = 24
SPI_PORT = 0
SPI_DEVICE = 0
# Hardware SPI usage:
disp = LCD.PCD8544(DC, RST, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=4000000))
# Initialize library.
disp.begin(contrast=60)
def power_on():
GPIO.output(17, True)
def power_off():
GPIO.output(17, False)
def read_temp():
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.AM2302, 4)
return temperature
def display_temp(temp):
# Clear display.
disp.clear()
disp.display()
image = Image.new('1', (LCD.LCDWIDTH, LCD.LCDHEIGHT))
draw = ImageDraw.Draw(image)
draw.rectangle((0,0,LCD.LCDWIDTH,LCD.LCDHEIGHT), outline=255, fill=255)
# Load default font.
font = ImageFont.truetype("/usr/share/fonts/truetype/roboto/Roboto-Regular.ttf", 30)
fontHumidity = ImageFont.truetype("/usr/share/fonts/truetype/roboto/Roboto-Regular.ttf", 16)
# Write some text.
draw.text((0,0), '{0:0.1f} C'.format(temp), font=font)
draw.text((5,30), '{0:0.1f} HUM'.format(humidity), font=fontHumidity)
disp.image(image)
disp.display()
return True
def execute(self, command, *args):
result = False
if (command == 'on'):
print('Execute on')
result = True
elif (command == 'off'):
print('Execute off')
result = True
elif (command == 'temp'):
result = read_temp()
elif (command == 'show_temp'):
result = display_temp(args[0])
return result