我们如何镜像两个 8x8 Adafruit NeoPixel,以便它们同步并显示相同的设计?

问题描述 投票:0回答:1

我们正在尝试对两个 Adafruit 8x8 NeoPixel 进行编码,但只有其中一个以我们指定的模式点亮。我们希望另一个能够反映相同的模式,但我们现有的代码不起作用。我们试图让 Arduino 将其理解为一块 128 网格板,但我们无法弄清楚这一点。

我们将不胜感激任何见解。

#include <Adafruit_NeoPixel.h>

#define PIN 6          // Pin where NeoPixel is connected
#define NUMPIXELS 128  // Number of pixels in 8x8 grid * 2
#define GRID_WIDTH 8   // Width of the grid

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

// Define colors
uint32_t WHITE = pixels.Color(255, 255, 255);
uint32_t YELLOW = pixels.Color(255, 236, 39);
uint32_t MUSTARD = pixels.Color(255, 163, 0);
uint32_t ORANGE = pixels.Color(171, 82, 59);
uint32_t PURPLE = pixels.Color(126, 37, 83);
uint32_t OFF = pixels.Color(0, 0, 0);

// Define an 8x8 color pattern using variables
uint32_t color_pattern[8][8] = {
    {OFF, OFF, OFF, ORANGE, OFF, OFF, OFF, OFF},
    {OFF, OFF, PURPLE, MUSTARD, PURPLE, OFF, OFF, OFF},
    {OFF, PURPLE, MUSTARD, YELLOW, MUSTARD, PURPLE, OFF, OFF},
    {ORANGE, MUSTARD, YELLOW, WHITE, YELLOW, MUSTARD, ORANGE, OFF},
    {OFF, PURPLE, MUSTARD, YELLOW, MUSTARD, PURPLE, OFF, OFF},
    {OFF, OFF, PURPLE, MUSTARD, PURPLE, OFF, OFF, OFF},
    {OFF, OFF, OFF, ORANGE, OFF, OFF, OFF, OFF},
    {OFF, OFF, OFF, OFF, OFF, OFF, OFF, OFF}
};

int posX = 0;  // Initial X position
int posY = 0;  // Initial Y position

void setup() {
  pixels.begin(); // Initialize NeoPixel library
}

void drawPattern(int offsetX, int offsetY) {
  pixels.clear();

  for (int y = 0; y < 8; y++) {
    for (int x = 0; x < 8; x++) {
      int pos1 = (y + offsetY) * GRID_WIDTH + (x + offsetX);
      int pos2 = pos1 + 64; // The second grid starts after the first 64 pixels
      if (pos1 >= 0 && pos1 < 64) {  // Ensure within first grid bounds
        pixels.setPixelColor(pos1, color_pattern[y][x]); // Set each pixel to its respective color
      }
      if (pos2 >= 64 && pos2 < 128) {  // Ensure within second grid bounds
        pixels.setPixelColor(pos2, color_pattern[y][x]); // Set each pixel to its respective color for the second grid
      }
    }
  }
  pixels.show();
}

void loop() {
  pixels.setBrightness(40);
  drawPattern(posX, posY); 
  delay(500); // Delay to see movement

  // Update position
  posX += 1; // Move right
  if (posX > GRID_WIDTH - 8) { // If at the right edge, move down and reset X
    posX = 0;
    posY += 1;
  }
  if (posY > GRID_WIDTH - 8) { // If at the bottom edge, reset Y
    posY = 0;
  }
}

enter image description here

c++ arduino led adafruit neopixel
1个回答
0
投票

警告:所有这些都来自对

Adafruit_NeoPixel
git 存储库的粗略检查:https://github.com/adafruit/Adafruit_NeoPixel.git

我不会尝试将两个显示器合并为一个,而是将它们分开。这样更直观。而且,我们只需要为每个要填充的像素调用 [the slow(?)]

setPixelColor
once

然后,我们可以使用

memcpy
将像素数据从一个实例复制到另一个实例。

在课堂上,有两个感兴趣的领域:

  1. pixels
    -- 指向原始像素数据的指针(访问函数:
    GetPixels
  2. numBytes
    -- pixels
    字节
    的数量(无访问功能)

这些字段是

protected
,并且由于 [AFAICT] numBytes 没有
访问功能,我们需要创建自己的类,从 
Adafruit_Neopixel 继承
以获得此类访问权限。

宽松地说,我们可以做:

    为每个显示创建
  1. 单独的实例
  2. 绘制到一个实例(主实例)
  3. 将像素数据从主复制到镜像
  4. 对每个
  5. 执行
    begin()
    show()

这是一些[写得不好的]伪代码:

// NOTE: pixels and numBytes are protected members of Adafruit_NeoPixel, so we // must inherit from it struct mypix : public Adafruit_NeoPixel { // AFAICT, there is _no_ exported public function for this uint16_t myGetBytes(void) { return numBytes; } // There _is_ a public for this: getPixels uint8_t * myGetBuf(void) { #if 0 return pixels; #else return Getpixels(); #endif } }; // [bad] pseudo code mypix pix_primary(NUMPIXELS, 6, NEO_GRB + NEO_KHZ800); mypix pix_mirror(NUMPIXELS, 7, NEO_GRB + NEO_KHZ800); // copy_all_data -- copy pixel data from one instance/display to another void copy_all_data(mypix *pdst,const mypix *psrc) { uint16 bytelen = pdst->myGetBytes(); assert(bytelen == psrc->myGetBytes()); memcpy(pdst->myGetBuf(),psrc->myGetBuf(),bytelen); } // draw_all_data -- draw all desired data void draw_all_data(mypix pix); void draw_frame() { pix_primary.begin(); pix_mirror.begin(); draw_all_data(&pix_primary); copy_all_data(&pix_mirror,&pix_primary); pix_primary.show(); pix_mirror.show(); }
    
© www.soinside.com 2019 - 2024. All rights reserved.