接着咱们聊聊学习过程,这可是满满的干货哦~
首先呢,咱们得说说这按钮信号的获取。想象一下,你轻轻一按,ESP32-S3就能立刻知道,这是不是很神奇?其实啊,就是利用了gpio6和gpio9这两个小伙伴,它们就像是小间谍,时刻盯着按钮的动静,一旦按钮有风吹草动,它们就立刻向ESP32-S3报告!
测试代码:
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) while True: if not button.value: # 注意:当按钮被按下时,value为False(因为使用了上拉电阻) led.value = True print("button pressed") elif not button2.value: # 同样,当button2被按下时,value为False led.value = True print("button1 pressed")
接下来,咱们聊聊BH1750数字光传感器这位大功臣。它的优点可不少,精度高、反应快,还能直接输出数字信号,省去了我们不少麻烦。它跟ESP32-S3之间,是通过IIC通信来传递信息的,就像是在说悄悄话一样,既快速又准确。IIC通信啊,就像是两个人之间的暗号,只要对上了,就能顺利交换信息啦!
# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries # SPDX-License-Identifier: Unlicense import time import board import adafruit_bh1750 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: print("%.2f Lux" % sensor.lux) time.sleep(1)
最后呢,咱们得说说这ST7789彩屏的驱动原理。这彩屏啊,就像是ESP32-S3的小脸蛋,想要它显示什么,就得靠驱动函数来帮忙。这些驱动函数啊,就像是魔术师的手,轻轻一挥,彩屏上就能出现各种各样的图案和文字啦!具体的测试代码嘛,我已经给大家准备好了,大家可以去试试哦~
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """ This test will initialize the display using displayio and draw a solid green background, a smaller purple rectangle, and some yellow text. """ import board import terminalio import displayio from adafruit_display_text import label # First set some parameters used for shapes and text 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) # Draw a label text = "Hello EEPW!" 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) while True: pass