This seems to be doing what I want. Will have to see what it looks like in low light conditions and maybe tweak some of the numbers. Back and neck are getting sore so calling it quits for now. I’ll strip the junk out latter on and post the condensed version.
import time
import machine
import micropython
from interstate75 import Interstate75
from interstate75 import Interstate75, SWITCH_A, SWITCH_B
i75 = Interstate75(display=Interstate75.DISPLAY_INTERSTATE75_128X32)
graphics = i75.display
graphics.set_font("bitmap8")
BACKLIGHT_LOW = micropython.const(0.35)
BACKLIGHT_HIGH = micropython.const(1.0)
LUMINANCE_LOW = micropython.const(256)
LUMINANCE_HIGH = micropython.const(2048)
brightness = (1.0)
black = graphics.create_pen_hsv(0, 0, 0)
red = graphics.create_pen_hsv(1 / 360, 1.0, brightness)
green = graphics.create_pen_hsv(130 / 360, 1.0, brightness)
blue = graphics.create_pen_hsv(250 / 360, 1.0, brightness)
yellow = graphics.create_pen_hsv(60 / 360, 1.0, brightness)
orange = graphics.create_pen_hsv(30 / 360, 1.0, brightness)
white = graphics.create_pen_hsv(1.0, 0, brightness)
graphics.set_pen(black)
graphics.clear()
i75.update(graphics)
i75.set_led(0, 0 ,0)
from pimoroni_i2c import PimoroniI2C
i2c = PimoroniI2C(sda=(20), scl=(21))
from breakout_ltr559 import BreakoutLTR559
ltr = BreakoutLTR559(i2c)
part_id = ltr.part_id()
print("Found LTR559. Part ID: 0x", '{:02x}'.format(part_id), sep="")
def auto_brightness(previous: float) -> (float, float):
luminance = reading[BreakoutLTR559.LUX]
luminance_frac = max(0.0, float(luminance - LUMINANCE_LOW))
luminance_frac = min(1.0, luminance_frac / (LUMINANCE_HIGH - LUMINANCE_LOW))
backlight = BACKLIGHT_LOW + (luminance_frac * (BACKLIGHT_HIGH - BACKLIGHT_LOW))
# Use the previous value to smooth out changes to reduce flickering.
# The "32" value here controls how quickly it reacts (larger = slower).
# The rate at which the main loop calls us also affects that!
backlight_diff = backlight - previous
backlight = previous + (backlight_diff * (1.0 / 32.0))
return (luminance, backlight)
backlight = BACKLIGHT_LOW
start_time = time.time()
while True:
reading = ltr.get_reading()
if reading is not None:
print("Lux:", reading[BreakoutLTR559.LUX], "Prox:", reading[BreakoutLTR559.PROXIMITY])
lux = reading[BreakoutLTR559.LUX]
(luminance, backlight) = auto_brightness(backlight)
time.sleep(0.1)
brightness = backlight
print (brightness)
black = graphics.create_pen_hsv(0, 0, 0)
red = graphics.create_pen_hsv(1 / 360, 1.0, brightness)
green = graphics.create_pen_hsv(130 / 360, 1.0, brightness)
blue = graphics.create_pen_hsv(250 / 360, 1.0, brightness)
yellow = graphics.create_pen_hsv(60 / 360, 1.0, brightness)
orange = graphics.create_pen_hsv(30 / 360, 1.0, brightness)
white = graphics.create_pen_hsv(1.0, 0, brightness)
graphics.set_pen(black)
graphics.clear()
i75.update(graphics)
time.sleep (1)