2017-04-21 28 views
3

我看到这个字节数组转换为uint64_t中转换本网站绘制字母一个;如何字节数组转换为uint64_t中的Arduino的LED矩阵

我觉得把这个字节数组转换成uint64_t的转换是减少了代码大小并且还保存了arduino的IC代码存储空间。

enter image description here

字节数组:

const byte IMAGES[][8] = { 
{ 
    B00011000, 
    B00100100, 
    B01000010, 
    B01111110, 
    B01000010, 
    B01000010, 
    B01000010, 
    B00000000 
}}; 

uint64_t中数组:

const uint64_t IMAGES[] = { 
    0x004242427e422418 
}; 

如何转换以上示出字节数组uint64_t中通过查看上述LED矩阵图像,而不任何工具(s)like this website

+0

为什么不首先使用'uint64_t',其余的留给编译器?一个好的编译器应该优化用于屏蔽单字节写入的移位。并且不要垃圾邮件标签。 Arduino绝对不是C,也不完全是C++。另外'B ...'不是有效的语法(除非你定义了256个符号常量或宏 - 你没有,是吗?)。 – Olaf

回答

0

如果是小端

uint64_t IMAGE = 0; 
for (int i = 0; i < (sizeof(byte) * 8); i++) 
{ 
    IMAGE += ((uint64_t) byteIMAGE[i] & 0xffL) << (8 * i); 
} 

如果是大端

uint64_t IMAGE = 0; 
for (int i = 0; i < (sizeof(byte) * 8); i++) 
{ 
    IMAGE = (IMAGE << 8) + (byteIMAGE[i] & 0xff); 
} 
0

希望这个例子可以帮助你,为转换的主要功能是 void displayImage(uint64_t image)

// ARDUINO NANO 
#include "LedControl.h" 
LedControl lc = LedControl(4, 3, 2, 1); 

const uint64_t IMAGES[] = { 
    0x6666667e66663c00, // A 
    0x3e66663e66663e00, // B 
    0x3c66060606663c00 // C 
}; 
const int IMAGES_LEN = sizeof(IMAGES)/sizeof(uint64_t); 

void setup() { 
    Serial.begin(9600); 
    lc.shutdown(0, false); 
    lc.setIntensity(0, 8); 
    lc.clearDisplay(0); 
} 

void loop() { 
    for (int i = 0; i < IMAGES_LEN; i++) { 
    displayImage(IMAGES[i]); 
    delay(1000); 
    } 
} 

void displayImage(uint64_t image) { 
    for (int i = 0; i < 8; i++) { 
    byte row = (image >> i * 8) & 0xFF; 
    Serial.println(row); // <- SHOW  
    for (int j = 0; j < 8; j++) { 
     lc.setLed(0, i, j, bitRead(row, j)); 
    } 
    } 
    Serial.println("---"); 
} 

0x8040201008040201

是函数等于

B10000000, // 1 
    B01000000, // 2 
    B00100000, // 4 
    B00010000, // 8 
    B00001000, // 16 
    B00000100, // 32 
    B00000010, // 64 
    B00000001 // 128 

是行Ĵ是列和bitRead(x,n) 告诉你是领导的位置(I,J)必须打开或关闭

// ex. for the value 128 on a generic row 
for (int j = 0; j < 8; j++) { 
    Serial.println(bitRead(128, j));  
} 

enter image description here