查看了下本次活动的视频讲解,使用的CircuitPython 开发,底层显示的驱动库已经适配完成我们只需要基于库使用就可以完成基本任务的开发工作,先来使用adafruit_display_text/adafruit_st7789 完成屏幕的显示功能。
TFT 屏幕显示
添加如下代码,会在屏幕上显示“Hello EEPW!”
import time import board import displayio import digitalio import terminalio from adafruit_display_text import label from adafruit_st7789 import ST7789 display = board.DISPLAY #创建显示文本 splash =displayio.Group() display.root_group= splash text = "Hello EEPW!" text_area = label.Label(terminalio.FONT, text=text, color=0xa5a555, x=00, y=00) text_group = displayio.Group(scale=3, x=20, y=20) text_group.append(text_area) splash.append(text_group)
按键检测
按键可以通过ESP32 S3 开发板的A0/A1脚进行连接,对应板子的PIN脚定义说明如下。
板子核按键模块连接如下
我们在上面TFT屏幕显示的基础上添加group 用于分别显示按键状态,添加如下代码在屏幕上显示按键状态。
import time import board import displayio import digitalio import terminalio from adafruit_display_text import label from adafruit_st7789 import ST7789 display = board.DISPLAY #创建显示文本 splash =displayio.Group() display.root_group= splash # Draw a label text = "Red :" text_area = label.Label(terminalio.FONT, text=text, color=0xff0000) text_width = text_area.bounding_box[2] * 2 text_group = displayio.Group( scale=2, x=0, y=display.height // 2, ) text_group.append(text_area) # Subgroup for text scaling splash.append(text_group) # Draw a label text1 = "Blue:" text_area1 = label.Label(terminalio.FONT, text=text1, color=0xff) text_width1 = text_area1.bounding_box[2] * 2 text_group1 = displayio.Group( scale=2, x=0, y=display.height // 3, ) text_group1.append(text_area1) # Subgroup for text scaling splash.append(text_group1) button_pin1 = board.A0 button1 = digitalio.DigitalInOut(button_pin1) button1.switch_to_input(pull=digitalio.Pull.UP) button_pin2 = board.A1 button2 = digitalio.DigitalInOut(button_pin2) button2.switch_to_input(pull=digitalio.Pull.UP) while True: if not button1.value: text_area1.text = "Blue: down" else: text_area1.text = "Blue: up" if not button2.value: text_area.text = "Red : down" else: text_area.text = "Red : up"
运行后已经可以读取到按键的状态。
BH1750 数据读取:
在此基础上我们继续添加传感器的数据读取处理,同样在TFT屏幕上添加一个group 用于显示sensor 数据,对应代码如下:
import time import board import displayio import digitalio import terminalio from adafruit_display_text import label from adafruit_st7789 import ST7789 import adafruit_bh1750 display = board.DISPLAY #创建显示文本 splash =displayio.Group() display.root_group= splash # Draw a label text = "Red :" text_area = label.Label(terminalio.FONT, text=text, color=0xff0000) text_width = text_area.bounding_box[2] * 2 text_group = displayio.Group( scale=2, x=0, y=10, ) text_group.append(text_area) # Subgroup for text scaling splash.append(text_group) # Draw a label text1 = "Blue:" text_area1 = label.Label(terminalio.FONT, text=text1, color=0xff) text_width1 = text_area1.bounding_box[2] * 2 text_group1 = displayio.Group( scale=2, x=0, y=40, ) text_group1.append(text_area1) # Subgroup for text scaling splash.append(text_group1) # Draw a label text2 = "Sensor:" text_area2 = label.Label(terminalio.FONT, text=text2, color=0xff00) text_width2 = text_area2.bounding_box[2] * 2 text_group2 = displayio.Group( scale=2, x=0, y=70, ) text_group2.append(text_area2) # Subgroup for text scaling splash.append(text_group2) button_pin1 = board.A0 button1 = digitalio.DigitalInOut(button_pin1) button1.switch_to_input(pull=digitalio.Pull.UP) button_pin2 = board.A1 button2 = digitalio.DigitalInOut(button_pin2) 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: if not button1.value: text_area1.text = "Blue: down" else: text_area1.text = "Blue: up" if not button2.value: text_area.text = "Red : down" else: text_area.text = "Red : up" text_area2.text = "Sensor:" + str(sensor.lux)
接上传感器下载到板子中后,在手指盖住传感器核松开时发现传感器的数据有明显的变化。