598 views|0 replies

6

Posts

2

Resources
The OP
 

[Digi-Key Follow Me Issue 2] Task 3: Use buttons to control the display and color switching of the onboard Neopixel LED [Copy link]

 

Task 3: Use buttons to control the display and color switching of the onboard Neopixel LED

Compared with the previous two tasks, a neopixel library is used to control WS2812B. First, import the library you need to use.

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

What? Why did I import a library that uses screens? Of course it is useful!

Let's define the pins to be used. When the boot button is pressed, the boot0 pin will become a low level, so it needs to be set to pull-up mode; the input mode is because the input voltage is to be detected, not the output voltage, which is a bit like stm32.

# 定义BOOT引脚
button_pin = board.BOOT0

# 创建一个button对象
button = digitalio.DigitalInOut(button_pin)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP

Define another color and text array for easy use and modification

# 颜色数组
COLORS = [
    (255, 0, 0),     # 红
    (255, 165, 0),   # 橙
    (255, 255, 0),   # 黄
    (0, 255, 0),     # 绿
    (0, 0, 255),     # 蓝
    (75, 0, 130),    # 青
    (238, 130, 238),  # 紫
    (0,0,0)  # 灭
]

# 显示颜色的文字
TEXT = ['红','橙','黄','绿','蓝','靛','紫','灭']

Then set the WS2812B. The neopixel pin is used for WS2812B control, and there is only one lamp bead. The brightness is set to 0.1 (the value can be set to 0-1).

# 创建一个pixel对象,控制WS2812B的输出
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
# 设置WS2812B的亮度
pixel.brightness = 0.1

Next is the old tradition, using the screen to display Chinese characters. The rgb_to_hex function changes data such as (255, 0, 0) to 0xff0000. The display function is the code of the previous task one. I encapsulated it for easy calling.

This code makes the Chinese characters on the screen and the color of the Chinese characters show the color of WS2812B together

# 使用格式化字符串,将颜色数组的三个分量转换为两位的十六进制数,并拼接在一起,最后返回该数值
def rgb_to_hex(rgb):
    hex_color = '%02x%02x%02x' % rgb
    return int(hex_color,16)

# 在屏幕上显示汉字,用汉字以及汉字的颜色提示当前的颜色
def display(color_num):
    text_color = rgb_to_hex(COLORS[color_num])
    if color_num==7:
        text_color = 0xffffff
    print(rgb_to_hex(COLORS[color_num]))
    text = TEXT[color_num]
    # 初始化屏幕
    display = board.DISPLAY
    # 设置汉字的字体、颜色、位置
    type = bitmap_font.load_font("/type/wenquanyi_9pt.pcf")
    character = label.Label(type, text=text, color=text_color,scale=5)
    character.x = 100
    character.y = 50
    # 显示汉字
    display.show(character)

Then comes the main loop. The code for flashing the LED is not added because it contains delay code, which will affect the detection of button presses, so it is deleted.

# 主循环,不断检测按键是否被按下
while True:
    # 如果按键被按下,button.value为false
    if not button.value: 
        # 睡眠一段时间
        time.sleep(sleep_time)
        # 再次检测按键是否被按下
        if not button.value:
            # 打印次数
            print(color_num)
            # 调用函数
            display(color_num)
            # 设置led颜色
            pixel.fill(COLORS[color_num])
            # 计数并清零,保证color_num的值不会越界
            color_num += 1
            if color_num == 8:
                color_num =0
    # 如果按键没有被按下,就跳过,啥也不干
    elif button.value: 
        pass

Attached video:

VID_20231010_161509


I covered the light beads with my hands a few times because the light beads were too bright and I couldn’t clearly take a picture of the color of the font, so I covered it a few times

This post is from DigiKey Technology Zone
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list