[solved] How to convert string to 8x8 dot matrix bytes?

Hello,

I'm working on my 4 modules dot matrix display and learning how to control the flow of data.

But, now I want to convert string into dot matrix 8-bytes chars.

I have another related question, on brainy bits website, the chars array has initial two numbers in the start of each 8-bytes, can anyone explain to me what they do?

This is the link:
scroll-text-using-the-max7219-led-dot-matrix/

This is same as my modules:

You may want to look at the series "Parola A to Z" on my blog site (link in signature block below). There was a whole series on fonts handling and display on 8x8 matrix, which is the question you are asking.

When you are done experimenting with your own string display, you may want to try using the Parola library (link below), which give you a whole lot more options.

I took a look at your library, actually I downloaded this library and tried to learn from it before weeks, but it was little complicated to me and I thought I better get back to my code and try to fix my problem, and I succeeded and I was so happy with this accomplishment.

The part I'm looking for is the methodology of converting the sting into dot matrix data.

I'm not interested into learning from MD_Parola because it requires another huge library which is MD_MAX72 library.

Even MD_Parola is bigger, I know I have to go to the part which deals with the specific process of converting the string pointer into the set of bytes, but it was so difficult for me to follow, also I don't know which source file is dealing with this function.

If you can just tell me how to take a pointer to string input and convert that to a set of 8-bytes of ASCII set for 8x8 displays.

What you are asking is very basic questions about strings (arrays of characters) in C/C++ that you should be able to get from any good tutorial.

The string is just an array of characters. To process the array you start at the beginning and increment the array index to get the next character. Each character is then what you need to display on the dot matrix display. How you do that and make the display scroll, etc, depends on your hardware and how you have set up your code.

I have another related question, on brainy bits website, the chars array has initial two numbers in the start of each 8-bytes, can anyone explain to me what they do?

One looks like it is the width of the character and the other the height (always '8').

I have done my code to read character 8-bytes from Alphabet array and scroll that on the display.

void shift_char_rl(uint8_t direction, uint8_t no_device, uint8_t *data_p, uint8_t data_size)
{
    uint8_t offset=8, buf_size=40;                                       // offset of 8-bytes for one character on 8x8 dot matrix display
                                                                         // buffer size is no. of bytes on 4-modules 8x8 dot matrix displays
    uint8_t shift_buffer[buf_size],bit_mask[buf_size];                   // shift_buffer to hold the data, bit_mask to hold the carry bits

    for (i=0;i<buf_size;i++)                                             // initialize arrays to zero
    {
        shift_buffer[i] = 0;                                             
        bit_mask[i] = 0;
    }

    for (l=0;l<26;l++)                                                   // for 26-Alphabet chars, also could be the size of the received data
    {
        for (u=0;u<8;u++)                                                // load the cascaded 8-bytes data to shift one the display
        shift_buffer[u]=Alphabet[s++];

        for (p=0;p<8;p++)                                                // for 8-bits shifts
        {
            for (i=0;i<no_device;i++)                                    // sending the data to all the devices **/
            {
                for (row=1;row<9;row++)                                  // for 8 matrix rows
                {
                    SPI_TX_m(data = (row<<8) | (shift_buffer[(8*(i+1))+(row-1)]),i);        // SPI function receives 16-bits data and send them as 2x 8-bits to the MAX7219
                }
                _delay_ms(10);
            }                                                           // check all buffer elements
                for (h=0;h<buf_size;h++)                                
                {
                    if (shift_buffer[h] & 0x80)                         // test MSB if set
                    {
                        if ((h+8)>buf_size)continue;                    // check for array overflow
                        bit_mask[h+8]|=0x01;                            // if MSB is set, then set the +7 bytes parallel byte's LSB
                    }
                }
                for (k=0;k<buf_size;k++)                                // shift every byte
                shift_buffer[k]<<=1;
                for (k=0;k<buf_size;k++)                                // ORing the carried bit to the parallel byte
                shift_buffer[k]|=bit_mask[k];
                for (k=0;k<buf_size;k++)                                // clear bit_mask content for the next test
                bit_mask[k]=0;
        }
    }
}

Now, the next step is develop another function which receives string pointer and for each character of the string, it forwards it to this function.

marco_c:
What you are asking is very basic questions about strings (arrays of characters) in C/C++ that you should be able to get from any good tutorial.

I know how to pass a string or an array of data. But, I want to know how convert this string pointer and map it to the ASCII array of chars and symbols, etc.

One looks like it is the width of the character and the other the height (always '8').

I think you're right, but how the programmer can use those number? And what is the name of that method? It's new to me.

