#!/usr/bin/python3
from tkgpio import TkCircuit
import time
# initialize the circuit inside the GUI
configuration = {
"width": 600,
"height": 600,
"leds": [
{"x": 50, "y": 40, "name": "Room light 1", "pin": 21},
{"x": 200, "y": 40, "name": "Room light 2", "pin": 22}
],
"lcds": [
{"x": 30, "y": 240, "name": "LCD", "pins":[2, 3, 4, 5, 6, 7], "columns": 16, "lines": 2}
],
"buttons": [
{"x": 50, "y": 130, "name": "Press", "pin": 11},
],
"light_sensors": [
{"x": 150, "y": 350, "name": "Light Sensor", "pin": 8}
],
"motion_sensors": [
{"x": 350, "y": 100, "name": "Motion Sensor", "pin": 27, "detection_radius": 50, "delay_duration": 5, "block_duration": 3 }
]
}
circuit = TkCircuit(configuration)
@circuit.run
def main ():
from gpiozero import LED, Button, LightSensor, MotionSensor, PWMLED
from time import sleep
from Adafruit_CharLCD import Adafruit_CharLCD
led1 = PWMLED(21)
led2 = PWMLED(22)
lcd = Adafruit_CharLCD(2, 3, 4, 5, 6, 7, 16, 2)
light_sensor = LightSensor(8)
motion_sensor = MotionSensor(27)
def motion_detected():
print("Motion detected")
#led1.value = 1.0
#led2.max_brightness = 1.0
led1.value = 1.0
led2.value = 1.0
lcd.clear()
lcd.message("Welcome To Home!")
def no_motion():
print("No motion detected")
sleep_duration = 10 # 1 minute of inactivity
while sleep_duration > 1 and not motion_sensor.motion_detected:
led1.value = sleep_duration / 10.0 # dim gradually
led2.value = sleep_duration / 10.0
#led1.value=0
#led2.value=0
lcd.clear()
lcd.message("Sl")
sleep(1)
sleep_duration -= 1
**if motion_sensor.motion_detected:
print("Motion detected during sleep, returning to full brightness")
#led1.max_brightness = 1.0
#led2.max_brightness = 1.0
led1.value = 1.0
led2.value = 1.0
#lcd.clear()
#lcd.message=("back to home")**
def button_pressed():
if led1.value > 0:
led1.value = 0.0
led2.value = 0.0
lcd.clear()
lcd.message("Lights off")
else:
led1.value = 1.0
led2.value = 1.0
lcd.clear()
lcd.message("Lights on")
motion_sensor.when_motion = motion_detected
motion_sensor.when_no_motion = no_motion
button = Button(11)
button.when_pressed = button_pressed
while True:
tm = round(time.time())
sleep(0.0)
我写了这段代码,但这段代码中的粗体(突出显示)部分没有执行。我想按照以下方式满足此代码中的一项要求。
当检测到一段时间不活动(例如1分钟)后灯光开始逐渐变暗,但是如果检测到任何活动,亮度应立即恢复到最大水平。