显示器亮度自动调节套装
import math | |
import queue | |
import threading | |
import serial | |
from vcp import * # https://github.com/dot-osk/monitor_ctrl | |
import logging | |
# Init VCP | |
try: | |
monitors = enumerate_monitors() | |
except OSError as err: | |
exit(1) | |
phy_monitors = [] | |
while not phy_monitors: | |
for i in monitors: | |
try: | |
monitor = PhyMonitor(i) | |
except OSError as err: | |
logging.error(err) | |
continue | |
phy_monitors.append(monitor) | |
pm = phy_monitors[0] | |
# Init serial port | |
connected = False | |
port = 'COM6' | |
baud = 9600 | |
try: | |
serial_port = serial.Serial(port, baud, timeout=0) | |
except serial.serialutil.SerialException: | |
logging.error('Can not open serial port.') | |
# Init lux queue | |
queue_len = 5 | |
lux_queue = queue.Queue(queue_len) | |
def handle_data(data): | |
global lux_queue | |
if data.startswith('Light') and data.endswith('lx'): | |
# Parse lux value | |
brightness_lx = float(data[7:-3]) | |
# Slide window average | |
if lux_queue.qsize() < queue_len: | |
lux_queue.put(brightness_lx) | |
return | |
else: | |
lux_queue.get() | |
lux_queue.put(brightness_lx) | |
lux_list = list(lux_queue.queue) | |
lux_centered_average = (sum(lux_list) - max(lux_list) - min(lux_list)) / (len(lux_list) - 2) | |
# control core | |
now_brightness = pm.brightness | |
ctl_factor = math.log(lux_centered_average+1) * 21 | |
final_result = 1 | |
if ctl_factor <= 0: | |
pass | |
elif ctl_factor >= 100: | |
final_result = 100 | |
else: | |
final_result = math.floor(ctl_factor) + 3 | |
if now_brightness != final_result: | |
pm.brightness = final_result | |
print(f'Sensor: {brightness_lx} lx Avg: {lux_centered_average:.2f} lx Factor: {ctl_factor:.2f} Display: {now_brightness}->{final_result}') | |
def read_from_port(ser): | |
global connected | |
while not connected: | |
connected = True | |
while True: | |
while serial_port.in_waiting: | |
try: | |
reading = ser.readline().decode().strip() | |
handle_data(reading) | |
except KeyboardInterrupt: | |
ser.close() | |
thread = threading.Thread(target=read_from_port, args=(serial_port,)) | |
thread.start() |
#include <Wire.h> | |
#include <BH1750.h> | |
BH1750 lightMeter; | |
void setup(){ | |
Serial.begin(9600); | |
// Initialize the I2C bus (BH1750 library doesn't do this automatically) | |
// On esp8266 devices you can select SCL and SDA pins using Wire.begin(D4, D3); | |
Wire.begin(); | |
if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) { | |
Serial.println(F("BH1750 Advanced begin")); | |
} | |
else { | |
Serial.println(F("Error initialising BH1750")); | |
} | |
} | |
void loop() { | |
float lux = lightMeter.readLightLevel(); | |
Serial.print("Light: "+lux+"lx"); | |
Serial.print(lux); | |
Serial.println(" lx"); | |
delay(250); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
硬件需求:
硬件连接:
程序需要改动的地方:
执行方法: