-
Notifications
You must be signed in to change notification settings - Fork 364
/
Copy pathcolor.py
111 lines (88 loc) · 3.18 KB
/
color.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
import board
import busio
import adafruit_apds9960.apds9960
import time
import paho.mqtt.client as mqtt
import uuid
import signal
import ssl
import digitalio
from PIL import Image, ImageDraw, ImageFont
import adafruit_rgb_display.st7789 as st7789
# Configuration for CS and DC pins (these are FeatherWing defaults on M0/M4):
cs_pin = digitalio.DigitalInOut(board.CE0)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = None
# Config for display baudrate (default max is 24mhz):
BAUDRATE = 64000000
backlight = digitalio.DigitalInOut(board.D22)
backlight.switch_to_output()
backlight.value = True
buttonA = digitalio.DigitalInOut(board.D23)
buttonB = digitalio.DigitalInOut(board.D24)
buttonA.switch_to_input()
buttonB.switch_to_input()
# Setup SPI bus using hardware SPI:
spi = board.SPI()
# Create the ST7789 display:
disp = st7789.ST7789(
spi,
cs=cs_pin,
dc=dc_pin,
rst=reset_pin,
baudrate=BAUDRATE,
width=135,
height=240,
x_offset=53,
y_offset=40,
)
height = disp.height
width = disp.width
image = Image.new("RGB", (width, height))
draw = ImageDraw.Draw(image)
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_apds9960.apds9960.APDS9960(i2c)
sensor.enable_color = True
r, g, b, a = sensor.color_data
topic = 'IDD/colors'
def on_connect(client, userdata, flags, rc):
print(f"connected with result code {rc}")
client.subscribe(topic)
def on_message(cleint, userdata, msg):
# if a message is recieved on the colors topic, parse it and set the color
if msg.topic == topic:
colors = list(map(int, msg.payload.decode('UTF-8').split(',')))
draw.rectangle((0, 0, width, height*0.5), fill=color)
disp.image(image)
client = mqtt.Client(str(uuid.uuid1()))
client.tls_set(cert_reqs=ssl.CERT_NONE)
client.username_pw_set('idd', 'device@theFarm')
client.on_connect = on_connect
client.on_message = on_message
client.connect(
'farlab.infosci.cornell.edu',
port=8883)
client.loop_start()
# this lets us exit gracefully (close the connection to the broker)
def handler(signum, frame):
print('exit gracefully')
client.loop_stop()
exit (0)
# hen sigint happens, do the handler callback function
signal.signal(signal.SIGINT, handler)
# our main loop
while True:
r, g, b, a = sensor.color_data
# there's a few things going on here
# colors are reported at 16bits (thats 65536 levels per color).
# we need to convert that to 0-255. thats what the 255*(x/65536) is doing
# color are also reported with an alpha (opacity, or in our case a proxy for ambient brightness)
# 255*(1-(a/65536)) acts as scaling factor for brightness, it worked well enough in the lab but
# your success may vary depenging on how much ambient light there is, you can mess with these constants
color =tuple(map(lambda x: int(255*(1-(a/65536))*255*(x/65536)) , [r,g,b,a]))
# if we press the button, send msg to cahnge everyones color
if not buttonA.value:
client.publish(topic, f"{r},{g},{b}")
draw.rectangle((0, height*0.5, width, height), fill=color[:3])
disp.image(image)
time.sleep(.01)