How to convert byte array to uint64_t for arduino led matrix

I saw this byte array in the uint64_t conversion on this website for drawing the letter A ;

I think this byte array to uint64_t conversion reduces the code size and saves the space for storing the arduino IC code as well.

enter image description here

Byte array:

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

      

uint64_t array:

const uint64_t IMAGES[] = {
  0x004242427e422418
};   

      

How to convert above showing byte array in uint64_t by looking above image of matrix with matrix without any tool (s) like this website

+3


source to share


2 answers


If it's a little endian

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

      



If this is the big end

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

      

0


source


Hope this example helps you, the main conversion function is 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

equally



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

      

in function i is row j - column and bitRead(x,n)

tells you that the LED at position (i, j) should be on or off

// 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

0


source







All Articles