【得捷电子Follow me第2期】任务提交(持续更新中.....)
[复制链接]
一,任务选择
任务1:控制屏幕显示中文(必做任务)
完成屏幕的控制,并且能显示中文
搭配器件:Adafruit ESP32-S3 TFT Feather
任务2:网络功能使用(必做任务)
完成网络功能的使用,能够创建热点和连接到WiFi
搭配器件:Adafruit ESP32-S3 TFT Feather
任务3:控制WS2812B(必做任务)
使用按键控制板载Neopixel LED的显示和颜色切换
搭配器件:Adafruit ESP32-S3 TFT Feather
任务4:从下方5个分任务中选择1个感兴趣的完成即可(必做任务)
■ 分任务2:WS2812B效果控制——完成一个Neopixel(12灯珠或以上)控制器,通过按键和屏幕切换展示效果
建议搭配器件:Adafruit ESP32-S3 TFT Feather、可寻址RGB LED
各任务演示视频:请到本帖5楼观看
二,任务1:控制屏幕显示中文
控制屏幕显示中文,主要重点在于中文字库的选择和制作上,为了调用更快和更小的占用空间,我们需要用FontForge工具进行编辑文字库,保持成.PCF文件。
再在代码中制定显示库的位置,以及把需要显示的文字用一个text保存,简单得做个试验,在屏幕的(50,50)和(30,20)开始的位置上相隔两秒交替显示浅绿色和白色的两行“周一:晴”汉
字,代码如下:
import board,time
from adafruit_display_text import bitmap_label
import terminalio
from adafruit_bitmap_font import bitmap_font
font = bitmap_font.load_font("font\sytq_16.pcf")
color = 0x00fa00
text2 = "周一:晴"
scale = 2
while True:
text_area2 = bitmap_label.Label(font,text=text2,scale=scale, color=color)
text_area2.x = 50
text_area2.y = 50
board.DISPLAY.show(text_area2)
time.sleep(2)
text_area3 = bitmap_label.Label(font,text=text2,scale=scale, color=0xffffff)
text_area3.x = 30
text_area3.y = 20
board.DISPLAY.show(text_area3)
time.sleep(2)
中文显示视频如下:
中文显示
三,任务2:网络功能使用
实验一:在手机上打开热点,用实验板连接热点并打印IP地址。
import ssl, wifi, time
print("ESP32-S3 network test")
print("MAC地址:",[hex(i) for i in wifi.radio.mac_address])
while not wifi.radio.connected:
print("connetting ...")
time.sleep(1)
print("wiFi is conneted!")
print("IP地址",wifi.radio.ipv4_address)
while True:
pass
打印输出的图示如下图
实验二:用开发板设置AP,使用手机连接开发板的wifi。
import board
import wifi
SSID = "esp_a0"
PASSWORD = "12345abc"
wifi.radio.start_ap(SSID,PASSWORD)
print("WiFi:",SSID)
print("PASSWORD:",PASSWORD)
while True:
pass
四,任务3:控制WS2812B(必做任务)
本次实验结合按键的使用,在按键按下的时候改变记录按键状态的status,如果按下使用pixel_status做记录,做一个循环,然后根据pixel_status做判断控制RGBWS2812B的不同颜色,如果按键不按下的时候使用ADC进行温度模块TMP235进行采样,并打印出来对应的温度。
代码如下
import board
import time
import digitalio
import analogio
import neopixel
# WS2812B 电源控制
power = digitalio.DigitalInOut(board.NEOPIXEL_POWER)
power.direction = digitalio.Direction.OUTPUT
power.value = True
# WS2812B设置
pixel = neopixel.NeoPixel(board.NEOPIXEL,1)
#亮度
pixel.brightness = 0.5
led=digitalio.DigitalInOut(board.LED)
led.direction=digitalio.Direction.OUTPUT
#led.switch_to_output()
# board.BUTTON 与 board.BOOTO 等同
button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)
#模拟输入 TMP235 温度传感器 T=V*100-50
analog_pin = analogio.AnalogIn(board.A1)
#普通按键状态
status = False
t=0
colour=0
pixel_status =0
while 1:
if not button.value:
if status==False:
print("Press down")
status= True
led.value=status
if pixel_status==0:
pixel.fill((125,0,0))# red: (255,0,0)
print("pixel_status is:",pixel_status)
if pixel_status==1:
pixel.fill((0,125,0))# green: (0,255,0)
print("pixel_status is:",pixel_status)
if pixel_status==2:
pixel.fill((0,0,125))# blue: (0,0,255)
print("pixel_status is:",pixel_status)
if pixel_status==3:
pixel.fill((0,125,125))# cyan: (0,255,255)
print("pixel_status is:",pixel_status)
if pixel_status==4:
pixel.fill((125,0,125))# purple: (255,0,255)
print("pixel_status is:",pixel_status)
if pixel_status==5:
pixel.fill((125,125,0))# yellow: (255,255,0)
print("pixel_status is:",pixel_status)
if pixel_status==6:
pixel.fill((125,125,125))# white: (255,255,255)
print("pixel_status is:",pixel_status)
if pixel_status==7:
pixel.fill((0,0,0))# black (off): (0,0,0)
print("pixel_status is:",pixel_status)
if pixel_status<7:
pixel_status=pixel_status+1
else:
pixel_status=0
else:
status=False
led.value=status
while(t==100):
print("DC value is:",analog_pin.value)
Tempt = analog_pin.value*3.3/65535*100-50
print("Tempt is", Tempt)
t=0
print("pixel_status is:",pixel_status-1)
break
t=t+1
time.sleep(0.01)
试验视频如下:
按键控制板载REB灯
五。任务四:分任务2:WS2812B效果控制——完成一个Neopixel(12灯珠或以上)控制器,通过按键和屏幕切换展示效果
继任务3,把WS2812B数量增加到16个,并在显示屏上面显示RGB灯显示的颜色。
这里我用改变一个圆的填充颜色来改变显示的颜色,每次修改填充颜色后都需要重新增加到shape_group中
circle = Circle(x0=200,y0=60,r=30,fill=CYAN,outline=0xFFFFFF)
shape_group.append(circle)
display.show(shape_group)
这里注意一下,如果显示字符和图形同时显示,前面显示的会被覆盖掉,而且text_grouo与shape_group还不能相互添加。只能舍弃字符显示了,
测试代码如下:
import board
import time
import digitalio
import analogio
import neopixel
#屏幕像素 240(长)*135(高)
import terminalio,displayio
from adafruit_display_text import label
from adafruit_display_shapes.circle import Circle
#普通按键状态
status = False
t=0
colour=0
pixel_status =0
RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255, 0)
PURPLE = (180, 0, 255)
WHITE = (255,255,255)
BLACK = (0,0,0)
# 1。屏幕基础配置
display = board.DISPLAY
#2。显示文本
text= "The neopixel test!"
text_area = label.Label(terminalio.FONT, text=text, color=0xFF0000, x=20 , y=20)
text_group = displayio.Group(scale=2,x=0,y=0)
text_group.append(text_area)
display.show(text_group)
# Subgroup for text scaling
#圆
#黑背景,白外框,内填RGB显示颜色
shape_group = displayio.Group()
circle = Circle(x0=200,y0=60,r=30,fill=BLACK,outline=0xFFFFFF)
shape_group.append(circle)
display.show(shape_group)
# WS2812B设置 数据口选择为A1
#pixel = neopixel.NeoPixel(board.A1,16)
pixel = neopixel.NeoPixel(board.A1, 16, brightness=0.1)
#亮度
#pixel.brightness = 0.5
led=digitalio.DigitalInOut(board.LED)
led.direction=digitalio.Direction.OUTPUT
#led.switch_to_output()
# board.BUTTON 与 board.BOOTO 等同
button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)
#模拟输入 TMP235 温度传感器 T=V*100-50
analog_pin = analogio.AnalogIn(board.A0)
while 1:
if not button.value:
if status==False:
print("Press down")
status= True
led.value=status
if pixel_status==0:
pixel.fill(RED)# red: (255,0,0)
circle = Circle(x0=200,y0=60,r=30,fill=RED,outline=0xFFFFFF)
shape_group.append(circle)
display.show(shape_group)
print("pixel_status is:",pixel_status)
if pixel_status==1:
pixel.fill(GREEN)# green: (0,255,0)
circle = Circle(x0=200,y0=60,r=30,fill=GREEN,outline=0xFFFFFF)
shape_group.append(circle)
display.show(shape_group)
print("pixel_status is:",pixel_status)
if pixel_status==2:
pixel.fill(BLUE)# blue: (0,0,255)
circle = Circle(x0=200,y0=60,r=30,fill=BLUE,outline=0xFFFFFF)
shape_group.append(circle)
display.show(shape_group)
print("pixel_status is:",pixel_status)
if pixel_status==3:
pixel.fill(CYAN)# cyan: (0,255,255)
circle = Circle(x0=200,y0=60,r=30,fill=CYAN,outline=0xFFFFFF)
shape_group.append(circle)
display.show(shape_group)
print("pixel_status is:",pixel_status)
if pixel_status==4:
pixel.fill(PURPLE)# purple: (255,0,255)
circle = Circle(x0=200,y0=60,r=30,fill=PURPLE,outline=0xFFFFFF)
shape_group.append(circle)
display.show(shape_group)
print("pixel_status is:",pixel_status)
if pixel_status==5:
pixel.fill(YELLOW)# yellow: (255,255,0)
circle = Circle(x0=200,y0=60,r=30,fill=YELLOW,outline=0xFFFFFF)
shape_group.append(circle)
display.show(shape_group)
print("pixel_status is:",pixel_status)
if pixel_status==6:
pixel.fill(WHITE)# white: (255,255,255)
circle = Circle(x0=200,y0=60,r=30,fill=WHITE,outline=0xFFFFFF)
shape_group.append(circle)
display.show(shape_group)
print("pixel_status is:",pixel_status)
if pixel_status==7:
pixel.fill(BLACK)# black (off): (0,0,0)
circle = Circle(x0=200,y0=60,r=30,fill=BLACK,outline=0xFFFFFF)
shape_group.append(circle)
display.show(shape_group)
print("pixel_status is:",pixel_status)
if pixel_status<7:
pixel_status=pixel_status+1
else:
pixel_status=0
else:
status=False
# display.show(shape_group)
led.value=status
while(t==100):
print("DC value is:",analog_pin.value)
Tempt = analog_pin.value*3.3/65535*100-50
print("Tempt is", Tempt)
t=0
print("pixel_status is:",pixel_status-1)
break
t=t+1
time.sleep(0.01)
视频如下
多RGB显示
六,总结:
ESP32一直都想玩一下,但是看到其他资料都是用官方的例程和开发环境不好开发就一直搁置着,这次eeworld和得捷电子的活动提供了一个Feather的开发板,开发板开发环境和资料都很齐全,而且使用CircuitPython进行开发,CircuitPython提供比较多的库,对我这些菜鸟来说是非常友好的。希望得捷电子follow me第三期能够提供更好玩的开发板,和论坛的网友一起边学习边玩起来。最后感谢eeworld和得捷电子提供的这次机会。
七,程序代码提交
|