这里写目录标题
- 0 前言
- 1 开箱
- 2 过程
- 2.1 下载固件
- 2.2 烧录固件
- 2.3 编程环境 Thonny
- 2.4 点灯大师
- 2.5 TFT屏幕
- 2.6 BH1750传感器
- 成果展示
0 前言
开发板:ESP32-S3-5691
开发环境:circuitpython+thony
1 开箱
2 过程
2.1 下载固件
使用circuitpython的方式开发,需要先安装circuitpython
我手里的开发板是ESP32-S3 5691 找到对应型号的开发板 找到对应的固件,烧录进去
(这里要注意选择本开发板的固件,不要选错了,我第一次就烧录错了,只有按键和LED可以使用,LCD不能使用,问了陈工,陈工说固件烧录错了,哈哈哈)
开发板资料网址:https://learn.adafruit.com/esp32-s3-reverse-tft-feather
固件网址:https://circuitpython.org/board/adafruit_feather_esp32s3_reverse_tft/
进入固件的网址之后,下载固件,下载UF2文件,如图所示,我已经下载好了
2.2 烧录固件
- 用数据线将核心板链接电脑后双击 RST 按钮
- 电脑将弹出 FTHRS3BOOT 驱动器
- 将下载好的固件拖进去即可完成 circuitpython的安装
安装好的样子
2.3 编程环境 Thonny
推荐使用编程环境Thonny
安装传送门:Thonny, Python IDE for beginner
安装网址:https://thonny.org/
小伙伴们可以自行安装
2.4 点灯大师
安装好编程环境之后,我们就开始写我们的第一个代码,我们作为点灯大师最熟悉的还是点亮一个LED。
复制例程
import board
import digitalio
import timeled = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUTwhile True:led.value = Truetime.sleep(0.5)led.value = Falsetime.sleep(0.5)
将例程粘贴到Thonny中,点击绿色的运行按钮,即可看到现象。
现象是对的:背面的红色LED在闪烁,1S亮灭一次
点亮LED之后,信心大增,开始点亮LCD屏幕吧!!!
2.5 TFT屏幕
开发板板载一块TFT屏幕,ST7789,此屏幕需要依赖库文件,我们先下载它的库文件
下载地址:https://github.com/adafruit/Adafruit_CircuitPython_ST7789
方法:https://learn.adafruit.com/esp32-s3-reverse-tft-feather/displayio-example
https://github.com/adafruit/Adafruit_CircuitPython_Display_Text
先在github下载这两个文件夹,从中选取所需移植的文件
- adafruit_st7789.py
- adafruit_display_text
将下载好的这两个文件,复制粘贴到开发板的lib文件夹下即可
ST7789屏幕的例程代码:https://github.com/adafruit/Adafruit_CircuitPython_ST7789
import board
import displayio
# Starting in CircuitPython 9.x fourwire will be a seperate internal library
# rather than a component of the displayio library
try:from fourwire import FourWire
except ImportError:from displayio import FourWirefrom adafruit_st7789 import ST7789displayio.release_displays()spi = board.SPI()
while not spi.try_lock():pass
spi.configure(baudrate=24000000) # Configure SPI for 24MHz
spi.unlock()
tft_cs = board.D5
tft_dc = board.D6display_bus = FourWire(spi, command=tft_dc, chip_select=tft_cs, reset=board.D9)display = ST7789(display_bus, width=240, height=240, rowstart=80)# Make the display context
splash = displayio.Group()
display.root_group = splashcolor_bitmap = displayio.Bitmap(240, 240, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0xFF0000bg_sprite = displayio.TileGrid(color_bitmap,pixel_shader=color_palette,x=0, y=0)
splash.append(bg_sprite)while True:pass
CV到code.py
运行,LCD没有反应……
寻找解决方法:https://learn.adafruit.com/esp32-s3-reverse-tft-feather/displayio-example
在这一页里其实介绍了怎么使用LCD,最开始没有仔细看
DisplayIO Example
Your board comes with a lovely TFT display built right in. You can use the display with CircuitPython and the displayio module. This module allows you to easily write Python code that lets you create graphics.
CircuitPython Usage
To use with CircuitPython, you need to first install the adafruit_display_text library, and its dependencies, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain the following folders:
- /adafruit_display_text
- /adafruit_bitmap_font
翻译一下就是
DisplayIO 示例
您的主板内置了一个可爱的 TFT 显示屏。您可以将显示器与 CircuitPython 和 displayio 模块一起使用。此模块允许您轻松编写用于创建图形的 Python 代码。
CircuitPython 用法
要与 CircuitPython 一起使用,您需要首先将 adafruit_display_text 库及其依赖项安装到 CIRCUITPY 驱动器上的 lib 文件夹中。然后,您需要使用示例脚本更新 code.py。
值得庆幸的是,我们可以一次性完成此操作。在下面的示例中,单击下面的 Download Project Bundle 按钮,以 zip 文件的形式下载必要的库和 code.py 文件。解压缩 zip 文件的内容,并将整个 lib 文件夹和 code.py 文件复制到 CIRCUITPY 驱动器。
您的 CIRCUITPY/lib 文件夹应包含以下文件夹:
- /adafruit_display_text
- /adafruit_bitmap_font
那我们现在需要按照官方给出的步骤操作即可。
下载这个zip文件,解压,将里面的文件复制粘贴到开发板的lib文件夹下面
\Adafruit_Feather_ESP32-S2_Reverse_TFT\display\CircuitPython 9.x 文件夹下的文件有两个
- lib
- code.py
将整个 lib 文件夹和 code.py 文件复制到 CIRCUITPY 驱动器。
然后就发现,lib文件夹下多了几个后缀为.mpy的文件
现在,我们再运行一下程序试试,能否点亮TFT屏幕
# 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 = 0xFFFF00display = board.DISPLAY# Make the display context
splash = displayio.Group()
display.root_group = splashcolor_bitmap = displayio.Bitmap(display.width, display.height, 1)
color_palette = displayio.Palette(1)
color_palette[0] = BACKGROUND_COLORbg_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 World!"
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
终于点亮了这块屏幕
2.6 BH1750传感器
BH1750 数字光传感器模块的优点以及信号的获取
-
硬件接线,本传感器是I2C协议的,开发板后面的丝印有显示SCL、SDA、3V3、GND,先把硬件接线完成
-
源码:https://github.com/adafruit/Adafruit_CircuitPython_ST7789
-
在github里下载库文件adafruit_st7789.py,然后放到lib文件夹下,复制源码
import time
import board
import adafruit_bh1750i2c = board.I2C()
sensor = adafruit_bh1750.BH1750(i2c)while True:print("%.2f Lux"%sensor.lux)time.sleep(1)
运行代码,效果如下:
可以读取到传感器的数据
参考大佬的代码:
https://forum.eepw.com.cn/thread/387489/1/#2
https://share.eepw.com.cn/share/download/id/394263
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 = 4key0 = DigitalInOut(board.BOOT0)
key1 = DigitalInOut(board.D1)
key2 = DigitalInOut(board.D2)def keyInit():key0.direction = Direction.INPUTkey0.pull = Pull.UPkey1.direction = Direction.INPUTkey1.pull = Pull.DOWNkey2.direction = Direction.INPUTkey2.pull = Pull.DOWNdef keyDetect():key_value = KEY_NULLif key0.value == 0:key_value +=KEY_D0if key1.value:key_value +=KEY_D1if key2.value:key_value +=KEY_D2return key_valuedef isKeyPressed():key_value = keyDetect()if key_value:time.sleep(0.01) if key_value == keyDetect():return key_valuereturn 0# display
BORDER = 20
TEXT_HIGH = 26
FONTSCALE = 2
BACKGROUND_COLOR = 0x00FF00 # Bright Green
FOREGROUND_COLOR = 0xAA0088 # Purple
TEXT_COLOR = 0xFFFF00 # Yellow
TEXT_COLOR_RED = 0xFF0000 # Red
TEXT_COLOR_WHITE = 0xFFFFFF # 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.luxev = math.floor(2 + math.log(lux / 10) / math.log(2))display = board.DISPLAYsplash = displayio.Group()display.root_group = splashtext_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 = "S: " + 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_valuesvIdx = 0avIdx = 0showCameraInfo(svIdx, avIdx)while True:key_value=isKeyPressed()if old_key_value != key_value:old_key_value = key_valueif key_value == KEY_D0:breakelif key_value == KEY_D1:svIdx += 1if len(SV) <= svIdx :svIdx = 0showCameraInfo(svIdx, avIdx)elif key_value == KEY_D2:avIdx +=1if avIdx >= len(AV):avIdx = 0showCameraInfo(svIdx, avIdx)def showLux(mode : int):display = board.DISPLAYsplash = displayio.Group()display.root_group = splashif 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_valuei = 0showLux(i)while True:key_value=isKeyPressed()if old_key_value != key_value:old_key_value = key_valueif key_value == KEY_D0:breakelif key_value == KEY_D1:i += 1if i == 1:sensor.set_mode(Resolution.LOW)elif i == 2:sensor.set_mode(Resolution.MID)elif i == 3:sensor.set_mode(Resolution.HIGH)i = 0showLux(i)def showHelloEEPW():display = board.DISPLAYsplash = displayio.Group()display.root_group = splashcolor_palette = displayio.Palette(1)"""color_bitmap = displayio.Bitmap(display.width, display.height, 1)color_palette[0] = BACKGROUND_COLORbg_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_COLORinner_sprite = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=0, y=BORDER)splash.append(inner_sprite)"""text = "EEPW & DigiKey"text_area = label.Label(terminalio.FONT, text=text, color=TEXT_COLOR)text_width = text_area.bounding_box[2] * FONTSCALEtext_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_valueshowHelloEEPW()while True:key_value=isKeyPressed()if old_key_value != key_value:old_key_value = key_valueif key_value == KEY_D0:breakdef showFunctionMenu(index: int):display = board.DISPLAYsplash = displayio.Group()display.root_group = splashtext_line0 = "EEPW & DigiKey"if index == 0:text_line0_area = label.Label(terminalio.FONT, text=text_line0, background_color=TEXT_COLOR_WHITE, color=TEXT_COLOR_RED)else: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 = "Lux Control"if index == 1:text_line1_area = label.Label(terminalio.FONT, text=text_line1, background_color=TEXT_COLOR_WHITE, color=TEXT_COLOR_RED)else: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 = "ISO"if index == 2:text_line2_area = label.Label(terminalio.FONT, text=text_line2, background_color=TEXT_COLOR_WHITE, color=TEXT_COLOR_RED)else: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)# 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_valueif 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 = 2else:i -= 1elif key_value == KEY_D2:if i == 2:i = 0else:i += 1showFunctionMenu(i)
成果展示
上电后,按D1和D2切换选择功能项,选中的功能项会高亮显示,按D0是确定按键,进入当前的功能。
进入亮度检测模式后,按D1键切换亮度检测精度,D2键刷新亮度,D0键返回上一级
进入logo界面,按D0键返回上一级
进入ISO界面,按D2切换光圈大小,D1键切换ISO,D0键退出