And what is the name of that method?

Its just a structured data table. You need to read the first 2 bytes to work out how many of the following bytes are valid. This is similar to the font file in MD_Parola/MD_MAX72xx.

To get the right character in this table you need to get to the right bytes for the start of the character. This can be either done by skipping to the correct offset based on the location of the character and number of bytes per character, or following the trail from the first char to the one that you need to get. Again, this is a similar technique to what is described in my blog explanation.

marco_c:
Its just a structured data table. You need to read the first 2 bytes to work out how many of the following bytes are valid. This is similar to the font file in MD_Parola/MD_MAX72xx.

To get the right character in this table you need to get to the right bytes for the start of the character. This can be either done by skipping to the correct offset based on the location of the character and number of bytes per character, or following the trail from the first char to the one that you need to get. Again, this is a similar technique to what is described in my blog explanation.

I'm sorry I couldn't understand the method in the library, it's quite a big library, there're a lot of source files, functions and variables.

I understood that there's a function to built the font index, the rest need more time to understand the related functions.

I want to be simple, and I only have one source file. I'm now looking for a way to index the characters in the array to any letter or symbol comes with the string.

I may get the value of any ASCII char or symbol in hex, and map that to the array.

I first have to develop a code in C to convert any ASCII char or symbol to a hex value, and then I store the hex value in an array, and then arrange them in a develop dot matrix chars and symbol array.

Then I call the specific related char with its matching hex value.

Finally I did it with this code.

// final result with the added ascii at 1st column

