你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【逢7发帖赢大礼】#5 Arduino UNO Q的led matrix

[复制链接]
北方 发布时间:2026-7-9 10:44

1 这个开发板是自带led点阵,这个其实是stm32U5来控制的。

可以任意书写,先来一个ST的logo

1385526479.jpg

2使用Arduino APP LAB启动,然后找到启动的代码,这里是一个电灯,先stop,停掉它。

st05-01.PNG

然后找到led matrix的范例,点击安装

st05-02.PNG

安装完以后就跳出了,终端的web GUI控制页面、

这个是一个交互页面,可以用鼠标绘图,结果就实时显示在点阵上,

st05-03.PNG

移动鼠标,画一条小蛇

st05-04.PNG

同步就出现了这个z形小蛇

524989099.jpg

还可以画一个心形。

st05-05.PNG

3 代码解析

下面是代码,

#include <Arduino_RouterBridge.h>
#include <Arduino_LED_Matrix.h>
#include <vector>
#include <zephyr/kernel.h>

Arduino_LED_Matrix matrix;

// Bridge providers run on a separate thread from loop().
// This mutex protects shared animation state and serializes LED matrix writes.
K_MUTEX_DEFINE(anim_mtx);

// Animation playback state
static const int MAX_FRAMES = 300;
static uint32_t animation_buf[MAX_FRAMES][5]; // 4 words + duration (ms)
static int animation_frame_count = 0;
static bool animation_running = false;
static int animation_current_frame = 0;
static unsigned long animation_next_time = 0;

void setup() {
  matrix.begin();
  // Configure grayscale bits to 3 so the display accepts 0..7 brightness.
  // The backend sends quantized values in 0..(2^3-1) == 0..7.
  matrix.setGrayscaleBits(3);
  matrix.clear();

  Bridge.begin();
  Bridge.provide("draw", draw);
  Bridge.provide("load_frame", load_frame);
  Bridge.provide("play_animation", play_animation);
  Bridge.provide("stop_animation", stop_animation);
}

void loop() {
  // Keep loop fast and let animation_tick handle playback timing
  animation_tick();
}

// --- Bridge providers --------------------------------------------------------

void draw(std::vector<uint8_t> frame) {
  if (frame.empty()) return;

  k_mutex_lock(&anim_mtx, K_FOREVER);
  matrix.draw(frame.data());
  k_mutex_unlock(&anim_mtx);
}

void load_frame(std::array<uint32_t, 5> data) {
  if (data.empty()) return;

  k_mutex_lock(&anim_mtx, K_FOREVER);

  if (animation_frame_count >= MAX_FRAMES) {
    k_mutex_unlock(&anim_mtx);
    return;
  }

  int idx = animation_frame_count++;
  animation_buf[idx][0] = data[0];
  animation_buf[idx][1] = data[1];
  animation_buf[idx][2] = data[2];
  animation_buf[idx][3] = data[3];
  animation_buf[idx][4] = data[4];

  k_mutex_unlock(&anim_mtx);
}

void play_animation() {
  k_mutex_lock(&anim_mtx, K_FOREVER);
  animation_current_frame = 0;
  animation_running = true;
  animation_next_time = millis();
  k_mutex_unlock(&anim_mtx);
}

void stop_animation() {
  k_mutex_lock(&anim_mtx, K_FOREVER);
  animation_running = false;
  animation_frame_count = 0;
  animation_current_frame = 0;
  k_mutex_unlock(&anim_mtx);
}

// --- Animation engine --------------------------------------------------------

void animation_tick() {
  k_mutex_lock(&anim_mtx, K_FOREVER);

  if (!animation_running || animation_frame_count == 0) {
    k_mutex_unlock(&anim_mtx);
    return;
  }

  unsigned long now = millis();
  if (now < animation_next_time) {
    k_mutex_unlock(&anim_mtx);
    return;
  }

  int cur = animation_current_frame;

  // Prepare frame words (reverse bits as the library expects)
  uint32_t frame[4];
  frame[0] = reverse(animation_buf[cur][0]);
  frame[1] = reverse(animation_buf[cur][1]);
  frame[2] = reverse(animation_buf[cur][2]);
  frame[3] = reverse(animation_buf[cur][3]);

  // Schedule next frame
  uint32_t interval = animation_buf[cur][4];
  if (interval == 0) interval = 1;
  animation_next_time = now + interval;

  animation_current_frame++;
  if (animation_current_frame >= animation_frame_count) {
    animation_running = false;
    animation_frame_count = 0;
    animation_current_frame = 0;
  }

  // Display frame while still under lock to prevent draw() interference
  matrixWrite(frame);

  k_mutex_unlock(&anim_mtx);
}

在这个代码中,具体解析来自高通芯片的数据解析,然后直接把数据组分别加载到led点阵。

就可以显示下述的效果。

收藏 评论0 发布时间:2026-7-9 10:44

举报

0个回答

所属标签

相似分享

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版