1

因此,我将 FastLED 库与 Arduino(8 位 AVR)一起使用,并且我的 LED 以常规的 2D 网格(蛇形图案)排列。为了生成位图文本和图像,我需要一种将二维笛卡尔坐标转换为物理 LED(阵列索引)地址(第一个 LED 为 0,第二个 LED 为 1...)的方法。我创建了一个函数 display_addr_get ,它可以满足我的需求,但是因为我经常使用它,所以我想知道它是否是最佳的,即我可以让它更快/更简单吗?我知道目前没有错误检查来查看行/列索引是否在一个字节的范围内,所以如果有使用饱和数学的解决方案(比如 qadd8() 将总和限制为 255),那将是理想的。

我的 SRAM 几乎满了,所以我无法在那里实现查找表(用于将 2D 坐标映射到数组地址),而且我不确定将查找表存储在 PROGMEM 中会牺牲多少速度并不断从那里读回数据。

下面的代码演示了我试图通过从左上角到右下角为每个合法行和列值打印数组索引值来实现的目标。

#include <stdio.h>

#define MATRIX_WIDTH 10
#define MATRIX_HEIGHT 8

typedef unsigned char uint8_t;

uint8_t display_addr_get(uint8_t row, uint8_t col);
uint8_t display_addr_get2(uint8_t row, uint8_t col);


int main()
{
    printf("Starting Test:\n");

    uint8_t col = 0;
    uint8_t row = 0;
    for (row = MATRIX_HEIGHT; row > 0; row--) {
        for (col = 1; col <= MATRIX_WIDTH; col++) {
            printf("|Index: %-3d|", display_addr_get(row, col));
        }
        printf("\n");
    }

    return 0;
}

// My custom function
uint8_t display_addr_get(uint8_t row, uint8_t col) {
  uint8_t AddrLED = 0; // var size limits the strip length to 256 LEDs

  if (row & 1) { // y (row) index is odd
    AddrLED = -col + MATRIX_WIDTH * (MATRIX_HEIGHT - row + 1);
  }
  else { // y (row) index is even
    AddrLED = col - 1 + MATRIX_WIDTH * (MATRIX_HEIGHT - row);
  }
  return AddrLED;
}

// Snippet from the FastLED lib (uses a different origin reference point)
uint8_t display_addr_get2(uint8_t y, uint8_t x) {
    uint8_t i = 0;
    if( y & 0x01) {
      // Odd rows run backwards
      uint8_t reverseX = (MATRIX_WIDTH - 1) - x;
      i = (y * MATRIX_WIDTH) + reverseX;
    } else {
      // Even rows run forwards
      i = (y * MATRIX_WIDTH) + x;
    }
    return i;
}

程序输出(注意蛇形图案意味着索引在奇数行上反转,这是由于 LED 的物理连接,因此无法更改):

Starting Test:                                                                                                                                         

|Index: 0  ||Index: 1  ||Index: 2  ||Index: 3  ||Index: 4  ||Index: 5  ||Index: 6  ||Index: 7  ||Index: 8  ||Index: 9  |                               

|Index: 19 ||Index: 18 ||Index: 17 ||Index: 16 ||Index: 15 ||Index: 14 ||Index: 13 ||Index: 12 ||Index: 11 ||Index: 10 |                               

|Index: 20 ||Index: 21 ||Index: 22 ||Index: 23 ||Index: 24 ||Index: 25 ||Index: 26 ||Index: 27 ||Index: 28 ||Index: 29 |                               

|Index: 39 ||Index: 38 ||Index: 37 ||Index: 36 ||Index: 35 ||Index: 34 ||Index: 33 ||Index: 32 ||Index: 31 ||Index: 30 |                               

|Index: 40 ||Index: 41 ||Index: 42 ||Index: 43 ||Index: 44 ||Index: 45 ||Index: 46 ||Index: 47 ||Index: 48 ||Index: 49 |                               

|Index: 59 ||Index: 58 ||Index: 57 ||Index: 56 ||Index: 55 ||Index: 54 ||Index: 53 ||Index: 52 ||Index: 51 ||Index: 50 |                               

|Index: 60 ||Index: 61 ||Index: 62 ||Index: 63 ||Index: 64 ||Index: 65 ||Index: 66 ||Index: 67 ||Index: 68 ||Index: 69 |                               

|Index: 79 ||Index: 78 ||Index: 77 ||Index: 76 ||Index: 75 ||Index: 74 ||Index: 73 ||Index: 72 ||Index: 71 ||Index: 70 |  
4

0 回答 0