首先在此感谢oxlm大佬的帖子,学习一下,附上链接电子测光表DIY--初步成果基础任务+进阶任务一-电子产品世界论坛
修改了一下使引脚A0,A1默认都拉高,好像A0,A1如果拉低,其电压也会大于0,导致判断语句中误识别为高电位,导致莫名其妙的错误。
照度是指光线照射在物体表面上的强度,通常用勒克斯(lux)作为单位。照度越高,物体表面看起来越亮。照度是环境光线的客观度量,不受相机设置的影响。
快门(T)是指控制光线照射到感光元件上时间的机制。快门速度越快,感光元件接收光线的时间越短,需要更大的光圈或更高的感光度来补偿;快门速度越慢,感光元件接收光线的时间越长,可以使用更小的光圈或更低的感光度。
感光度是指相机感光元件对光线的敏感程度。ISO值越高,感光元件对光线的反应越敏感,可以在光线较暗的环境中使用更快的快门速度或更小的光圈;ISO值越低,感光元件对光线的反应越不敏感,需要更多的光线。
光圈是镜头中控制进光量的开口大小,通常用f-stop(f值)表示。f值越小,光圈开口越大,进光量越多,曝光时间可以更短或感光度可以更低;f值越大,光圈开口越小,进光量越少,曝光时间需要更长或感光度需要更高。
曝光(EV)是指相机感光元件(如胶片或数码传感器)接收光线的程度。正确的曝光意味着照片既不太亮(过曝),也不太暗(欠曝)。曝光由光圈、快门和感光度共同决定。
import time import board import terminalio import displayio import adafruit_bh1750 import math from digitalio import DigitalInOut, Direction, Pull from adafruit_display_text import label from adafruit_bh1750 import Resolution #keypad KEY_NULL = 0 KEY_D0 = 1 KEY_D1 = 2 KEY_D2 = 4 key0 = DigitalInOut(board.BUTTON) key1 = DigitalInOut(board.A0) key2 = DigitalInOut(board.A1) def keyInit(): key0.direction = Direction.INPUT key0.pull = Pull.UP key1.direction = Direction.INPUT key1.pull = Pull.UP key2.direction = Direction.INPUT key2.pull = Pull.UP def keyDetect(): key_value = KEY_NULL if key0.value == 0: key_value +=KEY_D0 if not key1.value: key_value +=KEY_D1 if not key2.value: key_value +=KEY_D2 return key_value def isKeyPressed(): key_value = keyDetect() if key_value: time.sleep(0.1) if key_value == keyDetect(): return key_value return 0 # display BORDER = 20 TEXT_HIGH = 26 FONTSCALE = 2 BACKGROUND_COLOR = 0xFFFFFF # White FOREGROUND_COLOR = 0x855E42 # Dark Wood TEXT_COLOR = 0x4D4DFF # Blue TEXT_COLOR_WHITE = 0xf5ff33 # White # BH1750 i2c = board.I2C() sensor = adafruit_bh1750.BH1750(i2c) AV = [ '1 ', '1.4', '2 ', '2.8', '4 ', '5.6', '8 ', '11 ', '16 ', '22 ', '32 ' ] TV = [ '1 ', '1/2 ', '1/4 ', '1/8 ', '1/15 ', '1/30 ', '1/60 ', '1/125 ', '1/250 ', '1/500 ', '1/1000' ] SV = [ '100 ', '200 ', '400 ', '800 ', '1600' ] def showCameraInfo(svIdx : int, avIdx : int): lux = sensor.lux ev = math.floor(2 + math.log(lux / 10) / math.log(2)) display = board.DISPLAY splash = displayio.Group() display.root_group = splash text_Line0 = "LUX: " + ("%0.2f" % lux) text_Line0_area = label.Label(terminalio.FONT, text=text_Line0, color=TEXT_COLOR) text_Line0_group = displayio.Group( scale=FONTSCALE, x=0, y=TEXT_HIGH // 2 ) text_Line0_group.append(text_Line0_area) splash.append(text_Line0_group) text_Line1 = "EV: " + str(ev) text_Line1_area = label.Label(terminalio.FONT, text=text_Line1, color=TEXT_COLOR) text_Line1_group = displayio.Group( scale=FONTSCALE, x=0, y=TEXT_HIGH * 2 - TEXT_HIGH // 2 ) text_Line1_group.append(text_Line1_area) splash.append(text_Line1_group) text_Line2 = "f: " + AV[avIdx] text_Line2_area = label.Label(terminalio.FONT, text=text_Line2, color=TEXT_COLOR) text_Line2_group = displayio.Group( scale=FONTSCALE, x=0, y=TEXT_HIGH * 3 - TEXT_HIGH // 2 ) text_Line2_group.append(text_Line2_area) splash.append(text_Line2_group) text_Line3 = "ISO: " + SV[svIdx] text_Line3_area = label.Label(terminalio.FONT, text=text_Line3, color=TEXT_COLOR) text_Line3_group = displayio.Group( scale=FONTSCALE, x=0, y=TEXT_HIGH * 4 - TEXT_HIGH // 2 ) text_Line3_group.append(text_Line3_area) splash.append(text_Line3_group) text_Line4 = "T: " + TV[ev + svIdx - avIdx] text_Line4_area = label.Label(terminalio.FONT, text=text_Line4, color=TEXT_COLOR) text_Line4_group = displayio.Group( scale=FONTSCALE, x=0, y=TEXT_HIGH * 5 - TEXT_HIGH // 2 ) text_Line4_group.append(text_Line4_area) splash.append(text_Line4_group) def cameraInfoHdl(old_key_value : int): key_value = old_key_value svIdx = 0 avIdx = 0 showCameraInfo(svIdx, avIdx) while True: key_value=isKeyPressed() if old_key_value != key_value: old_key_value = key_value if key_value == KEY_D0: break elif key_value == KEY_D1: svIdx += 1 if len(SV) <= svIdx : svIdx = 0 showCameraInfo(svIdx, avIdx) elif key_value == KEY_D2: avIdx +=1 if avIdx >= len(AV): avIdx = 0 showCameraInfo(svIdx, avIdx) def showLux(mode : int): display = board.DISPLAY splash = displayio.Group() display.root_group = splash if mode == 0: text_mode = "Mode High" elif mode == 1: text_mode = "Mode Low" elif mode == 2: text_mode = "Mode Middle" text_mode_area = label.Label(terminalio.FONT, text=text_mode, color=TEXT_COLOR) text_mode_group = displayio.Group( scale=FONTSCALE, x=0, y=display.height // 4 ) text_mode_group.append(text_mode_area) splash.append(text_mode_group) text_lux = ("%0.2f" % sensor.lux) + "Lux" text_lux_area = label.Label(terminalio.FONT, text=text_lux, color=TEXT_COLOR) text_lux_group = displayio.Group( scale=FONTSCALE, x=0, y=display.height // 2 ) text_lux_group.append(text_lux_area) splash.append(text_lux_group) def luxHdl(old_key_value : int): key_value = old_key_value i = 0 showLux(i) while True: key_value=isKeyPressed() if old_key_value != key_value: old_key_value = key_value if key_value == KEY_D0: break elif key_value == KEY_D1: i += 1 if i == 1: sensor.set_mode(Resolution.LOW) elif i == 2: sensor.set_mode(Resolution.MID) elif i == 3: sensor.set_mode(Resolution.HIGH) i = 0 showLux(i) def showHelloEEPW(): display = board.DISPLAY splash = displayio.Group() display.root_group = splash color_palette = displayio.Palette(1) color_bitmap = displayio.Bitmap(display.width, display.height, 1) color_palette[0] = BACKGROUND_COLOR bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) splash.append(bg_sprite) inner_bitmap = displayio.Bitmap( display.width, display.height - BORDER * 2, 1 ) inner_palette = displayio.Palette(1) inner_palette[0] = FOREGROUND_COLOR inner_sprite = displayio.TileGrid( inner_bitmap, pixel_shader=inner_palette, x=0, y=BORDER ) splash.append(inner_sprite) text = "Hello EEPW & DigiKey" text_area = label.Label(terminalio.FONT, text=text, color=TEXT_COLOR) text_width = text_area.bounding_box[2] * FONTSCALE text_group = displayio.Group( scale=FONTSCALE, x=display.width // 2 - text_width // 2, y=display.height // 2 ) text_group.append(text_area) splash.append(text_group) def showEEPWHdl(old_key_value : int): key_value = old_key_value showHelloEEPW() while True: key_value=isKeyPressed() if old_key_value != key_value: old_key_value = key_value if key_value == KEY_D0: break def showFunctionMenu(index: int): display = board.DISPLAY splash = displayio.Group() display.root_group = splash text_line0 = "Hello EEPW & DigiKey" if index == 0: text_line0_area = label.Label(terminalio.FONT, text=text_line0, background_color=TEXT_COLOR_WHITE, color=TEXT_COLOR) else: text_line0_area = label.Label(terminalio.FONT, text=text_line0, color=TEXT_COLOR) text_line0_group = displayio.Group( scale=FONTSCALE, x=0, y=20 ) text_line0_group.append(text_line0_area) splash.append(text_line0_group) text_line1 = "Lux Control" if index == 1: text_line1_area = label.Label(terminalio.FONT, text=text_line1, background_color=TEXT_COLOR_WHITE, color=TEXT_COLOR) else: text_line1_area = label.Label(terminalio.FONT, text=text_line1, color=TEXT_COLOR) text_line1_group = displayio.Group( scale=FONTSCALE, x=0, y=60 ) text_line1_group.append(text_line1_area) splash.append(text_line1_group) text_line2 = "ISO" if index == 2: text_line2_area = label.Label(terminalio.FONT, text=text_line2, background_color=TEXT_COLOR_WHITE, color=TEXT_COLOR) else: text_line2_area = label.Label(terminalio.FONT, text=text_line2, color=TEXT_COLOR) text_line2_group = displayio.Group( scale=FONTSCALE, x=0, y=100 ) text_line2_group.append(text_line2_area) splash.append(text_line2_group) # Main function key_value = 0 old_key_value = key_value i = 0 keyInit() showFunctionMenu(i) while True: key_value=isKeyPressed() if old_key_value != key_value: old_key_value = key_value if key_value == KEY_D0: if i == 0: showEEPWHdl(key_value) elif i == 1: luxHdl(key_value) elif i == 2: cameraInfoHdl(key_value) elif key_value == KEY_D1: if i == 0: i = 2 else: i -= 1 elif key_value == KEY_D2: if i == 2: i = 0 else: i += 1 showFunctionMenu(i)
成果视频:【Let's do 第3期—DIY电子测光表-哔哩哔哩】 https://b23.tv/xzSLaHJ