uint8_t new_ascii[855]={
    0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   //
    0x21, 0x18, 0x3c, 0x3c, 0x18, 0x18, 0x00, 0x18, 0x00,   //>!
    0x22, 0x6c, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   //"
    0x23, 0x6c, 0x6c, 0xfe, 0x6c, 0xfe, 0x6c, 0x6c, 0x00,   //#
    0x24, 0x30, 0x7c, 0xc0, 0x78, 0x0c, 0xf8, 0x30, 0x00,   //$
    0x25, 0x00, 0xc6, 0xcc, 0x18, 0x30, 0x66, 0xc6, 0x00,   //%
    0x26, 0x38, 0x6c, 0x38, 0x76, 0xdc, 0xcc, 0x76, 0x00,   //&
    0x27, 0x60, 0x60, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,   //'
    0x28, 0x18, 0x30, 0x60, 0x60, 0x60, 0x30, 0x18, 0x00,   //(
    0x29, 0x60, 0x30, 0x18, 0x18, 0x18, 0x30, 0x60, 0x00,   //)
    0x2a, 0x00, 0x66, 0x3c, 0xff, 0x3c, 0x66, 0x00, 0x00,   //*
    0x2b, 0x00, 0x30, 0x30, 0xfc, 0x30, 0x30, 0x00, 0x00,   //+
    0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x60,   //,
    0x2d, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x00, 0x00,   //-
    0x2e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00,   //.
    0x2f, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0, 0x80, 0x00,   //>/
    0x30, 0x7c, 0xc6, 0xce, 0xde, 0xf6, 0xe6, 0x7c, 0x00,   //0
    0x31, 0x30, 0x70, 0x30, 0x30, 0x30, 0x30, 0xfc, 0x00,   //1
    0x32, 0x78, 0xcc, 0x0c, 0x38, 0x60, 0xcc, 0xfc, 0x00,   //2
    0x33, 0x78, 0xcc, 0x0c, 0x38, 0x0c, 0xcc, 0x78, 0x00,   //3
    0x34, 0x1c, 0x3c, 0x6c, 0xcc, 0xfe, 0x0c, 0x1e, 0x00,   //4
    0x35, 0xfc, 0xc0, 0xf8, 0x0c, 0x0c, 0xcc, 0x78, 0x00,   //5
    0x36, 0x38, 0x60, 0xc0, 0xf8, 0xcc, 0xcc, 0x78, 0x00,   //6
    0x37, 0xfc, 0xcc, 0x0c, 0x18, 0x30, 0x30, 0x30, 0x00,   //7
    0x38, 0x78, 0xcc, 0xcc, 0x78, 0xcc, 0xcc, 0x78, 0x00,   //8
    0x39, 0x78, 0xcc, 0xcc, 0x7c, 0x0c, 0x18, 0x70, 0x00,   //9
    0x3a, 0x00, 0x30, 0x30, 0x00, 0x00, 0x30, 0x30, 0x00,   //:
    0x3b, 0x00, 0x30, 0x30, 0x00, 0x00, 0x30, 0x30, 0x60,   //;
    0x3c, 0x18, 0x30, 0x60, 0xc0, 0x60, 0x30, 0x18, 0x00,   //<
    0x3d, 0x00, 0x00, 0xfc, 0x00, 0x00, 0xfc, 0x00, 0x00,   //=
    0x3e, 0x60, 0x30, 0x18, 0x0c, 0x18, 0x30, 0x60, 0x00,   //>
    0x3f, 0x78, 0xcc, 0x0c, 0x18, 0x30, 0x00, 0x30, 0x00,   //?
    0x40, 0x7c, 0xc6, 0xde, 0xde, 0xde, 0xc0, 0x78, 0x00,   //@
    0x41, 0x30, 0x78, 0xcc, 0xcc, 0xfc, 0xcc, 0xcc, 0x00,   //A
    0x42, 0xfc, 0x66, 0x66, 0x7c, 0x66, 0x66, 0xfc, 0x00,   //B
    0x43, 0x3c, 0x66, 0xc0, 0xc0, 0xc0, 0x66, 0x3c, 0x00,   //C
    0x44, 0xf8, 0x6c, 0x66, 0x66, 0x66, 0x6c, 0xf8, 0x00,   //D
    0x45, 0xfe, 0x62, 0x68, 0x78, 0x68, 0x62, 0xfe, 0x00,   //E
    0x46, 0xfe, 0x62, 0x68, 0x78, 0x68, 0x60, 0xf0, 0x00,   //F
    0x47, 0x3c, 0x66, 0xc0, 0xc0, 0xce, 0x66, 0x3e, 0x00,   //G
    0x48, 0xcc, 0xcc, 0xcc, 0xfc, 0xcc, 0xcc, 0xcc, 0x00,   //H
    0x49, 0x78, 0x30, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00,   //I
    0x4a, 0x1e, 0x0c, 0x0c, 0x0c, 0xcc, 0xcc, 0x78, 0x00,   //J
    0x4b, 0xe6, 0x66, 0x6c, 0x78, 0x6c, 0x66, 0xe6, 0x00,   //K
    0x4c, 0xf0, 0x60, 0x60, 0x60, 0x62, 0x66, 0xfe, 0x00,   //L
    0x4d, 0xc6, 0xee, 0xfe, 0xfe, 0xd6, 0xc6, 0xc6, 0x00,   //M
    0x4e, 0xc6, 0xe6, 0xf6, 0xde, 0xce, 0xc6, 0xc6, 0x00,   //N
    0x4f, 0x38, 0x6c, 0xc6, 0xc6, 0xc6, 0x6c, 0x38, 0x00,   //O
    0x50, 0xfc, 0x66, 0x66, 0x7c, 0x60, 0x60, 0xf0, 0x00,   //P
    0x51, 0x78, 0xcc, 0xcc, 0xcc, 0xdc, 0x78, 0x1c, 0x00,   //Q
    0x52, 0xfc, 0x66, 0x66, 0x7c, 0x6c, 0x66, 0xe6, 0x00,   //R
    0x53, 0x78, 0xcc, 0xe0, 0x70, 0x1c, 0xcc, 0x78, 0x00,   //S
    0x54, 0xfc, 0xb4, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00,   //T
    0x55, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xfc, 0x00,   //U
    0x56, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x78, 0x30, 0x00,   //V
    0x57, 0xc6, 0xc6, 0xc6, 0xd6, 0xfe, 0xee, 0xc6, 0x00,   //W
    0x58, 0xc6, 0xc6, 0x6c, 0x38, 0x38, 0x6c, 0xc6, 0x00,   //X
    0x59, 0xcc, 0xcc, 0xcc, 0x78, 0x30, 0x30, 0x78, 0x00,   //Y
    0x5a, 0xfe, 0xc6, 0x8c, 0x18, 0x32, 0x66, 0xfe, 0x00,   //Z
    0x5b, 0x78, 0x60, 0x60, 0x60, 0x60, 0x60, 0x78, 0x00,   //[
    0x5c, 0xc0, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x02, 0x00,   //\<
    0x5d, 0x78, 0x18, 0x18, 0x18, 0x18, 0x18, 0x78, 0x00,   //]
    0x5e, 0x10, 0x38, 0x6c, 0xc6, 0x00, 0x00, 0x00, 0x00,   //^
    0x5f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,   //_
    0x60, 0x30, 0x30, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,   //`
    0x61, 0x00, 0x00, 0x78, 0x0c, 0x7c, 0xcc, 0x76, 0x00,   //a
    0x62, 0xe0, 0x60, 0x60, 0x7c, 0x66, 0x66, 0xdc, 0x00,   //b
    0x63, 0x00, 0x00, 0x78, 0xcc, 0xc0, 0xcc, 0x78, 0x00,   //c
    0x64, 0x1c, 0x0c, 0x0c, 0x7c, 0xcc, 0xcc, 0x76, 0x00,   //d
    0x65, 0x00, 0x00, 0x78, 0xcc, 0xfc, 0xc0, 0x78, 0x00,   //e
    0x66, 0x38, 0x6c, 0x60, 0xf0, 0x60, 0x60, 0xf0, 0x00,   //f
    0x67, 0x00, 0x00, 0x76, 0xcc, 0xcc, 0x7c, 0x0c, 0xf8,   //g
    0x68, 0xe0, 0x60, 0x6c, 0x76, 0x66, 0x66, 0xe6, 0x00,   //h
    0x69, 0x30, 0x00, 0x70, 0x30, 0x30, 0x30, 0x78, 0x00,   //i
    0x6a, 0x0c, 0x00, 0x0c, 0x0c, 0x0c, 0xcc, 0xcc, 0x78,   //j
    0x6b, 0xe0, 0x60, 0x66, 0x6c, 0x78, 0x6c, 0xe6, 0x00,   //k
    0x6c, 0x70, 0x30, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00,   //l
    0x6d, 0x00, 0x00, 0xcc, 0xfe, 0xfe, 0xd6, 0xc6, 0x00,   //m
    0x6e, 0x00, 0x00, 0xf8, 0xcc, 0xcc, 0xcc, 0xcc, 0x00,   //n
    0x6f, 0x00, 0x00, 0x78, 0xcc, 0xcc, 0xcc, 0x78, 0x00,   //o
    0x70, 0x00, 0x00, 0xdc, 0x66, 0x66, 0x7c, 0x60, 0xf0,   //p
    0x71, 0x00, 0x00, 0x76, 0xcc, 0xcc, 0x7c, 0x0c, 0x1e,   //q
    0x72, 0x00, 0x00, 0xdc, 0x76, 0x66, 0x60, 0xf0, 0x00,   //r
    0x73, 0x00, 0x00, 0x7c, 0xc0, 0x78, 0x0c, 0xf8, 0x00,   //s
    0x74, 0x10, 0x30, 0x7c, 0x30, 0x30, 0x34, 0x18, 0x00,   //t
    0x75, 0x00, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0x76, 0x00,   //u
    0x76, 0x00, 0x00, 0xcc, 0xcc, 0xcc, 0x78, 0x30, 0x00,   //v
    0x77, 0x00, 0x00, 0xc6, 0xd6, 0xfe, 0xfe, 0x6c, 0x00,   //w
    0x78, 0x00, 0x00, 0xc6, 0x6c, 0x38, 0x6c, 0xc6, 0x00,   //x
    0x79, 0x00, 0x00, 0xcc, 0xcc, 0xcc, 0x7c, 0x0c, 0xf8,   //y
    0x7a, 0x00, 0x00, 0xfc, 0x98, 0x30, 0x64, 0xfc, 0x00,   //z
    0x7b, 0x1c, 0x30, 0x30, 0xe0, 0x30, 0x30, 0x1c, 0x00,   //{
    0x7c, 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00,   //|
    0x7d, 0xe0, 0x30, 0x30, 0x1c, 0x30, 0x30, 0xe0, 0x00,   //}
    0x7e, 0x76, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   //~
};

///////////////////////////////////////////////////////////////////
	  /*convert the string to 8x8 dot matrix 8-bytes packets*/
///////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include "sketch.h"

#define ascii_index 95
#define buffer_size str1_len*8
uint16_t i,j,k;

int main(void) {

uint8_t str1[]="Hi, how are you?";
uint8_t str1_len=strlen(str1);
uint8_t buffer[buffer_size];
string_read_to_8x8_bytes_out(str1,str1_len,buffer);

for (i=0;i<str1_len;i++)
{
    printf("letter %c:\n", str1[i]);
    for (j=0;j<8;j++){
    printf("0x%.2x ", buffer[(i*8)+j]);
    if (j%8==7)printf("\n");}
}

    return 0;
}

void string_read_to_8x8_bytes_out(uint8_t *str_in, uint8_t strlen, uint8_t *array_out)
{
    for (i=0;i<strlen;i++)
    {
        for (k=0;k<ascii_index;k++)
        {
            if (str_in[i]==new_ascii[k*9])
            {
                for (j=0;j<8;j++)
                {
                    array_out[(i*8)+j]=new_ascii[((k*9)+1)+j];
                }
            }
        }
    }
}