-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcess.py
139 lines (102 loc) · 4.24 KB
/
Process.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
from tuya_connector import TuyaOpenAPI
from keys import CELLO_LAMP_ID, CS_LAMP_ID, BEDSIDE_LAMP_ID, ACCESS_ID, ACCESS_KEY, API_ENDPOINT
import time
import RPi.GPIO as GPIO
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from rpi_ws281x import *
LED_COUNT = 30 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10 # DMA channel to use for generating a signal (try 10)
LED_BRIGHTNESS = 65 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
pixels = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
pixels.begin()
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.load_default()
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
openapi = TuyaOpenAPI(API_ENDPOINT, ACCESS_ID, ACCESS_KEY)
openapi.connect()
setLamps = False
pixelStatus = True
autoPixelStatus = False
def setValue(value):
CL_commands = {'commands': [{'code': 'switch_1', 'value': value}]}
CSL_commands = {'commands': [{'code': 'switch_1', 'value': value}]}
BSL_commands = {'commands': [{'code': 'switch_1', 'value': value}]}
openapi.post('/v1.0/iot-03/devices/{}/commands'.format(CELLO_LAMP_ID), CL_commands)
openapi.post('/v1.0/iot-03/devices/{}/commands'.format(CS_LAMP_ID), CSL_commands)
openapi.post('/v1.0/iot-03/devices/{}/commands'.format(BEDSIDE_LAMP_ID), BSL_commands)
def getStringValue(value):
return "On" if value else "Off"
def setPixels(pixelStatus):
if pixelStatus:
colorWipe(pixels, Color(255, 255, 255))
else:
colorWipe(pixels, Color(0, 0, 0))
def colorWipe(strip, color, wait_ms=50):
for i in range(strip.numPixels()):
strip.setPixelColor(i, color)
strip.show()
time.sleep(wait_ms/2000.0)
while True:
if GPIO.input(12) == True:
time.sleep(.3)
if GPIO.input(12) == True:
pixelStatus = not pixelStatus
setPixels(pixelStatus)
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((x+10, top + 16), "Successfully Turned", font=font, fill=255)
draw.text((x+20, top+24), getStringValue(pixelStatus) + " Watch Light", font=font, fill=255)
disp.image(image)
disp.display()
time.sleep(3)
disp.clear()
disp.display()
else:
openapi = TuyaOpenAPI(API_ENDPOINT, ACCESS_ID, ACCESS_KEY)
openapi.connect()
apiRequest = openapi.get("/v1.0/iot-03/devices/{}/status".format(CS_LAMP_ID))
if apiRequest["result"][0]["value"] == True:
setLamps = False
else:
setLamps = True
xDist = 7 if setLamps else 5
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((x+10, top + 16), "Successfully Turned", font=font, fill=255)
draw.text((x+xDist, top+24), getStringValue(setLamps) + " Omar's Room Lamps", font=font, fill=255)
setValue(setLamps)
disp.image(image)
disp.display()
time.sleep(3)
disp.clear()
disp.display()
elif GPIO.input(15) == True and autoPixelStatus == True and not pixelStatus:
setPixels(False)
autoPixelStatus = False
elif GPIO.input(15) == False and autoPixelStatus == False:
setPixels(True)
autoPixelStatus = True