395|0

39

帖子

1

TA的资源

一粒金砂(中级)

楼主
 

【得捷电子Follow me第2期】任务提交 [复制链接]

  本帖最后由 未见 于 2023-10-16 13:58 编辑

第一部分:视频简介

视频地址:https://training.eeworld.com.cn/course/68150


第二部分:任务提交

任务一】控制屏幕显示中文:

实现代码如下:

import board
import displayio
from adafruit_display_text import label, wrap_text_to_lines
from adafruit_bitmap_font import bitmap_font

# 设置要显示的字符串
dis_str = "你好,CircuitPython\n你好,EEWORLD!"

def screen_dispstr(str):
    # 根据屏幕旋转角度确定每行显示的字符数
    if board.DISPLAY.rotation % 180 == 0:
        char_num = 50  # 横屏
    else:
        char_num = 20  # 竖屏

    strbuf = ""
    for i in range(len(str) / char_num):
        # 将字符串按每行字符数拆分,并添加换行符
        strbuf = strbuf + str[i * char_num:(i + 1) * char_num] + "\n"
    return strbuf

# 初始化显示屏
display = board.DISPLAY
board.DISPLAY.brightness = 0.35
board.DISPLAY.rotation = 0

# 加载字体和设置颜色
font = bitmap_font.load_font("wenquanyi_10pt.pcf")
color = 0x00FFFF

# 创建显示文本的组和标签
text_group = displayio.Group()
text_area = label.Label(font, text=screen_dispstr(dis_str), color=color)
text_area.x = 70
text_area.y = 55
text_area.line_spacing = 0.8
text_area.scale = 1

text_group.append(text_area)
display.show(text_group)

# 持续显示
while True:
    pass

实现效果:

 

任务二】使用网络功能:

2.1创建AP热点

实现代码如下:

import os
import wifi
import adafruit_requests

print("ESP32-S3 Access Point Test")

print(f"My MAC address: {[hex(i) for i in wifi.radio.mac_address]}")
AP_SSID = "CircuitPythonAP"
AP_PASSWD = "12345678"

wifi.radio.start_ap(AP_SSID , AP_PASSWD )

print(f"My SSID: {AP_SSID}")

print(f"My PASSWORD: {AP_PASSWD}")

while True:
    pass

2.2连接外部网络

实现代码如下:

import board
import displayio
import terminalio
import wifi
from adafruit_display_text import label
# 配置屏幕显示
display = board.DISPLAY
# 填写WIFI名和密码
SSID = "MIX4_Phone"
PASSWD = "12345678"
# 连接WIFI
wifi.radio.connect(SSID, PASSWD)

# 打印连接信息
text="WiFi:"+SSID+"\n"+"IP:"+str(wifi.radio.ipv4_address)
text_group = displayio.Group(scale=2,x=0,y=0,)
text_area = label.Label(terminalio.FONT, text=text, color=0x00FF00, x=10, y=25)
text_group.append(text_area)  # Subgroup for text scaling
display.show(text_group)

while True:
    pass

连接成功后屏幕显示效果:

 

任务三】控制板载WS2812B

控制代码如下:

import time
import board
import neopixel
import digitalio
from adafruit_display_text import bitmap_label
from adafruit_bitmap_font import bitmap_font

# 加载字体文件
font = bitmap_font.load_font("wenquanyi_10pt.pcf")

# 定义颜色配置
colors = [(0x00, 0x00, 0x00),  # 黑色
          (0xFF, 0x00, 0x00),  # 红色
          (0x00, 0xFF, 0x00),  # 绿色
          (0x00, 0x00, 0xFF),  # 蓝色
          (0xFF, 0xFF, 0xFF)]  # 白色

# 初始化WS2812B
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
pixel.brightness = 0.5

# 按键初始化
button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)

# 初始化文本显示
text_area = bitmap_label.Label(font, scale=7)
text_area.x = 35
text_area.y = 60

current_color_index = -1

while True:
    # 获取按键状态
    button_state = not button.value
    
    if button_state:
        # Cycle through colors
        current_color_index = (current_color_index + 1) % len(colors)
        pixel.fill(colors[current_color_index])
        
        # Set the text and color
        text2 = ["黑色", "红色", "绿色", "蓝色", "白色"][current_color_index]
        text_area.text = text2
        text_area.color = colors[current_color_index]
        board.DISPLAY.show(text_area)
    
    time.sleep(0.2)

显示效果如下:

 

任务四】时钟和天气显示

import board, os, wifi
import time,ssl,socketpool,adafruit_requests
from adafruit_display_text import label, wrap_text_to_lines
from adafruit_bitmap_font import bitmap_font

# 设置显示颜色
color = 0x00FF00

# 配置中文字体文件
font = bitmap_font.load_font("wenquanyi_10pt.pcf")

# 显示初始化
display = board.DISPLAY

# 配置天气API
JSON_TIME_URL = "http://quan.suning.com/getSysTime.do"
JSON_weather_URL = "http://t.weather.sojson.com/api/weather/city/{}"
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
def refresh_screen(print_text):
    text_area = label.Label(font, text=print_text, scale=1)
    text_area.x = 5
    text_area.y = 15
    text_area.scale = 1
    text_area.color = color
    display.show(text_area)

# 获取并解析天气数据
def get_data():
    try:
        response = requests.get(JSON_TIME_URL)
    except ConnectionError as e:
        print("Connection Error:", e)
        print("Retrying in 60 seconds")
    time_data = response.json()['sysTime2'][:-3]
    try:
        city_code = "101190401"
        url = JSON_weather_URL.format(city_code)
        response = requests.get(url)
    except ConnectionError as e:
        print("connect error:{},retry in 60s".format(e))
    weather_data = response.json()
    cityInfo = weather_data['cityInfo']
    city_weather = weather_data['data']
    forecast = city_weather['forecast']
    dis_str = cityInfo['parent'] + ' '+ cityInfo['city'] + ' ' + forecast[0]['week']
    dis_str += "\n时间:"+time_data
    dis_str += "\n温度:"+city_weather['wendu']+"℃"
    dis_str += " 湿度:" + city_weather['shidu']
    dis_str += '\n空气质量:' + city_weather['quality']
    return dis_str

while not wifi.radio.ipv4_address:
    try:
        wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
    except ConnectionError as e:
        print("connecti error:{},retry in 2s".format(e))
    time.sleep(2)
print("get ip:", wifi.radio.ipv4_address)

while True:
    info = get_data()
    refresh_screen(info)
    time.sleep(60)

实际显示效果:

 

任务五】网络控制WS2812B

 

第三部分:代码提交

点此下载

点赞 关注
 
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表