这个帖子,咱们要来个大整合,把按键信号获取、光强获取和点亮屏幕这三件事儿揉到一起,让它们协同作战!
首先呢,咱们得把各种需要的库都请出来,就像是准备一桌丰盛的饭菜,得先把食材都备齐。
import time import adafruit_bh1750 import board import terminalio import displayio import digitalio from adafruit_display_text import label
接着,就是初始化对象啦!led、按钮、传感器、屏幕,它们都得亮相,就像是舞台上的演员,得先跟大家打个招呼。
led = digitalio.DigitalInOut(board.LED) led.direction = digitalio.Direction.OUTPUT button = digitalio.DigitalInOut(board.D9) button.switch_to_input(pull=digitalio.Pull.UP) button2 = digitalio.DigitalInOut(board.D6) button2.switch_to_input(pull=digitalio.Pull.UP) i2c = board.I2C() # uses board.SCL and board.SDA # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller sensor = adafruit_bh1750.BH1750(i2c)
然后,咱们得编写逻辑,这可是重头戏!就像是导演在排戏,得把每个演员的动作、台词都安排得明明白白。按键一按,光强一测,屏幕一亮,整个流程得行云流水,毫不拖沓。
while True: clear_display() text = "%.2f Lux" % sensor.lux print(text) led.value = False if not button.value: # 注意:当按钮被按下时,value为False(因为使用了上拉电阻) text = "Hello EEPW!" led.value = True elif not button2.value: # 同样,当button2被按下时,value为False text = "Hello soidea!" led.value = True
最后,就是调试运行啦!这可是检验咱们成果的时候,就像是观众在看戏,得看看这出戏演得顺不顺,有没有什么地方需要改进。
相关代码嘛,我已经给大家准备好了,就像是一桌色香味俱全的佳肴,就等着大家来品尝啦!大家快去试试吧,看看这整合后的效果是不是杠杠的!
import time import adafruit_bh1750 import board import terminalio import displayio import digitalio from adafruit_display_text import label led = digitalio.DigitalInOut(board.LED) led.direction = digitalio.Direction.OUTPUT button = digitalio.DigitalInOut(board.D9) button.switch_to_input(pull=digitalio.Pull.UP) button2 = digitalio.DigitalInOut(board.D6) button2.switch_to_input(pull=digitalio.Pull.UP) i2c = board.I2C() # uses board.SCL and board.SDA # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller sensor = adafruit_bh1750.BH1750(i2c) BORDER = 20 FONTSCALE = 2 BACKGROUND_COLOR = 0x00FF00 # Bright Green FOREGROUND_COLOR = 0xAA0088 # Purple TEXT_COLOR = 0xFFFF00 display = board.DISPLAY # Make the display context splash = displayio.Group() display.root_group = splash color_bitmap = displayio.Bitmap(display.width, display.height, 1) color_palette = displayio.Palette(1) color_palette[0] = BACKGROUND_COLOR bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) splash.append(bg_sprite) # Draw a smaller inner rectangle inner_bitmap = displayio.Bitmap( display.width - BORDER * 2, 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=BORDER, y=BORDER ) splash.append(inner_sprite) # Function to clear the display def clear_display(): global splash # Remove all children from the splash group while splash: splash.pop() # Re-add the background and inner rectangle splash.append(bg_sprite) splash.append(inner_sprite) while True: clear_display() text = "%.2f Lux" % sensor.lux print(text) led.value = False if not button.value: # 注意:当按钮被按下时,value为False(因为使用了上拉电阻) text = "Hello EEPW!" led.value = True elif not button2.value: # 同样,当button2被按下时,value为False text = "Hello soidea!" led.value = True # Create a new text label 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) # Subgroup for text scaling splash.append(text_group) # Small delay to debounce button press time.sleep(0.1)