예제 #1
0
def Light(tests, num_test_bytes, write_tests_to_nvm, reset):
    try:
        import board
        from pimoroni_circuitpython_adapter import not_SMBus
        from pimoroni_ltr559 import LTR559
        i2c = board.I2C()
        i2c_dev = not_SMBus(I2C=i2c)
        ltr559 = LTR559(i2c_dev=i2c_dev)
        if 0 <= ltr559.get_lux() <= 30000:
            tests["Light"]["Passed"] = True
            print("Passed with", ltr559.get_lux())
        else:
            tests["Light"]["Passed"] = False
            print("Failed")
    except Exception as e:
        tests["Light"]["Passed"] = False
        print("Failed with ", e)
    finally:
        tests["Light"]["Test Run"] = True
        write_tests_to_nvm(tests, num_test_bytes)
        reset()
import pulseio
from pimoroni_ltr559 import LTR559
from pimoroni_circuitpython_adapter import not_SMBus
import pimoroni_physical_feather_pins
from pimoroni_envirowing import screen

# set up the screen and tell it we want to handle the backlight ourselves
screen = screen.Screen(backlight_control=False)

# define a remap function to scale a value from an old range to a new range, preserving ratio
def remap(Value, OldMin,OldMax, NewMin, NewMax):
    return (((Value - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin

# set up connection with the sensor
i2c_dev = not_SMBus()
ltr559 = LTR559(i2c_dev=i2c_dev)

# define our pwm pin (for changing the screen brightness)
pwm = pulseio.PWMOut(pimoroni_physical_feather_pins.pin21())

try:
    while True:
        # take readings
        lux = ltr559.get_lux()
        prox = ltr559.get_proximity()

        # change screen brightness according to the amount of light detected
        pwm.duty_cycle = int(min(remap(lux, 0, 400, 3, (2**16 - 1)), (2**16 - 1)))

        print("Lux: {:06.2f}, Proximity: {:04d}".format(lux, prox))