2017-04-21 85 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