linux.ubtun02
from exboard import PhotosensitiveSensor
from exboard import RGB
import signal
import sys
import time
rgb = RGB()
sensor3 = PhotosensitiveSensor()
MAX_SAVE_NUM = 12
data_buffer = []
def exit_handle(sign_num, frame):
print("\n程序接收退出指令,关闭灯环,正在保存光照记录...")
rgb.set([(0, 0, 0)] * 24)
with open("light_data.log", "w", encoding="utf-8") as f:
for line_data in data_buffer:
f.write(line_data + "\n")
print("数据保存完成,程序正常退出")
sys.exit(0)
signal.signal(signal.SIGINT, exit_handle)
while True:
a3 = sensor3.read()
now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(f"[{now_time}] 光照强度:{a3}")
record_str = f"[{now_time}] 光照强度:{a3}"
data_buffer.append(record_str)
if len(data_buffer) > MAX_SAVE_NUM:
data_buffer.pop(0)
if a3 < 90:
rgb.set([(255, 150, 0)] * 24)
elif a3 >= 90 and a3 <= 180:
rgb.set([(255, 255, 255)] * 24)
elif a3 >180:
rgb.set([(255, 0, 255)] * 24)
time.sleep(1)
rgb.set([(0, 0, 0)] * 24)
time.sleep(1)
